[PG19] plpgsql: SELECT INTO sets FOUND wrongly after a function becomes a SRF

From: shihao zhong <zhong950419(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: [PG19] plpgsql: SELECT INTO sets FOUND wrongly after a function becomes a SRF
Date: 2026-10-05 16:34:18
Message-ID: CAGRkXqQB_aqWU9OeVPGvATBVZ7c9EYrNXJyHG0jKXW0FGAy2-g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

I used Opus to analyze the new features in PG19, and it found a problem
with the "SELECT simple-expression INTO var" fast path from ce8d5fe0e28.

create function f1() returns int language sql as 'select 1';
create function caller() returns bool language plpgsql as $$
declare v int;
begin
select f1() into v;
return found;
end $$;
select caller();
drop function f1();
create function f1() returns setof int language sql
as 'select 1 where false';
select caller();

The last call gives f on 18 and t on 19. Only the first call after the
change is wrong. The attached .sql has more cases.
It is hard to hit. f1() has to be recreated as set-returning while a
session has caller() cached, for example by an extension update.
I think it is because exec_stmt_execsql() calls exec_eval_expr(), which
falls back to SPI when a replan finds the query is no longer simple.
FOUND is then set as if one row came back.

0001 calls exec_eval_simple_expr() directly and uses the regular SPI
code when it returns false. That is the only time the new branch runs,
so the fast path costs the same. It also removes a doubled 'SQL
statement' context line in errors raised under recursion. 0002 is a
test, it is optional.

It is a regression from 18, but a narrow one, so I will not set the open
items list.

Thanks,
Shihao

Attachment Content-Type Size
v1-0001-plpgsql-fix-SELECT-INTO-when-the-expression-is-no.patch application/octet-stream 2.9 KB
v1-0002-plpgsql-test-SELECT-INTO-after-a-function-becomes.patch application/octet-stream 2.9 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Burd 2026-10-05 16:46:52 Re: Let an ordering index scan hand its ORDER BY value to the target list
Previous Message Tomas Vondra 2026-10-05 16:24:20 Re: hashjoins vs. Bloom filters (yet again)