Skip to content

Commit 7fe312f

Browse files
committed
Fix plpgsql's handling of simple expressions in scrollable cursors.
exec_save_simple_expr did not account for the possibility that standard_planner would stick a Materialize node atop the plan of even a simple Result, if CURSOR_OPT_SCROLL is set. This led to an "unexpected plan node type" error. This is a very old bug, but it'd only be reached by declaring a cursor for a "SELECT simple-expression" query and explicitly marking it scrollable, which is an odd thing to do. So the lack of prior reports isn't too surprising. Bug: #18859 Reported-by: Olleg Samoylov <splarv@ya.ru> Author: Andrei Lepikhov <lepihov@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/18859-0d5f28ac99a37059@postgresql.org Backpatch-through: 13
1 parent c5c239e commit 7fe312f

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/pl/plpgsql/src/expected/plpgsql_simple.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,14 @@ select simplecaller();
118118
44
119119
(1 row)
120120

121+
-- Check handling of simple expression in a scrollable cursor (bug #18859)
122+
do $$
123+
declare
124+
p_CurData refcursor;
125+
val int;
126+
begin
127+
open p_CurData scroll for select 42;
128+
fetch p_CurData into val;
129+
raise notice 'val = %', val;
130+
end; $$;
131+
NOTICE: val = 42

src/pl/plpgsql/src/pl_exec.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8302,10 +8302,12 @@ exec_save_simple_expr(PLpgSQL_expr *expr, CachedPlan *cplan)
83028302
/*
83038303
* Ordinarily, the plan node should be a simple Result. However, if
83048304
* debug_parallel_query is on, the planner might've stuck a Gather node
8305-
* atop that. The simplest way to deal with this is to look through the
8306-
* Gather node. The Gather node's tlist would normally contain a Var
8307-
* referencing the child node's output, but it could also be a Param, or
8308-
* it could be a Const that setrefs.c copied as-is.
8305+
* atop that; and/or if this plan is for a scrollable cursor, the planner
8306+
* might've stuck a Material node atop it. The simplest way to deal with
8307+
* this is to look through the Gather and/or Material nodes. The upper
8308+
* node's tlist would normally contain a Var referencing the child node's
8309+
* output, but it could also be a Param, or it could be a Const that
8310+
* setrefs.c copied as-is.
83098311
*/
83108312
plan = stmt->planTree;
83118313
for (;;)
@@ -8323,7 +8325,7 @@ exec_save_simple_expr(PLpgSQL_expr *expr, CachedPlan *cplan)
83238325
((Result *) plan)->resconstantqual == NULL);
83248326
break;
83258327
}
8326-
else if (IsA(plan, Gather))
8328+
else if (IsA(plan, Gather) || IsA(plan, Material))
83278329
{
83288330
Assert(plan->lefttree != NULL &&
83298331
plan->righttree == NULL &&

src/pl/plpgsql/src/sql/plpgsql_simple.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,15 @@ as $$select 22 + 22$$;
102102
select simplecaller();
103103

104104
select simplecaller();
105+
106+
-- Check handling of simple expression in a scrollable cursor (bug #18859)
107+
108+
do $$
109+
declare
110+
p_CurData refcursor;
111+
val int;
112+
begin
113+
open p_CurData scroll for select 42;
114+
fetch p_CurData into val;
115+
raise notice 'val = %', val;
116+
end; $$;

0 commit comments

Comments
 (0)