Subject: pgsql: Fix plpgsql's handling of simple expressions in scrollable curso

From: Tom Lane <[email-masked]>
Date: Fri, 21 Mar 2025 15:31:13 +0000

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 <[email-masked]>
Author: Andrei Lepikhov <[email-masked]>
Reviewed-by: Tom Lane <[email-masked]>
Discussion: https://postgr.es/m/[email-masked]
Backpatch-through: 13

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/a28c1fb619bfbb82e4dc4965502d33f5a77bde52

Modified Files
--------------
src/pl/plpgsql/src/expected/plpgsql_simple.out | 11 +++++++++++
src/pl/plpgsql/src/pl_exec.c                   | 12 +++++++-----
src/pl/plpgsql/src/sql/plpgsql_simple.sql      | 12 ++++++++++++
3 files changed, 30 insertions(+), 5 deletions(-)