aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/backend/rewrite/rewriteHandler.c35
-rw-r--r--src/test/regress/expected/updatable_views.out3
-rw-r--r--src/test/regress/sql/updatable_views.sql1
3 files changed, 25 insertions, 14 deletions
diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c
index 2510ce79c6e..6cef936f824 100644
--- a/src/backend/rewrite/rewriteHandler.c
+++ b/src/backend/rewrite/rewriteHandler.c
@@ -2963,7 +2963,7 @@ relation_is_updatable(Oid reloid,
*
* This is used with simply-updatable views to map column-permissions sets for
* the view columns onto the matching columns in the underlying base relation.
- * The targetlist is expected to be a list of plain Vars of the underlying
+ * Relevant entries in the targetlist must be plain Vars of the underlying
* relation (as per the checks above in view_query_is_auto_updatable).
*/
static Bitmapset *
@@ -3063,6 +3063,10 @@ rewriteTargetView(Query *parsetree, Relation view)
*/
viewquery = copyObject(get_view_query(view));
+ /* Locate RTE and perminfo describing the view in the outer query */
+ view_rte = rt_fetch(parsetree->resultRelation, parsetree->rtable);
+ view_perminfo = getRTEPermissionInfo(parsetree->rteperminfos, view_rte);
+
/* The view must be updatable, else fail */
auto_update_detail =
view_query_is_auto_updatable(viewquery,
@@ -3105,17 +3109,26 @@ rewriteTargetView(Query *parsetree, Relation view)
}
/*
- * For INSERT/UPDATE the modified columns must all be updatable. Note that
- * we get the modified columns from the query's targetlist, not from the
- * result RTE's insertedCols and/or updatedCols set, since
- * rewriteTargetListIU may have added additional targetlist entries for
- * view defaults, and these must also be updatable.
+ * For INSERT/UPDATE the modified columns must all be updatable.
*/
if (parsetree->commandType != CMD_DELETE)
{
- Bitmapset *modified_cols = NULL;
+ Bitmapset *modified_cols;
char *non_updatable_col;
+ /*
+ * Compute the set of modified columns as those listed in the result
+ * RTE's insertedCols and/or updatedCols sets plus those that are
+ * targets of the query's targetlist(s). We must consider the query's
+ * targetlist because rewriteTargetListIU may have added additional
+ * targetlist entries for view defaults, and these must also be
+ * updatable. But rewriteTargetListIU can also remove entries if they
+ * are DEFAULT markers and the column's default is NULL, so
+ * considering only the targetlist would also be wrong.
+ */
+ modified_cols = bms_union(view_perminfo->insertedCols,
+ view_perminfo->updatedCols);
+
foreach(lc, parsetree->targetList)
{
TargetEntry *tle = (TargetEntry *) lfirst(lc);
@@ -3173,9 +3186,6 @@ rewriteTargetView(Query *parsetree, Relation view)
}
}
- /* Locate RTE describing the view in the outer query */
- view_rte = rt_fetch(parsetree->resultRelation, parsetree->rtable);
-
/*
* If we get here, view_query_is_auto_updatable() has verified that the
* view contains a single base relation.
@@ -3270,10 +3280,7 @@ rewriteTargetView(Query *parsetree, Relation view)
* Note: the original view's RTEPermissionInfo remains in the query's
* rteperminfos so that the executor still performs appropriate
* permissions checks for the query caller's use of the view.
- */
- view_perminfo = getRTEPermissionInfo(parsetree->rteperminfos, view_rte);
-
- /*
+ *
* Disregard the perminfo in viewquery->rteperminfos that the base_rte
* would currently be pointing at, because we'd like it to point now to a
* new one that will be filled below. Must set perminfoindex to 0 to not
diff --git a/src/test/regress/expected/updatable_views.out b/src/test/regress/expected/updatable_views.out
index 1950e6f281f..ba46c32029c 100644
--- a/src/test/regress/expected/updatable_views.out
+++ b/src/test/regress/expected/updatable_views.out
@@ -1598,6 +1598,9 @@ DETAIL: View columns that refer to system columns are not updatable.
INSERT INTO rw_view1 (s, c, a) VALUES (null, null, 1.1); -- should fail
ERROR: cannot insert into column "s" of view "rw_view1"
DETAIL: View columns that are not columns of their base relation are not updatable.
+INSERT INTO rw_view1 (s, c, a) VALUES (default, default, 1.1); -- should fail
+ERROR: cannot insert into column "s" of view "rw_view1"
+DETAIL: View columns that are not columns of their base relation are not updatable.
INSERT INTO rw_view1 (a) VALUES (1.1) RETURNING a, s, c; -- OK
a | s | c
-----+-------------------+-------------------
diff --git a/src/test/regress/sql/updatable_views.sql b/src/test/regress/sql/updatable_views.sql
index eaee0b7e1d7..0a3176e25d1 100644
--- a/src/test/regress/sql/updatable_views.sql
+++ b/src/test/regress/sql/updatable_views.sql
@@ -845,6 +845,7 @@ CREATE VIEW rw_view1 AS
INSERT INTO rw_view1 VALUES (null, null, 1.1, null); -- should fail
INSERT INTO rw_view1 (s, c, a) VALUES (null, null, 1.1); -- should fail
+INSERT INTO rw_view1 (s, c, a) VALUES (default, default, 1.1); -- should fail
INSERT INTO rw_view1 (a) VALUES (1.1) RETURNING a, s, c; -- OK
UPDATE rw_view1 SET s = s WHERE a = 1.1; -- should fail
UPDATE rw_view1 SET a = 1.05 WHERE a = 1.1 RETURNING s; -- OK