Quick revision
The short answer
The likely cause is a missing index on orders.customer_id. Every delete of a customer makes PostgreSQL check orders for referencing rows, and without that index the check is a sequential scan of the whole orders table while the transaction holds its locks. The fix is CREATE INDEX CONCURRENTLY on orders(customer_id), followed by a catalog query that finds every other unindexed foreign key.
What to remember
In this answer, we saw that deleting a parent row makes PostgreSQL search the child table, and that without an index on orders.customer_id that search is a full scan which holds locks for 40 seconds and blocks other writers. We confirmed it with the trigger time in EXPLAIN ANALYZE, fixed it with CREATE INDEX CONCURRENTLY, and used a catalog query plus a review rule to catch the same mistake everywhere else. MySQL's InnoDB creates such indexes automatically, which is why this is mainly a PostgreSQL lesson.
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.
The likely cause is a missing index on orders.customer_id. Every delete of a customer makes PostgreSQL check orders for referencing rows, and without that index the check is a sequential scan of the whole orders table while the transaction holds its locks. The fix is CREATE INDEX CONCURRENTLY on orders(customer_id), followed by a catalog query that finds every other unindexed foreign key.
Why this question is asked
A delete of one row taking 40 seconds sounds like a database bug, but it is one of the most common performance surprises in PostgreSQL, and it often appears only after a table has grown for a year. Interviewers ask it because it tests whether we know what a foreign key actually does on the parent side, and whether we can fix it safely on a live system. In this answer, we will recap what the database does when a parent row is deleted, reproduce the slow plan, see why other writes block, fix it without downtime, and find every other foreign key with the same problem.
1A Quick Recap of What a Foreign Key Checks
A foreign key from orders(customer_id) to customers(id) protects both sides. When we insert an order, PostgreSQL looks up the customer by its primary key, which is always indexed, so that side is cheap. When we delete a customer, PostgreSQL must make sure no order still points to it (or cascade to those orders, depending on the ON DELETE action). That search goes the other way, from the parent to the child table, and it uses whatever index exists on orders.customer_id.
PostgreSQL creates indexes for primary keys and unique constraints, but never for foreign key columns. So unless someone added it, there is no index on customer_id, and the referential check has only one way to find child rows: read the whole orders table.
2Reproducing the Slow Delete
Internally, the check is an ordinary query run by a system trigger. We can see its cost by running the equivalent query ourselves.
- Find customer 123456Primary key index, under 1 ms
- Lock and delete the rowRow lock held until commit
- Run the foreign key triggerLook for orders with this customer_id
- Index on orders.customer_id?YesIndex lookup, a few msNoSequential scan, 40 s
- Commit and release locksWaiting writers can continue
5 steps in order. Play them, or step through them one at a time.
The customers table has 2 million rows, but the delete itself is fast: it finds the customer by primary key in microseconds. Almost all the time sits in the line 'Trigger for constraint', which is the referential check scanning tens of millions of orders. Notice that EXPLAIN ANALYZE on a DELETE lists trigger time separately, which is the quickest way to prove the diagnosis. (EXPLAIN ANALYZE really runs the delete, so wrap it in BEGIN and ROLLBACK on a real system.)
3Why Other Writes Block
The delete does not lock the orders table, but it holds a row lock on the customer row for all 40 seconds, and the check takes a key share lock on any order it finds. Any other transaction that wants to insert an order for that customer, update the customer, or delete it again must wait for our transaction to finish. If the application deletes many customers in a batch, or retries on timeout, those 40-second transactions stack up, connection pools fill, and the whole service appears frozen.
So the slow scan is not only a latency problem. Long transactions are what turn a slow query into an outage, because locks are held until commit.
4Fixing It Without Blocking the Table
A plain CREATE INDEX blocks inserts, updates and deletes on orders for the whole build, which on a big table can be minutes. CREATE INDEX CONCURRENTLY builds the index while writes continue, at the cost of being slower and needing two passes over the table.
There is a catch: CONCURRENTLY cannot run inside a transaction, so migration tools such as Flyway or Liquibase need that migration marked as non-transactional (check your tool's version for the exact setting). If the build fails halfway, it leaves an index marked INVALID that we must drop and create again. After the fix, the same delete takes well under a millisecond.
5Finding Every Other Unindexed Foreign Key
One missing index usually means there are more. The catalog tells us which foreign keys have no index whose leading columns match the constraint columns.
The query compares the first columns of each index with the columns of the constraint, in any order. We then make 'every foreign key column is indexed, or the exception is written down' a review rule, and ideally a CI check that runs this query against a migrated test database. MySQL users rarely meet this problem, because InnoDB requires an index on foreign key columns and creates one automatically if none exists. It is mostly a PostgreSQL (and Oracle and SQL Server) habit to learn.
6Follow-up questions
How would you find all unindexed foreign keys?
Query pg_constraint for constraints of type 'f' and, for each one, check pg_index for an index on the same table whose leading columns are exactly the constraint's columns. Anything without such an index is a candidate. I would run that query in CI against a database built from the migrations, fail the build on new findings, and keep a short allow list for tables that are tiny or never have parents deleted.
Could we avoid the index by never deleting customers?
Sometimes, yes. If customers are only ever deactivated, the parent-side check never runs, but the index is still useful for the very common query 'orders of this customer'. In practice almost every foreign key column is also a join or filter column, so the index pays for itself twice.
Why does EXPLAIN on the DELETE not show the sequential scan directly?
Because the referential check is not part of the delete's plan. It runs inside a system trigger after each row, so the plan only shows the delete on customers and a separate line with the trigger's total time. Running the equivalent SELECT by hand shows the actual sequential scan.
7Conclusion
In this answer, we saw that deleting a parent row makes PostgreSQL search the child table, and that without an index on orders.customer_id that search is a full scan which holds locks for 40 seconds and blocks other writers. We confirmed it with the trigger time in EXPLAIN ANALYZE, fixed it with CREATE INDEX CONCURRENTLY, and used a catalog query plus a review rule to catch the same mistake everywhere else. MySQL's InnoDB creates such indexes automatically, which is why this is mainly a PostgreSQL lesson.
Open with
My first suspect is a missing index on orders.customer_id. Deleting a customer is quick by itself, but the foreign key means PostgreSQL has to check that no order still references that customer, and PostgreSQL does not index foreign key columns automatically.
Then, if they want more
Without the index, that check is a sequential scan of the whole orders table on every delete. I would confirm it with EXPLAIN ANALYZE inside a transaction I roll back: the output shows a separate trigger line for the foreign key constraint, and nearly all of the 40 seconds sits there. The blocking follows from the same thing.
The delete holds a row lock on the customer and takes key share locks on matching orders, and since the scan keeps the transaction open, other writers on those rows wait, and with retries or batches the pool fills up.
The fix is CREATE INDEX CONCURRENTLY on orders customer_id, which builds without blocking writes; it has to run outside a transaction, so the migration has to be marked non-transactional, and a failed build leaves an invalid index to drop.
Then I would query pg_constraint against pg_index to list every other foreign key without a matching index, and add a CI rule. In MySQL this rarely happens, because InnoDB creates the index for you.
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.
- 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 "Deleting a single row from customers takes 40 seconds and blocks other writes. The table...": https://scrivio-interview-library.netlify.app/databases/data-modeling/8ecb047532/