Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit be420fa

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 24ace40 commit be420fa

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/backend/executor/execMain.c

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

src/test/regress/expected/matview.out

+6
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ REFRESH MATERIALIZED VIEW mv;
424424
REFRESH MATERIALIZED VIEW CONCURRENTLY mv;
425425
DROP TABLE foo CASCADE;
426426
NOTICE: drop cascades to materialized view mv
427+
-- allow subquery to reference unpopulated matview if WITH NO DATA is specified
428+
CREATE MATERIALIZED VIEW mv1 AS SELECT 1 AS col1 WITH NO DATA;
429+
CREATE MATERIALIZED VIEW mv2 AS SELECT * FROM mv1
430+
WHERE col1 = (SELECT LEAST(col1) FROM mv1) WITH NO DATA;
431+
DROP MATERIALIZED VIEW mv1 CASCADE;
432+
NOTICE: drop cascades to materialized view mv2
427433
-- make sure that types with unusual equality tests work
428434
CREATE TABLE boxes (id serial primary key, b box);
429435
INSERT INTO boxes (b) VALUES

src/test/regress/sql/matview.sql

+6
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ REFRESH MATERIALIZED VIEW mv;
155155
REFRESH MATERIALIZED VIEW CONCURRENTLY mv;
156156
DROP TABLE foo CASCADE;
157157

158+
-- allow subquery to reference unpopulated matview if WITH NO DATA is specified
159+
CREATE MATERIALIZED VIEW mv1 AS SELECT 1 AS col1 WITH NO DATA;
160+
CREATE MATERIALIZED VIEW mv2 AS SELECT * FROM mv1
161+
WHERE col1 = (SELECT LEAST(col1) FROM mv1) WITH NO DATA;
162+
DROP MATERIALIZED VIEW mv1 CASCADE;
163+
158164
-- make sure that types with unusual equality tests work
159165
CREATE TABLE boxes (id serial primary key, b box);
160166
INSERT INTO boxes (b) VALUES

0 commit comments

Comments
 (0)