pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Mario De Frutos Dieguez <mariodefrutos@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: Fwd: Problem with a "complex" upsert
Date: Fri, 22 Jun 2018 17:11:23 +0900
Message-ID: <8da3739d-da24-1125-cab6-b68d6ec1bc6a@lab.ntt.co.jp> (raw)
In-Reply-To: <18795.1529600753@sss.pgh.pa.us>
References: <CAFYwGJ0xfzy8jaK80hVN2eUWr6huce0RU8AgU04MGD00igqkTg@mail.gmail.com>
	<CAFYwGJ3Cpb2T+177rEopU7_aQE3W03UoqNZXdP5erCtSRgDGVA@mail.gmail.com>
	<18795.1529600753@sss.pgh.pa.us>

On 2018/06/22 2:05, Tom Lane wrote:
> Mario De Frutos Dieguez <mariodefrutos@gmail.com> 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

Attachments:

view-insert-on-conflict-bug-1.patch (text/plain, 3.9K) download 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;

view thread (30+ messages)  latest in thread

Message-ID: <8da3739d-da24-1125-cab6-b68d6ec1bc6a@lab.ntt.co.jp>
Permalink:  ../8da3739d-da24-1125-cab6-b68d6ec1bc6a@lab.ntt.co.jp/
Also on:    postgresql.org/message-id/8da3739d-da24-1125-cab6-b68d6ec1bc6a@lab.ntt.co.jp

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: Langote_Amit_f8@lab.ntt.co.jp, tgl@sss.pgh.pa.us, mariodefrutos@gmail.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: Fwd: Problem with a "complex" upsert
  In-Reply-To: <8da3739d-da24-1125-cab6-b68d6ec1bc6a@lab.ntt.co.jp>
  References: <CAFYwGJ0xfzy8jaK80hVN2eUWr6huce0RU8AgU04MGD00igqkTg@mail.gmail.com> <CAFYwGJ3Cpb2T+177rEopU7_aQE3W03UoqNZXdP5erCtSRgDGVA@mail.gmail.com> <18795.1529600753@sss.pgh.pa.us> <8da3739d-da24-1125-cab6-b68d6ec1bc6a@lab.ntt.co.jp>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox