From 2b9ca8aca7bd72e3769f92685fd41eda3fbe13a3 Mon Sep 17 00:00:00 2001
From: Sai Pranav <rajasaipranav0@gmail.com>
Date: Fri, 20 Nov 2026 15:37:52 +0000
Subject: [PATCH v1] Let DECLARE CURSOR read transition tables inside a trigger

A SQL-level DECLARE CURSOR run inside a trigger function could not
read the trigger's transition tables.  The query was parsed with the
query environment, so the table name was found, but the cursor's
portal was created without it, and the executor then failed with
"executor could not find named tuplestore".  Cursors opened through
SPI, such as PL/pgSQL's OPEN ... FOR, already set the portal's query
environment.

Set the portal's query environment when the cursor's query reads an
ephemeral named relation.  When the environment belongs to an SPI
connection, SPI_finish() drops the portal together with it, as it
does for SPI cursors, so the cursor can't outlive the transition
tables.  Other cursors keep their current lifetime.

Discussion: https://postgr.es/m/19902-3f34fad0a1199e2e@postgresql.org
---
 src/backend/commands/portalcmds.c      | 16 ++++++++++++
 src/test/regress/expected/triggers.out | 36 ++++++++++++++++++++++++++
 src/test/regress/sql/triggers.sql      | 31 ++++++++++++++++++++++
 3 files changed, 83 insertions(+)

diff --git a/src/backend/commands/portalcmds.c b/src/backend/commands/portalcmds.c
index 01efac3319..1771f06c64 100644
--- a/src/backend/commands/portalcmds.c
+++ b/src/backend/commands/portalcmds.c
@@ -120,6 +120,22 @@ PerformCursorOpen(ParseState *pstate, DeclareCursorStmt *cstmt, ParamListInfo pa
 					  list_make1(plan),
 					  NULL);
 
+	/*
+	 * If the query reads ephemeral named relations, such as a trigger's
+	 * transition tables, the portal needs the query environment to find them.
+	 * When that environment belongs to an SPI connection, SPI_finish() drops
+	 * the portal along with it, as it does for cursors opened through SPI.
+	 * Other cursors are left alone, so they keep their current lifetime.
+	 */
+	foreach_node(RangeTblEntry, rte, plan->rtable)
+	{
+		if (rte->rtekind == RTE_NAMEDTUPLESTORE)
+		{
+			portal->queryEnv = pstate->p_queryEnv;
+			break;
+		}
+	}
+
 	/*----------
 	 * Also copy the outer portal's parameter list into the inner portal's
 	 * memory context.  We want to pass down the parameter values in case we
diff --git a/src/test/regress/expected/triggers.out b/src/test/regress/expected/triggers.out
index 16e7134176..4f380b6288 100644
--- a/src/test/regress/expected/triggers.out
+++ b/src/test/regress/expected/triggers.out
@@ -3834,3 +3834,39 @@ ERROR:  null value in column "parentnotnull" of relation "trignulldiff_child" vi
 DETAIL:  Failing row contains (2, 444, null).
 DROP TABLE IF EXISTS trignulldiff_parent CASCADE;
 DROP FUNCTION IF EXISTS trignulldiff_return_parent();
+--
+-- DECLARE CURSOR inside a trigger can read the transition tables, and the
+-- cursor is closed when the trigger function returns
+--
+create table transition_cursor_tbl (a int);
+create function transition_cursor_trig() returns trigger
+language plpgsql as $$
+declare
+  c refcursor := 'transition_cursor';
+  v int;
+  n int := 0;
+begin
+  execute 'declare transition_cursor cursor for select a from new_table';
+  loop
+    fetch c into v;
+    exit when not found;
+    n := n + 1;
+  end loop;
+  raise notice 'transition cursor rows: %', n;
+  return null;
+end $$;
+create trigger transition_cursor_trig after insert on transition_cursor_tbl
+  referencing new table as new_table
+  for each statement execute function transition_cursor_trig();
+begin;
+insert into transition_cursor_tbl values (1), (2), (3);
+NOTICE:  transition cursor rows: 3
+select count(*) from pg_cursors where name = 'transition_cursor';
+ count 
+-------
+     0
+(1 row)
+
+commit;
+drop table transition_cursor_tbl;
+drop function transition_cursor_trig();
diff --git a/src/test/regress/sql/triggers.sql b/src/test/regress/sql/triggers.sql
index 99e85fac39..45bf61bc1d 100644
--- a/src/test/regress/sql/triggers.sql
+++ b/src/test/regress/sql/triggers.sql
@@ -2950,3 +2950,34 @@ INSERT INTO trignulldiff_child VALUES (2, 444, NULL) RETURNING *;
 
 DROP TABLE IF EXISTS trignulldiff_parent CASCADE;
 DROP FUNCTION IF EXISTS trignulldiff_return_parent();
+
+--
+-- DECLARE CURSOR inside a trigger can read the transition tables, and the
+-- cursor is closed when the trigger function returns
+--
+create table transition_cursor_tbl (a int);
+create function transition_cursor_trig() returns trigger
+language plpgsql as $$
+declare
+  c refcursor := 'transition_cursor';
+  v int;
+  n int := 0;
+begin
+  execute 'declare transition_cursor cursor for select a from new_table';
+  loop
+    fetch c into v;
+    exit when not found;
+    n := n + 1;
+  end loop;
+  raise notice 'transition cursor rows: %', n;
+  return null;
+end $$;
+create trigger transition_cursor_trig after insert on transition_cursor_tbl
+  referencing new table as new_table
+  for each statement execute function transition_cursor_trig();
+begin;
+insert into transition_cursor_tbl values (1), (2), (3);
+select count(*) from pg_cursors where name = 'transition_cursor';
+commit;
+drop table transition_cursor_tbl;
+drop function transition_cursor_trig();
-- 
2.50.1 (Apple Git-155)

