CREATE INDEX CONCURRENTLY Failed? Check indisready Before You Drop
A migration that timed out and left something behind
Last Tuesday a migration hit its statement timeout halfway through a CREATE INDEX CONCURRENTLY on a 3M row orders table. The rerun came back in 16ms with relation "idx_orders_customer" already exists. A failure that fast means the first attempt left a corpse, so I opened the catalog instead of dropping and retrying on instinct.
The cleanup advice online is one sentence: drop the INVALID index and try again. Right for one failure path, quietly wrong for two others. I rebuilt the scenario in a scratch cluster on 16.15 and timed each leftover.
Two of them cost nothing. One costs the full price of a working index while never returning a single row.
indisvalid tells you there is a problem. indisready tells you which one
Every writeup stops at indisvalid = false. The flag that decides whether you have an emergency is the one beside it. Three failures, one query:
SELECT i.indexrelid::regclass AS idx, i.indisvalid, i.indisready,
pg_size_pretty(pg_relation_size(i.indexrelid)) AS size
FROM pg_index i
WHERE i.indrelid = 'orders'::regclass AND NOT i.indisvalid;
-- 1. cancelled during the first table scan
idx | indisvalid | indisready | size
----------------+------------+------------+---------
idx_ord_status | f | f | 0 bytes
-- 2. unique violation caught in the second scan
idx_orders_cust| f | f | 0 bytes
-- 3. cancelled after both scans, while waiting for old transactions
idx_ord_status | f | t | 4864 kB
indisready = false means the index was never registered as live. No INSERT maintains it, because there is no index to maintain. Those first two rows are catalog entries and nothing else. Dropping them is housekeeping you can do at 3am, or never.
indisready = true is the one you don't want sitting there. Every write on the table maintains a structure the planner is forbidden to read. It is also the leftover that grows biggest, because the build reached the end before something cancelled it. A deploy timeout at the wrong moment, or an operator hitting Ctrl-C because the build was taking too long.
What the leftover costs, in milliseconds
Same data into two identical 200,000 row tables, one with an indisready leftover and one with no secondary index. 30,000 row INSERT ... SELECT batches, eight rounds, order alternated so checkpoint noise lands on both. Local Postgres 16.15 with fsync off, since I wanted index maintenance cost and not drive latency.
no secondary index 21.7 ms -- median of 8 rounds
leftover, indisready = t 59.0 ms -- median of 8 rounds
0.7 microseconds per row against 2.0: 2.7x the insert time for a structure no query will ever consult.
The obvious objection is that the second table does more file writes, so maybe this is about bytes rather than index logic. Fair. I built a valid index on the same column of the clean table and reran once both tables had grown: 85, 99 and 110ms per 30,000 rows against 76, 86 and 88 for the leftover. Read those against each other, not against the earlier numbers. Same cost. A dead index and a live one charge you identically on writes, and only one gives anything back on reads.
Drop it without stopping the site
Plain DROP INDEX takes AccessExclusiveLock. I checked pg_locks instead of trusting memory: granted. It does not care that the index is dead. It waits for every running query on that table to finish, and everything arriving behind it queues up. A forty millisecond report in flight ahead of your drop becomes a forty millisecond wall for everyone else, and if someone left an idle in transaction session open from a forgotten psql window, that wall has no ceiling.
DROP INDEX CONCURRENTLY asks for something smaller. I caught it mid-wait against another session holding the same lock:
pid | wait_event_type | query | mode | granted
--------+-----------------+------------------------------+-------------------------+---------
46265 | Timeout | LOCK TABLE big IN SHARE ... | ShareUpdateExclusiveLock| t
46500 | Lock | DROP INDEX CONCURRENTLY ... | ShareUpdateExclusiveLock| f
ShareUpdateExclusiveLock coexists with reads and writes, and only collides with other DDL, VACUUM and a second concurrent build. That is also why you check pg_stat_progress_create_index before dropping anything: an index still being built looks invalid too, and killing it is how you manufacture the problem you meant to clean up.
The retry line that hides all of this
Because the name stays occupied, the obvious retry fails, so people make it idempotent. This is the sentence that turns a broken deploy into a green one:
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_ord_status ON orders (status);
NOTICE: relation "idx_ord_status" already exists, skipping
CREATE INDEX
Exit code 0. Index still invalid, still 0 bytes, still absent from every plan. CI passes, the migration is marked applied, and the index meant to make the dashboard fast does not exist. The only thing separating this from success is a NOTICE most runners swallow.
Drop first, with the concurrent form, then build:
DROP INDEX CONCURRENTLY IF EXISTS idx_ord_status;
CREATE INDEX CONCURRENTLY idx_ord_status ON orders (status);
For a build that almost finished there is a shorter road. Older docs say REINDEX cannot run concurrently, so drop and start over. On 16.15 I ran REINDEX INDEX CONCURRENTLY against the indisready leftover and it went through: no error, indisvalid flipped to true, the planner used it on the next EXPLAIN. It rebuilds everything, so it is no cheaper than starting over, but it handles the name swap itself, which is the part I keep fumbling by hand at 2am.
The query I run after any migration that touches an index. Zero byte rows are noise; anything with a size and indisready = true is a real problem:
SELECT c.relname AS table_name, i.indexrelid::regclass AS index_name,
i.indisready, pg_size_pretty(pg_relation_size(i.indexrelid)) AS size
FROM pg_index i JOIN pg_class c ON c.oid = i.indrelid
WHERE NOT i.indisvalid
ORDER BY pg_relation_size(i.indexrelid) DESC;
These probes live in Snippet Ark next to the rest of my EXPLAIN material, because I refuse to retype them from memory during an incident. If the index turns out to be valid and Postgres still ignores it, the problem is elsewhere, and that checklist is where I start. Index bloat is the slower version of this same tax, with no failed build involved.
The lesson: indisvalid is a boolean about usability, and usability is the least interesting property of a leftover. Whether every write maintains the thing is what decides if you fix it tonight or add it to a list.