[{"data":1,"prerenderedAt":6},["ShallowReactive",2],{"post-content-create-index-concurrently-invalid-index":3},{"content":4,"lastModified":5},"\u003Ch2>A migration that timed out and left something behind\u003C\u002Fh2>\n\n\u003Cp>Last Tuesday a migration hit its statement timeout halfway through a \u003Ccode>CREATE INDEX CONCURRENTLY\u003C\u002Fcode> on a 3M row \u003Ccode>orders\u003C\u002Fcode> table. The rerun came back in 16ms with \u003Ccode>relation \"idx_orders_customer\" already exists\u003C\u002Fcode>. A failure that fast means the first attempt left a corpse, so I opened the catalog instead of dropping and retrying on instinct.\u003C\u002Fp>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cp>Two of them cost nothing. One costs the full price of a working index while never returning a single row.\u003C\u002Fp>\n\n\u003Ch2>indisvalid tells you there is a problem. indisready tells you which one\u003C\u002Fh2>\n\n\u003Cp>Every writeup stops at \u003Ccode>indisvalid = false\u003C\u002Fcode>. The flag that decides whether you have an emergency is the one beside it. Three failures, one query:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-sql\">SELECT i.indexrelid::regclass AS idx, i.indisvalid, i.indisready,\n       pg_size_pretty(pg_relation_size(i.indexrelid)) AS size\nFROM pg_index i\nWHERE i.indrelid = 'orders'::regclass AND NOT i.indisvalid;\n\n-- 1. cancelled during the first table scan\n      idx       | indisvalid | indisready |  size\n----------------+------------+------------+---------\n idx_ord_status | f          | f          | 0 bytes\n\n-- 2. unique violation caught in the second scan\n idx_orders_cust| f          | f          | 0 bytes\n\n-- 3. cancelled after both scans, while waiting for old transactions\n idx_ord_status | f          | t          | 4864 kB\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>\u003Ccode>indisready = false\u003C\u002Fcode> 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.\u003C\u002Fp>\n\n\u003Cp>\u003Ccode>indisready = true\u003C\u002Fcode> 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.\u003C\u002Fp>\n\n\u003Ch2>What the leftover costs, in milliseconds\u003C\u002Fh2>\n\n\u003Cp>Same data into two identical 200,000 row tables, one with an \u003Ccode>indisready\u003C\u002Fcode> leftover and one with no secondary index. 30,000 row \u003Ccode>INSERT ... SELECT\u003C\u002Fcode> batches, eight rounds, order alternated so checkpoint noise lands on both. Local Postgres 16.15 with \u003Ccode>fsync\u003C\u002Fcode> off, since I wanted index maintenance cost and not drive latency.\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-sql\">no secondary index        21.7 ms   -- median of 8 rounds\nleftover, indisready = t  59.0 ms   -- median of 8 rounds\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>0.7 microseconds per row against 2.0: 2.7x the insert time for a structure no query will ever consult.\u003C\u002Fp>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cfigure>\n  \u003Cimg src=\"https:\u002F\u002Fimages.pexels.com\u002Fphotos\u002F6275820\u002Fpexels-photo-6275820.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1200\" alt=\"Wooden jigsaw puzzle with one piece missing, leaving a blue gap in the assembled surface\" loading=\"lazy\" width=\"1200\" height=\"800\">\n  \u003Cfigcaption>Complete enough to look finished, useless until the last piece is in.\u003C\u002Ffigcaption>\n\u003C\u002Ffigure>\n\n\u003Ch2>Drop it without stopping the site\u003C\u002Fh2>\n\n\u003Cp>Plain \u003Ccode>DROP INDEX\u003C\u002Fcode> takes AccessExclusiveLock. I checked \u003Ccode>pg_locks\u003C\u002Fcode> 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 \u003Ccode>idle in transaction\u003C\u002Fcode> session open from a forgotten psql window, that wall has no ceiling.\u003C\u002Fp>\n\n\u003Cp>\u003Ccode>DROP INDEX CONCURRENTLY\u003C\u002Fcode> asks for something smaller. I caught it mid-wait against another session holding the same lock:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-sql\">  pid   | wait_event_type |            query             |          mode           | granted\n--------+-----------------+------------------------------+-------------------------+---------\n 46265  | Timeout         | LOCK TABLE big IN SHARE ...  | ShareUpdateExclusiveLock| t\n 46500  | Lock            | DROP INDEX CONCURRENTLY ...  | ShareUpdateExclusiveLock| f\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>ShareUpdateExclusiveLock coexists with reads and writes, and only collides with other DDL, VACUUM and a second concurrent build. That is also why you check \u003Ccode>pg_stat_progress_create_index\u003C\u002Fcode> 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.\u003C\u002Fp>\n\n\u003Ch2>The retry line that hides all of this\u003C\u002Fh2>\n\n\u003Cp>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:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-sql\">CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_ord_status ON orders (status);\n\nNOTICE:  relation \"idx_ord_status\" already exists, skipping\nCREATE INDEX\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cp>Drop first, with the concurrent form, then build:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-sql\">DROP INDEX CONCURRENTLY IF EXISTS idx_ord_status;\nCREATE INDEX CONCURRENTLY idx_ord_status ON orders (status);\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>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 \u003Ccode>REINDEX INDEX CONCURRENTLY\u003C\u002Fcode> against the \u003Ccode>indisready\u003C\u002Fcode> leftover and it went through: no error, \u003Ccode>indisvalid\u003C\u002Fcode> flipped to true, the planner used it on the next \u003Ccode>EXPLAIN\u003C\u002Fcode>. 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.\u003C\u002Fp>\n\n\u003Cp>The query I run after any migration that touches an index. Zero byte rows are noise; anything with a size and \u003Ccode>indisready = true\u003C\u002Fcode> is a real problem:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-sql\">SELECT c.relname AS table_name, i.indexrelid::regclass AS index_name,\n       i.indisready, pg_size_pretty(pg_relation_size(i.indexrelid)) AS size\nFROM pg_index i JOIN pg_class c ON c.oid = i.indrelid\nWHERE NOT i.indisvalid\nORDER BY pg_relation_size(i.indexrelid) DESC;\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>These probes live in \u003Ca href=\"\u002Fsnippetark\u002F\" rel=\"noopener noreferrer\" target=\"_blank\">Snippet Ark\u003C\u002Fa> 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 \u003Ca href=\"\u002Fposts\u002Fpostgres-not-using-my-index-what-i-check\u002F\">that checklist\u003C\u002Fa> is where I start. \u003Ca href=\"\u002Fposts\u002Fpostgres-index-bloat-avg-leaf-density\u002F\">Index bloat\u003C\u002Fa> is the slower version of this same tax, with no failed build involved.\u003C\u002Fp>\n\n\u003Cp>The lesson: \u003Ccode>indisvalid\u003C\u002Fcode> 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.\u003C\u002Fp>\n","2026-11-25",1795678594071]