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

Started by PG Bug reporting form4 days ago3 messagesbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19939
Logged by: Daniel Pedersen
Email address: daniel.horsens@gmail.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.

#2Lukas Fittl
lukas@fittl.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19939: In a read-only transaction, a plain SELECT ... INTO is refused, but the same statement under EXPLAIN

Hi,

On Wed, Nov 25, 2026 at 9:55 AM PG Bug reporting form
<noreply@postgresql.org> wrote:

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.

Thanks for the report!

I believe this is already being discussed in this mailing list thread
on pgsql-hackers:

/messages/by-id/CAN4CZFNz40w6BDC7nXzmoXVokwCJ+36spPOSZjEkq8=3OGWqsw@mail.gmail.com

Thanks,
Lukas

--
Lukas Fittl

#3Daniel Pedersen
daniel.horsens@gmail.com
In reply to: Lukas Fittl (#2)
Re: BUG #19939: In a read-only transaction, a plain SELECT ... INTO is refused, but the same statement under EXPLAIN

Hi Lukas,

Thanks for sharing the thread.

Yes, I've checked the thread, and it looks like it covers the same
underlying issue as my BUG #19939.

In particular, Chao Li explicitly mentions in his November 20 reply that he
discovered that EXPLAIN ANALYZE also behaves incorrectly in read-only
transactions, creating tables when it should not.

I'll follow the thread there.

Thanks again!

Best regards,
Daniel

On Wed, 25 Nov 2026 at 20:44, Lukas Fittl <lukas@fittl.com> wrote:

Show quoted text

Hi,

On Wed, Nov 25, 2026 at 9:55 AM PG Bug reporting form
<noreply@postgresql.org> wrote:

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.

Thanks for the report!

I believe this is already being discussed in this mailing list thread
on pgsql-hackers:

/messages/by-id/CAN4CZFNz40w6BDC7nXzmoXVokwCJ+36spPOSZjEkq8=3OGWqsw@mail.gmail.com

Thanks,
Lukas

--
Lukas Fittl