Fix infer_arbiter_indexes() to not assume resultRelation is 1.
authorTom Lane <[email protected]>
Tue, 11 Jun 2024 21:57:46 +0000 (17:57 -0400)
committerTom Lane <[email protected]>
Tue, 11 Jun 2024 21:57:46 +0000 (17:57 -0400)
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

src/backend/optimizer/util/plancat.c
src/test/regress/expected/insert_conflict.out
src/test/regress/sql/insert_conflict.sql

index 39932d3c2d27c21a2cddf55d4ee86b67bdf2a92e..07c4ba384ab6ef6eb02b51b5f4a106438403a776 100644 (file)
@@ -667,6 +667,7 @@ infer_arbiter_indexes(PlannerInfo *root)
    OnConflictExpr *onconflict = root->parse->onConflict;
 
    /* Iteration state */
+   Index       varno;
    RangeTblEntry *rte;
    Relation    relation;
    Oid         indexOidFromConstraint = InvalidOid;
@@ -695,7 +696,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);
 
@@ -829,6 +831,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);
@@ -880,6 +885,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;
index 9e9e3bd00cd13fd02d9114c2ceb604bb24bbf72a..15fc426d661eb98c5ec40a33e74eecc2401cba48 100644 (file)
@@ -2,6 +2,8 @@
 -- insert...on conflict do unique index inference
 --
 create table insertconflicttest(key int4, fruit text);
+-- These things should work through a view, as well
+create view insertconflictview as select * from insertconflicttest;
 --
 -- Test unique index inference with operator class specifications and
 -- named collations
@@ -43,6 +45,15 @@ explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on con
    ->  Result
 (4 rows)
 
+explain (costs off) insert into insertconflictview values(0, 'Crowberry') on conflict (lower(fruit), key, lower(fruit), key) do nothing;
+                   QUERY PLAN                    
+-------------------------------------------------
+ Insert on insertconflicttest
+   Conflict Resolution: NOTHING
+   Conflict Arbiter Indexes: both_index_expr_key
+   ->  Result
+(4 rows)
+
 explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (key, fruit) do update set fruit = excluded.fruit
   where exists (select 1 from insertconflicttest ii where ii.key = excluded.key);
                                   QUERY PLAN                                   
@@ -374,6 +385,7 @@ create unique index partial_key_index on insertconflicttest(key) where fruit lik
 -- Succeeds
 insert into insertconflicttest values (23, 'Blackberry') on conflict (key) where fruit like '%berry' do update set fruit = excluded.fruit;
 insert into insertconflicttest as t values (23, 'Blackberry') on conflict (key) where fruit like '%berry' and t.fruit = 'inconsequential' do nothing;
+insert into insertconflictview as t values (23, 'Blackberry') on conflict (key) where fruit like '%berry' and t.fruit = 'inconsequential' do nothing;
 -- fails
 insert into insertconflicttest values (23, 'Blackberry') on conflict (key) do update set fruit = excluded.fruit;
 ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
@@ -439,6 +451,7 @@ explain (costs off) insert into insertconflicttest as i values (23, 'Avocado') o
 
 drop index plain;
 -- Cleanup
+drop view insertconflictview;
 drop table insertconflicttest;
 --
 -- Verify that EXCLUDED does not allow system column references. These
index 23d5778b821e03acc55fedecc812ab679f7ce773..144c3470caa7d008963a5f3e681f4565cd4b2551 100644 (file)
@@ -3,6 +3,9 @@
 --
 create table insertconflicttest(key int4, fruit text);
 
+-- These things should work through a view, as well
+create view insertconflictview as select * from insertconflicttest;
+
 --
 -- Test unique index inference with operator class specifications and
 -- named collations
@@ -20,6 +23,7 @@ explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on con
 explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (key, fruit) do nothing;
 explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (fruit, key, fruit, key) do nothing;
 explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (lower(fruit), key, lower(fruit), key) do nothing;
+explain (costs off) insert into insertconflictview values(0, 'Crowberry') on conflict (lower(fruit), key, lower(fruit), key) do nothing;
 explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (key, fruit) do update set fruit = excluded.fruit
   where exists (select 1 from insertconflicttest ii where ii.key = excluded.key);
 -- Neither collation nor operator class specifications are required --
@@ -215,6 +219,7 @@ create unique index partial_key_index on insertconflicttest(key) where fruit lik
 -- Succeeds
 insert into insertconflicttest values (23, 'Blackberry') on conflict (key) where fruit like '%berry' do update set fruit = excluded.fruit;
 insert into insertconflicttest as t values (23, 'Blackberry') on conflict (key) where fruit like '%berry' and t.fruit = 'inconsequential' do nothing;
+insert into insertconflictview as t values (23, 'Blackberry') on conflict (key) where fruit like '%berry' and t.fruit = 'inconsequential' do nothing;
 
 -- fails
 insert into insertconflicttest values (23, 'Blackberry') on conflict (key) do update set fruit = excluded.fruit;
@@ -247,6 +252,7 @@ explain (costs off) insert into insertconflicttest as i values (23, 'Avocado') o
 drop index plain;
 
 -- Cleanup
+drop view insertconflictview;
 drop table insertconflicttest;