BUG #19939: In a read-only transaction, a plain SELECT ... INTO is refused, but the same statement under EXPLAIN

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: daniel(dot)horsens(at)gmail(dot)com
Subject: BUG #19939: In a read-only transaction, a plain SELECT ... INTO is refused, but the same statement under EXPLAIN
Date: 2026-11-25 07:39:07
Message-ID: 19939-1bc5bfcb8f10197a@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19939
Logged by: Daniel Pedersen
Email address: daniel(dot)horsens(at)gmail(dot)com
PostgreSQL version: 19.0
Operating system: Debian 13 (trixie), aarch64, postgres Docker
Description:

In a read-only transaction, a plain SELECT ... INTO is refused, but the same
statement under EXPLAIN ANALYZE runs. It creates the table, inserts the
rows, and the table remains after COMMIT.

The documentation for SET TRANSACTION says that in a read-only transaction
"all CREATE, ALTER, and DROP commands" are disallowed, as well as "EXPLAIN
ANALYZE and EXECUTE if the command they would execute is among those listed"
(https://www.postgresql.org/docs/18/sql-set-transaction.html) SELECT ...
INTO is CREATE TABLE AS, so I expected EXPLAIN ANALYZE SELECT ... INTO to be
refused with the same error.

Steps to reproduce (on a fresh cluster):
SQL:

BEGIN READ ONLY;
SELECT 1 AS a INTO plain_into;
-- ERROR: cannot execute SELECT INTO in a read-only transaction
ROLLBACK;

BEGIN READ ONLY;
SHOW transaction_read_only; -- on
EXPLAIN (ANALYZE) SELECT 1 AS a INTO explain_into; -- succeeds
SELECT to_regclass('public.explain_into'); -- explain_into
COMMIT;

SELECT * FROM explain_into; -- one row, a = 1

Expected: EXPLAIN (ANALYZE) SELECT ... INTO fails with "cannot execute
SELECT INTO in a read-only transaction" (or the CREATE TABLE AS equivalent),
as the documentation describes.
Actual: the table is created and populated inside the read-only transaction,
and it persists after commit.

The same happens for SELECT ... INTO TEMP, EXPLAIN (ANALYZE) CREATE TABLE
... AS, and EXPLAIN (ANALYZE) CREATE MATERIALIZED VIEW ... AS. Without
EXPLAIN, each is refused with "cannot execute CREATE TABLE AS / CREATE
MATERIALIZED VIEW in a read-only transaction". Under EXPLAIN ANALYZE, each
creates its object, and the object persists after COMMIT (tested on 19.0).

If the current behaviour is intended, the SET TRANSACTION documentation
should say so, since it currently reads as if this case is covered.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Fujii Masao 2026-11-25 07:47:05 Re: Lost walsender wakeup stalls synchronous commits until the standby's next reply (16+)
Previous Message PG Bug reporting form 2026-11-25 06:40:19 BUG #19938: BRIN index on box silently drops ordinary matching rows when a NaN box shares the same page range