From: Pavan Deolasee Date: Tue, 9 May 2017 07:11:56 +0000 (+0530) Subject: Allow COPY (INSERT RETURNING), but block COPY (SELECT INTO) X-Git-Tag: XL_10_R1BETA1~297 X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=55dd95c665d443883e9351af3b67b37bb1b02fe5;p=postgres-xl.git Allow COPY (INSERT RETURNING), but block COPY (SELECT INTO) Since SELECT INTO is transformed into a CREATE TABLE AS SELECT, which then further transformed into CREATE TABLE + INSERT INTO by XL, we must do the check for SELECT INTO a bit differently in XL. This patch does that and as a result also now allow COPY (INSERT RETURNING) correctly. Backpatched to XL9_5_STABLE --- diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index bf431d77e0..461e94ed0b 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -1455,6 +1455,34 @@ BeginCopy(bool is_from, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("COPY (query) WITH OIDS is not supported"))); + /* + * The grammar allows SELECT INTO, but we don't support that. We do + * this check before transforming the query since QueryRewriteCTAS will + * transform the statemet into a CREATE TABLE followed by INSERT INTO. + * The CREATE TABLE is processed separately by QueryRewriteCTAS and + * what we get back is just the INSERT statement. Not doing a check + * will ultimately lead to an error, but doing it here allows us to + * throw a more friendly and PG-compatible error. + */ + if (IsA(raw_query, SelectStmt)) + { + SelectStmt *stmt = (SelectStmt *) raw_query; + + /* + * If it's a set-operation tree, drilldown to leftmost SelectStmt + */ + while (stmt && stmt->op != SETOP_NONE) + stmt = stmt->larg; + Assert(stmt && IsA(stmt, SelectStmt) && stmt->larg == NULL); + + if (stmt->intoClause) + { + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("COPY (SELECT INTO) is not supported"))); + } + } + /* * Run parse analysis and rewrite. Note this also acquires sufficient * locks on the source table(s). @@ -1501,23 +1529,6 @@ BeginCopy(bool is_from, query = (Query *) linitial(rewritten); -#ifdef PGXC - /* - * The grammar allows SELECT INTO, but we don't support that. - * Postgres-XC uses an INSERT SELECT command in this case - */ - if ((query->utilityStmt != NULL && - IsA(query->utilityStmt, CreateTableAsStmt)) || - query->commandType == CMD_INSERT) -#else - /* The grammar allows SELECT INTO, but we don't support that */ - if (query->utilityStmt != NULL && - IsA(query->utilityStmt, CreateTableAsStmt)) -#endif - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("COPY (SELECT INTO) is not supported"))); - Assert(query->utilityStmt == NULL); /*