Everything I know about Postgres locks
Some locks in Postgres are implicit, and expensive, and that would've been nice to know earlier.
Postgres has become very popular in recent years, and has also been a personal favorite of mine. However, it was only over the past few months I got a taste of Postgres' implicit table locking and how easy it is to starve your applications on stupid database migrations.
In this post I will try to teach you everything I know about locking in Postgres so you can avoid my mistakes.
Concurrency in Postgres
As the operating system courses taught us, locks are one of the common methods for limiting concurrency (and the bugs and issues surrounding concurrency). When we want to prevent other users from using a resource, we can acquire an (exclusive) lock on the resource. Other users will have to wait until the lock has been released before they can use the resource.
Now Postgres is a database which uses the Multiversion Concurrency Control paradigm which is capable of storing multiple versions of a table or row when multiple concurrent transactions attempt to modify the same table or row. MVCC is a non-locking concurrency mechanism which means Postgres itself doesn't need to acquire internal locks for the concurrent transactions (unless you're at a serializable isolation level, in which all transactions must behave as if they were sequential).
That being said, Postgres will still use locks to coordinate work between transactions. Postgres has four types of locks, table-level, row-level, page-level, and advisory locks. Page locks are mostly internal to Postgres and is used by Postgres' own concurrency control (since Postgres does concurrency at OS process level). I won't explain them in detail.
Table-level Locks
As the name implies, these locks take ownership over an entire table at once. This should sound alarming because it is. If you're not careful with these locks it is VERY EASY to starve your application and eventually create a massive outage.
The worst part about table locks is because they're often taken implicitly along with other operations you do on the table (you can (and sometimes should) take these locks exclusively yourself too). If you're not cautious you can do a lot of damage here. These kind of locks have been the cause for several incidents at my current workplace.
ACCESS EXCLUSIVEis the most hazardous lock. It takes an exclusive write lock on your table which means ALL reads from the table are blocked until this lock is released. If you take this lock for a prolonged time on a hot table you will fuck the service, and should prepare for your postmortem.ACCESS SHAREis a shared read lock, and is implicitly taken for reads. It is rather harmless. Its existence is primarily to have your transaction be blocked on other locks (such as SHARE/ACCESS EXCLUSIVE/EXCLUSIVE).EXCLUSIVEis a weaker variant of ACCESS EXCLUSIVE, which allows for ACCESS SHARE reads concurrently. REFRESH MATERIALIZED VIEW CONCURRENTLY will take this lock. I am not a big materialized view user so I don't know too much about its implications, but I believe this one can hurt your other writes.SHAREprevents concurrent writes on the table, but others can still read.SHARE UPDATE EXCLUSIVEis a mode to prevent schema changes on the given table.SHARE ROW EXCLUSIVEis similar to the share lock, preventing concurrent writes, but there may only be one holder of this lock at a time.ROW EXCLUSIVEis taken when you make a write to a row.ROW SHAREis implicitly taken when doing a row-level lock (see below) and will block ACCESS EXCLUSIVE and EXCLUSIVE, naturally.
Row-level Locks
These locks acquire a lock on a specific row within a table. I think most
programmers have seen these used through SELECT x FOR UPDATE.
- FOR UPDATE acquires a mutually exclusive write lock on the selected row(s) so both reads and writes are blocked until the lock is released.
- FOR SHARE takes a shared lock which allows other readers, it's like an RwLock in Rust, which means others can read, but you block writes.
There are also FOR NO KEYUPDATE and FOR KEY SHARE modes, but I have honestly never used either. They are weaker versions of the other two with some exceptions for which types of conflicts they block.
Advisory Locks
Advisory locks are effectively distributed locks for your application. Postgres itself doesn't really concern itself with these locks. They are my go-to tool if I need cross-service/replica locking/synchronization.
You can of course do the same with a throwaway column/table and use
SELECT FOR UPDATE to achieve the same, but advisory locks have the added
advantage that they're released when your connection to the database closes (or
if you're using pg_advisory_xact_lock it will be released upon transaction
commit/rollback).
Operations taking implicit locks
Creating an index
Creating a constraint
Creating a column
Deleting a constraint
Deleting a table
Writing correct migrations
Because some common schema operations want to hold dangerous locks it is imperative that we don't hold them for too long since they can block both writes and reads to the tables.
Blog content licensed under CC BY-NC-SA 4.0 DEED. No language models were used in the creation of the content.