Improve comments for trivial_subqueryscan().
authorEtsuro Fujita <[email protected]>
Thu, 9 Jun 2022 10:30:00 +0000 (19:30 +0900)
committerEtsuro Fujita <[email protected]>
Thu, 9 Jun 2022 10:30:00 +0000 (19:30 +0900)
This function can be called from mark_async_capable_plan(), a helper
function for create_append_plan(), before set_subqueryscan_references(),
to determine the triviality of a SubqueryScan that is a child of an
Append plan node, which is done before doing finalize_plan() on the
SubqueryScan (if necessary) and set_plan_references() on the subplan,
unlike when called from set_subqueryscan_references().  The reason why
this is safe wouldn't be that obvious, so add comments explaining this.

Follow-up for commit c2bb02bc2.

Reviewed by Zhihong Yu.

Discussion: https://postgr.es/m/CAPmGK17%2BGiJBthC6va7%2B9n6t75e-M1N0U18YB2G1B%2BE5OdrNTA%40mail.gmail.com

src/backend/optimizer/plan/setrefs.c

index d95fd8980778200e5ecfb4bc5f0b506da164671a..9cef92cab241a7b33eb78cb0c2d565711d4de5a4 100644 (file)
@@ -1349,8 +1349,23 @@ set_subqueryscan_references(PlannerInfo *root,
  * We can delete it if it has no qual to check and the targetlist just
  * regurgitates the output of the child plan.
  *
- * This might be called repeatedly on a SubqueryScan node, so we cache the
- * result in the SubqueryScan node to avoid repeated computation.
+ * This can be called from mark_async_capable_plan(), a helper function for
+ * create_append_plan(), before set_subqueryscan_references(), to determine
+ * triviality of a SubqueryScan that is a child of an Append node.  So we
+ * cache the result in the SubqueryScan node to avoid repeated computation.
+ *
+ * Note: when called from mark_async_capable_plan(), we determine the result
+ * before running finalize_plan() on the SubqueryScan node (if needed) and
+ * set_plan_references() on the subplan tree, but this would be safe, because
+ * 1) finalize_plan() doesn't modify the tlist or quals for the SubqueryScan
+ *   node (or that for any plan node in the subplan tree), and
+ * 2) set_plan_references() modifies the tlist for every plan node in the
+ *   subplan tree, but keeps const/resjunk columns as const/resjunk ones and
+ *   preserves the length and order of the tlist, and
+ * 3) set_plan_references() might delete the topmost plan node like an Append
+ *   or MergeAppend from the subplan tree and pull up the child plan node,
+ *   but in that case, the tlist for the child plan node exactly matches the
+ *   parent.
  */
 bool
 trivial_subqueryscan(SubqueryScan *plan)
@@ -1359,7 +1374,7 @@ trivial_subqueryscan(SubqueryScan *plan)
    ListCell   *lp,
               *lc;
 
-   /* We might have detected this already (see mark_async_capable_plan) */
+   /* We might have detected this already; in which case reuse the result */
    if (plan->scanstatus == SUBQUERY_SCAN_TRIVIAL)
        return true;
    if (plan->scanstatus == SUBQUERY_SCAN_NONTRIVIAL)