From 28bbf7a81b3a30504cc7dfdbd76b410d1f127b8e Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Mon, 29 Jul 2019 09:58:49 +0900 Subject: Fix handling of expressions and predicates in REINDEX CONCURRENTLY When copying the definition of an index rebuilt concurrently for the new entry, the index information was taken directly from the old index using the relation cache. In this case, predicates and expressions have some post-processing to prepare things for the planner, which loses some information including the collations added in any of them. This inconsistency can cause issues when attempting for example a table rewrite, and makes the new indexes rebuilt concurrently inconsistent with the old entries. In order to fix the problem, fetch expressions and predicates directly from the catalog of the old entry, and fill in IndexInfo for the new index with that. This makes the process more consistent with DefineIndex(), and the code is refactored with the addition of a routine to create an IndexInfo node. Reported-by: Manuel Rigger Author: Michael Paquier Discussion: https://postgr.es/m/CA+u7OA5Hp0ra235F3czPom_FyAd-3+XwSJmX95r1+sRPOJc9VQ@mail.gmail.com Backpatch-through: 12 --- src/backend/commands/indexcmds.c | 46 ++++++++++++---------------------------- 1 file changed, 13 insertions(+), 33 deletions(-) (limited to 'src/backend/commands/indexcmds.c') diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 6fa5738bbfb..cbac31476c4 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -202,18 +202,8 @@ CheckIndexCompatible(Oid oldId, * contains only key attributes, thus we're filling ii_NumIndexAttrs and * ii_NumIndexKeyAttrs with same value. */ - indexInfo = makeNode(IndexInfo); - indexInfo->ii_NumIndexAttrs = numberOfAttributes; - indexInfo->ii_NumIndexKeyAttrs = numberOfAttributes; - indexInfo->ii_Expressions = NIL; - indexInfo->ii_ExpressionsState = NIL; - indexInfo->ii_PredicateState = NULL; - indexInfo->ii_ExclusionOps = NULL; - indexInfo->ii_ExclusionProcs = NULL; - indexInfo->ii_ExclusionStrats = NULL; - indexInfo->ii_Am = accessMethodId; - indexInfo->ii_AmCache = NULL; - indexInfo->ii_Context = CurrentMemoryContext; + indexInfo = makeIndexInfo(numberOfAttributes, numberOfAttributes, + accessMethodId, NIL, NIL, false, false, false); typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid)); collationObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid)); classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid)); @@ -780,27 +770,17 @@ DefineIndex(Oid relationId, /* * Prepare arguments for index_create, primarily an IndexInfo structure. - * Note that ii_Predicate must be in implicit-AND format. - */ - indexInfo = makeNode(IndexInfo); - indexInfo->ii_NumIndexAttrs = numberOfAttributes; - indexInfo->ii_NumIndexKeyAttrs = numberOfKeyAttributes; - indexInfo->ii_Expressions = NIL; /* for now */ - indexInfo->ii_ExpressionsState = NIL; - indexInfo->ii_Predicate = make_ands_implicit((Expr *) stmt->whereClause); - indexInfo->ii_PredicateState = NULL; - indexInfo->ii_ExclusionOps = NULL; - indexInfo->ii_ExclusionProcs = NULL; - indexInfo->ii_ExclusionStrats = NULL; - indexInfo->ii_Unique = stmt->unique; - /* In a concurrent build, mark it not-ready-for-inserts */ - indexInfo->ii_ReadyForInserts = !stmt->concurrent; - indexInfo->ii_Concurrent = stmt->concurrent; - indexInfo->ii_BrokenHotChain = false; - indexInfo->ii_ParallelWorkers = 0; - indexInfo->ii_Am = accessMethodId; - indexInfo->ii_AmCache = NULL; - indexInfo->ii_Context = CurrentMemoryContext; + * Note that predicates must be in implicit-AND format. In a concurrent + * build, mark it not-ready-for-inserts. + */ + indexInfo = makeIndexInfo(numberOfAttributes, + numberOfKeyAttributes, + accessMethodId, + NIL, /* expressions, NIL for now */ + make_ands_implicit((Expr *) stmt->whereClause), + stmt->unique, + !stmt->concurrent, + stmt->concurrent); typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid)); collationObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid)); -- cgit v1.2.3