]> granicus.if.org Git - postgresql/commitdiff
Fix _SPI_execute_plan() for CREATE TABLE IF NOT EXISTS foo AS ...
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 12 Apr 2016 00:07:17 +0000 (20:07 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 12 Apr 2016 00:07:17 +0000 (20:07 -0400)
When IF NOT EXISTS was added to CREATE TABLE AS, this logic didn't get
the memo, possibly resulting in an Assert failure.  It looks like there
would have been no ill effects in a non-Assert build, though.  Back-patch
to 9.5 where the IF NOT EXISTS option was added.

Stas Kelvich

src/backend/executor/spi.c

index fd9417904fd23a9bcebf229a549e86ae580d839d..23cb6f407dddfe4955f2d4b6b0cc052852da057f 100644 (file)
@@ -2217,15 +2217,23 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
                                 */
                                if (IsA(stmt, CreateTableAsStmt))
                                {
-                                       Assert(strncmp(completionTag, "SELECT ", 7) == 0);
-                                       _SPI_current->processed = pg_strtouint64(completionTag + 7,
-                                                                                                                        NULL, 10);
+                                       CreateTableAsStmt *ctastmt = (CreateTableAsStmt *) stmt;
+
+                                       if (strncmp(completionTag, "SELECT ", 7) == 0)
+                                               _SPI_current->processed =
+                                                       pg_strtouint64(completionTag + 7, NULL, 10);
+                                       else
+                                       {
+                                               /* Must be an IF NOT EXISTS that did nothing */
+                                               Assert(ctastmt->if_not_exists);
+                                               _SPI_current->processed = 0;
+                                       }
 
                                        /*
                                         * For historical reasons, if CREATE TABLE AS was spelled
                                         * as SELECT INTO, return a special return code.
                                         */
-                                       if (((CreateTableAsStmt *) stmt)->is_select_into)
+                                       if (ctastmt->is_select_into)
                                                res = SPI_OK_SELINTO;
                                }
                                else if (IsA(stmt, CopyStmt))