pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix stale pg_index flags in the relcache for TOAST indexes
Date: Wed, 28 Oct 2026 05:24:17 +0000
Message-ID: <E1xLw93-0000000159j-0Xiu@gemulon.postgresql.org> (raw)

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

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/b3852eec68aec0078460e5409db3f78f2f200ebf

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(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1xLw93-0000000159j-0Xiu@gemulon.postgresql.org>
Permalink:  ../E1xLw93-0000000159j-0Xiu@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xLw93-0000000159j-0Xiu@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: michael@paquier.xyz
  Subject: Re: pgsql: Fix stale pg_index flags in the relcache for TOAST indexes
  In-Reply-To: <E1xLw93-0000000159j-0Xiu@gemulon.postgresql.org>
  References: <E1xLw93-0000000159j-0Xiu@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox