Oracle TM and TX locks
Oracle has two kinds of DML locks. A TM lock is taken on a table. It shows who is modifying the table and prevents conflicts with DDL. A TX lock is created once per transaction, and each row the transaction modifies gets a marker in the block that points to this transaction. When another session tries to modify the same row, it sees that marker and waits for that TX lock to be released (by commit or rollback).
Key specs
| Item | Value |
|---|---|
| Table lock | TM lock, modes RS(2), RX(3), S(4), SRX(5), X(6) |
| TM mode taken by DML | INSERT, UPDATE, DELETE, MERGE, SELECT ... FOR UPDATE: RX(3) |
| Row lock | TX lock (one per transaction) + ITL slot in the block header + lock byte in the row header |
Plain SELECT | Takes no lock. Reads the read-consistent version built from undo |
| Lock escalation | None |
| ITL slots | Start at INITRANS (table default 1, index default 2) and grow from the free space in the block |
| Wait options | FOR UPDATE, FOR UPDATE NOWAIT, FOR UPDATE WAIT n, FOR UPDATE SKIP LOCKED |
| Typical wait events | enq: TX - row lock contention, enq: TX - allocate ITL entry, enq: TM - contention |
| Deadlock | Detected automatically, one statement is rolled back with ORA-00060 |
TM lock modes
| Mode | Name | Taken by | Blocks |
|---|---|---|---|
| 2 | RS (Row Share) | LOCK TABLE ... IN ROW SHARE MODE | X |
| 3 | RX (Row Exclusive) | DML, SELECT ... FOR UPDATE | S, SRX, X |
| 4 | S (Share) | LOCK TABLE ... IN SHARE MODE, the child table when the parent of an unindexed foreign key is modified | RX, SRX, X |
| 5 | SRX (Share Row Exclusive) | LOCK TABLE ... IN SHARE ROW EXCLUSIVE MODE | RX, S, SRX, X |
| 6 | X (Exclusive) | LOCK TABLE ... IN EXCLUSIVE MODE, many DDL statements | All modes |
RS corresponds to intent share (IS), RX to intent exclusive (IX), and SRX to SIX. Between DML statements, RX and RX are compatible, so different rows of the same table can be modified at the same time.
How it works
Figure. How a row lock is recorded in the block
block-beta
columns 6
h["Block header"]:2 i1["ITL 1<br/>Transaction T1, undo address"]:2 i2["ITL 2<br/>Empty"]:2
r1["Row A<br/>Lock byte = 1"]:3 r2["Row B<br/>Lock byte = 0"]:3
w["T2 tries to modify Row A → sees T1 in ITL 1 and waits on T1's TX lock"]:6
class h meta
class i1 hot
class i2 free
class r1 hot
class r2 data
class w warn
Diagram explanation: When T1 modifies Row A, it writes its transaction number and undo address into an empty ITL slot (ITL 1) in the block header, and writes that slot number (1) into the lock byte of Row A. When T2 tries to modify the same row, it sees in the ITL that the lock byte points to that T1 is still active, and waits for T1's TX lock (enq: TX - row lock contention). A row whose lock byte is 0, like Row B, can be modified immediately.
Why there is no escalation. Row locks are recorded inside the data block, so lock manager memory does not grow no matter how many rows you lock. There is no need to promote row locks to a table lock as SQL Server does. Instead, the lock marker can remain in the block after commit, and the next session that reads that block may clean it up (delayed block cleanout).
ITL waits. If the number of transactions modifying rows of one block at the same time exceeds the number of ITL slots, and the block has no free space to add slots, sessions wait on enq: TX - allocate ITL entry even though they touch different rows. For tables with heavy concurrent updates, increase INITRANS and PCTFREE.
Foreign key indexes. If the foreign key column of a child table has no index, deleting a parent row or changing its primary key briefly places an S(4)-type TM lock on the whole child table, which conflicts with DML on the child table. As a rule, create an index on foreign key columns.
Settings and checks
-- Who is blocking whom
SELECT sid, serial#, username, event, blocking_session, seconds_in_wait, sql_id
FROM v$session WHERE blocking_session IS NOT NULL;
-- Lock list: for TM, id1 is the object number; for TX, it identifies the transaction
SELECT l.sid, l.type, l.id1, l.id2, l.lmode, l.request, l.block, o.object_name
FROM v$lock l LEFT JOIN dba_objects o ON l.type = 'TM' AND o.object_id = l.id1
WHERE l.type IN ('TM', 'TX');
SELECT * FROM dba_blockers;
SELECT * FROM dba_waiters;
-- Wait at most 3 seconds, and raise ORA-30006 if the lock cannot be taken
SELECT * FROM orders WHERE id = 42 FOR UPDATE WAIT 3;
-- Work queue that skips locked rows and takes the rest
SELECT id FROM jobs WHERE status = 'READY' FOR UPDATE SKIP LOCKED;
ALTER TABLE orders INITRANS 8; -- applies from newly written blocks onward
Comparison with other DBMSs
| Oracle | SQL Server | InnoDB | PostgreSQL | |
|---|---|---|---|---|
| Where row locks are recorded | ITL and row header in the block | Lock manager memory | Per-page lock bitmap (memory) | Row header (tuple header) |
| Lock escalation | None | Yes (about 5,000 locks) | None | None |
| Locking gaps between rows | None | Key-range locks (Serializable) | Gap locks, next-key locks | None (SSI uses predicate locks) |
Plain SELECT | No lock | S lock by default (none with RCSI) | No lock | No lock |
In the articles
- 7.2 Lock basics
- 7.3 The lock family: S, U, X, and intent locks
- 7.4 Lock escalation: when row locks become a table lock
- 7.6 Locking reads: FOR UPDATE, optimistic and pessimistic locking
NOTE The default
INITRANSand the range ofSKIP LOCKEDsupport can vary by version.