Skip to content

Commit b2cd72c

Browse files
committed
Fix subquery reference to non-populated MV in CMV.
A subquery reference to a matview should be allowed by CREATE MATERIALIZED VIEW WITH NO DATA, just like a direct reference is. Per bug report from Laurent Sartran. Backpatch to 9.3.
1 parent 86dab9c commit b2cd72c

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/backend/executor/execMain.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,8 @@ InitPlan(QueryDesc *queryDesc, int eflags)
864864
* it is a parameterless subplan (not initplan), we suggest that it be
865865
* prepared to handle REWIND efficiently; otherwise there is no need.
866866
*/
867-
sp_eflags = eflags & EXEC_FLAG_EXPLAIN_ONLY;
867+
sp_eflags = eflags
868+
& (EXEC_FLAG_EXPLAIN_ONLY | EXEC_FLAG_WITH_NO_DATA);
868869
if (bms_is_member(i, plannedstmt->rewindPlanIDs))
869870
sp_eflags |= EXEC_FLAG_REWIND;
870871

src/test/regress/expected/matview.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,9 @@ SELECT * FROM hogeview WHERE i < 10;
385385

386386
DROP TABLE hoge CASCADE;
387387
NOTICE: drop cascades to materialized view hogeview
388+
-- allow subquery to reference unpopulated matview if WITH NO DATA is specified
389+
CREATE MATERIALIZED VIEW mv1 AS SELECT 1 AS col1 WITH NO DATA;
390+
CREATE MATERIALIZED VIEW mv2 AS SELECT * FROM mv1
391+
WHERE col1 = (SELECT LEAST(col1) FROM mv1) WITH NO DATA;
392+
DROP MATERIALIZED VIEW mv1 CASCADE;
393+
NOTICE: drop cascades to materialized view mv2

src/test/regress/sql/matview.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,9 @@ SELECT * FROM hogeview WHERE i < 10;
124124
VACUUM ANALYZE;
125125
SELECT * FROM hogeview WHERE i < 10;
126126
DROP TABLE hoge CASCADE;
127+
128+
-- allow subquery to reference unpopulated matview if WITH NO DATA is specified
129+
CREATE MATERIALIZED VIEW mv1 AS SELECT 1 AS col1 WITH NO DATA;
130+
CREATE MATERIALIZED VIEW mv2 AS SELECT * FROM mv1
131+
WHERE col1 = (SELECT LEAST(col1) FROM mv1) WITH NO DATA;
132+
DROP MATERIALIZED VIEW mv1 CASCADE;

0 commit comments

Comments
 (0)