From 02fd1e6c03db344c9546c7aeb24c2093557c93c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emel=20=C5=9Eim=C5=9Fek?= Date: Wed, 19 Oct 2022 18:04:45 +0300 Subject: [PATCH] Fix the crash that happens when using auto_explain extension with recursive queries (#6406) This crash happens with recursively planned queries. For such queries, subplans are explained via the ExplainOnePlan function of postgresql. This function reconstructs the query description from the plan therefore it expects the ActiveSnaphot for the query be available. This fix makes sure that the snapshot is in the stack before calling ExplainOnePlan. Fixes #2920. --- src/backend/distributed/planner/multi_explain.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/backend/distributed/planner/multi_explain.c b/src/backend/distributed/planner/multi_explain.c index 502c47b7a..cbfde29c9 100644 --- a/src/backend/distributed/planner/multi_explain.c +++ b/src/backend/distributed/planner/multi_explain.c @@ -201,6 +201,13 @@ CitusExplainScan(CustomScanState *node, List *ancestors, struct ExplainState *es ExplainOpenGroup("Distributed Query", "Distributed Query", true, es); + /* + * ExplainOnePlan function of postgres might be called in this codepath. + * It requires an ActiveSnapshot being set. Make sure to make ActiveSnapshot available before calling into + * Citus Explain functions. + */ + PushActiveSnapshot(executorState->es_snapshot); + if (distributedPlan->subPlanList != NIL) { ExplainSubPlans(distributedPlan, es); @@ -208,6 +215,8 @@ CitusExplainScan(CustomScanState *node, List *ancestors, struct ExplainState *es ExplainJob(scanState, distributedPlan->workerJob, es, params); + PopActiveSnapshot(); + ExplainCloseGroup("Distributed Query", "Distributed Query", true, es); }