diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c
new file mode 100644
index c52a374..e6a9e7b
*** a/src/backend/rewrite/rewriteHandler.c
--- b/src/backend/rewrite/rewriteHandler.c
*************** rewriteTargetView(Query *parsetree, Rela
*** 2589,2594 ****
--- 2589,2604 ----
  	heap_close(base_rel, NoLock);
  
  	/*
+ 	 * If the view query contains any sublink subqueries, we should 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)
+ 		query_tree_walker(viewquery, acquireLocksOnSubLinks, NULL,
+ 						  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
  	 * very much like what the planner would do to "pull up" the view into the
