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

From: Roberto Mello <roberto(dot)mello(at)gmail(dot)com>
To: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: EXPLAIN ANALYZE CREATE TABLE AS EXECUTE runs non-SELECT prepared statements
Date: 2026-11-20 04:02:34
Message-ID: CAKz==bLAxd-eKQcOtR7nPLnz-UdMPMRnmPhD3krFR518+-pWuA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Nov 19, 2026 at 5:52 PM Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
wrote:

> 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.
>

Confirmed on current master. EXPLAIN ANALYZE CREATE TABLE AS EXECUTE ins
inserts into src and creates t2 from the RETURNING row, while CREATE TABLE
AS EXECUTE ins is correctly rejected. Plain EXPLAIN shows the Insert plan.

v1 adds the same check ExecuteQuery() already uses... if there's an INTO
clause the prepared plan list must be a single CMD_SELECT.

I applied it and retried the same cases; they now all fail with "prepared
statement is not a SELECT", and the INSERT is not executed. A prepared
SELECT still works with CREATE TABLE AS EXECUTE, with or without EXPLAIN
ANALYZE. The new select_into
tests pass, as do the rest of the regular regression tests.

This is just closing the EXPLAIN hole, which seems right. The comment in
ExecuteQuery() still leaves INSERT ... RETURNING as a separate question.

LGTM.

Roberto

>
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bertrand Drouvot 2026-11-20 04:32:25 Re: Add a hook for handling logical decoding messages on subscribers.
Previous Message shveta malik 2026-11-20 03:39:38 Re: Proposal: Conflict log history table for Logical Replication