Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Explain lock granularity in relational databases: row locks, page locks and table locks. Which granularity do PostgreSQL, MySQL InnoDB and SQL Server use for an ordinary UPDATE ... WHERE id = 42, and what is the trade-off between fine and coarse locks?

Lock granularity is the size of what one lock covers: a single row, a page of rows, or a whole table. Fine row locks let many transactions work on the same table at once but cost memory and lock-manager work for every row, while coarse table locks are cheap to manage but block everyone. For an UPDATE ... WHERE id = 42, PostgreSQL, MySQL InnoDB and SQL Server all lock just that one row, plus a weak table-level lock: ROW EXCLUSIVE in PostgreSQL and an intention exclusive (IX) lock in InnoDB and SQL Server.

Background to this question

When two requests wait for each other in production, the first question is what exactly is locked. The answer depends on lock granularity, and it differs between databases in ways that explain real behaviour: why PostgreSQL can lock millions of rows without running out of memory, why SQL Server sometimes blocks a whole table during a big update, and why InnoDB blocks inserts into ranges nobody has used yet. In this answer, we will recap what a lock protects, compare row, page and table locks and their trade-off, and then see what each of the three databases does for a single-row UPDATE by primary key.

1A Quick Recap of Locks and Granularity

A lock is a reservation a transaction takes so that others cannot make a conflicting change at the same time. Writes take exclusive locks, held until the transaction commits or rolls back. Granularity is how much data one lock covers. A row lock covers one row; a page lock covers one page, the fixed-size block of storage, often 8 KB, that holds many rows; a table lock covers the whole table.

2The Trade-Off Between Fine and Coarse Locks

Fine-grained row locks give the most concurrency: two transactions updating rows 42 and 43 do not wait for each other. But each lock has to be recorded and checked, so a transaction that updates 5 million rows needs 5 million locks' worth of memory and lock-manager work. Coarse table locks are the opposite: one lock, almost free to manage, but nobody else can write to the table while it is held, and possibly not even read.

Databases resolve this in different ways: by storing row locks where they cost no extra memory, by locking index records, or by starting fine and escalating to coarse when a transaction takes too many locks.

Granularity trade-off
GranularityConcurrencyManagement costTypical use
Rowhighestone lock per rowOLTP updates by key
Pagemediumone lock per page of rowsSQL Server, some scans
Tablelowestone lock in totalDDL, bulk loads, escalation

3PostgreSQL: Row Locks in the Tuple Header

PostgreSQL does not keep a lock entry in shared memory for each locked row. When a transaction updates or locks a row, it writes its own transaction id into the row version's xmax field and sets flag bits in the header, the infomask, that say what kind of lock it is. A second transaction that wants the row sees the xmax of a transaction still in progress and waits on that transaction's id in the lock manager instead. So locking a million rows costs no more lock memory than locking one, and PostgreSQL never needs lock escalation. Table-level locks, by contrast, are kept in the shared lock manager.

SQL
BEGIN;
UPDATE accounts SET balance = balance - 10 WHERE id = 42;
SELECT locktype, relation::regclass, mode FROM pg_locks WHERE pid = pg_backend_pid();
-- relation | accounts | RowExclusiveLock <- weak table lock
-- relation | accounts_pkey | RowExclusiveLock
-- transactionid | | ExclusiveLock <- our xid, others wait on it
-- (no entry for row 42 itself: that lock lives in the row's xmax)

ROW EXCLUSIVE is a weak table lock: it conflicts only with strong locks taken by things like ALTER TABLE, VACUUM FULL or LOCK TABLE, never with other ordinary reads and writes. Its job is to stop someone from dropping or rewriting the table while we update it.

4MySQL InnoDB: Index Record Locks and Intention Locks

InnoDB locks index records, not rows in the abstract. Because its table is stored as a clustered index on the primary key, an UPDATE WHERE id = 42 takes an exclusive record lock on the index entry for 42. Under REPEATABLE READ, scans that are not unique lookups also take gap locks, on the space between index entries, and next-key locks, a record plus the gap before it, to stop phantom inserts. A unique equality match like ours needs no gap lock.

Before taking the record lock, InnoDB takes an intention exclusive lock, IX, on the table. It announces 'some rows in this table are being locked exclusively' so that anyone wanting a full table lock knows to wait, without checking every row. Locks are kept in memory in InnoDB's lock system, organised per page, so they are cheap but not free; InnoDB does not escalate them.

5SQL Server: Rows, Pages, Tables and Escalation

SQL Server's lock manager can lock a row (a key in an index, or a RID in a heap), a page or the whole table, and picks the granularity per statement. For UPDATE WHERE id = 42 it takes an exclusive key lock on the row, intention exclusive locks on the page and on the table, and nothing else. If a single statement acquires a very large number of row or page locks, around 5,000 on one table, or lock memory gets tight, SQL Server tries to escalate them to one table lock, which frees memory but blocks other users of the table. Large batch updates are usually split into chunks partly to avoid this; escalation can also be configured per table (check your version).

UPDATE accounts ... WHERE id = 42
DatabaseRow-level lockTable-level lock
PostgreSQLxid in the row's xmaxROW EXCLUSIVE
MySQL InnoDBX record lock on PK entry 42IX (intention exclusive)
SQL ServerX key lock, plus IX on the pageIX, may escalate if many rows

6Follow-up questions

What are intention locks and why are they needed?

An intention lock is a table-level marker saying 'I hold, or will hold, locks of this kind on some rows inside'. IX means exclusive row locks inside, IS means shared ones. Without them, a transaction asking for a full table lock would have to check every row to see whether anyone held a conflicting row lock. With them, it only checks the table: an exclusive table lock conflicts with any IX or IS, while IX locks are compatible with each other, so many transactions can lock different rows at once. InnoDB and SQL Server use them explicitly; PostgreSQL's ROW EXCLUSIVE and ROW SHARE table locks play the same role.

Can PostgreSQL run out of memory from row locks?

Not from ordinary row locks, since they live in the tuple header. It can from very many table-level locks in one transaction, for example touching thousands of partitions, which is governed by max_locks_per_transaction.

Why does InnoDB sometimes lock rows the query did not match?

Because it locks the index records it scans. Without a suitable index, an UPDATE may scan, and lock, many records, so good indexes reduce locking as well as speeding queries up.

7Conclusion

In this answer, we saw that lock granularity trades concurrency against management cost: row locks let transactions work side by side but cost work per row, while table locks are cheap but block everyone. PostgreSQL stores row locks in the tuple's xmax and infomask bits and keeps only table locks in the lock manager; InnoDB locks index records, with gap and next-key locks under REPEATABLE READ, after an IX table lock; SQL Server locks rows, pages or tables and may escalate. For an UPDATE by primary key, all three lock one row plus a weak table lock.

What to say in the interviewAbout 90 seconds aloud

Open with

Lock granularity is how much one lock covers: a row, a page of rows, or the whole table. Row locks give the most concurrency, because two transactions can update different rows at once, but every lock costs memory and lock-manager work.

Then, if they want more

Table locks are cheap, one lock, but they block everybody. For an UPDATE where id = 42, all three databases lock just that one row plus a weak lock on the table.

In PostgreSQL the row lock isn't kept in shared memory at all; the transaction writes its id into the row's xmax with some infomask bits, and others wait on that transaction id. The table gets a ROW EXCLUSIVE lock, which only conflicts with things like ALTER TABLE. Because row locks live in the tuple, PostgreSQL never needs lock escalation.

InnoDB locks index records; for a primary key match it's an exclusive record lock on entry 42 in the clustered index, and before that an intention exclusive lock on the table. For range scans under REPEATABLE READ it also takes gap and next-key locks to stop phantoms. SQL Server can lock rows, pages or tables.

Here it takes an exclusive key lock and IX locks on the page and table, and if a statement takes thousands of row locks it may escalate to a table lock. Intention locks let a table-level request see conflicts without checking every row.

About this answer

Last updated
12 November 2026
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.
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 "Explain lock granularity in relational databases: row locks, page locks and table locks....": https://scrivio-interview-library.netlify.app/databases/database-locking/2c83496824/