summaryrefslogtreecommitdiff
path: root/src/backend/optimizer/util/plancat.c
diff options
context:
space:
mode:
authorTom Lane2024-06-11 21:57:46 +0000
committerTom Lane2024-06-11 21:57:46 +0000
commit915de706d28c433283e9dc63701e8f978488a2b9 (patch)
tree0329cea84f33a685a0c78b1174e2cad693a06f44 /src/backend/optimizer/util/plancat.c
parentc2fab70248d8b9f129d2333c91b7a6e279a591e3 (diff)
Fix infer_arbiter_indexes() to not assume resultRelation is 1.
infer_arbiter_indexes failed to renumber varnos in index expressions or predicates that it got from the catalogs. This escaped detection up to now because the stored varnos in such trees will be 1, and an INSERT's result relation is usually the first rangetable entry, so that that was fine. However, in cases such as inserting through an updatable view, it's not fine, leading to failure to match the expressions to the query with ensuing "there is no unique or exclusion constraint matching the ON CONFLICT specification" errors. Fix by copy-and-paste from get_relation_info(). Per bug #18502 from Michael Wang. Back-patch to all supported versions. Discussion: https://postgr.es/m/[email protected]
Diffstat (limited to 'src/backend/optimizer/util/plancat.c')
-rw-r--r--src/backend/optimizer/util/plancat.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index a51fc34e6e0..775955363ef 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -695,6 +695,7 @@ infer_arbiter_indexes(PlannerInfo *root)
OnConflictExpr *onconflict = root->parse->onConflict;
/* Iteration state */
+ Index varno;
RangeTblEntry *rte;
Relation relation;
Oid indexOidFromConstraint = InvalidOid;
@@ -723,7 +724,8 @@ infer_arbiter_indexes(PlannerInfo *root)
* the rewriter or when expand_inherited_rtentry() added it to the query's
* rangetable.
*/
- rte = rt_fetch(root->parse->resultRelation, root->parse->rtable);
+ varno = root->parse->resultRelation;
+ rte = rt_fetch(varno, root->parse->rtable);
relation = table_open(rte->relid, NoLock);
@@ -857,6 +859,9 @@ infer_arbiter_indexes(PlannerInfo *root)
/* Expression attributes (if any) must match */
idxExprs = RelationGetIndexExpressions(idxRel);
+ if (idxExprs && varno != 1)
+ ChangeVarNodes((Node *) idxExprs, 1, varno, 0);
+
foreach(el, onconflict->arbiterElems)
{
InferenceElem *elem = (InferenceElem *) lfirst(el);
@@ -908,6 +913,8 @@ infer_arbiter_indexes(PlannerInfo *root)
* CONFLICT's WHERE clause.
*/
predExprs = RelationGetIndexPredicate(idxRel);
+ if (predExprs && varno != 1)
+ ChangeVarNodes((Node *) predExprs, 1, varno, 0);
if (!predicate_implied_by(predExprs, (List *) onconflict->arbiterWhere, false))
goto next;