On Thu, Dec 3, 2026 at 1:53 PM Jeevan Chalke <[email protected]>
wrote:

> Hi,
>
> With track_functions enabled, a procedure that ROLLBACKs the CREATE of a
> procedure that is still executing corrupts memory and can crash the server.
>
> pgstat_init_function_usage() stores a pointer to the function's pending
> stats entry in PgStat_FunctionCallUsage.  If the ROLLBACK drops the stats
> entry, the pending counters are pfree'd, and when the call returns
> pgstat_end_function_usage() does fs->numcalls++ on the freed chunk.
> numcalls is the first field, and the first 8 bytes of a freed AllocSet
> chunk are the freelist link, so the link becomes 0x1 and a later
> allocation from "PgStat Pending" crashes in AllocSetAlloc():
>
>     SET track_functions = 'pl';
>     CREATE PROCEDURE outer_p() LANGUAGE plpgsql AS $o$
>     BEGIN
>         EXECUTE $c$ CREATE PROCEDURE pg_temp.inner_p() LANGUAGE plpgsql AS
> $b$
>                     BEGIN ROLLBACK; END $b$ $c$;
>         CALL pg_temp.inner_p();
>     END $o$;
>     CREATE PROCEDURE q1() LANGUAGE plpgsql AS $$ BEGIN NULL; END $$;
>     CREATE PROCEDURE q2() LANGUAGE plpgsql AS $$ BEGIN NULL; END $$;
>     CALL outer_p();
>     CALL q1();
>     CALL q2();      -- SIGSEGV
>
> The write goes into freed memory on all branches, but only corrupts the
> freelist since v16.
>
> The attached patch adds a backend-local counter, bumped whenever the
> pending data of a function stats entry is freed.  PgStat_FunctionCallUsage
> records the counter and the function OID at call start; at call end, if the
> counter changed, the entry is looked up again and the update is skipped
> unless it is still the same one.  The common case costs one integer
> comparison.
>
> It also adds a regression test to plpgsql_transaction, which crashes
> without the fix.
>
> I think this should be back-patched.
>
> Comments...
>
> Thanks
>
>
I reproduced this, and the patch fixes the issue.

Stacktrace for better indexing:
Program received signal SIGSEGV, Segmentation fault.
0x000060abe294630b in AllocSetAlloc (context=0x60abf4637750, size=24,
flags=0) at ../src/backend/utils/mmgr/aset.c:1085
1085                    set->freelist[fidx] = link->next;
(gdb) where
#0  0x000060abe294630b in AllocSetAlloc (context=0x60abf4637750, size=24,
flags=0) at ../src/backend/utils/mmgr/aset.c:1085
#1  0x000060abe2950471 in MemoryContextAllocZero (context=0x60abf4637750,
size=24) at ../src/backend/utils/mmgr/mcxt.c:1279
#2  0x000060abe2753774 in pgstat_prep_pending_from_entry_ref
(entry_ref=0x60abf46383f0) at ../src/backend/utils/activity/pgstat.c:1410
#3  0x000060abe27535fd in pgstat_prep_pending_entry (kind=4, dboid=5,
objid=16427, created_entry=0x7ffe1be1dc87) at
../src/backend/utils/activity/pgstat.c:1337
#4  0x000060abe2758ce1 in pgstat_init_function_usage
(fcinfo=0x7ffe1be1dde0, fcu=0x7ffe1be1dd90) at
../src/backend/utils/activity/pgstat_function.c:86
#5  0x000060abe23be75d in ExecuteCallStmt (stmt=0x60abf4610478, params=0x0,
atomic=false, dest=0x60abf4610a80) at
../src/backend/commands/functioncmds.c:2332
[..]

Code on surface looks good to me, but I'm not an expert. It might not merge
out of the box on v15 and v18 too. I think it could have CF bug entry with
RfC.

-J.

Reply via email to