postgres.git / summary / log / commit / refs
commit b3852eec68aec0078460e5409db3f78f2f200ebf
Author: Michael Paquier <michael@paquier.xyz>
Date: Wed Oct 28 05:22:27 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/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(-)
diff --git a/src/backend/access/common/toast_internals.c b/src/backend/access/common/toast_internals.c
index 52972d894e2..d0bcc17a163 100644
--- a/src/backend/access/common/toast_internals.c
+++ b/src/backend/access/common/toast_internals.c
@@ -23,6 +23,7 @@
#include "catalog/catalog.h"
#include "miscadmin.h"
#include "utils/fmgroids.h"
+#include "utils/injection_point.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
@@ -160,6 +161,8 @@ toast_save_datum(Relation rel, Datum value,
&toastidxs,
&num_indexes);
+ INJECTION_POINT("toast-save-datum-indexes-opened");
+
/*
* Get the data pointer and length, and compute va_rawsize and va_extinfo.
*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index b8d903505a5..a465f267bcc 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -2328,14 +2328,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;
diff --git a/src/test/modules/injection_points/Makefile b/src/test/modules/injection_points/Makefile
index 3843ffc9e1e..e3d58d2888e 100644
--- a/src/test/modules/injection_points/Makefile
+++ b/src/test/modules/injection_points/Makefile
@@ -19,6 +19,7 @@ WAIT_CLEANUP_TST = $(if $(filter win32,$(PORTNAME)),,wait_cleanup)
ISOLATION = inplace \
reindex_concurrently_deferred \
+ reindex_concurrently_toast \
syscache-update-pruned \
$(WAIT_CLEANUP_TST) \
heap_lock_update \
diff --git a/src/test/modules/injection_points/expected/reindex_concurrently_toast.out b/src/test/modules/injection_points/expected/reindex_concurrently_toast.out
new file mode 100644
index 00000000000..8730b2eac9a
--- /dev/null
+++ b/src/test/modules/injection_points/expected/reindex_concurrently_toast.out
@@ -0,0 +1,46 @@
+Parsed test spec with 3 sessions
+
+starting permutation: lock reindex ins1 unlock snapshot wake inserted ins2 release reindexed read
+injection_points_attach
+-----------------------
+
+(1 row)
+
+step lock: BEGIN; LOCK TABLE reind_toast IN ROW EXCLUSIVE MODE;
+step reindex: REINDEX TABLE CONCURRENTLY reind_toast; <waiting ...>
+step ins1: INSERT INTO reind_toast VALUES (2, repeat('y', 10000)); <waiting ...>
+step unlock: COMMIT;
+step snapshot: BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1;
+?column?
+--------
+ 1
+(1 row)
+
+step wake:
+ SELECT injection_points_detach('toast-save-datum-indexes-opened');
+ SELECT injection_points_wakeup('toast-save-datum-indexes-opened');
+
+injection_points_detach
+-----------------------
+
+(1 row)
+
+injection_points_wakeup
+-----------------------
+
+(1 row)
+
+step ins1: <... completed>
+step inserted:
+step ins2: INSERT INTO reind_toast VALUES (3, repeat('z', 10000));
+step release: COMMIT;
+step reindex: <... completed>
+step reindexed:
+step read: SELECT id, left(val, 1) FROM reind_toast ORDER BY id;
+id|left
+--+----
+ 1|x
+ 2|y
+ 3|z
+(3 rows)
+
diff --git a/src/test/modules/injection_points/meson.build b/src/test/modules/injection_points/meson.build
index fefbcabcf76..af54f858c38 100644
--- a/src/test/modules/injection_points/meson.build
+++ b/src/test/modules/injection_points/meson.build
@@ -29,6 +29,7 @@ test_install_data += files(
injection_points_isolation = [
'inplace',
'reindex_concurrently_deferred',
+ 'reindex_concurrently_toast',
'syscache-update-pruned',
'heap_lock_update',
'on_conflict_probe_window',
diff --git a/src/test/modules/injection_points/specs/reindex_concurrently_toast.spec b/src/test/modules/injection_points/specs/reindex_concurrently_toast.spec
new file mode 100644
index 00000000000..f3e25a833e5
--- /dev/null
+++ b/src/test/modules/injection_points/specs/reindex_concurrently_toast.spec
@@ -0,0 +1,55 @@
+# REINDEX CONCURRENTLY of a TOAST index racing a TOAST insert.
+#
+# s2 holds the TOAST indexes open while the new index becomes ready, then
+# inserts again before the swap.
+setup
+{
+ CREATE EXTENSION injection_points;
+ CREATE TABLE reind_toast (id int, val text);
+ ALTER TABLE reind_toast ALTER COLUMN val SET STORAGE EXTERNAL;
+ INSERT INTO reind_toast VALUES (1, repeat('x', 10000));
+}
+
+teardown
+{
+ DROP TABLE reind_toast;
+ DROP EXTENSION injection_points;
+}
+
+session s1
+step reindex { REINDEX TABLE CONCURRENTLY reind_toast; }
+# Empty step, to force the REINDEX to finish.
+step reindexed { }
+
+# Writer, stopped with the TOAST indexes open.
+session s2
+setup
+{
+ SELECT injection_points_set_local();
+ SELECT injection_points_attach('toast-save-datum-indexes-opened', 'wait');
+}
+# TOAST indexes opened, new index is not ready yet.
+step ins1 { INSERT INTO reind_toast VALUES (2, repeat('y', 10000)); }
+# Empty step, to force ins1 to finish.
+step inserted { }
+# Runs once new index is validated, before swapping.
+step ins2 { INSERT INTO reind_toast VALUES (3, repeat('z', 10000)); }
+# left() always detoasts the value.
+step read { SELECT id, left(val, 1) FROM reind_toast ORDER BY id; }
+
+session s3
+# Holds the REINDEX back until ins1 has opened the TOAST indexes.
+step lock { BEGIN; LOCK TABLE reind_toast IN ROW EXCLUSIVE MODE; }
+step unlock { COMMIT; }
+# Keeps the REINDEX waiting for old snapshots after it validates the new
+# index.
+step snapshot { BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1; }
+# Resumes ins1.
+step wake
+{
+ SELECT injection_points_detach('toast-save-datum-indexes-opened');
+ SELECT injection_points_wakeup('toast-save-datum-indexes-opened');
+}
+step release { COMMIT; }
+
+permutation lock reindex ins1 unlock snapshot wake inserted ins2 release reindexed read
[parent: 4aeefb26d62b]