Michael Paquier 17c5a0659f 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 8ec943856a, and was only used for CREATE INDEX
CONCURRENTLY.  5dc92b844e, 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 5dc92b844e.

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
2026-10-28 14:23:17 +09:00
2025-08-14 12:03:27 -04:00
2019-12-18 09:13:13 +01:00
2025-08-15 10:31:52 +02:00
2022-12-04 15:23:00 -05:00
2026-08-10 16:56:00 -04:00
2026-08-10 16:56:00 -04:00
2026-01-01 13:24:10 -05:00
2020-02-10 20:47:50 +01:00
2026-08-10 16:56:00 -04:00
2020-02-10 20:47:50 +01:00
2020-02-10 20:47:50 +01:00

PostgreSQL Database Management System
=====================================

This directory contains the source code distribution of the PostgreSQL
database management system.

PostgreSQL is an advanced object-relational database management system
that supports an extended subset of the SQL standard, including
transactions, foreign keys, subqueries, triggers, user-defined types
and functions.  This distribution also contains C language bindings.

PostgreSQL has many language interfaces, many of which are listed here:

	https://www.postgresql.org/download/

See the file INSTALL for instructions on how to build and install
PostgreSQL.  That file also lists supported operating systems and
hardware platforms and contains information regarding any other
software packages that are required to build or run the PostgreSQL
system.  Copyright and license information can be found in the
file COPYRIGHT.  A comprehensive documentation set is included in this
distribution; it can be read as described in the installation
instructions.

The latest version of this software may be obtained at
https://www.postgresql.org/download/.  For more information look at our
web site located at https://www.postgresql.org/.
Languages
C 83.9%
PLpgSQL 6.2%
Perl 5.4%
Yacc 1.2%
Meson 0.7%
Other 2.4%