postgres.git / summary / log / commit / refs

commit    894993ffee387383b371ebc515b5e8ea156f9574
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Date:     Thu Aug 11 15:22:25 2016 +0000
First released in: 9.6.0, 9.3.15, 9.4.10, 9.5.5

    Fix busted Assert for CREATE MATVIEW ... WITH NO DATA.
    
    Commit 874fe3aea 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>


src/backend/executor/spi.c | 8 ++++++-- src/test/regress/expected/matview.out | 25 +++++++++++++++++++++++++ src/test/regress/sql/matview.sql | 14 ++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c index dc039f41748..0eba31d0d0e 100644 --- a/src/backend/executor/spi.c +++ b/src/backend/executor/spi.c @@ -2224,8 +2224,12 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, strtoul(completionTag + 7, NULL, 10); else { - /* Must be an IF NOT EXISTS that did nothing */ - Assert(ctastmt->if_not_exists); + /* + * Must be an IF NOT EXISTS that did nothing, or a + * CREATE ... WITH NO DATA. + */ + Assert(ctastmt->if_not_exists || + ctastmt->into->skipData); _SPI_current->processed = 0; } diff --git a/src/test/regress/expected/matview.out b/src/test/regress/expected/matview.out index 4ab23700299..ec11c852ad1 100644 --- a/src/test/regress/expected/matview.out +++ b/src/test/regress/expected/matview.out @@ -557,3 +557,28 @@ REFRESH MATERIALIZED VIEW mv_foo; REFRESH MATERIALIZED VIEW CONCURRENTLY mv_foo; DROP OWNED BY user_dw CASCADE; DROP ROLE user_dw; +-- make sure that create WITH NO DATA works via SPI +BEGIN; +CREATE FUNCTION mvtest_func() + RETURNS void AS $$ +BEGIN + CREATE MATERIALIZED VIEW mvtest1 AS SELECT 1 AS x; + CREATE MATERIALIZED VIEW mvtest2 AS SELECT 1 AS x WITH NO DATA; +END; +$$ LANGUAGE plpgsql; +SELECT mvtest_func(); + mvtest_func +------------- + +(1 row) + +SELECT * FROM mvtest1; + x +--- + 1 +(1 row) + +SELECT * FROM mvtest2; +ERROR: materialized view "mvtest2" has not been populated +HINT: Use the REFRESH MATERIALIZED VIEW command. +ROLLBACK; diff --git a/src/test/regress/sql/matview.sql b/src/test/regress/sql/matview.sql index a37bf5ac5b3..8e623cc1ebc 100644 --- a/src/test/regress/sql/matview.sql +++ b/src/test/regress/sql/matview.sql @@ -226,3 +226,17 @@ REFRESH MATERIALIZED VIEW mv_foo; REFRESH MATERIALIZED VIEW CONCURRENTLY mv_foo; DROP OWNED BY user_dw CASCADE; DROP ROLE user_dw; + +-- make sure that create WITH NO DATA works via SPI +BEGIN; +CREATE FUNCTION mvtest_func() + RETURNS void AS $$ +BEGIN + CREATE MATERIALIZED VIEW mvtest1 AS SELECT 1 AS x; + CREATE MATERIALIZED VIEW mvtest2 AS SELECT 1 AS x WITH NO DATA; +END; +$$ LANGUAGE plpgsql; +SELECT mvtest_func(); +SELECT * FROM mvtest1; +SELECT * FROM mvtest2; +ROLLBACK; [parent: a470858078e1]