Re: [PATCH] Add NESTED_STATEMENTS option to EXPLAIN
Mohamed ALi <moali.pg@gmail.com>
From: Mohamed ALi <moali.pg@gmail.com>
To: Zsolt Parragi <zsolt.parragi@percona.com>
Cc: pgsql-hackers@lists.postgresql.org
Date: 2026-05-16T14:52:48Z
Lists: pgsql-hackers
Hi Zsolt,
Thanks for testing! I've identified the root causes and am working on
v2 with the fixes. Will share soon.
-- Mohamed Ali
On Sat, May 16, 2026 at 4:15 AM Zsolt Parragi <zsolt.parragi@percona.com> wrote:
>
> Hello!
>
> With some initial testing I was able to find 2 server crashes with the patch.
>
> 1: error during explain
>
> CREATE OR REPLACE FUNCTION divz_plpgsql(x int) RETURNS int LANGUAGE
> plpgsql AS $$
> DECLARE r int;
> BEGIN
> SELECT 1/x INTO r;
> RETURN r;
> END;
> $$;
> -- error during explain
> EXPLAIN (ANALYZE, NESTED_STATEMENTS) SELECT divz_plpgsql(0);
> -- run another explain
> EXPLAIN (ANALYZE, NESTED_STATEMENTS) SELECT 1;
> -- any next query crashes the server
> SELECT 1;
>
> 2: any nested explain
>
> CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$
> DECLARE r record;
> BEGIN
> FOR r IN EXECUTE 'EXPLAIN (ANALYZE, NESTED_STATEMENTS) SELECT 1'
> LOOP NULL; END LOOP;
> END;
> $$;
>
> EXPLAIN (ANALYZE, NESTED_STATEMENTS) SELECT f();
>
>
> + /*
> + * Switch to TopMemoryContext so the captured plan text survives
> + * until we print it.
> + */
> + oldcxt = MemoryContextSwitchTo(TopMemoryContext);
> ...
> + ExplainBeginOutput(nes);
> + ExplainPrintPlan(nes, queryDesc);
> + ExplainEndOutput(nes);
> ...
> + plan_info->query_text = queryDesc->sourceText ?
> + pstrdup(queryDesc->sourceText) : pstrdup("<unknown>");
>
> When is queryDesc freed? This seems like a memory leak.
>
>