Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fWHAd-0005if-Ue for pgsql-bugs@arkaria.postgresql.org; Fri, 22 Jun 2018 08:11:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fWHAb-0003ho-FX for pgsql-bugs@arkaria.postgresql.org; Fri, 22 Jun 2018 08:11:49 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fWHAb-0003hf-9j for pgsql-bugs@lists.postgresql.org; Fri, 22 Jun 2018 08:11:49 +0000 Received: from tama50.ecl.ntt.co.jp ([129.60.39.147]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fWHAW-0001KU-VD for pgsql-bugs@lists.postgresql.org; Fri, 22 Jun 2018 08:11:48 +0000 Received: from vc2.ecl.ntt.co.jp (vc2.ecl.ntt.co.jp [129.60.86.154]) by tama50.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id w5M8Be1a020915; Fri, 22 Jun 2018 17:11:40 +0900 Received: from vc2.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id 0CAF7638D1F; Fri, 22 Jun 2018 17:11:40 +0900 (JST) Received: from jcms-pop21.ecl.ntt.co.jp (jcms-pop21.ecl.ntt.co.jp [129.60.87.134]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id 00531638CE6; Fri, 22 Jun 2018 17:11:40 +0900 (JST) Received: from [127.0.0.1] (unknown [129.60.241.61]) by jcms-pop21.ecl.ntt.co.jp (Postfix) with ESMTPSA id E8829400574; Fri, 22 Jun 2018 17:11:39 +0900 (JST) Subject: Re: Fwd: Problem with a "complex" upsert References: <18795.1529600753@sss.pgh.pa.us> From: Amit Langote Message-ID: <8da3739d-da24-1125-cab6-b68d6ec1bc6a@lab.ntt.co.jp> Date: Fri, 22 Jun 2018 17:11:23 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <18795.1529600753@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------49BBEE52E1E119EEB2395863" Content-Language: en-US X-CC-Mail-RelayStamp: 1 To: Tom Lane , Mario De Frutos Dieguez Cc: pgsql-bugs@lists.postgresql.org X-TM-AS-MML: disable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------49BBEE52E1E119EEB2395863 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit On 2018/06/22 2:05, Tom Lane wrote: > Mario De Frutos Dieguez writes: >> I'm trying to do an upsert to an updatable view with the following SQL >> query: >> ... >> If I don't get any conflict everything works as intended but if we hit a >> conflict then I get the following error message: >> ERROR: attribute 2 of type record has the wrong type >> DETAIL: Table has type character varying, but query expects double >> precision. > > When filing a bug report, it's a good idea to provide both a self- > contained test case and a mention of what PG version you're using. > > I guess from the ROW() syntax you used here, which isn't accepted pre-v10, > that you're using 10.0 or later, but that's not specific enough. > > I tried to duplicate this problem using the attached script, but it > works for me. > > FWIW, that error message definitely looks like a bug, but I can't > tell whether it's an already-fixed bug or there's some triggering > detail you didn't mention. Having worked a little bit on the ON CONFLICT code recently, I was able to guess at the triggering detail. At least, I was able to reproduce the error and crash seen in the OP's report. Here's a minimal example: create table foo (a text unique, b float); insert into foo values ('xyxyxy', 1); -- note the different order of columns in the view create view foo_view as select b, a from foo; insert into foo_view values (1, 'xyxyxy') on conflict (a) do update set b = excluded.b; ERROR: attribute 1 of type record has wrong type DETAIL: Table has type text, but query expects double precision. -- crash occurs if, like OP, I change EXCLUDED reference to target table insert into foo_view values (1, 'xyxyxy') on conflict (a) do update set b = foo_view.b; server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. The connection to the server was lost. Attempting reset: Failed. I tried debugging why that happens and concluded that rewriteTargetView fails to *completely* account for the fact that the view's column's may have different attribute numbers than the underlying table that the DO UPDATE action will be applied to. Especially, even if the view's Vars are replaced with those corresponding underlying base table's columns, the TargetEntry's into which those Vars are contained are not refreshed, that is, their resnos don't match varattnos. I created a patch that seems to fix the issue, which also adds a representative test in updatable_view.sql. Thanks, Amit --------------49BBEE52E1E119EEB2395863 Content-Type: text/plain; charset=UTF-8; name="view-insert-on-conflict-bug-1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="view-insert-on-conflict-bug-1.patch" diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c index 5b87c554f5..f951cb33a9 100644 --- a/src/backend/rewrite/rewriteHandler.c +++ b/src/backend/rewrite/rewriteHandler.c @@ -3032,6 +3032,52 @@ rewriteTargetView(Query *parsetree, Relation view) elog(ERROR, "attribute number %d not found in view targetlist", tle->resno); } + + /* + * Do the same for targetlists related to handling ON CONFLICT + * DO UPDATE. + */ + if (parsetree->onConflict && + parsetree->onConflict->action == ONCONFLICT_UPDATE) + { + foreach(lc, parsetree->onConflict->onConflictSet) + { + TargetEntry *tle = (TargetEntry *) lfirst(lc); + TargetEntry *view_tle; + + if (tle->resjunk) + continue; + + view_tle = get_tle_by_resno(view_targetlist, tle->resno); + if (view_tle != NULL && !view_tle->resjunk && IsA(view_tle->expr, Var)) + tle->resno = ((Var *) view_tle->expr)->varattno; + else + elog(ERROR, "attribute number %d not found in view targetlist", + tle->resno); + } + + /* + * exclRelTlist contains Var nodes corresponding to all attributes + * of the target (view) relation, although the corresponding + * entries in view_targetlist may not actually be Vars. Modify + * resnos of only those for which we found a Var node in + * view_targetlist. + */ + foreach(lc, parsetree->onConflict->exclRelTlist) + { + TargetEntry *tle = (TargetEntry *) lfirst(lc); + TargetEntry *view_tle; + + if (tle->resjunk) + continue; + + view_tle = get_tle_by_resno(view_targetlist, tle->resno); + + if (view_tle != NULL && !view_tle->resjunk && + IsA(view_tle->expr, Var)) + tle->resno = ((Var *) view_tle->expr)->varattno; + } + } } /* diff --git a/src/test/regress/expected/updatable_views.out b/src/test/regress/expected/updatable_views.out index b34bab4b29..cf5bd9584e 100644 --- a/src/test/regress/expected/updatable_views.out +++ b/src/test/regress/expected/updatable_views.out @@ -2578,3 +2578,20 @@ ERROR: new row violates check option for view "wcowrtest_v2" DETAIL: Failing row contains (2, no such row in sometable). drop view wcowrtest_v, wcowrtest_v2; drop table wcowrtest, sometable; +-- Check INSERT .. ON CONFLICT DO UPDATE works correctly when the view's +-- columns may be ordered differently than the underlying table's. +create table uv_iocu_tab (a text unique, b float); +insert into uv_iocu_tab values ('xyxyxy', 1); +-- note the different order of columns in the view +create view uv_iocu_view as select b, a from uv_iocu_tab; +insert into uv_iocu_view (a, b) values ('xyxyxy', 2) on conflict (a) do update set b = uv_iocu_view.b; +insert into uv_iocu_view (a, b) values ('xyxyxy', 3) on conflict (a) do update set b = excluded.b; +-- should display 'xyxyxy, 3' +select * from uv_iocu_view; + b | a +---+-------- + 3 | xyxyxy +(1 row) + +drop view uv_iocu_view; +drop table uv_iocu_tab; diff --git a/src/test/regress/sql/updatable_views.sql b/src/test/regress/sql/updatable_views.sql index a7786b26e9..cd3f7a9f33 100644 --- a/src/test/regress/sql/updatable_views.sql +++ b/src/test/regress/sql/updatable_views.sql @@ -1244,3 +1244,16 @@ insert into wcowrtest_v2 values (2, 'no such row in sometable'); drop view wcowrtest_v, wcowrtest_v2; drop table wcowrtest, sometable; + +-- Check INSERT .. ON CONFLICT DO UPDATE works correctly when the view's +-- columns may be ordered differently than the underlying table's. +create table uv_iocu_tab (a text unique, b float); +insert into uv_iocu_tab values ('xyxyxy', 1); +-- note the different order of columns in the view +create view uv_iocu_view as select b, a from uv_iocu_tab; +insert into uv_iocu_view (a, b) values ('xyxyxy', 2) on conflict (a) do update set b = uv_iocu_view.b; +insert into uv_iocu_view (a, b) values ('xyxyxy', 3) on conflict (a) do update set b = excluded.b; +-- should display 'xyxyxy, 3' +select * from uv_iocu_view; +drop view uv_iocu_view; +drop table uv_iocu_tab; --------------49BBEE52E1E119EEB2395863--