Re: RFC: Logging plan of the running query

torikoshia <torikoshia@oss.nttdata.com>

From: torikoshia <torikoshia@oss.nttdata.com>
To: Akshat Jaimini <destrex271@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org
Date: 2025-11-19T03:43:22Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. ecpg: Catch zero-length Unicode identifiers correctly

  2. Improve warning message in pg_signal_backend()

  3. Add assert to ensure that page locks don't participate in deadlock cycle.

On 2025-11-19 05:19, Akshat Jaimini wrote:

Thanks for your review!

> Hi,
> I have a question:
> 
> In src/backend/executor/execMain.c:
> 
> ```
> +	SetCurrentQueryDesc(oldQueryDesc);
> +
> +	/*
> +	 * Ensure LogQueryPlanPending is initialized in case there was no 
> time for
> +	 * logging the plan. Othewise plan will be logged at the next query
> +	 * execution on the same session.
> +	 */
> +	LogQueryPlanPending = false;
> ```
> 
> It would be really helpful if you could elaborate on any cases where
> this specific situation might arise i.e. where 'there was no time for
> logging the plan'. Are we referencing to something like a sudden
> shutdown of the postmaster process or is this referring to something
> else entirely?

What I have in mind are cases where a query finishes before 
LogQueryPlan() is ever invoked.
Since LogQueryPlan() is called from ExecProcNodeFirst(), this generally 
means pg_log_query_plan() was called at the moment just before query 
execution completes.
Also, very short queries fall into this category:

   =# select pg_log_query_plan(pg_backend_pid());
    pg_log_query_plan
   -------------------
    t
   (1 row)

   =# select 1;

With the current patch, nothing is logged here.
But if I comment out the "LogQueryPlanPending = false" line, the plan 
for "SELECT 1" ends up being logged:

   LOG:  00000: query and its plan running on backend with PID 33040 are:
   Query Text: select 1;
   Result  (cost=0.00..0.01 rows=1 width=4)
          Output: 1
          Settings: jit = 'off'


-- 
Regards,

--
Atsushi Torikoshi
Seconded from NTT DATA Japan Corporation to SRA OSS K.K.