1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-07 00:36:50 +03:00

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/18502-545b53f5b81e54e0@postgresql.org
This commit is contained in:
Tom Lane
2024-06-11 17:57:46 -04:00
parent c2fab70248
commit 915de706d2
3 changed files with 27 additions and 1 deletions

View File

@ -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;