Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9eaf5be

Browse files
committed
Mark read/write expanded values as read-only in ValuesNext(), too.
Further thought about bug #14174 motivated me to try the case of a R/W datum being returned from a VALUES list, and sure enough it was broken. Fix that. Also add a regression test case exercising the same scenario for FunctionScan. That's not broken right now, because the function's result will get shoved into a tuplestore between generation and use; but it could easily become broken whenever we get around to optimizing FunctionScan better. There don't seem to be any other places where we put the result of expression evaluation into a virtual tuple slot that could then be the source for Vars of further expression evaluation, so I think this is the end of this bug.
1 parent 69f526a commit 9eaf5be

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/backend/executor/nodeValuesscan.c

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "executor/executor.h"
2727
#include "executor/nodeValuesscan.h"
28+
#include "utils/expandeddatum.h"
2829

2930

3031
static TupleTableSlot *ValuesNext(ValuesScanState *node);
@@ -94,6 +95,7 @@ ValuesNext(ValuesScanState *node)
9495
List *exprstatelist;
9596
Datum *values;
9697
bool *isnull;
98+
Form_pg_attribute *att;
9799
ListCell *lc;
98100
int resind;
99101

@@ -129,6 +131,7 @@ ValuesNext(ValuesScanState *node)
129131
*/
130132
values = slot->tts_values;
131133
isnull = slot->tts_isnull;
134+
att = slot->tts_tupleDescriptor->attrs;
132135

133136
resind = 0;
134137
foreach(lc, exprstatelist)
@@ -139,6 +142,17 @@ ValuesNext(ValuesScanState *node)
139142
econtext,
140143
&isnull[resind],
141144
NULL);
145+
146+
/*
147+
* We must force any R/W expanded datums to read-only state, in
148+
* case they are multiply referenced in the plan node's output
149+
* expressions, or in case we skip the output projection and the
150+
* output column is multiply referenced in higher plan nodes.
151+
*/
152+
values[resind] = MakeExpandedObjectReadOnly(values[resind],
153+
isnull[resind],
154+
att[resind]->attlen);
155+
142156
resind++;
143157
}
144158

src/test/regress/expected/plpgsql.out

+32
Original file line numberDiff line numberDiff line change
@@ -5404,6 +5404,38 @@ select i, a from
54045404
1 | {1,1}
54055405
(1 row)
54065406

5407+
explain (verbose, costs off)
5408+
select consumes_rw_array(a), a from returns_rw_array(1) a;
5409+
QUERY PLAN
5410+
--------------------------------------------
5411+
Function Scan on public.returns_rw_array a
5412+
Output: consumes_rw_array(a), a
5413+
Function Call: returns_rw_array(1)
5414+
(3 rows)
5415+
5416+
select consumes_rw_array(a), a from returns_rw_array(1) a;
5417+
consumes_rw_array | a
5418+
-------------------+-------
5419+
1 | {1,1}
5420+
(1 row)
5421+
5422+
explain (verbose, costs off)
5423+
select consumes_rw_array(a), a from
5424+
(values (returns_rw_array(1)), (returns_rw_array(2))) v(a);
5425+
QUERY PLAN
5426+
---------------------------------------------------------------------
5427+
Values Scan on "*VALUES*"
5428+
Output: consumes_rw_array("*VALUES*".column1), "*VALUES*".column1
5429+
(2 rows)
5430+
5431+
select consumes_rw_array(a), a from
5432+
(values (returns_rw_array(1)), (returns_rw_array(2))) v(a);
5433+
consumes_rw_array | a
5434+
-------------------+-------
5435+
1 | {1,1}
5436+
2 | {2,2}
5437+
(2 rows)
5438+
54075439
--
54085440
-- Test access to call stack
54095441
--

src/test/regress/sql/plpgsql.sql

+12
Original file line numberDiff line numberDiff line change
@@ -4263,6 +4263,18 @@ select i, a from
42634263
(select returns_rw_array(1) as a offset 0) ss,
42644264
lateral consumes_rw_array(a) i;
42654265
4266+
explain (verbose, costs off)
4267+
select consumes_rw_array(a), a from returns_rw_array(1) a;
4268+
4269+
select consumes_rw_array(a), a from returns_rw_array(1) a;
4270+
4271+
explain (verbose, costs off)
4272+
select consumes_rw_array(a), a from
4273+
(values (returns_rw_array(1)), (returns_rw_array(2))) v(a);
4274+
4275+
select consumes_rw_array(a), a from
4276+
(values (returns_rw_array(1)), (returns_rw_array(2))) v(a);
4277+
42664278
42674279
--
42684280
-- Test access to call stack

0 commit comments

Comments
 (0)