Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea80d8d

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/[email protected]
1 parent 643428c commit ea80d8d

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
@@ -2235,8 +2235,8 @@ exec_stmt_call(PLpgSQL_execstate *estate, PLpgSQL_stmt_call *stmt)
22352235
int i;
22362236
ListCell *lc;
22372237

2238-
/* Use eval_mcontext for any cruft accumulated here */
2239-
oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
2238+
/* Use stmt_mcontext for any cruft accumulated here */
2239+
oldcontext = MemoryContextSwitchTo(get_stmt_mcontext(estate));
22402240

22412241
/*
22422242
* 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)