Every Oracle DBA learns this one early. Delete a parent row when the child table's foreign-key column has no index, and Oracle locks the whole child table. Every insert and update on the child queues behind your DELETE, the wait event is enq: TM - contention, and when it turns into a deadlock, the trace file shows TM resources. Index your foreign keys and it goes away.
The documentation still says so. The 26ai Concepts guide describes a full table lock on the child, and its example adds: "This lock enables other sessions to query but not update the employees table." The 23ai guide has the same text. My own ORA-00060 deadlock post calls TM in the deadlock graph the fingerprint of an unindexed foreign key.
I built a lab to measure it, and on Oracle Database Free 26ai it didn't happen. No enq: TM - contention, nothing queued, and the deadlock graph showed TX. I ran the same script on 23ai Free 23.5: same result. On 21c XE, the textbook behaviour came back, exactly as described. Somewhere between 21c and 23ai, this changed, and I haven't found it in Oracle's documentation.
The fix is still the same index. But the problem looks different now, so the way you find it has to change. This post shows what changed, what still hurts, and how to spot it on 23ai and 26ai.
The test
The lab has two tables: CUSTOMERS (1,000 rows) and ORDERS (200,000 rows). ORDERS.CUSTOMER_ID has a foreign key to CUSTOMERS, and no index. Customers 1 to 500 have orders. Customers 501 to 1,000 have none.
One session updates an order and doesn't commit. It's order 7, which belongs to customer 7: a batch that paused, or a screen someone left open. Then a second session deletes customer 998, who has no orders at all. While that DELETE runs, a third session inserts a new order for another customer.
| 21c XE (21.3) | 23ai Free (23.5) and 26ai Free (23.26.3) | |
|---|---|---|
The DELETE of customer 998 | waits for the open transaction | waits for the open transaction (14 s behind a 15 s blocker) |
| Its wait event | enq: TM - contention | enq: TX - row lock contention |
| A new order inserted while it waits | queues for 8 s | runs in 34 ms |
| The deadlock graph after ORA-00060 | TM resources, SX held, SSX wanted | TX resources, X held, S wanted |
So the good news on 23ai and 26ai: the child table no longer freezes. A waiting parent DELETE doesn't stop anyone else from working on ORDERS. On 21c it did, and that queue is what turned a slow DELETE into an outage.
The bad news: the DELETE itself still waits, and now it's harder to see why.
What still hurts
The DELETE still waits for every open transaction on the child. Customer 998 has no orders. The open transaction changed an order of customer 7. They have nothing in common, and the DELETE still waited until that transaction ended. My reading of it: to prove that no order points at customer 998, Oracle scans ORDERS, and it can't settle rows that another transaction has changed but not committed, so it waits for those transactions to end. Whatever the mechanism, the effect is measured. If one of them is idle for an hour, the DELETE waits an hour.
It only waits for transactions that were open when it started. In the lab, a second transaction that started on ORDERS while the DELETE was waiting didn't extend the wait: the DELETE finished when the first one ended. That's what keeps the queue from forming.
DDL_LOCK_TIMEOUT doesn't help. With DDL_LOCK_TIMEOUT = 3 in the deleting session, the DELETE still waited 14 seconds and finished without an error. It isn't waiting for a DDL lock. There is no NOWAIT for a DELETE, so the only limit on the wait is the other transaction.
It still deadlocks. Two sessions each insert an order, then each deletes a different customer. Each DELETE waits for the other session's open transaction on ORDERS, and one session gets ORA-00060. That's the same deadlock as on 21c. Only the graph changed: on 23ai and 26ai, it shows two TX resources, held in X and wanted in S.
------------Blocker(s)----------- ------------Waiter(s)------------ Resource Name process session holds waits serial process session holds waits serial TX-00030004-0000024B-25BFE757-00000000 31 220 X 57219 85 174 S 57601 TX-00060021-00000254-25BFE757-00000000 85 174 X 57601 31 220 S 57219 If you were taught "TM in the graph means unindexed foreign key, TX in S mode means something else", this graph points you the wrong way. In the ORA-00060 post a TX wait in mode S points to an ITL shortage or a unique key. On 23ai and 26ai it can also be an unindexed foreign key.
It still reads the whole child table, once per row. Deleting 100 customers that have no orders took about 57,000 logical reads without the index (57,318 to 57,847 across runs). ORDERS has 622 blocks, so that's one full scan of the child for each parent row deleted. With the index: about 510. The cost grows with the size of the child table times the number of parent rows you delete, so a purge of thousands of parents against a large child table pays it thousands of times.
A parent DELETE with an unindexed foreign key, while another session has an uncommitted change on the child table. On 21c the DELETE waits on enq: TM - contention and every new DML on the child queues behind it. On 23ai and 26ai only the DELETE waits, on enq: TX - row lock contention; with an index on the foreign key, it doesn't wait at all.
How to recognise it on 23ai and 26ai
The wait event is the one you see for every row-lock queue, enq: TX - row lock contention, and V$SESSION.BLOCKING_SESSION points at the right session. What gives it away is the mismatch:
- The waiting statement is a
DELETEon a parent table, or anUPDATEof its key. - The blocking session has no uncommitted change on that table. Its open transaction is on a child table.
- The rows don't overlap. In the lab, the blocker had changed an order of customer 7; the
DELETEwas for customer 998.
In the lab an UPDATE of the parent key waited just like the DELETE, and an UPDATE of a non-key column of the parent didn't wait at all, so if the waiting statement doesn't touch the key, look elsewhere.
When you see that pattern, check the child tables for foreign keys with no index.
The fix: index the foreign key, with its columns first
The fix hasn't changed. Create an index on the child's foreign-key columns:
create index orders_cust_ix on orders (customer_id); With it, the DELETE of customer 998 behind the same open transaction took 37 ms. Oracle checks the index for CUSTOMER_ID = 998 instead of scanning ORDERS, and an uncommitted change to an unrelated order doesn't matter.
The index has to start with the foreign-key columns. In the lab, an index on (AMOUNT, CUSTOMER_ID) didn't help: the DELETE still waited. An index on (CUSTOMER_ID, AMOUNT) did. For a foreign key with several columns, they all have to come first, in any order.
One wait remains, and it's correct. If the open transaction inserted an order for customer 998, the DELETE waits for it, index or not. If that insert commits, the DELETE must fail; if it rolls back, the DELETE can go ahead. That isn't a locking problem. It's the constraint doing its job.
To find the foreign keys that need an index, this query lists every foreign key in the schema that doesn't have an index starting with its columns:
select c.table_name || '.' || c.constraint_name || ' (' || listagg(cc.column_name, ', ') within group (order by cc.position) || ')' as unindexed_fk from user_constraints c join user_cons_columns cc on cc.constraint_name = c.constraint_name where c.constraint_type = 'R' and not exists ( select 1 from user_ind_columns ic where ic.table_name = c.table_name and ic.column_position <= (select count(*) from user_cons_columns x where x.constraint_name = c.constraint_name) and ic.column_name in (select column_name from user_cons_columns x where x.constraint_name = c.constraint_name) group by ic.index_name having count(*) = (select count(*) from user_cons_columns x where x.constraint_name = c.constraint_name)) group by c.table_name, c.constraint_name; In the lab it listed ORDERS.ORDERS_CUSTOMER_FK (CUSTOMER_ID) with no index and with the (AMOUNT, CUSTOMER_ID) index, and nothing with (CUSTOMER_ID, AMOUNT). For other schemas, use the DBA_ views and add the owner.
You don't need an index on every foreign key. If the parent's rows are never deleted and its key is never updated, nothing ever scans the child for it. A status or country lookup table is the usual example. But check the purge jobs and the archiving scripts before you decide that.
Prove it
The lab runs Oracle Database Free 26ai in a container, plus a throwaway 21c XE container for the comparison. The blocker changes one order and stays open for 15 seconds:
drill case result block parent DELETE vs an unrelated open change waits 14 s on enq: TX - row lock contention, blocking_session = the blocker, no TM wait; a new order inserted meanwhile: 34 ms; a transaction started later doesn't extend the wait; UPDATE of the parent key: waits too; UPDATE of a non-key parent column: 34 ms timeout DDL_LOCK_TIMEOUT = 3 still waits 14 s, no error deadlock two sessions: insert an order, delete a customer ORA-00060; graph shows TX X/S, not TM cost delete 100 customers with no orders ~57,000 logical reads; ~510 with the index index index on ORDERS(CUSTOMER_ID) 37 ms; an open order for the SAME customer still waits order (AMOUNT, CUSTOMER_ID) / (CUSTOMER_ID, AMOUNT) still waits 7 s / 35 ms find the query above lists the FK until a leading index exists legacy block + deadlock on 21c XE enq: TM - contention; new order queues 8 s; graph shows TM SX/SSX Don't take my word for it — run it. The unindexed-fk lab asserts every row: the wait event and the blocking session, that nothing queues behind the
DELETEon 26ai and that it does on 21c, that a later transaction doesn't extend the wait, theDDL_LOCK_TIMEOUTcase,ORA-00060withTX(26ai) orTM(21c) in the graph, the logical reads, and each index. If any of that doesn't reproduce, the run fails. The 26ai drills run on every CI push.
What teams get wrong
- Looking for
enq: TM - contention. On 23ai and 26ai, an unindexed foreign key shows up asenq: TX - row lock contention. If your monitoring or your runbook searches for the TM event, it finds nothing. - Reading the deadlock graph the old way.
TXheld inXand wanted inScan be an unindexed foreign key now, not only an ITL shortage or a unique key. - Deciding the index isn't needed after an upgrade. The child table no longer freezes, so the problem looks smaller. But the
DELETEstill waits for every open transaction on the child, still deadlocks, and still scans the whole child table for each row it deletes. - Indexing the column, second. An index on
(AMOUNT, CUSTOMER_ID)doesn't count. The foreign-key columns have to lead. - Setting
DDL_LOCK_TIMEOUTto bound the wait. It doesn't apply to this wait.
Frequently asked questions
Does an unindexed foreign key still lock the child table on Oracle 23ai and 26ai?
Not in the way the documentation describes. In the companion lab on Oracle Database Free 26ai (23.26.3), and with the same script on 23ai Free 23.5, a parent DELETE behind an open transaction on the child waited on enq: TX - row lock contention, and new inserts into the child ran in 34 ms. On 21c XE the same DELETE waited on enq: TM - contention and a new insert queued for 8 seconds. The 23ai and 26ai Concepts guides still describe a full table lock on the child.
Why does my DELETE on a parent table wait when no child row references it?
If the child's foreign-key column has no index, Oracle scans the child table to prove no row references the parent, and it waits for transactions that have uncommitted changes on the child, whatever rows they changed. In the companion lab, deleting a customer with no orders waited 14 seconds for an unrelated, uncommitted update to another customer's order. With an index on the foreign-key column it took 37 ms.
What does enq: TM - contention mean?
A session is waiting for a table-level (TM) lock that another session holds in an incompatible mode. On releases before 23ai, the most common cause was a DELETE or primary-key UPDATE on a parent table whose child foreign key had no index: the parent statement needed a table lock on the child and everyone else's DML on the child queued behind it. In the companion lab on 21c XE that's what happened; on 23ai and 26ai the same case waited on enq: TX - row lock contention instead.
How do I find unindexed foreign keys in Oracle?
List every foreign key (USER_CONSTRAINTS, CONSTRAINT_TYPE = 'R') for which no index has all of the foreign-key columns as its leading columns (USER_IND_COLUMNS, COLUMN_POSITION up to the number of FK columns). The query in this post does that. In the companion lab it reported ORDERS.CUSTOMER_ID with no index and with an index on (AMOUNT, CUSTOMER_ID), and nothing once an index on (CUSTOMER_ID, AMOUNT) existed.
Does a composite index cover a foreign key?
Only if the foreign-key columns are its leading columns. In the companion lab an index on (CUSTOMER_ID, AMOUNT) removed the wait, and an index on (AMOUNT, CUSTOMER_ID) didn't: the parent DELETE still waited for the open transaction on the child.
Can an unindexed foreign key cause ORA-00060 on 23ai and 26ai?
Yes. In the companion lab, two sessions each inserted a child row and then each deleted a different parent row; one got ORA-00060. On 26ai the deadlock graph showed two TX resources held in X mode and wanted in S mode. On 21c XE the same case showed TM resources, SX held and SSX wanted, the classic fingerprint.
Does DDL_LOCK_TIMEOUT limit how long a parent DELETE waits?
No. In the companion lab, with DDL_LOCK_TIMEOUT = 3 in the deleting session, the DELETE still waited 14 seconds for the open transaction on the child and finished without an error. The wait ends only when that transaction commits or rolls back.
On 23ai and 26ai an unindexed foreign key no longer freezes the child table, and it no longer shows up as enq: TM - contention. That's a real improvement, and it also hides the problem: the parent DELETE still waits for every open transaction on the child, still deadlocks, and still scans the child for each row, under a wait event that looks like any other row lock. Index the foreign keys whose parents get deleted or re-keyed, with their columns first, and prove it on both releases with the unindexed-fk lab.
Originally published at uptimearchitect.com.
