Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sH8Vq-003hna-9p for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Jun 2024 20:54:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sH8Vo-009x87-IK for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Jun 2024 20:54:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sH8Vo-009x7z-8E for pgsql-bugs@lists.postgresql.org; Tue, 11 Jun 2024 20:54:37 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sH8Vm-001A3R-FL for pgsql-bugs@lists.postgresql.org; Tue, 11 Jun 2024 20:54:36 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 45BKsV5H1153169; Tue, 11 Jun 2024 16:54:31 -0400 From: Tom Lane To: michael.wanghai.a@outlook.com cc: Peter Geoghegan , pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18502: Upsert on view returns 42P10 error when condition is an expression In-reply-to: <18502-545b53f5b81e54e0@postgresql.org> References: <18502-545b53f5b81e54e0@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Tue, 11 Jun 2024 09:01:25 -0000" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <1153157.1718139267.0@sss.pgh.pa.us> Date: Tue, 11 Jun 2024 16:54:31 -0400 Message-ID: <1153168.1718139271@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1153157.1718139267.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable PG Bug reporting form writes: > I have a table, index and view like following: > ``` > CREATE TABLE my_table > ( > id uuid primary key, > data jsonb not null > ); > CREATE UNIQUE INDEX ON my_table ((data ->> 'key')); > CREATE VIEW my_view AS SELECT * FROM my_table; > ``` > The upsert on view returns 42P10 error when I execute the following SQL > ``` > INSERT INTO my_view (id, data) > VALUES ('990cc75c-2e60-4c0d-8bec-9ac976dc03bc'::uuid, > '{ > "key": "value" > }'::jsonb) > ON CONFLICT ((data ->> 'key')) > DO NOTHING; > ``` For the archives' sake: the error being complained of is ERROR: there is no unique or exclusion constraint matching the ON CONFLIC= T specification Thanks for the report. The fault lies with infer_arbiter_indexes(), which assumes that it can match the output of RelationGetIndexExpressions or RelationGetIndexPredicate directly to the query without adjusting varnos. That works most of the time, because the INSERT target typically has varno 1, matching the way these trees are stored in the catalogs. But not so much when we've flattened an updatable view; so the match fails even when it should succeed. The attached seems to be enough to fix it. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="adjust-varnos-in-infer_arbiter_indexes.patch"; charset="us-ascii" Content-ID: <1153157.1718139267.2@sss.pgh.pa.us> Content-Description: adjust-varnos-in-infer_arbiter_indexes.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/= util/plancat.c index a51fc34e6e..775955363e 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 =3D root->parse->onConflict; = /* Iteration state */ + Index varno; RangeTblEntry *rte; Relation relation; Oid indexOidFromConstraint =3D 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 =3D rt_fetch(root->parse->resultRelation, root->parse->rtable); + varno =3D root->parse->resultRelation; + rte =3D rt_fetch(varno, root->parse->rtable); = relation =3D table_open(rte->relid, NoLock); = @@ -857,6 +859,9 @@ infer_arbiter_indexes(PlannerInfo *root) = /* Expression attributes (if any) must match */ idxExprs =3D RelationGetIndexExpressions(idxRel); + if (idxExprs && varno !=3D 1) + ChangeVarNodes((Node *) idxExprs, 1, varno, 0); + foreach(el, onconflict->arbiterElems) { InferenceElem *elem =3D (InferenceElem *) lfirst(el); @@ -908,6 +913,8 @@ infer_arbiter_indexes(PlannerInfo *root) * CONFLICT's WHERE clause. */ predExprs =3D RelationGetIndexPredicate(idxRel); + if (predExprs && varno !=3D 1) + ChangeVarNodes((Node *) predExprs, 1, varno, 0); = if (!predicate_implied_by(predExprs, (List *) onconflict->arbiterWhere,= false)) goto next; diff --git a/src/test/regress/expected/insert_conflict.out b/src/test/regr= ess/expected/insert_conflict.out index 701217ddbc..5cb9cde030 100644 --- a/src/test/regress/expected/insert_conflict.out +++ b/src/test/regress/expected/insert_conflict.out @@ -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 valu= es(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 =3D excluded.fruit where exists (select 1 from insertconflicttest ii where ii.key =3D excl= uded.key); QUERY PLAN = = @@ -380,6 +391,7 @@ create unique index partial_key_index on insertconflic= ttest(key) where fruit lik -- Succeeds insert into insertconflicttest values (23, 'Blackberry') on conflict (key= ) where fruit like '%berry' do update set fruit =3D excluded.fruit; insert into insertconflicttest as t values (23, 'Blackberry') on conflict= (key) where fruit like '%berry' and t.fruit =3D 'inconsequential' do noth= ing; +insert into insertconflictview as t values (23, 'Blackberry') on conflict= (key) where fruit like '%berry' and t.fruit =3D 'inconsequential' do noth= ing; -- fails insert into insertconflicttest values (23, 'Blackberry') on conflict (key= ) do update set fruit =3D excluded.fruit; ERROR: there is no unique or exclusion constraint matching the ON CONFLI= CT specification @@ -445,6 +457,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 diff --git a/src/test/regress/sql/insert_conflict.sql b/src/test/regress/s= ql/insert_conflict.sql index 653f9baf98..549c46452e 100644 --- a/src/test/regress/sql/insert_conflict.sql +++ b/src/test/regress/sql/insert_conflict.sql @@ -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 value= s(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 =3D excluded.fruit where exists (select 1 from insertconflicttest ii where ii.key =3D excl= uded.key); -- Neither collation nor operator class specifications are required -- @@ -218,6 +222,7 @@ create unique index partial_key_index on insertconflic= ttest(key) where fruit lik -- Succeeds insert into insertconflicttest values (23, 'Blackberry') on conflict (key= ) where fruit like '%berry' do update set fruit =3D excluded.fruit; insert into insertconflicttest as t values (23, 'Blackberry') on conflict= (key) where fruit like '%berry' and t.fruit =3D 'inconsequential' do noth= ing; +insert into insertconflictview as t values (23, 'Blackberry') on conflict= (key) where fruit like '%berry' and t.fruit =3D 'inconsequential' do noth= ing; = -- fails insert into insertconflicttest values (23, 'Blackberry') on conflict (key= ) do update set fruit =3D excluded.fruit; @@ -250,6 +255,7 @@ explain (costs off) insert into insertconflicttest as = i values (23, 'Avocado') o drop index plain; = -- Cleanup +drop view insertconflictview; drop table insertconflicttest; = = ------- =_aaaaaaaaaa0--