Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xR8rW-00000000idN-2ccS for pgsql-bugs@arkaria.postgresql.org; Wed, 11 Nov 2026 13:59:42 +0000 Received: from localhost ([127.0.0.1]:46710 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xR8rV-0000000BMB8-1DEZ for pgsql-bugs@arkaria.postgresql.org; Wed, 11 Nov 2026 13:59:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:54300) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xR7Wh-0000000Araf-2796 for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 12:34:07 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xR7Wf-00000000N5M-3VBl for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 12:34:06 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=2BsmQySI1sKnHziCKw5WZUPxqQ6gbDz78uOXrc99Jhg=; b=oSMNDCBFlHG1l3nFWDn8Y7r3JR aoWHafI7QRSh3f1tudm1tBDp/qnem7sxdqmekmjj4isLSdrIbrttGAu7Z32/fNvovWPYybsy3V4Pw +H2p/YwBKHndcOsEEtek884CbK/UZXxEH4z2Vj7IVjtK4AOZYb1d0UDk/hOp5p1sa70LpmLHZCiWw E4G1YY9roFUyCXoxdTkAmJEyilKyNHpL+LQD5fh9Ccp908BQcd+kjUnYeJzT9PI0vNN/HZCrUzUqf VEjfopDcOFQMCVl80Syk959dtwNaYMe5EpKHQwfgUWEw020yYdDFIB271VE1a952o+4GkAs1rxFZT HPyNYShA==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xR7We-000000011Xq-3ty3 for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 12:34:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xR7Wd-00000002goY-06CZ for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 12:34:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19877: auto_explain omits a sampled cursor plan after sample_rate changes To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: idrissaajalloh@gmail.com Reply-To: idrissaajalloh@gmail.com, pgsql-bugs@lists.postgresql.org Date: Wed, 11 Nov 2026 12:33:09 +0000 Message-ID: <19877-d045e13baa7e9857@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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: =20 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 =3D notice; SET auto_explain.log_level =3D notice; SET auto_explain.log_min_duration =3D 0; SET auto_explain.log_analyze =3D off; -- Control: a cursor selected at sample_rate=3D1 is logged when it ends. SET auto_explain.sample_rate =3D 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 =3D 1; BEGIN; DECLARE victim_cursor CURSOR FOR SELECT 'victim_marker'::text; FETCH ALL FROM victim_cursor; SET auto_explain.sample_rate =3D 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=3D0.00..0.01 rows=3D1 width=3D32) ``` There is no corresponding NOTICE for victim_cursor. Since both cursors started with sample_rate=3D1 and log_min_duration=3D0, both should emit a p= lan. 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.