From 0b7733c62a4bc80aab9dd36bd593982da1586429 Mon Sep 17 00:00:00 2001 From: Greg Nancarrow Date: Fri, 6 Aug 2021 13:39:45 +1000 Subject: [PATCH] Workaround for query rewriter bug which results in modifyingCTE flag not being set. If a query uses a modifying CTE, the hasModifyingCTE flag should be set in the query tree, and the query will be regarded as parallel-unsafe. However, in some cases, a re-written query with a modifying CTE does not have that flag set, due to a bug in the query rewriter. The workaround is to update the max_parallel_hazard_walker() to detect a modifying CTE in the query and indicate in this case that the query is parallel-unsafe. Discussion: https://postgr.es/m/CAJcOf-fAdj=nDKMsRhQzndm-O13NY4dL6xGcEvdX5Xvbbi0V7g@mail.gmail.com --- src/backend/optimizer/util/clauses.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 7187f17da5..7eb305ffda 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -758,6 +758,30 @@ max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context) return true; } + /* + * ModifyingCTE expressions are treated as parallel-unsafe. + * + * XXX Normally, if the Query has a modifying CTE, the hasModifyingCTE + * flag is set in the Query tree, and the query will be regarded as + * parallel-usafe. However, in some cases, a re-written query with a + * modifying CTE does not have that flag set, due to a bug in the query + * rewriter. The following else-if is a workaround for this bug, to detect + * a modifying CTE in the query and regard it as parallel-unsafe. This + * comment, and the else-if block immediately below, may be removed once + * the bug in the query rewriter is fixed. + */ + else if (IsA(node, CommonTableExpr)) + { + CommonTableExpr *cte = (CommonTableExpr *) node; + Query *ctequery = castNode(Query, cte->ctequery); + + if (ctequery->commandType != CMD_SELECT) + { + context->max_hazard = PROPARALLEL_UNSAFE; + return true; + } + } + /* * As a notational convenience for callers, look through RestrictInfo. */ -- 2.27.0