From: Kevin Grittner Date: Sun, 3 Nov 2013 00:18:08 +0000 (-0500) Subject: Acquire appropriate locks when rewriting during RMV. X-Git-Tag: REL9_4_BETA1~993 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2a781d57dcd027df32d15ee2378b84d0c4d005d1;p=postgresql Acquire appropriate locks when rewriting during RMV. Since the query has not been freshly parsed when executing REFRESH MATERIALIZED VIEW, locks must be explicitly taken before rewrite. Backpatch to 9.3. Andres Freund --- diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c index fcfc678813..d5a10adc67 100644 --- a/src/backend/commands/matview.c +++ b/src/backend/commands/matview.c @@ -283,6 +283,7 @@ refresh_matview_datafill(DestReceiver *dest, Query *query, Oid save_userid; int save_sec_context; int save_nestlevel; + Query *copied_query; /* * Switch to the owner's userid, so that any functions are run as that @@ -294,8 +295,10 @@ refresh_matview_datafill(DestReceiver *dest, Query *query, save_sec_context | SECURITY_RESTRICTED_OPERATION); save_nestlevel = NewGUCNestLevel(); - /* Rewrite, copying the given Query to make sure it's not changed */ - rewritten = QueryRewrite((Query *) copyObject(query)); + /* Lock and rewrite, using a copy to preserve the original query. */ + copied_query = copyObject(query); + AcquireRewriteLocks(copied_query, false); + rewritten = QueryRewrite(copied_query); /* SELECT should never rewrite to more or less than one SELECT query */ if (list_length(rewritten) != 1)