From 042d1f394332697ee0a4d085313786a6149968c4 Mon Sep 17 00:00:00 2001 From: jian he Date: Thu, 29 Feb 2024 17:02:34 +0800 Subject: [PATCH v2 1/1] minor refactor based on v2. --- src/backend/access/spgist/spgutils.c | 2 +- src/backend/catalog/index.c | 4 ++-- src/backend/commands/matview.c | 2 +- src/backend/commands/tablecmds.c | 4 ++-- src/backend/executor/execIndexing.c | 2 +- src/backend/optimizer/plan/planner.c | 14 ++++++++------ src/backend/optimizer/util/plancat.c | 8 ++++---- src/backend/parser/parse_utilcmd.c | 4 ++-- src/backend/utils/cache/relcache.c | 4 ++-- 9 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/backend/access/spgist/spgutils.c b/src/backend/access/spgist/spgutils.c index 7acdc3a4..35fa8848 100644 --- a/src/backend/access/spgist/spgutils.c +++ b/src/backend/access/spgist/spgutils.c @@ -137,7 +137,7 @@ GetIndexInputType(Relation index, AttrNumber indexcol) if (index->rd_indexprs) indexprs = index->rd_indexprs; else - indexprs = RelationGetIndexExpressions(index, true); + indexprs = RelationGetIndexExpressions(index, false); indexpr_item = list_head(indexprs); for (int i = 1; i <= index->rd_index->indnkeyatts; i++) { diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index d49b86a7..a5a1f7db 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -2455,8 +2455,8 @@ BuildIndexInfo(Relation index) ii = makeIndexInfo(indexStruct->indnatts, indexStruct->indnkeyatts, index->rd_rel->relam, - RelationGetIndexExpressions(index, true), - RelationGetIndexPredicate(index, true), + RelationGetIndexExpressions(index, false), + RelationGetIndexPredicate(index, false), indexStruct->indisunique, indexStruct->indnullsnotdistinct, indexStruct->indisready, diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c index 847f34b0..2d12e06e 100644 --- a/src/backend/commands/matview.c +++ b/src/backend/commands/matview.c @@ -898,7 +898,7 @@ is_usable_unique_index(Relation indexRel) indexStruct->indimmediate && indexRel->rd_rel->relam == BTREE_AM_OID && indexStruct->indisvalid && - RelationGetIndexPredicate(indexRel, true) == NIL && + RelationGetIndexPredicate(indexRel, false) == NIL && indexStruct->indnatts > 0) { /* diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 8db97903..667a96c6 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -17172,13 +17172,13 @@ ATExecReplicaIdentity(Relation rel, ReplicaIdentityStmt *stmt, LOCKMODE lockmode errmsg("cannot use non-immediate index \"%s\" as replica identity", RelationGetRelationName(indexRel)))); /* Expression indexes aren't supported. */ - if (RelationGetIndexExpressions(indexRel, true) != NIL) + if (RelationGetIndexExpressions(indexRel, false) != NIL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cannot use expression index \"%s\" as replica identity", RelationGetRelationName(indexRel)))); /* Predicate indexes aren't supported. */ - if (RelationGetIndexPredicate(indexRel, true) != NIL) + if (RelationGetIndexPredicate(indexRel, false) != NIL) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cannot use partial index \"%s\" as replica identity", diff --git a/src/backend/executor/execIndexing.c b/src/backend/executor/execIndexing.c index ad6e5ab2..2ab484ce 100644 --- a/src/backend/executor/execIndexing.c +++ b/src/backend/executor/execIndexing.c @@ -1045,7 +1045,7 @@ index_unchanged_by_update(ResultRelInfo *resultRelInfo, EState *estate, * If we find any matching Vars, don't pass hint for index. Otherwise * pass hint. */ - idxExprs = RelationGetIndexExpressions(indexRelation, true); + idxExprs = RelationGetIndexExpressions(indexRelation, false); hasexpression = index_expression_changed_walker((Node *) idxExprs, allUpdatedCols); list_free(idxExprs); diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 3951d192..d73118c9 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -6675,14 +6675,16 @@ plan_create_index_workers(Oid tableOid, Oid indexOid) * Furthermore, any index predicate or index expressions must be parallel * safe. * - * We would check the raw index expression not optimized expression. - * ReationGetIndexExpression() will optimize the index expression even though - * user provide PARALLEL UNSAFE label to the expression. - * It will report error in some cases. So we pass FALSE to function. + * We need to check the raw index expression or index perdicate, not the optimized one. + * Normally ReationGetIndexExpression, RelationGetIndexPredicate will optimize + * the index expression or index predicate, even though user provided + * PARALLEL UNSAFE label to it. It will report error in some cases. + * So we pass TRUE to the second argument to get the raw index expression + * or index predicate. */ if (heap->rd_rel->relpersistence == RELPERSISTENCE_TEMP || - !is_parallel_safe(root, (Node *) RelationGetIndexExpressions(index, false)) || - !is_parallel_safe(root, (Node *) RelationGetIndexPredicate(index, false))) + !is_parallel_safe(root, (Node *) RelationGetIndexExpressions(index, true)) || + !is_parallel_safe(root, (Node *) RelationGetIndexPredicate(index, true))) { parallel_workers = 0; goto done; diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c index 0a503269..8c7afe3e 100644 --- a/src/backend/optimizer/util/plancat.c +++ b/src/backend/optimizer/util/plancat.c @@ -434,8 +434,8 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, * correct varno for the parent relation, so that they match up * correctly against qual clauses. */ - info->indexprs = RelationGetIndexExpressions(indexRelation, true); - info->indpred = RelationGetIndexPredicate(indexRelation, true); + info->indexprs = RelationGetIndexExpressions(indexRelation, false); + info->indpred = RelationGetIndexPredicate(indexRelation, false); if (info->indexprs && varno != 1) ChangeVarNodes((Node *) info->indexprs, 1, varno, 0); if (info->indpred && varno != 1) @@ -847,7 +847,7 @@ infer_arbiter_indexes(PlannerInfo *root) goto next; /* Expression attributes (if any) must match */ - idxExprs = RelationGetIndexExpressions(idxRel, true); + idxExprs = RelationGetIndexExpressions(idxRel, false); foreach(el, onconflict->arbiterElems) { InferenceElem *elem = (InferenceElem *) lfirst(el); @@ -898,7 +898,7 @@ infer_arbiter_indexes(PlannerInfo *root) * If it's a partial index, its predicate must be implied by the ON * CONFLICT's WHERE clause. */ - predExprs = RelationGetIndexPredicate(idxRel, true); + predExprs = RelationGetIndexPredicate(idxRel, false); if (!predicate_implied_by(predExprs, (List *) onconflict->arbiterWhere, false)) goto next; diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 07dab10a..c84996a7 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -2422,14 +2422,14 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) errdetail("Cannot create a primary key or unique constraint using such an index."), parser_errposition(cxt->pstate, constraint->location))); - if (RelationGetIndexExpressions(index_rel, true) != NIL) + if (RelationGetIndexExpressions(index_rel, false) != NIL) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("index \"%s\" contains expressions", index_name), errdetail("Cannot create a primary key or unique constraint using such an index."), parser_errposition(cxt->pstate, constraint->location))); - if (RelationGetIndexPredicate(index_rel, true) != NIL) + if (RelationGetIndexPredicate(index_rel, false) != NIL) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("\"%s\" is a partial index", index_name), diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index 002c8b25..30f71d9d 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -5040,7 +5040,7 @@ RelationGetIndexExpressions(Relation relation, bool get_raw_expr) pfree(exprsString); /* Return, if we need raw index expression */ - if (!get_raw_expr) + if (get_raw_expr) return result; /* @@ -5157,7 +5157,7 @@ RelationGetIndexPredicate(Relation relation, bool get_raw_expr) pfree(predString); /* Return, if we need a raw index predicate */ - if (!get_raw_expr) + if (get_raw_expr) return result; /* -- 2.34.1