Skip to content

Commit 84266bf

Browse files
Muzzammil Sarwarhackorum
authored andcommitted
Fix leak when a plpgsql exception block catches an error from CALL.
When a non-atomic PL/pgSQL procedure, or any DO block, executes a CALL or DO statement, it pins the statement's plan in a resource owner that lives as long as the procedure or DO block, so that the pin survives a COMMIT or ROLLBACK in the called procedure. SPI releases the pin when the statement completes, but if the CALL fails, that step is skipped. If an exception block then catches the error, nothing releases the pin until the procedure or DO block exits, because rolling back the exception block's subtransaction does not touch that resource owner. A CALL whose arguments use PL/pgSQL variables is given a new custom plan each time it is executed, so each such failure kept a CachedPlan of about 2kB, and a procedure that catches failing CALLs in a loop could use memory without limit. DO blocks were affected in atomic contexts as well, since plpgsql_inline_handler() passes the block's simple-expression resource owner as the procedure resource owner whether or not the block is atomic. To fix, use the procedure-lifespan resource owner only when the called procedure could end the transaction, that is, when SPI will run the CALL non-atomically: in a non-atomic context with no subtransaction active, as SPI_inside_nonatomic_context() reports. Otherwise let SPI use the current resource owner, as it does for other statements. An error from the CALL can only be caught inside a subtransaction, and aborting that subtransaction now releases the pin. Oversight in commit ee895a6, so back-patch to v14.
1 parent 4255812 commit 84266bf

3 files changed

Lines changed: 144 additions & 4 deletions

File tree

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,67 @@ NOTICE: f_print_x(1)
635635
NOTICE: f_get_x(2)
636636
NOTICE: f_print_x(2)
637637
ROLLBACK;
638+
-- A CALL whose error is caught by an exception block must not leave its
639+
-- plan pinned until the calling procedure or DO block exits. Count the
640+
-- session's cached plans before and after ten caught failures, in a second
641+
-- round, so that the first round has created every plan the loop needs.
642+
CREATE PROCEDURE test_proc_error(x int)
643+
LANGUAGE plpgsql
644+
AS $$
645+
BEGIN
646+
RAISE EXCEPTION 'error %', x;
647+
END;
648+
$$;
649+
CREATE PROCEDURE test_proc_catch()
650+
LANGUAGE plpgsql
651+
AS $$
652+
DECLARE
653+
before_count int;
654+
after_count int;
655+
BEGIN
656+
FOR round IN 1..2 LOOP
657+
before_count := (SELECT count(*) FROM pg_backend_memory_contexts
658+
WHERE name = 'CachedPlan');
659+
FOR i IN 1..10 LOOP
660+
BEGIN
661+
CALL test_proc_error(i);
662+
EXCEPTION WHEN OTHERS THEN
663+
NULL;
664+
END;
665+
END LOOP;
666+
after_count := (SELECT count(*) FROM pg_backend_memory_contexts
667+
WHERE name = 'CachedPlan');
668+
END LOOP;
669+
RAISE NOTICE 'cached plans kept: %', after_count - before_count;
670+
END;
671+
$$;
672+
CALL test_proc_catch();
673+
NOTICE: cached plans kept: 0
674+
-- Likewise in a DO block, which has a long-lived resowner of its own even
675+
-- in an atomic context
676+
BEGIN;
677+
DO $$
678+
DECLARE
679+
before_count int;
680+
after_count int;
681+
BEGIN
682+
FOR round IN 1..2 LOOP
683+
before_count := (SELECT count(*) FROM pg_backend_memory_contexts
684+
WHERE name = 'CachedPlan');
685+
FOR i IN 1..10 LOOP
686+
BEGIN
687+
CALL test_proc_error(i);
688+
EXCEPTION WHEN OTHERS THEN
689+
NULL;
690+
END;
691+
END LOOP;
692+
after_count := (SELECT count(*) FROM pg_backend_memory_contexts
693+
WHERE name = 'CachedPlan');
694+
END LOOP;
695+
RAISE NOTICE 'cached plans kept: %', after_count - before_count;
696+
END
697+
$$;
698+
NOTICE: cached plans kept: 0
699+
COMMIT;
700+
DROP PROCEDURE test_proc_catch;
701+
DROP PROCEDURE test_proc_error;

‎src/pl/plpgsql/src/pl_exec.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,17 +2266,24 @@ exec_stmt_call(PLpgSQL_execstate *estate, PLpgSQL_stmt_call *stmt)
22662266
before_lxid = MyProc->vxid.lxid;
22672267

22682268
/*
2269-
* If we have a procedure-lifespan resowner, use that to hold the refcount
2270-
* for the plan. This avoids refcount leakage complaints if the called
2271-
* procedure ends the current transaction.
2269+
* If the called procedure could end the current transaction, hold the
2270+
* refcount for the plan in the procedure-lifespan resowner, which avoids
2271+
* refcount leakage complaints if it does. That can happen only when SPI
2272+
* executes the CALL non-atomically, which requires a non-atomic context
2273+
* and no active subtransaction (see _SPI_execute_plan). In other cases
2274+
* let SPI use the current resowner. That matters when the CALL fails
2275+
* with an error that an exception block catches: aborting the block's
2276+
* subtransaction then releases the refcount, which the procedure-lifespan
2277+
* resowner would otherwise keep until we exit.
22722278
*
22732279
* Also, tell SPI to allow non-atomic execution.
22742280
*/
22752281
memset(&options, 0, sizeof(options));
22762282
options.params = paramLI;
22772283
options.read_only = estate->readonly_func;
22782284
options.allow_nonatomic = true;
2279-
options.owner = estate->procedure_resowner;
2285+
if (SPI_inside_nonatomic_context())
2286+
options.owner = estate->procedure_resowner;
22802287

22812288
rc = SPI_execute_plan_extended(expr->plan, &options);
22822289

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,3 +589,72 @@ END
589589
$$;
590590

591591
ROLLBACK;
592+
593+
-- A CALL whose error is caught by an exception block must not leave its
594+
-- plan pinned until the calling procedure or DO block exits. Count the
595+
-- session's cached plans before and after ten caught failures, in a second
596+
-- round, so that the first round has created every plan the loop needs.
597+
CREATE PROCEDURE test_proc_error(x int)
598+
LANGUAGE plpgsql
599+
AS $$
600+
BEGIN
601+
RAISE EXCEPTION 'error %', x;
602+
END;
603+
$$;
604+
605+
CREATE PROCEDURE test_proc_catch()
606+
LANGUAGE plpgsql
607+
AS $$
608+
DECLARE
609+
before_count int;
610+
after_count int;
611+
BEGIN
612+
FOR round IN 1..2 LOOP
613+
before_count := (SELECT count(*) FROM pg_backend_memory_contexts
614+
WHERE name = 'CachedPlan');
615+
FOR i IN 1..10 LOOP
616+
BEGIN
617+
CALL test_proc_error(i);
618+
EXCEPTION WHEN OTHERS THEN
619+
NULL;
620+
END;
621+
END LOOP;
622+
after_count := (SELECT count(*) FROM pg_backend_memory_contexts
623+
WHERE name = 'CachedPlan');
624+
END LOOP;
625+
RAISE NOTICE 'cached plans kept: %', after_count - before_count;
626+
END;
627+
$$;
628+
629+
CALL test_proc_catch();
630+
631+
-- Likewise in a DO block, which has a long-lived resowner of its own even
632+
-- in an atomic context
633+
BEGIN;
634+
635+
DO $$
636+
DECLARE
637+
before_count int;
638+
after_count int;
639+
BEGIN
640+
FOR round IN 1..2 LOOP
641+
before_count := (SELECT count(*) FROM pg_backend_memory_contexts
642+
WHERE name = 'CachedPlan');
643+
FOR i IN 1..10 LOOP
644+
BEGIN
645+
CALL test_proc_error(i);
646+
EXCEPTION WHEN OTHERS THEN
647+
NULL;
648+
END;
649+
END LOOP;
650+
after_count := (SELECT count(*) FROM pg_backend_memory_contexts
651+
WHERE name = 'CachedPlan');
652+
END LOOP;
653+
RAISE NOTICE 'cached plans kept: %', after_count - before_count;
654+
END
655+
$$;
656+
657+
COMMIT;
658+
659+
DROP PROCEDURE test_proc_catch;
660+
DROP PROCEDURE test_proc_error;

0 commit comments

Comments
 (0)