Re: BUG #16006: Update queries fail on a table having any policy with a function that takes a whole-row var as arg - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #16006: Update queries fail on a table having any policy with a function that takes a whole-row var as arg
Date
Msg-id 28076.1568320435@sss.pgh.pa.us
Whole thread Raw
In response to BUG #16006: Update queries fail on a table having any policy with a function that takes a whole-row var as arg  (PG Bug reporting form <noreply@postgresql.org>)
Responses Re: BUG #16006: Update queries fail on a table having any policywith a function that takes a whole-row var as arg
List pgsql-bugs
PG Bug reporting form <noreply@postgresql.org> writes:
> Update queries fail on a table having any policy with a function that takes
> a whole-row var as argument.

Hm.  You really should have shown the failure you were seeing, but
for the archives' sake: I can reproduce this on 9.5 and 9.6 (if I
run the queries as non-superuser!), and it looks like

regression=> insert into usertable
values('key','field0','field1','field2','field3','field4','field5','field6','field7','field8','field9');
INFO:  log: (key,field0,field1,field2,field3,field4,field5,field6,field7,field8,field9,"2019-09-12 16:29:37.511329")
INSERT 0 1
regression=> update usertable set field0 = 'f0';
INFO:  log: (key,field0,field1,field2,field3,field4,field5,field6,field7,field8,field9,"2019-09-12 16:29:37.511329")
ERROR:  table row type and query-specified row type do not match
DETAIL:  Table has type tid at ordinal position 1, but query expects character varying.

Digging into this, it seems the short answer is "Andres should have
back-patched 148e632c0".  The plan shape in 9.6.x is

 Update on usertable usertable_1  (cost=0.00..66.00 rows=70 width=366)
   ->  Subquery Scan on usertable  (cost=0.00..66.00 rows=70 width=366)
         ->  LockRows  (cost=0.00..65.30 rows=70 width=340)
               ->  Seq Scan on usertable usertable_2  (cost=0.00..64.60 rows=70 width=340)
                     Filter: log_record(usertable_2.*)

and because ExecInitModifyTable incorrectly passes the SubqueryScan
as parent of the WCO expressions, the horrible examine-the-parent
kluge in ExecEvalWholeRowVar fires, causing it to apply a completely
inappropriate junkfilter to the scan tuple.  After which, of course,
the tuple rowtype is wrong.

I do not see the bug in v10 and up, but I think that's accidental in
v10/v11, because they produce a plan without the not-really-necessary
SubqueryScan:

 Update on usertable  (cost=0.00..64.60 rows=70 width=366)
   ->  Seq Scan on usertable  (cost=0.00..64.60 rows=70 width=366)
         Filter: log_record(usertable.*)

The whole-row var is still being initialized with respect to the wrong
parent, but it doesn't do anything funny when it's pointed at a plain
SeqScan, so all is well.  I suspect it's possible to develop a test
case that will fail in v10/v11, and will go look for one.

Thanks for the report!

            regards, tom lane



pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #16007: Regarding patch for BUG #3995: pqSocketCheck doesn't return
Next
From: Andres Freund
Date:
Subject: Re: BUG #16006: Update queries fail on a table having any policywith a function that takes a whole-row var as arg