From e1127515a49b14f0e7b938f853b7ef1f6f0e7de2 Mon Sep 17 00:00:00 2001 From: Alexander Korotkov Date: Mon, 30 Mar 2026 04:17:11 +0300 Subject: [PATCH v16 1/3] mark_async_capable(): subpath should match subplan mark_async_capable() believes that the path corresponds to the plan. This is not true when creating_[merge_]append_plan() inserts sort node. In this case, mark_async_capable() can treat the Sort plan node as some other node and crash. Fix this by handling the Sort node separately. This is needed to make the MergeAppend node async-capable, which will be implemented in the subsequent commits. Discussion: https://postgr.es/m/59be194c5a409fb9fc9f2031581b8a44%40postgrespro.ru Author: Alexander Pyhalov Reviewed-by: Matheus Alcantara Reviewed-by: Alena Rybakina --- src/backend/optimizer/plan/createplan.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index 50b0e10308b..26d0dbc2e1d 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -1138,10 +1138,12 @@ mark_async_capable_plan(Plan *plan, Path *path) SubqueryScan *scan_plan = (SubqueryScan *) plan; /* - * If the generated plan node includes a gating Result node, - * we can't execute it asynchronously. + * Check that the plan is really a SubqueryScan before using + * it. It can be not true if the generated plan node includes + * a gating Result node or a Sort node. In such a case, we + * can't execute it asynchronously. */ - if (IsA(plan, Result)) + if (!IsA(plan, SubqueryScan)) return false; /* @@ -1159,10 +1161,10 @@ mark_async_capable_plan(Plan *plan, Path *path) FdwRoutine *fdwroutine = path->parent->fdwroutine; /* - * If the generated plan node includes a gating Result node, - * we can't execute it asynchronously. + * If the generated plan node includes a gating Result node or + * a Sort node, we can't execute it asynchronously. */ - if (IsA(plan, Result)) + if (IsA(plan, Result) || IsA(plan, Sort)) return false; Assert(fdwroutine != NULL); @@ -1175,9 +1177,9 @@ mark_async_capable_plan(Plan *plan, Path *path) /* * If the generated plan node includes a Result node for the - * projection, we can't execute it asynchronously. + * projection or a Sort node, we can't execute it asynchronously. */ - if (IsA(plan, Result)) + if (IsA(plan, Result) || IsA(plan, Sort)) return false; /* -- 2.39.5 (Apple Git-154)