Skip to content

Commit fd1347f

Browse files
committed
Fix thinko in plpgsql memory leak fix.
Commit a6b1f53 intended to place the transient "target" list of a CALL statement in the function's statement-lifespan context, but I fat-fingered that and used get_eval_mcontext() instead of get_stmt_mcontext(). The eval_mcontext belongs to the "simple expression" infrastructure, which is destroyed at transaction end. The net effect is that a CALL in a procedure to another procedure that has OUT or INOUT parameters would fail if the called procedure did a COMMIT. Per report from Peter Eisentraut. Back-patch to v11, like the prior patch. Discussion: https://postgr.es/m/f075f7be-c654-9aa8-3ffc-e9214622f02a@enterprisedb.com
1 parent 190f0af commit fd1347f

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/pl/plpgsql/src/expected/plpgsql_call.out

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,27 @@ CALL test_proc7(100, -1, -1);
152152
0 | 1
153153
(1 row)
154154

155+
-- inner COMMIT with output arguments
156+
CREATE PROCEDURE test_proc7c(x int, INOUT a int, INOUT b numeric)
157+
LANGUAGE plpgsql
158+
AS $$
159+
BEGIN
160+
a := x / 10;
161+
b := x / 2;
162+
COMMIT;
163+
END;
164+
$$;
165+
CREATE PROCEDURE test_proc7cc(_x int)
166+
LANGUAGE plpgsql
167+
AS $$
168+
DECLARE _a int; _b numeric;
169+
BEGIN
170+
CALL test_proc7c(_x, _a, _b);
171+
RAISE NOTICE '_x: %,_a: %, _b: %', _x, _a, _b;
172+
END
173+
$$;
174+
CALL test_proc7cc(10);
175+
NOTICE: _x: 10,_a: 1, _b: 5
155176
-- named parameters and defaults
156177
CREATE PROCEDURE test_proc8a(INOUT a int, INOUT b int)
157178
LANGUAGE plpgsql

src/pl/plpgsql/src/pl_exec.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,8 +2212,8 @@ exec_stmt_call(PLpgSQL_execstate *estate, PLpgSQL_stmt_call *stmt)
22122212
int i;
22132213
ListCell *lc;
22142214

2215-
/* Use eval_mcontext for any cruft accumulated here */
2216-
oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
2215+
/* Use stmt_mcontext for any cruft accumulated here */
2216+
oldcontext = MemoryContextSwitchTo(get_stmt_mcontext(estate));
22172217

22182218
/*
22192219
* Get the parsed CallStmt, and look up the called procedure

src/pl/plpgsql/src/sql/plpgsql_call.sql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,30 @@ $$;
141141

142142
CALL test_proc7(100, -1, -1);
143143

144+
-- inner COMMIT with output arguments
145+
146+
CREATE PROCEDURE test_proc7c(x int, INOUT a int, INOUT b numeric)
147+
LANGUAGE plpgsql
148+
AS $$
149+
BEGIN
150+
a := x / 10;
151+
b := x / 2;
152+
COMMIT;
153+
END;
154+
$$;
155+
156+
CREATE PROCEDURE test_proc7cc(_x int)
157+
LANGUAGE plpgsql
158+
AS $$
159+
DECLARE _a int; _b numeric;
160+
BEGIN
161+
CALL test_proc7c(_x, _a, _b);
162+
RAISE NOTICE '_x: %,_a: %, _b: %', _x, _a, _b;
163+
END
164+
$$;
165+
166+
CALL test_proc7cc(10);
167+
144168

145169
-- named parameters and defaults
146170

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy