BUG #17579: 15beta2: strange error when trying to use MERGE statement as a CTE

The Post Office <noreply@postgresql.org>

From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: borz_off@cs.msu.su
Date: 2022-08-07T21:44:25Z
Lists: pgsql-bugs
The following bug has been logged on the website:

Bug reference:      17579
Logged by:          Alexey Borzov
Email address:      borz_off@cs.msu.su
PostgreSQL version: Unsupported/Unknown
Operating system:   Windows
Description:        

create table foo (id int);

with cte_failure as (
    merge into foo as target
    using foo as source
    on target.id = source.id
    when matched then do nothing
)
select 'fail!';

When executing the above code I get the following error:

> ERROR:  DO INSTEAD NOTIFY rules are not supported for data-modifying
statements in WITH

I suspect that MERGE was never intended to work as a CTE, but right now the
grammar allows any PreparableStmt in a common_table_expr and the above error
is triggered a lot later due to implementation details.

Commits

  1. Reject MERGE in CTEs and COPY