postgres.git / summary / log / commit / refs

commit    17c5a0659fcbfa8f831cc7a887ea3b457349401d
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Wed Oct 28 05:23:17 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0

    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 <saicharh@amazon.com>
    Author: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
    Reviewed-by: Chao Li <li.evan.chao@gmail.com>
    Reviewed-by: Michael Paquier <michael@paquier.xyz>
    Discussion: https://postgr.es/m/CALj2ACUAv=7wjOV3pRFhQP4WHME_JazuhoCr01i4_F2XA=JKog@mail.gmail.com
    Backpatch-through: 14


src/backend/utils/cache/relcache.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index dff3a29777e..cda85969256 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -2288,14 +2288,15 @@ RelationReloadIndexInfo(Relation relation) RelationInitPhysicalAddr(relation); /* - * For a non-system index, there are fields of the pg_index row that are - * allowed to change, so re-read that row and update the relcache entry. - * Most of the info derived from pg_index (such as support function lookup - * info) cannot change, and indeed the whole point of this routine is to - * update the relcache entry without clobbering that data; so wholesale + * For a non-catalog index (this includes the TOAST index of a user + * table), there are fields of the pg_index row that are allowed to + * change, so re-read that row and update the relcache entry. Most of the + * info derived from pg_index (such as support function lookup info) + * cannot change, and indeed the whole point of this routine is to update + * the relcache entry without clobbering that data; so wholesale * replacement is not appropriate. */ - if (!IsSystemRelation(relation)) + if (!IsCatalogRelation(relation)) { HeapTuple tuple; Form_pg_index index; [parent: 5dc3cfbb9023]