Skip to content

Commit 942ff00

Browse files
committed
Fix busted Assert for CREATE MATVIEW ... WITH NO DATA.
Commit 874fe3a changed the command tag returned for CREATE MATVIEW/CREATE TABLE AS ... WITH NO DATA, but missed that there was code in spi.c that expected the command tag to always be "SELECT". Fortunately, the consequence was only an Assert failure, so this oversight should have no impact in production builds. Since this code path was evidently un-exercised, add a regression test. Per report from Shivam Saxena. Back-patch to 9.3, like the previous commit. Michael Paquier Report: <97218716-480B-4527-B5CD-D08D798A0C7B@dresources.com>
1 parent c4c91df commit 942ff00

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

src/backend/executor/spi.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,15 +2180,23 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
21802180
*/
21812181
if (IsA(stmt, CreateTableAsStmt))
21822182
{
2183-
Assert(strncmp(completionTag, "SELECT ", 7) == 0);
2184-
_SPI_current->processed = strtoul(completionTag + 7,
2185-
NULL, 10);
2183+
CreateTableAsStmt *ctastmt = (CreateTableAsStmt *) stmt;
2184+
2185+
if (strncmp(completionTag, "SELECT ", 7) == 0)
2186+
_SPI_current->processed =
2187+
strtoul(completionTag + 7, NULL, 10);
2188+
else
2189+
{
2190+
/* Must be a CREATE ... WITH NO DATA */
2191+
Assert(ctastmt->into->skipData);
2192+
_SPI_current->processed = 0;
2193+
}
21862194

21872195
/*
21882196
* For historical reasons, if CREATE TABLE AS was spelled
21892197
* as SELECT INTO, return a special return code.
21902198
*/
2191-
if (((CreateTableAsStmt *) stmt)->is_select_into)
2199+
if (ctastmt->is_select_into)
21922200
res = SPI_OK_SELINTO;
21932201
}
21942202
else if (IsA(stmt, CopyStmt))

src/test/regress/expected/matview.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,28 @@ SELECT * FROM mv_v;
477477

478478
DROP TABLE v CASCADE;
479479
NOTICE: drop cascades to materialized view mv_v
480+
-- make sure that create WITH NO DATA works via SPI
481+
BEGIN;
482+
CREATE FUNCTION mvtest_func()
483+
RETURNS void AS $$
484+
BEGIN
485+
CREATE MATERIALIZED VIEW mvtest1 AS SELECT 1 AS x;
486+
CREATE MATERIALIZED VIEW mvtest2 AS SELECT 1 AS x WITH NO DATA;
487+
END;
488+
$$ LANGUAGE plpgsql;
489+
SELECT mvtest_func();
490+
mvtest_func
491+
-------------
492+
493+
(1 row)
494+
495+
SELECT * FROM mvtest1;
496+
x
497+
---
498+
1
499+
(1 row)
500+
501+
SELECT * FROM mvtest2;
502+
ERROR: materialized view "mvtest2" has not been populated
503+
HINT: Use the REFRESH MATERIALIZED VIEW command.
504+
ROLLBACK;

src/test/regress/sql/matview.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,17 @@ DELETE FROM v WHERE EXISTS ( SELECT * FROM mv_v WHERE mv_v.a = v.a );
166166
SELECT * FROM v;
167167
SELECT * FROM mv_v;
168168
DROP TABLE v CASCADE;
169+
170+
-- make sure that create WITH NO DATA works via SPI
171+
BEGIN;
172+
CREATE FUNCTION mvtest_func()
173+
RETURNS void AS $$
174+
BEGIN
175+
CREATE MATERIALIZED VIEW mvtest1 AS SELECT 1 AS x;
176+
CREATE MATERIALIZED VIEW mvtest2 AS SELECT 1 AS x WITH NO DATA;
177+
END;
178+
$$ LANGUAGE plpgsql;
179+
SELECT mvtest_func();
180+
SELECT * FROM mvtest1;
181+
SELECT * FROM mvtest2;
182+
ROLLBACK;

0 commit comments

Comments
 (0)