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

Commit 3973034

Browse files
committed
Don't reject ROW_MARK_REFERENCE rowmarks for materialized views.
We should allow this so that matviews can be referenced in UPDATE/DELETE statements in READ COMMITTED isolation level. The requirement for that is that a re-fetch by TID will see the same row version the query saw earlier, which is true of matviews, so there's no reason for the restriction. Per bug #9398. Michael Paquier, after a suggestion by me
1 parent 13ea43a commit 3973034

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/backend/executor/execMain.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,14 +1100,15 @@ CheckValidRowMarkRel(Relation rel, RowMarkType markType)
11001100
RelationGetRelationName(rel))));
11011101
break;
11021102
case RELKIND_MATVIEW:
1103-
/* Should not get here */
1104-
ereport(ERROR,
1105-
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1106-
errmsg("cannot lock rows in materialized view \"%s\"",
1107-
RelationGetRelationName(rel))));
1103+
/* Allow referencing a matview, but not actual locking clauses */
1104+
if (markType != ROW_MARK_REFERENCE)
1105+
ereport(ERROR,
1106+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1107+
errmsg("cannot lock rows in materialized view \"%s\"",
1108+
RelationGetRelationName(rel))));
11081109
break;
11091110
case RELKIND_FOREIGN_TABLE:
1110-
/* Should not get here */
1111+
/* Should not get here; planner should have used ROW_MARK_COPY */
11111112
ereport(ERROR,
11121113
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
11131114
errmsg("cannot lock rows in foreign table \"%s\"",

src/test/regress/expected/matview.out

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,29 @@ SELECT * FROM mv_v;
412412

413413
DROP TABLE v CASCADE;
414414
NOTICE: drop cascades to materialized view mv_v
415+
-- make sure that matview rows can be referenced as source rows (bug #9398)
416+
CREATE TABLE v AS SELECT generate_series(1,10) AS a;
417+
CREATE MATERIALIZED VIEW mv_v AS SELECT a FROM v WHERE a <= 5;
418+
DELETE FROM v WHERE EXISTS ( SELECT * FROM mv_v WHERE mv_v.a = v.a );
419+
SELECT * FROM v;
420+
a
421+
----
422+
6
423+
7
424+
8
425+
9
426+
10
427+
(5 rows)
428+
429+
SELECT * FROM mv_v;
430+
a
431+
---
432+
1
433+
2
434+
3
435+
4
436+
5
437+
(5 rows)
438+
439+
DROP TABLE v CASCADE;
440+
NOTICE: drop cascades to materialized view mv_v

src/test/regress/sql/matview.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,11 @@ REFRESH MATERIALIZED VIEW mv_v;
141141
SELECT * FROM v;
142142
SELECT * FROM mv_v;
143143
DROP TABLE v CASCADE;
144+
145+
-- make sure that matview rows can be referenced as source rows (bug #9398)
146+
CREATE TABLE v AS SELECT generate_series(1,10) AS a;
147+
CREATE MATERIALIZED VIEW mv_v AS SELECT a FROM v WHERE a <= 5;
148+
DELETE FROM v WHERE EXISTS ( SELECT * FROM mv_v WHERE mv_v.a = v.a );
149+
SELECT * FROM v;
150+
SELECT * FROM mv_v;
151+
DROP TABLE v CASCADE;

0 commit comments

Comments
 (0)