Fix stale pg_index flags in the relcache for TOAST indexes RelationReloadIndexInfo() re-reads the mutable pg_index fields (indisready, indisvalid, indislive, etc.) only for indexes that are not system relations. The TOAST index of a user table lives in pg_toast and counts as one, but REINDEX CONCURRENTLY changes these fields on it like on any other index.
A backend that had the TOAST indexes open when the invalidation arrived kept indisready = false for the new index, and did not insert its chunks there. Once the old index is dropped, reading such a value fails with "missing chunk number 0 for toast value N in pg_toast_X". The chunks remain in the TOAST table, only the index references were gone, so a rebuild of the TOAST index would fix this issue. This issue is fixed by re-reading the pg_index row for TOAST indexes, filling in their relcache fields. This is the same as for an index on a user table. Previouly, IsSystemRelation() was used, meaning the pg_index data was not re-read for a TOAST relation or a relation with a pinned OID (created by initdb). This check is meant for indexes whose values never change, so IsCatalogRelation() is fine, implying that only indexes created by initdb should be skipped (REINDEX CONCURRENTLY does not work on catalogs). This code is as old as 8ec943856a4, and was only used for CREATE INDEX CONCURRENTLY. 5dc92b844e68, that has introduced REINDEX CONCURRENTLY, has made possible to reach the problem with TOAST indexes due to these getting concurrently built, validated and then swapped. A test is added, able to reproduce the issue. It forces an insert before a swap, with a second session that maintains the TOAST indexes open while the new index becomes ready. This has been slightly simplified by me compared to what the author has sent. Oversight in 5dc92b844e68. Reported-by: Saicharanreddy Chinthareddy <[email protected]> Author: Bharath Rupireddy <[email protected]> Reviewed-by: Chao Li <[email protected]> Reviewed-by: Michael Paquier <[email protected]> Discussion: https://postgr.es/m/CALj2ACUAv=7wjOV3pRFhQP4WHME_JazuhoCr01i4_F2XA=j...@mail.gmail.com Backpatch-through: 14 Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/27417270ab4e929aa3731e55945872206c425f75 Modified Files -------------- src/backend/access/common/toast_internals.c | 3 ++ src/backend/utils/cache/relcache.c | 13 ++--- src/test/modules/injection_points/Makefile | 1 + .../expected/reindex_concurrently_toast.out | 46 ++++++++++++++++++ src/test/modules/injection_points/meson.build | 1 + .../specs/reindex_concurrently_toast.spec | 55 ++++++++++++++++++++++ 6 files changed, 113 insertions(+), 6 deletions(-)
