Re: RFC: Logging plan of the running query

From: Atsushi Torikoshi <torikoshia(dot)tech(at)gmail(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Andrei Lepikhov <lepihov(at)gmail(dot)com>, Lukas Fittl <lukas(at)fittl(dot)com>, PostgreSQL-development <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Atsushi Torikoshi <torikoshia(dot)tech(at)gmail(dot)com>, samimseih(at)gmail(dot)com, Akshat Jaimini <destrex271(at)gmail(dot)com>
Subject: Re: RFC: Logging plan of the running query
Date: 2026-12-03 14:06:19
Message-ID: CAM6-o=CeYUypq8WLHKG9PQqJvEb2PGsCojciezsaW9BoC8_Jfw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, Nov 29, 2026 at 10:53 AM Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
>
> On Mon, Oct 19, 2026 at 9:09 AM torikoshia <torikoshia(at)oss(dot)nttdata(dot)com> wrote:
> > 0001 patch has not been changed from v60, which focuses on obtaining
> > the execution plan of a running query safely.
>
> I'm finding that this patch still has correctness bugs. Simply
> applying the patch locally and asking Claude to "review HEAD" turns up
> a bunch of them. I'd strongly encourage you to do at least that much
> LLM review of each patch before posting a new version, because human
> time is very precious right now, and computers can find a lot of
> things. For example, Claude found:

Thanks for your review again.
I had been reviewing the patch with another AI tool, but it didn't catch most
of these issues, so I've started using Claude and updated the patch.

This version contains only 0001. 0002 (request IDs and management of
logging requests) is omitted for now and will be posted again later.

> - PushTransaction() doesn't copy the parent TransactionStateData's
> queryDesc into the new transaction level, so the instant you enter a
> subtransaction, it looks like there's no longer a query running. It
> might be better to keep the active query descriptors on a separate
> stack with the nestLevel() attached to each entry instead of directly
> in the TransactionStateData. Then you could just do
> PushQueryDesc(desc) and PopQueryDesc(desc). The latter would assert
> that desc is in fact what's at the top of the stack and then pop it.
> This isn't entirely necessary; you can make the current structure
> work, too, but not without an adjustment in PushTransaction().

Modified PushTransaction() to inherit the outer query's descriptor.

> - The LogQueryPlanPending logic at the bottom of standard_ExecutorRun
> is incorrect because it assumes that we're ending the only query that
> is currently running; but there might be an outer query level still
> executing.

Fixed so that an unfulfilled request is handed over to the outer query level,
and discarded only when leaving the outermost one.

> - LogQueryPlan() should clear the flag at the beginning, not the end,
> because we've already committed to logging the query plan at that
> point, and if ProcessLogQueryPlanInterrupt() is called again we'll
> rewrap the whole tree for nothing. And that's entirely plausible:
> nothing stops us from reentering ProcessInterrupts(). It might be
> better to split the flag that triggers entry into
> ProcessLogQueryPlanInterrupt() from the one that triggers
> ExecProcNodeFirst() to call LogQueryPlan(). If you have two flags
> then, you can clear one of them after wrapping and the other one after
> logging.

Split the flag in two as suggested and clear the flag at the beginning.

> - when pg_log_query_plan() calls pgstat_get_beentry_by_proc_number(),
> that's actually not safe. It may use a cached copy of the backend
> status data, and that cached copy could have been cached from,
> potentially, an hour ago, because it's transaction-lifespan. So if
> you're targeting a process that's newer than our transaction's stat
> snapshot, you'll get an error saying it's not a PostgreSQL backend
> process, which will not be true.

Fixed by looking up the target via BackendPidGetProc() and
checking proc->backendType instead of the stats snapshot.

> Leaving Claude aside, I see several things here that I don't like:
>
> - LogQueryPlan() uses a try/catch block to clean up a child of the
> current memory context, but the error we're throwing should be doing
> that anyway. You only need this if there's no error, and that doesn't
> need a try/catch block. (If you did need a try/catch block, it ought
> to be in effect from the moment cxt was created, which it isn't,
> because NewExplainState() could OOM.)

Removed the try/catch block.

> - I don't have a good feeling about the message "query plan logging
> was requested but there was no opportunity to do it". It seems like a
> weird message to me. Maybe it's good for debugging the patch, but if
> the query finishes running before we can log the plan, then I don't
> think we need to apologize for that. I feel like that should be a very
> rare case, though: a plan cannot really do anything without calling
> ExecProcNode(), so the only way we should be able to hit these
> messages is if (a) the interrupt arrives while we're not executing a
> query or (b) the interrupt arrives after the query has already done
> substantially all of the work, but just hasn't quite finished
> unwinding the call stack. It's OK to treat that the same as if no
> query was executing. But I think these messages aren't just noise but
> rather pointing to real logic bugs, some of the ones above plus this:
> we could also set the QueryDesc on the transaction state in
> ExecutorFinish(), in case we spend a lot of time inside
> ExecPostprocessPlan().

Removed the messages and made standard_ExecutorFinish()
register its QueryDesc.

Also, on error, the previous QueryDesc is restored and any
unfulfilled request is handed over to the outer query level or
discarded by (sub)transaction abort, so a request can't carry over
to the next statement.

--
Atsushi Torikoshi

Attachment Content-Type Size
v62-0001-Add-function-to-log-the-plan-of-the-currently-ru.patch application/octet-stream 40.3 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message brianmacy 2026-12-03 14:21:08 Re: [PATCH v1] Open only the target segment to reach existing blocks
Previous Message Ilia Evdokimov 2026-12-03 13:58:52 Re: Incorrect selectivity estimates for NOT on nullable boolean columns