[PATCH] auto_explain: preserve per-query sampling decisions (#19877)

From: Hristos Nenkov <nenkov2004(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: idrissaajalloh(at)gmail(dot)com
Subject: [PATCH] auto_explain: preserve per-query sampling decisions (#19877)
Date: 2026-11-14 01:28:17
Message-ID: CAO7_G+xq=H6NTQzavQKvJuh_0U5S_aJZmsgu_DZ_cSBtaaVniQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I reproduced #19877 and attached a candidate fix for review.

auto_explain currently checks its backend-global sampling flag again at
ExecutorEnd. A statement executed while a cursor remains open can replace
that flag, so a sampled cursor loses its plan. With pg_stat_statements
requesting instrumentation, the reverse also happens: an unsampled cursor
can be logged after another statement enables sampling.

The patch records selection and nesting depth in auto_explain's own
QueryDesc extension state, following the existing pg_stat_statements
pattern. ExecutorEnd uses that state rather than the latest global flag.
It also adds cursor tests for explicit CLOSE and implicit COMMIT, with
pg_stat_statements preloaded.

Tested in WSL on Ubuntu, against
70c4a57b64d39c526eb4c44a925eb92a6e56f3c5, with --enable-cassert:

- Unpatched: 4 failures out of 18 sampling assertions.
- Patched: all 18 sampling assertions pass.
- Full auto_explain and pg_stat_statements check targets pass.
- PostgreSQL core make check passes.

Meson registration was reviewed, but I have not run a Meson build. This is
a master-only candidate using the QueryDesc extension-state API, not a
backpatch for released branches. It does not change sampling inheritance
when nested queries start during cursor execution.

I used Codex to assist with the patch and test preparation, and ran the
Linux validation locally.

Report:
https://www.postgresql.org/message-id/19877-d045e13baa7e9857@postgresql.org

Feedback on the approach is welcome.

The change adds a small state allocation for selected queries; I have not
benchmarked its overhead. I'm sending this for review for the next
CommitFest.

Thanks,
Hristos Nenkov

Attachment Content-Type Size
v1-0001-auto-explain-preserve-query-sampling.patch application/octet-stream 4.5 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Manu 2026-11-14 01:28:33 Re: Make COPY format extendable: Extract COPY TO format implementations
Previous Message Manu 2026-11-14 01:23:07 Re: REPACK enhancements