Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit497e79b

Browse files
committed
Repair failure with SubPlans in multi-row VALUES lists.
When nodeValuesscan.c was written, it was impossible to have a SubPlan inVALUES --- any sub-SELECT there would have to be uncorrelated and therebywould produce an InitPlan instead. We therefore took a shortcut in thelogic that throws away a ValuesScan's per-row expression evaluation datastructures. This was broken by the introduction of LATERAL however; asub-SELECT containing a lateral reference produces a correlated SubPlan.The cleanest fix for this would be to give up the optimization ofdiscarding the expression eval state. But that still seems prettyunappetizing for long VALUES lists. It seems to work to just preventthe subexpressions from hooking into the ValuesScan node's subPlanlist, so let's do that and see how well it works. (If this breaks,due to additional connections between the subexpressions and the outerquery structures, we might consider compromises like throwing away dataonly for VALUES rows not containing SubPlans.)Per bug #14924 from Christian Duta. Back-patch to 9.3 where LATERALwas introduced.Discussion:https://postgr.es/m/20171124120836.1463.5310@wrigleys.postgresql.org
1 parentdf2361c commit497e79b

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

‎src/backend/executor/nodeValuesscan.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ ValuesNext(ValuesScanState *node)
9292
if (exprlist)
9393
{
9494
MemoryContextoldContext;
95+
List*oldsubplans;
9596
List*exprstatelist;
9697
Datum*values;
9798
bool*isnull;
@@ -115,12 +116,22 @@ ValuesNext(ValuesScanState *node)
115116
oldContext=MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
116117

117118
/*
118-
* Pass NULL, not my plan node, because we don't want anything in this
119-
* transient state linking into permanent state. The only possibility
120-
* is a SubPlan, and there shouldn't be any (any subselects in the
121-
* VALUES list should be InitPlans).
119+
* The expressions might contain SubPlans (this is currently only
120+
* possible if there's a sub-select containing a LATERAL reference,
121+
* otherwise sub-selects in a VALUES list should be InitPlans). Those
122+
* subplans will want to hook themselves into our subPlan list, which
123+
* would result in a corrupted list after we delete the eval state. We
124+
* can work around this by saving and restoring the subPlan list.
125+
* (There's no need for the functionality that would be enabled by
126+
* having the list entries, since the SubPlans aren't going to be
127+
* re-executed anyway.)
122128
*/
123-
exprstatelist= (List*)ExecInitExpr((Expr*)exprlist,NULL);
129+
oldsubplans=node->ss.ps.subPlan;
130+
node->ss.ps.subPlan=NIL;
131+
132+
exprstatelist= (List*)ExecInitExpr((Expr*)exprlist,&node->ss.ps);
133+
134+
node->ss.ps.subPlan=oldsubplans;
124135

125136
/* parser should have checked all sublists are the same length */
126137
Assert(list_length(exprstatelist)==slot->tts_tupleDescriptor->natts);

‎src/test/regress/expected/subselect.out

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,36 @@ select exists(select * from nocolumns);
795795
f
796796
(1 row)
797797

798+
--
799+
-- Check behavior with a SubPlan in VALUES (bug #14924)
800+
--
801+
select val.x
802+
from generate_series(1,10) as s(i),
803+
lateral (
804+
values ((select s.i + 1)), (s.i + 101)
805+
) as val(x)
806+
where s.i < 10 and (select val.x) < 110;
807+
x
808+
-----
809+
2
810+
102
811+
3
812+
103
813+
4
814+
104
815+
5
816+
105
817+
6
818+
106
819+
7
820+
107
821+
8
822+
108
823+
9
824+
109
825+
10
826+
(17 rows)
827+
798828
--
799829
-- Check sane behavior with nested IN SubLinks
800830
--

‎src/test/regress/sql/subselect.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,16 @@ explain (verbose, costs off)
444444
create temp table nocolumns();
445445
select exists(select*from nocolumns);
446446

447+
--
448+
-- Check behavior with a SubPlan in VALUES (bug #14924)
449+
--
450+
selectval.x
451+
from generate_series(1,10)as s(i),
452+
lateral (
453+
values ((selects.i+1)), (s.i+101)
454+
)as val(x)
455+
wheres.i<10and (selectval.x)<110;
456+
447457
--
448458
-- Check sane behavior with nested IN SubLinks
449459
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp