Re: RFC: Logging plan of the running query

From: torikoshia <torikoshia(at)oss(dot)nttdata(dot)com>
To: Akshat Jaimini <destrex271(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: RFC: Logging plan of the running query
Date: 2025-11-19 03:43:22
Message-ID: 4d067d7934fd94f696e72feedc5fc1f9@oss.nttdata.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Xuneng Zhou 2025-11-19 03:44:05 Re: Improve read_local_xlog_page_guts by replacing polling with latch-based waiting
Previous Message cca5507 2025-11-19 03:42:59 Re: Why is_admin_of_role() use ROLERECURSE_MEMBERS rather than ROLERECURSE_PRIVS?