Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

Deleting one row from customers takes 40 seconds and blocks other sessions. orders.customer_id references customers(id) ON DELETE CASCADE. What is the likely cause and fix?

The likely cause is that orders.customer_id has no index. PostgreSQL does not create one for the referencing side of a foreign key, so for each deleted customer it must scan the whole orders table to find the rows to cascade, while holding locks that make other sessions wait. The fix is CREATE INDEX CONCURRENTLY orders_customer_id_idx ON orders (customer_id), and then auditing every foreign key for a missing index. MySQL InnoDB creates such an index automatically, so this trap is mainly PostgreSQL's.

Why this question is asked

A single-row delete that takes 40 seconds is a strong hint, because deleting one row by primary key should take milliseconds. The time is going somewhere the delete statement does not show, and with ON DELETE CASCADE that somewhere is the child table. Interviewers like this debugging question because it tests whether we know what the database does behind a foreign key and which engine creates which index for us. In this answer, we will recap how foreign keys are enforced, follow the delete step by step, see why other sessions block, apply the fix, compare with MySQL, and audit for other missing indexes.

1A Quick Recap of Foreign Key Enforcement

A foreign key says that every orders.customer_id must match an existing customers.id. The database enforces it in two directions. When we insert or update an order, it checks that the customer exists, which is fast because customers.id is a primary key and has an index. When we delete or update a customer, it must find the orders that reference that customer, and either cascade the delete to them, set them to NULL, or refuse the delete, depending on the ON DELETE rule.

The second direction searches the child table by the referencing column. PostgreSQL requires an index on the referenced column, the parent's key, but it does not create one on the referencing column, the child's foreign key. Without that index, finding a customer's orders means reading the whole orders table.

2Following the Delete Step by Step

Here is what happens when we delete customer 42, with orders holding 80 million rows and no index on customer_id.

Java
DELETE FROM customers WHERE id = 42;
-- 1. find customer 42 by primary key -- 0.1 ms
-- 2. foreign key trigger: DELETE FROM orders WHERE customer_id = 42
-- no index on customer_id -> Seq Scan on orders -- 80,000,000 rows read, about 40 s
-- 3. delete the 12 matching orders, cascade further if they have children
-- DELETE 1 Time: 40213 ms
Step throughDELETE customer 42 with an unindexed FK
Session AcustomersordersSession B
DELETE id = 42, lock the row
FK trigger: find customer_id = 42
Seq Scan of 80 M rows, about 40 s
UPDATE customer 42 -> waits on A's row lock
12 orders deleted
COMMIT, locks released
B continues

7 messages in order. Play them, or step through them one at a time.

The slow part is the hidden child-table query, and every lock taken by the delete is held for its whole duration.

Notice that EXPLAIN on the DELETE does not show the cascade, because foreign keys are enforced by internal triggers that run their own queries. EXPLAIN ANALYZE reports the time spent in the trigger, for example 'Trigger for constraint orders_customer_id_fkey: time=40150', which is the clearest evidence. The scan cost is per deleted customer, so a batch that deletes 100 customers scans the orders table 100 times.

3Why Other Sessions Block

The delete holds a lock on customer 42 from the first step until the transaction commits. Any session that wants to update or delete that customer waits. Sessions that insert new orders for customer 42 must lock the customer row too, in a shared mode, to make sure it still exists, so they conflict with the delete and wait as well. As the cascade deletes orders, those order rows are locked for the rest of the transaction. If the delete runs inside a larger transaction, or deletes several customers, these locks pile up for minutes, and application requests time out.

4The Fix: Index the Referencing Column

We create the index without blocking writes on orders. CONCURRENTLY builds the index while the table stays in use, at the cost of a slower build.

SQL
CREATE INDEX CONCURRENTLY orders_customer_id_idx ON orders (customer_id);
EXPLAIN ANALYZE DELETE FROM customers WHERE id = 43;
-- Delete on customers (actual time=0.05..0.05)
-- Trigger for constraint orders_customer_id_fkey: time=0.21 calls=1
-- Execution Time: 0.4 ms

So the delete drops from 40 seconds to under a millisecond, and the locks are held for that short time only. If an existing index already starts with customer_id, such as one on (customer_id, created_at), it serves the foreign key too and no new index is needed. A concurrent build that fails leaves an invalid index, which we drop and retry.

5What About MySQL?

InnoDB requires an index on the foreign key column. If none exists when we add the foreign key, InnoDB creates one automatically, and it refuses to drop an index that a foreign key needs. So this exact trap rarely happens in MySQL. PostgreSQL leaves the decision to us, because an index costs writes and space, and some child tables are never searched by their foreign key.

6Auditing Every Foreign Key

Where one foreign key lacks an index, others usually do too. A catalog query over pg_constraint and pg_index lists foreign keys whose columns are not the leading columns of any index. We run it on every database and add the missing indexes where the parent is ever deleted or its key updated.

SQL
SELECT c.conrelid::regclass AS child_table, c.conname, a.attname AS fk_column
FROM pg_constraint c
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = c.conkey[1]
WHERE c.contype = 'f'
AND NOT EXISTS (
SELECT 1 FROM pg_index i
WHERE i.indrelid = c.conrelid AND i.indkey[0] = c.conkey[1])
ORDER BY 1;
-- orders | orders_customer_id_fkey | customer_id
-- payments | payments_order_id_fkey | order_id

This version checks the first column of each foreign key, which covers the common single-column case; multi-column foreign keys need a fuller comparison of conkey with the index's leading columns. Adding the check to CI for new migrations keeps the problem from coming back.

7Follow-up questions

Is there any case where you would deliberately leave a foreign key unindexed?

Yes, when the parent row is never deleted and its key never changes, and nobody queries the child by that column. A lookup table of currencies or countries referenced by a huge, write-heavy events table is a typical example: an index on events.currency_code would cost write time and gigabytes of space for a check that never happens. We record the decision in the schema, and if the parent ever needs a delete, we add the index first or delete in a maintenance window.

Why does PostgreSQL not create the index automatically?

Because it is not always worth its cost, and PostgreSQL leaves index choices to the schema designer. The referenced side must be unique, so it always has an index; the referencing side can have millions of rows per value or never be searched. Creating indexes silently would also hide their write and storage cost.

8Conclusion

In this answer, we traced a 40-second delete to an unindexed foreign key. PostgreSQL does not index the referencing column, so ON DELETE CASCADE scans the whole orders table for every deleted customer while the delete holds locks that block other sessions. CREATE INDEX CONCURRENTLY on orders.customer_id turned the delete into a sub-millisecond operation. MySQL InnoDB creates such indexes automatically, so the trap is mainly PostgreSQL's, and a catalog query over pg_constraint and pg_index finds the other foreign keys that need one.

What to say in the interviewAbout 80 seconds aloud

Open with

Deleting one customer by primary key should take milliseconds, so the 40 seconds is almost certainly the cascade. PostgreSQL enforces the foreign key with an internal trigger that runs a query like delete from orders where customer_id equals 42.

Then, if they want more

PostgreSQL requires an index on the referenced primary key, but it does not create one on the referencing column, so without an index on orders.customer_id that query is a sequential scan of the whole orders table, for every deleted customer. EXPLAIN ANALYZE shows it as time spent in the trigger for the constraint.

Meanwhile the delete holds the lock on the customer row, and later on the order rows, so other sessions that update that customer or insert orders for it have to wait, which is why other requests block.

The fix is to create an index on orders.customer_id with CREATE INDEX CONCURRENTLY so we do not block writes while building it, and then the delete takes well under a millisecond. In MySQL InnoDB this is less likely, because it automatically creates an index on a foreign key column if none exists.

I would then audit all foreign keys with a catalog query over pg_constraint and pg_index to find others without a supporting index, and add a check in CI for new migrations.

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 "Deleting one row from customers takes 40 seconds and blocks other sessions....": https://scrivio-interview-library.netlify.app/databases/indexing/96986ee6bb/