Re: RFC: Logging plan of the running query

From: Andrei Lepikhov <lepihov(at)gmail(dot)com>
To: torikoshia <torikoshia(at)oss(dot)nttdata(dot)com>
Cc: Lukas Fittl <lukas(at)fittl(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org, Atsushi Torikoshi <torikoshia(dot)tech(at)gmail(dot)com>, samimseih(at)gmail(dot)com, destrex271(at)gmail(dot)com
Subject: Re: RFC: Logging plan of the running query
Date: 2026-07-12 23:28:55
Message-ID: b7985819-a760-4e59-b1f4-d793b8cddcc5@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 07/07/2026 16:46, torikoshia wrote:
> On 2026-07-06 22:05, Andrei Lepikhov wrote:
>>> That said, I think a similar situation can still occur if execution
>>> moves to an external or deeper queryDesc after the individual plan
>>> nodes have been wrapped. In that case, the plan does not be logged
>>> even though the query continues executing.
>>
>> That's the problem.
>
> Sorry, I misremembered the logic.
> In this case, even if execution moves to an external or deeper
> QueryDesc, LogQueryPlanPending flag remains set. Therefore, the plan
> tree for that external or deeper QueryDesc will be wrapped, and
> the plan will be logged.
Hmm, maybe I misunderstood the code in the standard_ExecutorRun:

/*
* Ensure LogQueryPlanPending is initialized in case there was no time for
* logging the plan. Otherwise plan will be logged at the next query
* execution on the same session.
*/
LogQueryPlanPending = false;

But here is kind of reproduction:

-- Session 1:
BEGIN;
SELECT pg_advisory_xact_lock(1);

-- Session 2:
SELECT pg_backend_pid();
BEGIN;
DECLARE c CURSOR FOR
SELECT pg_advisory_xact_lock(g) FROM generate_series(1, 5) g;
FETCH 1 FROM c;

-- Session 1:
SELECT pg_log_query_plan(:pid);
SELECT pg_sleep(1);
COMMIT;

-- Session 2:
FETCH 1 FROM c;
COMMIT;

I don't see any explain or other logging activity in the log. I wonder if
extended protocol faces the same problem.
So, it potentially makes sense to add a statistics field to the activity stat to
let users know whether the logging (and how many signals, if replace flag with a
counter) is awaiting execution.

Maybe it doesn't need to clean up the 'pending' flag at all if no query has been
executed yet - the query might be under planning, and the EXPLAIN will be logged
right at the beginning of execution.

--
regards, Andrei Lepikhov,
pgEdge

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2026-07-13 00:56:05 Re: Change checkpoint‑record‑missing PANIC to FATAL
Previous Message Ayoub Kazar 2026-07-12 22:44:06 Re: Add pg_stat_vfdcache view for VFD cache statistics