What are advisory locks vs regular locks in PostgreSQL?
Answer
Regular (object) locks are automatically acquired by PostgreSQL when accessing tables, rows, or system objects — they protect internal data structures and enforce ACID properties. Types include: AccessShareLock (SELECT), RowShareLock (SELECT FOR UPDATE), RowExclusiveLock (INSERT/UPDATE/DELETE), ShareLock (CREATE INDEX), AccessExclusiveLock (ALTER TABLE — blocks all). View held locks: SELECT * FROM pg_locks;. Advisory locks have no inherent meaning to PostgreSQL — they are application-defined semaphores. They don't affect database objects and are managed entirely by application code via pg_advisory_lock(). Regular locks protect data integrity; advisory locks protect application-level invariants like "only one job processor should run at a time." Both are visible in pg_locks (advisory locks have locktype = 'advisory').
Previous
How does PostgreSQL handle deadlocks?
Next
How do you implement partitioned tables efficiently in PostgreSQL?
More PostgreSQL Questions
View all →- Advanced What is the query planner in PostgreSQL and how does it work?
- Advanced What causes poor query plans and how do you fix them?
- Advanced What is logical replication in PostgreSQL?
- Advanced How do you implement row-level security (RLS) in PostgreSQL?
- Advanced What is table bloat in PostgreSQL and how do you fix it?