PostgreSQL Enterprise (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
How does PostgreSQL handle Lock Modes and Lock Contention?
senior LevelDatabases & SQLPostgreSQL
In Simple Words: PostgreSQL features 8 table-level lock modes and multiple row-level lock modes:
ACCESS EXCLUSIVE: Most restrictive. Acquired byDROP TABLE,ALTER TABLE, andVACUUM FULL. Blocks all reads and writes!SHARE UPDATE EXCLUSIVE: Acquired byVACUUM(non-full),ANALYZE, andCREATE INDEX CONCURRENTLY. Allows concurrent reads and writes (SELECT,INSERT,UPDATE).- Rule: Always create indexes in production using:
CREATE INDEX CONCURRENTLY idx_name ON table(col);(takes longer to build, but never acquires exclusive locks!).