EXPLAIN ANALYZE CREATE TABLE AS EXECUTE runs non-SELECT prepared statements

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: EXPLAIN ANALYZE CREATE TABLE AS EXECUTE runs non-SELECT prepared statements
Date: 2026-11-19 09:51:55
Message-ID: CAN4CZFNz40w6BDC7nXzmoXVokwCJ+36spPOSZjEkq8=3OGWqsw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello

CREATE TABLE AS EXECUTE only accepts a prepared SELECT, rejecting
anything else with "prepared statement is not a SELECT". EXPLAIN
ANALYZE doesn't have the same check, so it runs whatever the prepared
statement is.

This is another issue I found while working on EXPLAIN ANALYZE CTAS
event triggers [1].

Simple repro:

CREATE TABLE src (a int);
PREPARE ins AS INSERT INTO src VALUES (1) RETURNING *;
CREATE TABLE t1 AS EXECUTE ins; -- ERROR: prepared statement is not a SELECT
EXPLAIN ANALYZE CREATE TABLE t2 AS EXECUTE ins; -- inserts, creates t2
with the RETURNING rows

It seems clearly wrong, and the plain EXPLAIN variant also happily
shows the plan of something CREATE TABLE AS would reject.

The attached patch adds the same check to ExplainExecuteQuery. It goes
back to 9dbf2b7d75d, and the fix is small enough to backpatch.

[1]: https://postgr.es/m/CAN4CZFOycbakF5swg0i_hH2dZTkMP2%2BoA%3DnoU0VFFFe3687ojw%40mail.gmail.com

Attachment Content-Type Size
v1-0001-Reject-non-SELECT-prepared-statements-in-EXPLAIN-.patch application/octet-stream 4.7 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message vignesh C 2026-11-19 10:06:34 Re: Proposal: Conflict log history table for Logical Replication
Previous Message Zsolt Parragi 2026-11-19 09:44:00 Re: Inconsistency with EXPLAIN ANALYZE CREATE MATERIALIZED VIEW