Skip to content

Commit 1432349

Browse files
committed
Fix potential assertion failure when reindexing a pg_class index.
When reindexing individual indexes on pg_class it was possible to either trigger an assertion failure: TRAP: FailedAssertion("!(!ReindexIsProcessingIndex(((index)->rd_id))) That's because reindex_index() called SetReindexProcessing() - which enables an asserts ensuring no index insertions happen into the index - before calling RelationSetNewRelfilenode(). That not correct for indexes on pg_class, because RelationSetNewRelfilenode() updates the relevant pg_class row, which needs to update the indexes. The are two reasons this wasn't noticed earlier. Firstly the bug doesn't trigger when reindexing all of pg_class, as reindex_relation has code "hiding" all yet-to-be-reindexed indexes. Secondly, the bug only triggers when the the update to pg_class doesn't turn out to be a HOT update - otherwise there's no index insertion to trigger the bug. Most of the time there's enough space, making this bug hard to trigger. To fix, move RelationSetNewRelfilenode() to before the SetReindexProcessing() (and, together with some other code, to outside of the PG_TRY()). To make sure the error checking intended by SetReindexProcessing() is more robust, modify CatalogIndexInsert() to check ReindexIsProcessingIndex() even when the update is a HOT update. Also add a few regression tests for REINDEXing of system catalogs. The last two improvements would have prevented some of the issues fixed in 5c15606 from being introduced in the first place. Reported-By: Michael Paquier Diagnosed-By: Tom Lane and Andres Freund Author: Andres Freund Reviewed-By: Tom Lane Discussion: https://postgr.es/m/20190418011430.GA19133@paquier.xyz Backpatch: 9.4-, the bug is present in all branches
1 parent 474982f commit 1432349

File tree

6 files changed

+91
-20
lines changed

6 files changed

+91
-20
lines changed

contrib/test_decoding/expected/rewrite.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ COMMIT;
103103
-- repeated rewrites in different transactions
104104
VACUUM FULL pg_class;
105105
VACUUM FULL pg_class;
106+
-- reindexing of important relations / indexes
107+
REINDEX TABLE pg_class;
108+
REINDEX INDEX pg_class_oid_index;
109+
REINDEX INDEX pg_class_tblspc_relfilenode_index;
106110
INSERT INTO replication_example(somedata, testcolumn1) VALUES (5, 3);
107111
BEGIN;
108112
INSERT INTO replication_example(somedata, testcolumn1) VALUES (6, 4);

contrib/test_decoding/sql/rewrite.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ COMMIT;
7474
VACUUM FULL pg_class;
7575
VACUUM FULL pg_class;
7676

77+
-- reindexing of important relations / indexes
78+
REINDEX TABLE pg_class;
79+
REINDEX INDEX pg_class_oid_index;
80+
REINDEX INDEX pg_class_tblspc_relfilenode_index;
81+
7782
INSERT INTO replication_example(somedata, testcolumn1) VALUES (5, 3);
7883

7984
BEGIN;

src/backend/catalog/index.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3699,29 +3699,35 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
36993699
*/
37003700
TransferPredicateLocksToHeapRelation(iRel);
37013701

3702+
/* Fetch info needed for index_build */
3703+
indexInfo = BuildIndexInfo(iRel);
3704+
3705+
/* If requested, skip checking uniqueness/exclusion constraints */
3706+
if (skip_constraint_checks)
3707+
{
3708+
if (indexInfo->ii_Unique || indexInfo->ii_ExclusionOps != NULL)
3709+
skipped_constraint = true;
3710+
indexInfo->ii_Unique = false;
3711+
indexInfo->ii_ExclusionOps = NULL;
3712+
indexInfo->ii_ExclusionProcs = NULL;
3713+
indexInfo->ii_ExclusionStrats = NULL;
3714+
}
3715+
3716+
/*
3717+
* Build a new physical relation for the index. Need to do that before
3718+
* "officially" starting the reindexing with SetReindexProcessing -
3719+
* otherwise the necessary pg_class changes cannot be made with
3720+
* encountering assertions.
3721+
*/
3722+
RelationSetNewRelfilenode(iRel, persistence, InvalidTransactionId,
3723+
InvalidMultiXactId);
3724+
3725+
/* ensure SetReindexProcessing state isn't leaked */
37023726
PG_TRY();
37033727
{
37043728
/* Suppress use of the target index while rebuilding it */
37053729
SetReindexProcessing(heapId, indexId);
37063730

3707-
/* Fetch info needed for index_build */
3708-
indexInfo = BuildIndexInfo(iRel);
3709-
3710-
/* If requested, skip checking uniqueness/exclusion constraints */
3711-
if (skip_constraint_checks)
3712-
{
3713-
if (indexInfo->ii_Unique || indexInfo->ii_ExclusionOps != NULL)
3714-
skipped_constraint = true;
3715-
indexInfo->ii_Unique = false;
3716-
indexInfo->ii_ExclusionOps = NULL;
3717-
indexInfo->ii_ExclusionProcs = NULL;
3718-
indexInfo->ii_ExclusionStrats = NULL;
3719-
}
3720-
3721-
/* We'll build a new physical relation for the index */
3722-
RelationSetNewRelfilenode(iRel, persistence, InvalidTransactionId,
3723-
InvalidMultiXactId);
3724-
37253731
/* Initialize the index and rebuild */
37263732
/* Note: we do not need to re-establish pkey setting */
37273733
index_build(heapRelation, iRel, indexInfo, false, true, true);

src/backend/catalog/indexing.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,15 @@ CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
8080
Datum values[INDEX_MAX_KEYS];
8181
bool isnull[INDEX_MAX_KEYS];
8282

83-
/* HOT update does not require index inserts */
83+
/*
84+
* HOT update does not require index inserts. But with asserts enabled we
85+
* want to check that it'd be legal to currently insert into the
86+
* table/index.
87+
*/
88+
#ifndef USE_ASSERT_CHECKING
8489
if (HeapTupleIsHeapOnly(heapTuple))
8590
return;
91+
#endif
8692

8793
/*
8894
* Get information from the state structure. Fall out if nothing to do.
@@ -104,8 +110,10 @@ CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
104110
for (i = 0; i < numIndexes; i++)
105111
{
106112
IndexInfo *indexInfo;
113+
Relation index;
107114

108115
indexInfo = indexInfoArray[i];
116+
index = relationDescs[i];
109117

110118
/* If the index is marked as read-only, ignore it */
111119
if (!indexInfo->ii_ReadyForInserts)
@@ -118,9 +126,18 @@ CatalogIndexInsert(CatalogIndexState indstate, HeapTuple heapTuple)
118126
Assert(indexInfo->ii_Expressions == NIL);
119127
Assert(indexInfo->ii_Predicate == NIL);
120128
Assert(indexInfo->ii_ExclusionOps == NULL);
121-
Assert(relationDescs[i]->rd_index->indimmediate);
129+
Assert(index->rd_index->indimmediate);
122130
Assert(indexInfo->ii_NumIndexKeyAttrs != 0);
123131

132+
/* see earlier check above */
133+
#ifdef USE_ASSERT_CHECKING
134+
if (HeapTupleIsHeapOnly(heapTuple))
135+
{
136+
Assert(!ReindexIsProcessingIndex(RelationGetRelid(index)));
137+
continue;
138+
}
139+
#endif /* USE_ASSERT_CHECKING */
140+
124141
/*
125142
* FormIndexDatum fills in its values and isnull parameters with the
126143
* appropriate values for the column(s) of the index.

src/test/regress/expected/create_index.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,6 +3071,24 @@ REINDEX (VERBOSE) TABLE reindex_verbose;
30713071
INFO: index "reindex_verbose_pkey" was reindexed
30723072
DROP TABLE reindex_verbose;
30733073
--
3074+
-- check that system tables can be reindexed
3075+
--
3076+
-- whole tables
3077+
REINDEX TABLE pg_class; -- mapped, non-shared, critical
3078+
REINDEX TABLE pg_index; -- non-mapped, non-shared, critical
3079+
REINDEX TABLE pg_operator; -- non-mapped, non-shared, critical
3080+
REINDEX TABLE pg_database; -- mapped, shared, critical
3081+
REINDEX TABLE pg_shdescription; -- mapped, shared non-critical
3082+
-- Check that individual system indexes can be reindexed. That's a bit
3083+
-- different from the entire-table case because reindex_relation
3084+
-- treats e.g. pg_class special.
3085+
REINDEX INDEX pg_class_oid_index; -- mapped, non-shared, critical
3086+
REINDEX INDEX pg_class_relname_nsp_index; -- mapped, non-shared, non-critical
3087+
REINDEX INDEX pg_index_indexrelid_index; -- non-mapped, non-shared, critical
3088+
REINDEX INDEX pg_index_indrelid_index; -- non-mapped, non-shared, non-critical
3089+
REINDEX INDEX pg_database_oid_index; -- mapped, shared, critical
3090+
REINDEX INDEX pg_shdescription_o_c_index; -- mapped, shared, non-critical
3091+
--
30743092
-- REINDEX SCHEMA
30753093
--
30763094
REINDEX SCHEMA schema_to_reindex; -- failure, schema does not exist

src/test/regress/sql/create_index.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,27 @@ CREATE TABLE reindex_verbose(id integer primary key);
10801080
REINDEX (VERBOSE) TABLE reindex_verbose;
10811081
DROP TABLE reindex_verbose;
10821082

1083+
--
1084+
-- check that system tables can be reindexed
1085+
--
1086+
1087+
-- whole tables
1088+
REINDEX TABLE pg_class; -- mapped, non-shared, critical
1089+
REINDEX TABLE pg_index; -- non-mapped, non-shared, critical
1090+
REINDEX TABLE pg_operator; -- non-mapped, non-shared, critical
1091+
REINDEX TABLE pg_database; -- mapped, shared, critical
1092+
REINDEX TABLE pg_shdescription; -- mapped, shared non-critical
1093+
1094+
-- Check that individual system indexes can be reindexed. That's a bit
1095+
-- different from the entire-table case because reindex_relation
1096+
-- treats e.g. pg_class special.
1097+
REINDEX INDEX pg_class_oid_index; -- mapped, non-shared, critical
1098+
REINDEX INDEX pg_class_relname_nsp_index; -- mapped, non-shared, non-critical
1099+
REINDEX INDEX pg_index_indexrelid_index; -- non-mapped, non-shared, critical
1100+
REINDEX INDEX pg_index_indrelid_index; -- non-mapped, non-shared, non-critical
1101+
REINDEX INDEX pg_database_oid_index; -- mapped, shared, critical
1102+
REINDEX INDEX pg_shdescription_o_c_index; -- mapped, shared, non-critical
1103+
10831104
--
10841105
-- REINDEX SCHEMA
10851106
--

0 commit comments

Comments
 (0)