Re: RFC: Logging plan of the running query

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: torikoshia <torikoshia(at)oss(dot)nttdata(dot)com>
Cc: Andrei Lepikhov <lepihov(at)gmail(dot)com>, Lukas Fittl <lukas(at)fittl(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-11-29 01:53:01
Message-ID: CA+TgmoYb05=cqP5+qNNuXa9aJR4=Q3ONgdXYgKue6z4zxHzSpQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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:

- 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().

- 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.

- 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.

- 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.

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.)

- 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().

--
Robert Haas
Databricks

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tender Wang 2026-11-29 01:56:12 Re: Preserve presorted ORDER BY keys when reordering DISTINCT ON keys
Previous Message jian he 2026-11-29 01:10:47 Re: Proposal: Include CHECK constraint expression in violation error DETAIL