Skip to content

Commit 39c283e

Browse files
committed
Fix _SPI_execute_plan() for CREATE TABLE IF NOT EXISTS foo AS ...
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
1 parent b0e40d1 commit 39c283e

File tree

1 file changed

+12
-4
lines changed
  • src/backend/executor

1 file changed

+12
-4
lines changed

src/backend/executor/spi.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,15 +2217,23 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
22172217
*/
22182218
if (IsA(stmt, CreateTableAsStmt))
22192219
{
2220-
Assert(strncmp(completionTag, "SELECT ", 7) == 0);
2221-
_SPI_current->processed = pg_strtouint64(completionTag + 7,
2222-
NULL, 10);
2220+
CreateTableAsStmt *ctastmt = (CreateTableAsStmt *) stmt;
2221+
2222+
if (strncmp(completionTag, "SELECT ", 7) == 0)
2223+
_SPI_current->processed =
2224+
pg_strtouint64(completionTag + 7, NULL, 10);
2225+
else
2226+
{
2227+
/* Must be an IF NOT EXISTS that did nothing */
2228+
Assert(ctastmt->if_not_exists);
2229+
_SPI_current->processed = 0;
2230+
}
22232231

22242232
/*
22252233
* For historical reasons, if CREATE TABLE AS was spelled
22262234
* as SELECT INTO, return a special return code.
22272235
*/
2228-
if (((CreateTableAsStmt *) stmt)->is_select_into)
2236+
if (ctastmt->is_select_into)
22292237
res = SPI_OK_SELINTO;
22302238
}
22312239
else if (IsA(stmt, CopyStmt))

0 commit comments

Comments
 (0)