Lock all relations referred to in updatable views
authorStephen Frost
Tue, 8 Sep 2015 21:02:59 +0000 (17:02 -0400)
committerStephen Frost
Tue, 8 Sep 2015 21:02:59 +0000 (17:02 -0400)
Even views considered "simple" enough to be automatically updatable may
have mulitple relations involved (eg: in a where clause).  We need to
make sure and lock those relations when rewriting the query.

Back-patch to 9.3 where updatable views were added.

Pointed out by Andres, patch thanks to Dean Rasheed.

src/backend/rewrite/rewriteHandler.c

index 6a31c62331e377893348c6b032fa520bf1839e9c..d4c32e308fca2e924f1d45164b2af02bcd834616 100644 (file)
@@ -2405,6 +2405,21 @@ rewriteTargetView(Query *parsetree, Relation view)
 
    heap_close(base_rel, NoLock);
 
+   /*
+    * If the view query contains any sublink subqueries then we need to also
+    * acquire locks on any relations they refer to.  We know that there won't
+    * be any subqueries in the range table or CTEs, so we can skip those, as
+    * in AcquireRewriteLocks.
+    */
+   if (viewquery->hasSubLinks)
+   {
+       acquireLocksOnSubLinks_context context;
+
+       context.for_execute = true;
+       query_tree_walker(viewquery, acquireLocksOnSubLinks, &context,
+                         QTW_IGNORE_RC_SUBQUERIES);
+   }
+
    /*
     * Create a new target RTE describing the base relation, and add it to the
     * outer query's rangetable.  (What's happening in the next few steps is