pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: idrissaajalloh@gmail.com
Subject: BUG #19877: auto_explain omits a sampled cursor plan after sample_rate changes
Date: Wed, 11 Nov 2026 12:33:09 +0000
Message-ID: <19877-d045e13baa7e9857@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      19877
Logged by:          Wren
Email address:      idrissaajalloh@gmail.com
PostgreSQL version: 18.6
Operating system:   Linux/amd64
Description:        

A cursor started while auto_explain.sample_rate is 1 does not emit its plan
if another top-level statement runs with sample_rate set to 0 before the
cursor ends. The cursor was selected and instrumented when it started, so
the later statement should not change whether its plan is emitted at
ExecutorEnd.

How to repeat:

Save the following as case.sql and run it with `psql -X -f case.sql`:

```sql
\set ON_ERROR_STOP on

LOAD 'auto_explain';
SET client_min_messages = notice;
SET auto_explain.log_level = notice;
SET auto_explain.log_min_duration = 0;
SET auto_explain.log_analyze = off;

-- Control: a cursor selected at sample_rate=1 is logged when it ends.
SET auto_explain.sample_rate = 1;
BEGIN;
DECLARE control_cursor CURSOR FOR
  SELECT 'control_marker'::text;
FETCH ALL FROM control_cursor;
COMMIT;

-- This cursor is also selected and instrumented at start. A later top-level
-- statement changes auto_explain's backend-global sampling state.
SET auto_explain.sample_rate = 1;
BEGIN;
DECLARE victim_cursor CURSOR FOR
  SELECT 'victim_marker'::text;
FETCH ALL FROM victim_cursor;
SET auto_explain.sample_rate = 0;
SELECT 'resampler_marker'::text;
COMMIT;

RESET auto_explain.sample_rate;
RESET auto_explain.log_analyze;
RESET auto_explain.log_min_duration;
RESET auto_explain.log_level;
RESET client_min_messages;
```

The recorded stdout confirms that both cursors ran and returned their rows:

```
BEGIN
DECLARE CURSOR
     text
----------------
 control_marker
(1 row)

COMMIT
SET
BEGIN
DECLARE CURSOR
     text
---------------
 victim_marker
(1 row)

SET
       text
------------------
 resampler_marker
(1 row)

COMMIT
```

However, stderr contains a plan only for the control cursor:

```
psql:case.sql:15: NOTICE:  duration: 0.004 ms  plan:
Query Text: DECLARE control_cursor CURSOR FOR
  SELECT 'control_marker'::text;
Result  (cost=0.00..0.01 rows=1 width=32)
```

There is no corresponding NOTICE for victim_cursor. Since both cursors
started with sample_rate=1 and log_min_duration=0, both should emit a plan.
The control cursor also confirms that NOTICE-level auto_explain output is
reaching the client.

This was reproduced on PostgreSQL 18.6 on Linux/amd64 using the packaged
auto_explain module. The evidence suggests that the sampling decision is
kept in backend-global state and consulted again at ExecutorEnd, allowing
the intervening statement to overwrite the decision made for the still-open
cursor. This occurs when auto_explain sampling is changed by a top-level
statement while an instrumented cursor remains open.








Message-ID: <19877-d045e13baa7e9857@postgresql.org>
Permalink:  ../19877-d045e13baa7e9857@postgresql.org/
Also on:    postgresql.org/message-id/19877-d045e13baa7e9857@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, idrissaajalloh@gmail.com
  Subject: Re: BUG #19877: auto_explain omits a sampled cursor plan after sample_rate changes
  In-Reply-To: <19877-d045e13baa7e9857@postgresql.org>
  References: <19877-d045e13baa7e9857@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox