Don't reject ROW_MARK_REFERENCE rowmarks for materialized views.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 6 Mar 2014 16:37:02 +0000 (11:37 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 6 Mar 2014 16:37:02 +0000 (11:37 -0500)
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

src/backend/executor/execMain.c
src/test/regress/expected/matview.out
src/test/regress/sql/matview.sql

index 6b5f198c054ff85a30be482724ef1517709f3580..9e3d879ae778837d41754063663617346ffac24c 100644 (file)
@@ -1102,14 +1102,15 @@ CheckValidRowMarkRel(Relation rel, RowMarkType markType)
                            RelationGetRelationName(rel))));
            break;
        case RELKIND_MATVIEW:
-           /* Should not get here */
-           ereport(ERROR,
-                   (errcode(ERRCODE_WRONG_OBJECT_TYPE),
-                    errmsg("cannot lock rows in materialized view \"%s\"",
-                           RelationGetRelationName(rel))));
+           /* Allow referencing a matview, but not actual locking clauses */
+           if (markType != ROW_MARK_REFERENCE)
+               ereport(ERROR,
+                       (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+                      errmsg("cannot lock rows in materialized view \"%s\"",
+                             RelationGetRelationName(rel))));
            break;
        case RELKIND_FOREIGN_TABLE:
-           /* Should not get here */
+           /* Should not get here; planner should have used ROW_MARK_COPY */
            ereport(ERROR,
                    (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                     errmsg("cannot lock rows in foreign table \"%s\"",
index 31751ebd9edff4b8a45bc64fd61be579f6c2f5e0..daf3b9e91d5a45463c91fcb9a167350f8a8b4737 100644 (file)
@@ -476,3 +476,29 @@ SELECT * FROM mv_v;
 
 DROP TABLE v CASCADE;
 NOTICE:  drop cascades to materialized view mv_v
+-- make sure that matview rows can be referenced as source rows (bug #9398)
+CREATE TABLE v AS SELECT generate_series(1,10) AS a;
+CREATE MATERIALIZED VIEW mv_v AS SELECT a FROM v WHERE a <= 5;
+DELETE FROM v WHERE EXISTS ( SELECT * FROM mv_v WHERE mv_v.a = v.a );
+SELECT * FROM v;
+ a  
+----
+  6
+  7
+  8
+  9
+ 10
+(5 rows)
+
+SELECT * FROM mv_v;
+ a 
+---
+ 1
+ 2
+ 3
+ 4
+ 5
+(5 rows)
+
+DROP TABLE v CASCADE;
+NOTICE:  drop cascades to materialized view mv_v
index 83fd6870d27ebbd5b12a72ab2147a64b874c5fa5..3a6a3276f844553b3a8f5ba420e54ec020c9090c 100644 (file)
@@ -186,3 +186,11 @@ REFRESH MATERIALIZED VIEW CONCURRENTLY mv_v;
 SELECT * FROM v;
 SELECT * FROM mv_v;
 DROP TABLE v CASCADE;
+
+-- make sure that matview rows can be referenced as source rows (bug #9398)
+CREATE TABLE v AS SELECT generate_series(1,10) AS a;
+CREATE MATERIALIZED VIEW mv_v AS SELECT a FROM v WHERE a <= 5;
+DELETE FROM v WHERE EXISTS ( SELECT * FROM mv_v WHERE mv_v.a = v.a );
+SELECT * FROM v;
+SELECT * FROM mv_v;
+DROP TABLE v CASCADE;