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
List *rewritten;
PlannedStmt *plan;
QueryDesc *queryDesc;
+ Query *copied_query;
- /* 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)