Quick revision
The short answer
A shared (S) lock lets other shared locks in but keeps exclusive locks out, while an exclusive (X) lock conflicts with both. In PostgreSQL a plain SELECT reads a snapshot through MVCC and takes no row lock at all, so it never blocks an UPDATE and is never blocked by one. SELECT ... FOR SHARE does take a shared row lock, so an UPDATE or DELETE of that row must wait until the reading transaction ends.
What to remember
In this answer, we saw that shared locks are compatible with each other and exclusive locks are compatible with nothing. PostgreSQL's plain SELECT reads a snapshot through MVCC and takes no row lock, so readers and writers never block each other, while SELECT ... FOR SHARE takes a real shared row lock that makes UPDATE and DELETE wait. The four row lock modes separate key changes from other changes, so an ordinary UPDATE takes FOR NO KEY UPDATE and foreign key checks take FOR KEY SHARE without blocking each other.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A shared (S) lock lets other shared locks in but keeps exclusive locks out, while an exclusive (X) lock conflicts with both. In PostgreSQL a plain SELECT reads a snapshot through MVCC and takes no row lock at all, so it never blocks an UPDATE and is never blocked by one. SELECT ... FOR SHARE does take a shared row lock, so an UPDATE or DELETE of that row must wait until the reading transaction ends.
Why this question is asked
Shared and exclusive locks are the vocabulary of every locking discussion, and PostgreSQL adds a twist that surprises people coming from SQL Server: plain reads take no row locks at all. Interviewers ask this to see whether we understand the compatibility rule, why MVCC removes reader-writer blocking, and what the four PostgreSQL row lock modes are for. In this answer, we will recap S and X locks and their compatibility, see why a plain SELECT never waits, reproduce the FOR SHARE case, and then look at the four row lock modes and how foreign keys use the weakest of them.
1A Quick Recap of Shared and Exclusive Locks
A lock is a promise the database makes to one transaction about a piece of data. A shared lock, written S, promises that nobody will change the data while we hold it, so many readers can hold S locks on the same row at once. An exclusive lock, written X, promises that nobody else can read under a lock or change the data, so only one transaction can hold it, and it waits for all S locks to go away first.
The whole rule fits in a small grid. S with S is compatible, because two readers do not disturb each other. S with X conflicts, and X with X conflicts. When a transaction asks for a lock that conflicts with one already held, it waits until the holder commits or rolls back.
| Held \ Requested | S (shared) | X (exclusive) |
|---|---|---|
| S (shared) | compatible | waits |
| X (exclusive) | waits | waits |
2Why a Plain SELECT Does Not Block an UPDATE
PostgreSQL uses multi-version concurrency control, MVCC for short. An UPDATE does not overwrite a row in place; it writes a new version and marks the old one as replaced by its transaction. A plain SELECT reads through a snapshot, a rule that says which versions are visible, namely those committed before the snapshot was taken.
So the reader does not need a lock to be sure the data is stable. It reads the version that was valid for its snapshot, while the writer creates a newer one next to it. The reader neither waits for the writer nor makes the writer wait. This is the main practical advantage of MVCC databases: long reports and short updates can run side by side on the same rows.
3Why SELECT ... FOR SHARE Does Block It
Sometimes we do want a row to stay unchanged until we finish, for example while we insert something that depends on it. SELECT ... FOR SHARE asks for a shared row lock. PostgreSQL records it in the row itself, and it lasts until our transaction ends. An UPDATE needs a stronger, exclusive-style lock on the same row, which conflicts, so it waits.
Notice that another session could run SELECT ... FOR SHARE on the same row at the same time without waiting, because two shared locks are compatible. Only writers, and FOR UPDATE, are kept out.
4The Four Row Lock Modes in PostgreSQL
PostgreSQL refines S and X into four row-level modes so that unrelated operations do not block each other. From strongest to weakest they are FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE and FOR KEY SHARE. FOR UPDATE is taken by DELETE, by SELECT ... FOR UPDATE, and by an UPDATE that changes a column with a unique index that a foreign key could use. FOR NO KEY UPDATE is what an ordinary UPDATE takes when it leaves key columns alone. FOR SHARE protects the whole row against changes, and FOR KEY SHARE protects only the key against changes and deletion.
The useful insight is the split between key and non-key changes. Most updates change ordinary columns such as status or amount, and they only need FOR NO KEY UPDATE, which does not conflict with FOR KEY SHARE.
| Held \ Requested | KEY SHARE | SHARE | NO KEY UPDATE | UPDATE |
|---|---|---|---|---|
| FOR KEY SHARE | ok | ok | ok | waits |
| FOR SHARE | ok | ok | waits | waits |
| FOR NO KEY UPDATE | ok | waits | waits | waits |
| FOR UPDATE | waits | waits | waits | waits |
5How Foreign Keys Use FOR KEY SHARE
When we insert an order_line that references orders.id = 42, PostgreSQL must make sure order 42 exists and is not deleted before we commit. It does this by locking the parent row FOR KEY SHARE. That lock says the key must not change or disappear, and nothing more.
So while our insert's transaction is open, another session can still run UPDATE orders SET status = 'PAID' WHERE id = 42, because that update only takes FOR NO KEY UPDATE, which is compatible. Only a DELETE of order 42, or an update of its id, waits. Before PostgreSQL 9.3, foreign key checks used FOR SHARE, and busy parent rows blocked constantly; the finer modes were added to fix exactly that.
6Follow-up questions
How does SQL Server's default READ COMMITTED differ without RCSI?
Without READ_COMMITTED_SNAPSHOT, SQL Server's READ COMMITTED is implemented with locks: a reader takes a short shared lock on each row or page as it reads it and releases it soon after. That shared lock conflicts with a writer's exclusive lock, so a plain SELECT waits for any uncommitted update of the row, and a long scan can briefly block writers. PostgreSQL's plain SELECT never waits because it reads row versions. With RCSI on, SQL Server behaves much like PostgreSQL, reading versions from the version store.
Is SELECT ... FOR UPDATE ever needed if UPDATE locks the row anyway?
Yes, when we must read a value, make a decision in the application, and then write, all without anyone changing the row in between. FOR UPDATE takes the lock at read time, so the decision is based on data that cannot change before our UPDATE. A single UPDATE with the condition in its WHERE clause is often a simpler alternative.
Do row locks in PostgreSQL use memory in the lock manager?
Mostly no. A row lock is recorded in the row's header (the xmax field and some flag bits), and when several transactions share a lock, PostgreSQL stores them in a MultiXact structure. Waiting is done on the holder's transaction id. That is why PostgreSQL can lock millions of rows without escalating to a table lock, although writing the lock marks does cost I/O.
7Conclusion
In this answer, we saw that shared locks are compatible with each other and exclusive locks are compatible with nothing. PostgreSQL's plain SELECT reads a snapshot through MVCC and takes no row lock, so readers and writers never block each other, while SELECT ... FOR SHARE takes a real shared row lock that makes UPDATE and DELETE wait. The four row lock modes separate key changes from other changes, so an ordinary UPDATE takes FOR NO KEY UPDATE and foreign key checks take FOR KEY SHARE without blocking each other.
Open with
Shared locks are compatible with other shared locks, so many readers can hold them, while an exclusive lock conflicts with both shared and exclusive locks. PostgreSQL is an MVCC database: an UPDATE writes a new row version, and a plain SELECT reads whatever version its snapshot allows.
Then, if they want more
It takes no row lock, so it neither blocks the update nor waits for it. SELECT ... FOR SHARE is different because it explicitly takes a shared row lock, stored in the row header, held until the transaction ends, and an UPDATE or DELETE of that row has to wait for it. PostgreSQL actually has four row lock modes.
FOR UPDATE is the strongest and comes from DELETE, SELECT FOR UPDATE, or an update of a unique key column. An ordinary update that leaves keys alone takes FOR NO KEY UPDATE. Then FOR SHARE, and the weakest, FOR KEY SHARE, which foreign key checks take on the parent row.
FOR KEY SHARE conflicts only with FOR UPDATE, so inserting an order line doesn't block someone updating the order's status, only deleting the order or changing its id.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What are shared (S) and exclusive (X) locks and their compatibility? In PostgreSQL, why...": https://scrivio-interview-library.netlify.app/databases/database-locking/cbb05f654e/