Re: [PATCH] Add NESTED_STATEMENTS option to EXPLAIN

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: [PATCH] Add NESTED_STATEMENTS option to EXPLAIN
Date: 2026-05-16 11:15:34
Message-ID: CAN4CZFM+b5mD4o=5WzSQjBsZOeeZaRfUPKeFgif4bwsccOLaHA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2026-05-16 11:22:03 Re: Random pg_upgrade 004_subscription test failure on drongo
Previous Message Dean Rasheed 2026-05-16 09:39:30 Re: [PATCH] Fix overflow and underflow in regr_r2()