Reject MERGE in CTEs and COPY
authorAlvaro Herrera <[email protected]>
Fri, 12 Aug 2022 10:05:50 +0000 (12:05 +0200)
committerAlvaro Herrera <[email protected]>
Fri, 12 Aug 2022 10:05:50 +0000 (12:05 +0200)
The grammar added for MERGE inadvertently made it accepted syntax in
places that were not prepared to deal with it -- namely COPY and inside
CTEs, but invoking these things with MERGE currently causes assertion
failures or weird misbehavior in non-assertion builds.  Protect those
places by checking for it explicitly until somebody decides to implement
it.

Reported-by: Alexey Borzov <[email protected]>
Discussion: https://postgr.es/m/17579-82482cd7b267b862@postgresql.org

src/backend/commands/copy.c
src/backend/parser/parse_cte.c
src/test/regress/expected/merge.out
src/test/regress/sql/merge.sql

index 3ac731803bd41533ad05b9434d2ee14339bbc787..49924e476afb17d7a5e3361a198f3a4915219fcb 100644 (file)
@@ -273,6 +273,12 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
        {
                Assert(stmt->query);
 
+               /* MERGE is allowed by parser, but unimplemented. Reject for now */
+               if (IsA(stmt->query, MergeStmt))
+                       ereport(ERROR,
+                                       errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                       errmsg("MERGE not supported in COPY"));
+
                query = makeNode(RawStmt);
                query->stmt = stmt->query;
                query->stmt_location = stmt_location;
index efb4af706e16215cc57b2afbeff15a450c387257..8fc86586085ef28b3c13c1e5650d09eba814e2e7 100644 (file)
@@ -126,6 +126,13 @@ transformWithClause(ParseState *pstate, WithClause *withClause)
                CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
                ListCell   *rest;
 
+               /* MERGE is allowed by parser, but unimplemented. Reject for now */
+               if (IsA(cte->ctequery, MergeStmt))
+                       ereport(ERROR,
+                                       errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                       errmsg("MERGE not supported in WITH query"),
+                                       parser_errposition(pstate, cte->location));
+
                for_each_cell(rest, withClause->ctes, lnext(withClause->ctes, lc))
                {
                        CommonTableExpr *cte2 = (CommonTableExpr *) lfirst(rest);
index af670e28e7f730478a18f20c93c9dca5c9a25cc3..729ae2eb06543dede0571de3742c315938a664b9 100644 (file)
@@ -123,6 +123,20 @@ ON tid = tid
 WHEN MATCHED THEN DO NOTHING;
 ERROR:  name "target" specified more than once
 DETAIL:  The name is used both as MERGE target table and data source.
+-- used in a CTE
+WITH foo AS (
+  MERGE INTO target USING source ON (true)
+  WHEN MATCHED THEN DELETE
+) SELECT * FROM foo;
+ERROR:  MERGE not supported in WITH query
+LINE 1: WITH foo AS (
+             ^
+-- used in COPY
+COPY (
+  MERGE INTO target USING source ON (true)
+  WHEN MATCHED THEN DELETE
+) TO stdout;
+ERROR:  MERGE not supported in COPY
 -- unsupported relation types
 -- view
 CREATE VIEW tv AS SELECT * FROM target;
index afeb212f3c826f9d67eaf531e7b29ff43aabd2a2..e0c450736bd1475108c31f906fc7c62c098b16b8 100644 (file)
@@ -88,6 +88,16 @@ MERGE INTO target
 USING target
 ON tid = tid
 WHEN MATCHED THEN DO NOTHING;
+-- used in a CTE
+WITH foo AS (
+  MERGE INTO target USING source ON (true)
+  WHEN MATCHED THEN DELETE
+) SELECT * FROM foo;
+-- used in COPY
+COPY (
+  MERGE INTO target USING source ON (true)
+  WHEN MATCHED THEN DELETE
+) TO stdout;
 
 -- unsupported relation types
 -- view