LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/plan - subselect.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 96.2 % 1008 970 38 1 13 956 1
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 38 38 2 36
Baseline: lcov-20250906-005545-baseline Branches: 81.4 % 727 592 8 127 2 12 578
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 62 62 13 49
(360..) days: 96.0 % 946 908 38 1 907
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 37 37 2 35
Branch coverage date bins:
(30,360] days: 80.0 % 70 56 8 6 12 44
(360..) days: 81.6 % 657 536 121 2 534

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * subselect.c
                                  4                 :                :  *    Planning routines for subselects.
                                  5                 :                :  *
                                  6                 :                :  * This module deals with SubLinks and CTEs, but not subquery RTEs (i.e.,
                                  7                 :                :  * not sub-SELECT-in-FROM cases).
                                  8                 :                :  *
                                  9                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                 10                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 11                 :                :  *
                                 12                 :                :  * IDENTIFICATION
                                 13                 :                :  *    src/backend/optimizer/plan/subselect.c
                                 14                 :                :  *
                                 15                 :                :  *-------------------------------------------------------------------------
                                 16                 :                :  */
                                 17                 :                : #include "postgres.h"
                                 18                 :                : 
                                 19                 :                : #include "access/htup_details.h"
                                 20                 :                : #include "catalog/pg_operator.h"
                                 21                 :                : #include "catalog/pg_type.h"
                                 22                 :                : #include "executor/executor.h"
                                 23                 :                : #include "miscadmin.h"
                                 24                 :                : #include "nodes/makefuncs.h"
                                 25                 :                : #include "nodes/nodeFuncs.h"
                                 26                 :                : #include "optimizer/clauses.h"
                                 27                 :                : #include "optimizer/cost.h"
                                 28                 :                : #include "optimizer/optimizer.h"
                                 29                 :                : #include "optimizer/paramassign.h"
                                 30                 :                : #include "optimizer/pathnode.h"
                                 31                 :                : #include "optimizer/planmain.h"
                                 32                 :                : #include "optimizer/planner.h"
                                 33                 :                : #include "optimizer/prep.h"
                                 34                 :                : #include "optimizer/subselect.h"
                                 35                 :                : #include "parser/parse_relation.h"
                                 36                 :                : #include "rewrite/rewriteManip.h"
                                 37                 :                : #include "utils/builtins.h"
                                 38                 :                : #include "utils/lsyscache.h"
                                 39                 :                : #include "utils/syscache.h"
                                 40                 :                : 
                                 41                 :                : 
                                 42                 :                : typedef struct convert_testexpr_context
                                 43                 :                : {
                                 44                 :                :     PlannerInfo *root;
                                 45                 :                :     List       *subst_nodes;    /* Nodes to substitute for Params */
                                 46                 :                : } convert_testexpr_context;
                                 47                 :                : 
                                 48                 :                : typedef struct process_sublinks_context
                                 49                 :                : {
                                 50                 :                :     PlannerInfo *root;
                                 51                 :                :     bool        isTopQual;
                                 52                 :                : } process_sublinks_context;
                                 53                 :                : 
                                 54                 :                : typedef struct finalize_primnode_context
                                 55                 :                : {
                                 56                 :                :     PlannerInfo *root;
                                 57                 :                :     Bitmapset  *paramids;       /* Non-local PARAM_EXEC paramids found */
                                 58                 :                : } finalize_primnode_context;
                                 59                 :                : 
                                 60                 :                : typedef struct inline_cte_walker_context
                                 61                 :                : {
                                 62                 :                :     const char *ctename;        /* name and relative level of target CTE */
                                 63                 :                :     int         levelsup;
                                 64                 :                :     Query      *ctequery;       /* query to substitute */
                                 65                 :                : } inline_cte_walker_context;
                                 66                 :                : 
                                 67                 :                : 
                                 68                 :                : static Node *build_subplan(PlannerInfo *root, Plan *plan, Path *path,
                                 69                 :                :                            PlannerInfo *subroot, List *plan_params,
                                 70                 :                :                            SubLinkType subLinkType, int subLinkId,
                                 71                 :                :                            Node *testexpr, List *testexpr_paramids,
                                 72                 :                :                            bool unknownEqFalse);
                                 73                 :                : static List *generate_subquery_params(PlannerInfo *root, List *tlist,
                                 74                 :                :                                       List **paramIds);
                                 75                 :                : static List *generate_subquery_vars(PlannerInfo *root, List *tlist,
                                 76                 :                :                                     Index varno);
                                 77                 :                : static Node *convert_testexpr(PlannerInfo *root,
                                 78                 :                :                               Node *testexpr,
                                 79                 :                :                               List *subst_nodes);
                                 80                 :                : static Node *convert_testexpr_mutator(Node *node,
                                 81                 :                :                                       convert_testexpr_context *context);
                                 82                 :                : static bool subplan_is_hashable(Plan *plan);
                                 83                 :                : static bool subpath_is_hashable(Path *path);
                                 84                 :                : static bool testexpr_is_hashable(Node *testexpr, List *param_ids);
                                 85                 :                : static bool test_opexpr_is_hashable(OpExpr *testexpr, List *param_ids);
                                 86                 :                : static bool hash_ok_operator(OpExpr *expr);
                                 87                 :                : static bool contain_dml(Node *node);
                                 88                 :                : static bool contain_dml_walker(Node *node, void *context);
                                 89                 :                : static bool contain_outer_selfref(Node *node);
                                 90                 :                : static bool contain_outer_selfref_walker(Node *node, Index *depth);
                                 91                 :                : static void inline_cte(PlannerInfo *root, CommonTableExpr *cte);
                                 92                 :                : static bool inline_cte_walker(Node *node, inline_cte_walker_context *context);
                                 93                 :                : static bool simplify_EXISTS_query(PlannerInfo *root, Query *query);
                                 94                 :                : static Query *convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
                                 95                 :                :                                     Node **testexpr, List **paramIds);
                                 96                 :                : static Node *replace_correlation_vars_mutator(Node *node, PlannerInfo *root);
                                 97                 :                : static Node *process_sublinks_mutator(Node *node,
                                 98                 :                :                                       process_sublinks_context *context);
                                 99                 :                : static Bitmapset *finalize_plan(PlannerInfo *root,
                                100                 :                :                                 Plan *plan,
                                101                 :                :                                 int gather_param,
                                102                 :                :                                 Bitmapset *valid_params,
                                103                 :                :                                 Bitmapset *scan_params);
                                104                 :                : static bool finalize_primnode(Node *node, finalize_primnode_context *context);
                                105                 :                : static bool finalize_agg_primnode(Node *node, finalize_primnode_context *context);
                                106                 :                : 
                                107                 :                : 
                                108                 :                : /*
                                109                 :                :  * Get the datatype/typmod/collation of the first column of the plan's output.
                                110                 :                :  *
                                111                 :                :  * This information is stored for ARRAY_SUBLINK execution and for
                                112                 :                :  * exprType()/exprTypmod()/exprCollation(), which have no way to get at the
                                113                 :                :  * plan associated with a SubPlan node.  We really only need the info for
                                114                 :                :  * EXPR_SUBLINK and ARRAY_SUBLINK subplans, but for consistency we save it
                                115                 :                :  * always.
                                116                 :                :  */
                                117                 :                : static void
 5251 tgl@sss.pgh.pa.us         118                 :CBC       21913 : get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod,
                                119                 :                :                    Oid *colcollation)
                                120                 :                : {
                                121                 :                :     /* In cases such as EXISTS, tlist might be empty; arbitrarily use VOID */
 6232                           122         [ +  + ]:          21913 :     if (plan->targetlist)
                                123                 :                :     {
 3071                           124                 :          20559 :         TargetEntry *tent = linitial_node(TargetEntry, plan->targetlist);
                                125                 :                : 
 6232                           126         [ +  - ]:          20559 :         if (!tent->resjunk)
                                127                 :                :         {
 6024                           128                 :          20559 :             *coltype = exprType((Node *) tent->expr);
                                129                 :          20559 :             *coltypmod = exprTypmod((Node *) tent->expr);
 5324 peter_e@gmx.net           130                 :          20559 :             *colcollation = exprCollation((Node *) tent->expr);
 6024 tgl@sss.pgh.pa.us         131                 :          20559 :             return;
                                132                 :                :         }
                                133                 :                :     }
                                134                 :           1354 :     *coltype = VOIDOID;
                                135                 :           1354 :     *coltypmod = -1;
 5324 peter_e@gmx.net           136                 :           1354 :     *colcollation = InvalidOid;
                                137                 :                : }
                                138                 :                : 
                                139                 :                : /*
                                140                 :                :  * Convert a SubLink (as created by the parser) into a SubPlan.
                                141                 :                :  *
                                142                 :                :  * We are given the SubLink's contained query, type, ID, and testexpr.  We are
                                143                 :                :  * also told if this expression appears at top level of a WHERE/HAVING qual.
                                144                 :                :  *
                                145                 :                :  * Note: we assume that the testexpr has been AND/OR flattened (actually,
                                146                 :                :  * it's been through eval_const_expressions), but not converted to
                                147                 :                :  * implicit-AND form; and any SubLinks in it should already have been
                                148                 :                :  * converted to SubPlans.  The subquery is as yet untouched, however.
                                149                 :                :  *
                                150                 :                :  * The result is whatever we need to substitute in place of the SubLink node
                                151                 :                :  * in the executable expression.  If we're going to do the subplan as a
                                152                 :                :  * regular subplan, this will be the constructed SubPlan node.  If we're going
                                153                 :                :  * to do the subplan as an InitPlan, the SubPlan node instead goes into
                                154                 :                :  * root->init_plans, and what we return here is an expression tree
                                155                 :                :  * representing the InitPlan's result: usually just a Param node representing
                                156                 :                :  * a single scalar result, but possibly a row comparison tree containing
                                157                 :                :  * multiple Param nodes, or for a MULTIEXPR subquery a simple NULL constant
                                158                 :                :  * (since the real output Params are elsewhere in the tree, and the MULTIEXPR
                                159                 :                :  * subquery itself is in a resjunk tlist entry whose value is uninteresting).
                                160                 :                :  */
                                161                 :                : static Node *
 4098 tgl@sss.pgh.pa.us         162                 :          19625 : make_subplan(PlannerInfo *root, Query *orig_subquery,
                                163                 :                :              SubLinkType subLinkType, int subLinkId,
                                164                 :                :              Node *testexpr, bool isTopQual)
                                165                 :                : {
                                166                 :                :     Query      *subquery;
 6224                           167                 :          19625 :     bool        simple_exists = false;
                                168                 :                :     double      tuple_fraction;
                                169                 :                :     PlannerInfo *subroot;
                                170                 :                :     RelOptInfo *final_rel;
                                171                 :                :     Path       *best_path;
                                172                 :                :     Plan       *plan;
                                173                 :                :     List       *plan_params;
                                174                 :                :     Node       *result;
                                175                 :                : 
                                176                 :                :     /*
                                177                 :                :      * Copy the source Query node.  This is a quick and dirty kluge to resolve
                                178                 :                :      * the fact that the parser can generate trees with multiple links to the
                                179                 :                :      * same sub-Query node, but the planner wants to scribble on the Query.
                                180                 :                :      * Try to clean this up when we do querytree redesign...
                                181                 :                :      */
 3103 peter_e@gmx.net           182                 :          19625 :     subquery = copyObject(orig_subquery);
                                183                 :                : 
                                184                 :                :     /*
                                185                 :                :      * If it's an EXISTS subplan, we might be able to simplify it.
                                186                 :                :      */
 6224 tgl@sss.pgh.pa.us         187         [ +  + ]:          19625 :     if (subLinkType == EXISTS_SUBLINK)
 3941                           188                 :           1227 :         simple_exists = simplify_EXISTS_query(root, subquery);
                                189                 :                : 
                                190                 :                :     /*
                                191                 :                :      * For an EXISTS subplan, tell lower-level planner to expect that only the
                                192                 :                :      * first tuple will be retrieved.  For ALL and ANY subplans, we will be
                                193                 :                :      * able to stop evaluating if the test condition fails or matches, so very
                                194                 :                :      * often not all the tuples will be retrieved; for lack of a better idea,
                                195                 :                :      * specify 50% retrieval.  For EXPR, MULTIEXPR, and ROWCOMPARE subplans,
                                196                 :                :      * use default behavior (we're only expecting one row out, anyway).
                                197                 :                :      *
                                198                 :                :      * NOTE: if you change these numbers, also change cost_subplan() in
                                199                 :                :      * path/costsize.c.
                                200                 :                :      *
                                201                 :                :      * XXX If an ANY subplan is uncorrelated, build_subplan may decide to hash
                                202                 :                :      * its output.  In that case it would've been better to specify full
                                203                 :                :      * retrieval.  At present, however, we can only check hashability after
                                204                 :                :      * we've made the subplan :-(.  (Determining whether it'll fit in hash_mem
                                205                 :                :      * is the really hard part.)  Therefore, we don't want to be too
                                206                 :                :      * optimistic about the percentage of tuples retrieved, for fear of
                                207                 :                :      * selecting a plan that's bad for the materialization case.
                                208                 :                :      */
 6224                           209         [ +  + ]:          19625 :     if (subLinkType == EXISTS_SUBLINK)
 9335                           210                 :           1227 :         tuple_fraction = 1.0;   /* just like a LIMIT 1 */
 6224                           211   [ +  +  +  + ]:          18398 :     else if (subLinkType == ALL_SUBLINK ||
                                212                 :                :              subLinkType == ANY_SUBLINK)
 9335                           213                 :            281 :         tuple_fraction = 0.5;   /* 50% */
                                214                 :                :     else
 8216                           215                 :          18117 :         tuple_fraction = 0.0;   /* default behavior */
                                216                 :                : 
                                217                 :                :     /* plan_params should not be in use in current query level */
 4749                           218         [ -  + ]:          19625 :     Assert(root->plan_params == NIL);
                                219                 :                : 
                                220                 :                :     /* Generate Paths for the subquery */
  473 rhaas@postgresql.org      221                 :          19625 :     subroot = subquery_planner(root->glob, subquery, root, false,
                                222                 :                :                                tuple_fraction, NULL);
                                223                 :                : 
                                224                 :                :     /* Isolate the params needed by this specific subplan */
 4749 tgl@sss.pgh.pa.us         225                 :          19625 :     plan_params = root->plan_params;
                                226                 :          19625 :     root->plan_params = NIL;
                                227                 :                : 
                                228                 :                :     /*
                                229                 :                :      * Select best Path and turn it into a Plan.  At least for now, there
                                230                 :                :      * seems no reason to postpone doing that.
                                231                 :                :      */
 3470                           232                 :          19625 :     final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
                                233                 :          19625 :     best_path = get_cheapest_fractional_path(final_rel, tuple_fraction);
                                234                 :                : 
                                235                 :          19625 :     plan = create_plan(subroot, best_path);
                                236                 :                : 
                                237                 :                :     /* And convert to SubPlan or InitPlan format. */
  529                           238                 :          19625 :     result = build_subplan(root, plan, best_path,
                                239                 :                :                            subroot, plan_params,
                                240                 :                :                            subLinkType, subLinkId,
                                241                 :                :                            testexpr, NIL, isTopQual);
                                242                 :                : 
                                243                 :                :     /*
                                244                 :                :      * If it's a correlated EXISTS with an unimportant targetlist, we might be
                                245                 :                :      * able to transform it to the equivalent of an IN and then implement it
                                246                 :                :      * by hashing.  We don't have enough information yet to tell which way is
                                247                 :                :      * likely to be better (it depends on the expected number of executions of
                                248                 :                :      * the EXISTS qual, and we are much too early in planning the outer query
                                249                 :                :      * to be able to guess that).  So we generate both plans, if possible, and
                                250                 :                :      * leave it to setrefs.c to decide which to use.
                                251                 :                :      */
 6224                           252   [ +  +  +  + ]:          19625 :     if (simple_exists && IsA(result, SubPlan))
                                253                 :                :     {
                                254                 :                :         Node       *newtestexpr;
                                255                 :                :         List       *paramIds;
                                256                 :                : 
                                257                 :                :         /* Make a second copy of the original subquery */
 3103 peter_e@gmx.net           258                 :           1085 :         subquery = copyObject(orig_subquery);
                                259                 :                :         /* and re-simplify */
 3941 tgl@sss.pgh.pa.us         260                 :           1085 :         simple_exists = simplify_EXISTS_query(root, subquery);
 6224                           261         [ -  + ]:           1085 :         Assert(simple_exists);
                                262                 :                :         /* See if it can be converted to an ANY query */
                                263                 :           1085 :         subquery = convert_EXISTS_to_ANY(root, subquery,
                                264                 :                :                                          &newtestexpr, &paramIds);
                                265         [ +  + ]:           1085 :         if (subquery)
                                266                 :                :         {
                                267                 :                :             /* Generate Paths for the ANY subquery; we'll need all rows */
  473 rhaas@postgresql.org      268                 :            848 :             subroot = subquery_planner(root->glob, subquery, root, false, 0.0,
                                269                 :                :                                        NULL);
                                270                 :                : 
                                271                 :                :             /* Isolate the params needed by this specific subplan */
 4749 tgl@sss.pgh.pa.us         272                 :            848 :             plan_params = root->plan_params;
                                273                 :            848 :             root->plan_params = NIL;
                                274                 :                : 
                                275                 :                :             /* Select best Path */
 3470                           276                 :            848 :             final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
                                277                 :            848 :             best_path = final_rel->cheapest_total_path;
                                278                 :                : 
                                279                 :                :             /* Now we can check if it'll fit in hash_mem */
 1805                           280         [ +  + ]:            848 :             if (subpath_is_hashable(best_path))
                                281                 :                :             {
                                282                 :                :                 SubPlan    *hashplan;
                                283                 :                :                 AlternativeSubPlan *asplan;
                                284                 :                : 
                                285                 :                :                 /* OK, finish planning the ANY subquery */
                                286                 :            845 :                 plan = create_plan(subroot, best_path);
                                287                 :                : 
                                288                 :                :                 /* ... and convert to SubPlan format */
 3119 peter_e@gmx.net           289                 :            845 :                 hashplan = castNode(SubPlan,
                                290                 :                :                                     build_subplan(root, plan, best_path,
                                291                 :                :                                                   subroot, plan_params,
                                292                 :                :                                                   ANY_SUBLINK, 0,
                                293                 :                :                                                   newtestexpr,
                                294                 :                :                                                   paramIds,
                                295                 :                :                                                   true));
                                296                 :                :                 /* Check we got what we expected */
 6224 tgl@sss.pgh.pa.us         297         [ -  + ]:            845 :                 Assert(hashplan->parParam == NIL);
                                298         [ -  + ]:            845 :                 Assert(hashplan->useHashTable);
                                299                 :                : 
                                300                 :                :                 /* Leave it to setrefs.c to decide which plan to use */
                                301                 :            845 :                 asplan = makeNode(AlternativeSubPlan);
                                302                 :            845 :                 asplan->subplans = list_make2(result, hashplan);
                                303                 :            845 :                 result = (Node *) asplan;
 1805                           304                 :            845 :                 root->hasAlternativeSubPlans = true;
                                305                 :                :             }
                                306                 :                :         }
                                307                 :                :     }
                                308                 :                : 
 6224                           309                 :          19625 :     return result;
                                310                 :                : }
                                311                 :                : 
                                312                 :                : /*
                                313                 :                :  * Build a SubPlan node given the raw inputs --- subroutine for make_subplan
                                314                 :                :  *
                                315                 :                :  * Returns either the SubPlan, or a replacement expression if we decide to
                                316                 :                :  * make it an InitPlan, as explained in the comments for make_subplan.
                                317                 :                :  */
                                318                 :                : static Node *
  529                           319                 :          20470 : build_subplan(PlannerInfo *root, Plan *plan, Path *path,
                                320                 :                :               PlannerInfo *subroot, List *plan_params,
                                321                 :                :               SubLinkType subLinkType, int subLinkId,
                                322                 :                :               Node *testexpr, List *testexpr_paramids,
                                323                 :                :               bool unknownEqFalse)
                                324                 :                : {
                                325                 :                :     Node       *result;
                                326                 :                :     SubPlan    *splan;
                                327                 :                :     bool        isInitPlan;
                                328                 :                :     ListCell   *lc;
                                329                 :                : 
                                330                 :                :     /*
                                331                 :                :      * Initialize the SubPlan node.  Note plan_id, plan_name, and cost fields
                                332                 :                :      * are set further down.
                                333                 :                :      */
 6766                           334                 :          20470 :     splan = makeNode(SubPlan);
 6224                           335                 :          20470 :     splan->subLinkType = subLinkType;
 6766                           336                 :          20470 :     splan->testexpr = NULL;
                                337                 :          20470 :     splan->paramIds = NIL;
 5251                           338                 :          20470 :     get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
                                339                 :                :                        &splan->firstColCollation);
 6766                           340                 :          20470 :     splan->useHashTable = false;
 6224                           341                 :          20470 :     splan->unknownEqFalse = unknownEqFalse;
 3069                           342                 :          20470 :     splan->parallel_safe = plan->parallel_safe;
 6766                           343                 :          20470 :     splan->setParam = NIL;
                                344                 :          20470 :     splan->parParam = NIL;
                                345                 :          20470 :     splan->args = NIL;
                                346                 :                : 
                                347                 :                :     /*
                                348                 :                :      * Make parParam and args lists of param IDs and expressions that current
                                349                 :                :      * query level will pass to this child plan.
                                350                 :                :      */
 4749                           351   [ +  +  +  +  :          42839 :     foreach(lc, plan_params)
                                              +  + ]
                                352                 :                :     {
                                353                 :          22369 :         PlannerParamItem *pitem = (PlannerParamItem *) lfirst(lc);
                                354                 :          22369 :         Node       *arg = pitem->item;
                                355                 :                : 
                                356                 :                :         /*
                                357                 :                :          * The Var, PlaceHolderVar, Aggref, GroupingFunc, or ReturningExpr has
                                358                 :                :          * already been adjusted to have the correct varlevelsup, phlevelsup,
                                359                 :                :          * agglevelsup, or retlevelsup.
                                360                 :                :          *
                                361                 :                :          * If it's a PlaceHolderVar, Aggref, GroupingFunc, or ReturningExpr,
                                362                 :                :          * its arguments might contain SubLinks, which have not yet been
                                363                 :                :          * processed (see the comments for SS_replace_correlation_vars).  Do
                                364                 :                :          * that now.
                                365                 :                :          */
                                366         [ +  + ]:          22369 :         if (IsA(arg, PlaceHolderVar) ||
 1265                           367         [ +  + ]:          22363 :             IsA(arg, Aggref) ||
  233 dean.a.rasheed@gmail      368         [ +  + ]:          22337 :             IsA(arg, GroupingFunc) ||
                                369         [ +  + ]:          22305 :             IsA(arg, ReturningExpr))
 4749 tgl@sss.pgh.pa.us         370                 :             73 :             arg = SS_process_sublinks(root, arg, false);
                                371                 :                : 
                                372                 :          22369 :         splan->parParam = lappend_int(splan->parParam, pitem->paramId);
                                373                 :          22369 :         splan->args = lappend(splan->args, arg);
                                374                 :                :     }
                                375                 :                : 
                                376                 :                :     /*
                                377                 :                :      * Un-correlated or undirect correlated plans of EXISTS, EXPR, ARRAY,
                                378                 :                :      * ROWCOMPARE, or MULTIEXPR types can be used as initPlans.  For EXISTS,
                                379                 :                :      * EXPR, or ARRAY, we return a Param referring to the result of evaluating
                                380                 :                :      * the initPlan.  For ROWCOMPARE, we must modify the testexpr tree to
                                381                 :                :      * contain PARAM_EXEC Params instead of the PARAM_SUBLINK Params emitted
                                382                 :                :      * by the parser, and then return that tree.  For MULTIEXPR, we return a
                                383                 :                :      * null constant: the resjunk targetlist item containing the SubLink does
                                384                 :                :      * not need to return anything useful, since the referencing Params are
                                385                 :                :      * elsewhere.
                                386                 :                :      */
 6224                           387   [ +  +  +  + ]:          20470 :     if (splan->parParam == NIL && subLinkType == EXISTS_SUBLINK)
 9427                           388                 :            126 :     {
                                389                 :                :         Param      *prm;
                                390                 :                : 
 6224                           391         [ -  + ]:            126 :         Assert(testexpr == NULL);
 2430                           392                 :            126 :         prm = generate_new_exec_param(root, BOOLOID, -1, InvalidOid);
 6766                           393                 :            126 :         splan->setParam = list_make1_int(prm->paramid);
                                394                 :            126 :         isInitPlan = true;
 9427                           395                 :            126 :         result = (Node *) prm;
                                396                 :                :     }
 6224                           397   [ +  +  +  + ]:          20344 :     else if (splan->parParam == NIL && subLinkType == EXPR_SUBLINK)
 9427                           398                 :           5000 :     {
 7773 neilc@samurai.com         399                 :           5000 :         TargetEntry *te = linitial(plan->targetlist);
                                400                 :                :         Param      *prm;
                                401                 :                : 
 7458 tgl@sss.pgh.pa.us         402         [ -  + ]:           5000 :         Assert(!te->resjunk);
 6224                           403         [ -  + ]:           5000 :         Assert(testexpr == NULL);
 2430                           404                 :           5000 :         prm = generate_new_exec_param(root,
                                405                 :           5000 :                                       exprType((Node *) te->expr),
                                406                 :           5000 :                                       exprTypmod((Node *) te->expr),
                                407                 :           5000 :                                       exprCollation((Node *) te->expr));
 6766                           408                 :           5000 :         splan->setParam = list_make1_int(prm->paramid);
                                409                 :           5000 :         isInitPlan = true;
 9427                           410                 :           5000 :         result = (Node *) prm;
                                411                 :                :     }
 6224                           412   [ +  +  +  + ]:          15344 :     else if (splan->parParam == NIL && subLinkType == ARRAY_SUBLINK)
 8187                           413                 :             61 :     {
 7773 neilc@samurai.com         414                 :             61 :         TargetEntry *te = linitial(plan->targetlist);
                                415                 :                :         Oid         arraytype;
                                416                 :                :         Param      *prm;
                                417                 :                : 
 7458 tgl@sss.pgh.pa.us         418         [ -  + ]:             61 :         Assert(!te->resjunk);
 6224                           419         [ -  + ]:             61 :         Assert(testexpr == NULL);
 3938                           420                 :             61 :         arraytype = get_promoted_array_type(exprType((Node *) te->expr));
 8187                           421         [ -  + ]:             61 :         if (!OidIsValid(arraytype))
 8079 tgl@sss.pgh.pa.us         422         [ #  # ]:UBC           0 :             elog(ERROR, "could not find array type for datatype %s",
                                423                 :                :                  format_type_be(exprType((Node *) te->expr)));
 2430 tgl@sss.pgh.pa.us         424                 :CBC          61 :         prm = generate_new_exec_param(root,
                                425                 :                :                                       arraytype,
                                426                 :             61 :                                       exprTypmod((Node *) te->expr),
                                427                 :             61 :                                       exprCollation((Node *) te->expr));
 6766                           428                 :             61 :         splan->setParam = list_make1_int(prm->paramid);
                                429                 :             61 :         isInitPlan = true;
 8187                           430                 :             61 :         result = (Node *) prm;
                                431                 :                :     }
 6224                           432   [ +  +  +  + ]:          15283 :     else if (splan->parParam == NIL && subLinkType == ROWCOMPARE_SUBLINK)
10067 vadim4o@yahoo.com         433                 :              9 :     {
                                434                 :                :         /* Adjust the Params */
                                435                 :                :         List       *params;
                                436                 :                : 
 6224 tgl@sss.pgh.pa.us         437         [ -  + ]:              9 :         Assert(testexpr != NULL);
 6442                           438                 :              9 :         params = generate_subquery_params(root,
                                439                 :                :                                           plan->targetlist,
                                440                 :                :                                           &splan->paramIds);
 6774                           441                 :              9 :         result = convert_testexpr(root,
                                442                 :                :                                   testexpr,
                                443                 :                :                                   params);
 6766                           444                 :              9 :         splan->setParam = list_copy(splan->paramIds);
                                445                 :              9 :         isInitPlan = true;
                                446                 :                : 
                                447                 :                :         /*
                                448                 :                :          * The executable expression is returned to become part of the outer
                                449                 :                :          * plan's expression tree; it is not kept in the initplan node.
                                450                 :                :          */
                                451                 :                :     }
 4098                           452         [ +  + ]:          15274 :     else if (subLinkType == MULTIEXPR_SUBLINK)
                                453                 :                :     {
                                454                 :                :         /*
                                455                 :                :          * Whether it's an initplan or not, it needs to set a PARAM_EXEC Param
                                456                 :                :          * for each output column.
                                457                 :                :          */
                                458                 :                :         List       *params;
                                459                 :                : 
                                460         [ -  + ]:             66 :         Assert(testexpr == NULL);
                                461                 :             66 :         params = generate_subquery_params(root,
                                462                 :                :                                           plan->targetlist,
                                463                 :                :                                           &splan->setParam);
                                464                 :                : 
                                465                 :                :         /*
                                466                 :                :          * Save the list of replacement Params in the n'th cell of
                                467                 :                :          * root->multiexpr_params; setrefs.c will use it to replace
                                468                 :                :          * PARAM_MULTIEXPR Params.
                                469                 :                :          */
                                470         [ +  + ]:            132 :         while (list_length(root->multiexpr_params) < subLinkId)
                                471                 :             66 :             root->multiexpr_params = lappend(root->multiexpr_params, NIL);
                                472                 :             66 :         lc = list_nth_cell(root->multiexpr_params, subLinkId - 1);
                                473         [ -  + ]:             66 :         Assert(lfirst(lc) == NIL);
                                474                 :             66 :         lfirst(lc) = params;
                                475                 :                : 
                                476                 :                :         /* It can be an initplan if there are no parParams. */
                                477         [ +  + ]:             66 :         if (splan->parParam == NIL)
                                478                 :                :         {
                                479                 :             15 :             isInitPlan = true;
                                480                 :             15 :             result = (Node *) makeNullConst(RECORDOID, -1, InvalidOid);
                                481                 :                :         }
                                482                 :                :         else
                                483                 :                :         {
                                484                 :             51 :             isInitPlan = false;
                                485                 :             51 :             result = (Node *) splan;
                                486                 :                :         }
                                487                 :                :     }
                                488                 :                :     else
                                489                 :                :     {
                                490                 :                :         /*
                                491                 :                :          * Adjust the Params in the testexpr, unless caller already took care
                                492                 :                :          * of it (as indicated by passing a list of Param IDs).
                                493                 :                :          */
 1849                           494   [ +  +  +  + ]:          15208 :         if (testexpr && testexpr_paramids == NIL)
 6267                           495                 :            287 :         {
                                496                 :                :             List       *params;
                                497                 :                : 
                                498                 :            287 :             params = generate_subquery_params(root,
                                499                 :                :                                               plan->targetlist,
                                500                 :                :                                               &splan->paramIds);
                                501                 :            287 :             splan->testexpr = convert_testexpr(root,
                                502                 :                :                                                testexpr,
                                503                 :                :                                                params);
                                504                 :                :         }
                                505                 :                :         else
                                506                 :                :         {
 6224                           507                 :          14921 :             splan->testexpr = testexpr;
 1849                           508                 :          14921 :             splan->paramIds = testexpr_paramids;
                                509                 :                :         }
                                510                 :                : 
                                511                 :                :         /*
                                512                 :                :          * We can't convert subplans of ALL_SUBLINK or ANY_SUBLINK types to
                                513                 :                :          * initPlans, even when they are uncorrelated or undirect correlated,
                                514                 :                :          * because we need to scan the output of the subplan for each outer
                                515                 :                :          * tuple.  But if it's a not-direct-correlated IN (= ANY) test, we
                                516                 :                :          * might be able to use a hashtable to avoid comparing all the tuples.
                                517                 :                :          */
 6224                           518         [ +  + ]:          15208 :         if (subLinkType == ANY_SUBLINK &&
                                519   [ +  +  +  - ]:           2183 :             splan->parParam == NIL &&
                                520         [ +  + ]:           2132 :             subplan_is_hashable(plan) &&
 1849                           521                 :           1066 :             testexpr_is_hashable(splan->testexpr, splan->paramIds))
 6766                           522                 :           1054 :             splan->useHashTable = true;
                                523                 :                : 
                                524                 :                :         /*
                                525                 :                :          * Otherwise, we have the option to tack a Material node onto the top
                                526                 :                :          * of the subplan, to reduce the cost of reading it repeatedly.  This
                                527                 :                :          * is pointless for a direct-correlated subplan, since we'd have to
                                528                 :                :          * recompute its results each time anyway.  For uncorrelated/undirect
                                529                 :                :          * correlated subplans, we add Material unless the subplan's top plan
                                530                 :                :          * node would materialize its output anyway.  Also, if enable_material
                                531                 :                :          * is false, then the user does not want us to materialize anything
                                532                 :                :          * unnecessarily, so we don't.
                                533                 :                :          */
 5619 rhaas@postgresql.org      534   [ +  +  +  - ]:          14154 :         else if (splan->parParam == NIL && enable_material &&
 5838 tgl@sss.pgh.pa.us         535         [ +  - ]:             21 :                  !ExecMaterializesOutput(nodeTag(plan)))
                                536                 :             21 :             plan = materialize_finished_plan(plan);
                                537                 :                : 
 6766                           538                 :          15208 :         result = (Node *) splan;
                                539                 :          15208 :         isInitPlan = false;
                                540                 :                :     }
                                541                 :                : 
                                542                 :                :     /*
                                543                 :                :      * Add the subplan, its path, and its PlannerInfo to the global lists.
                                544                 :                :      */
 6224                           545                 :          20470 :     root->glob->subplans = lappend(root->glob->subplans, plan);
  529                           546                 :          20470 :     root->glob->subpaths = lappend(root->glob->subpaths, path);
 5117                           547                 :          20470 :     root->glob->subroots = lappend(root->glob->subroots, subroot);
 6766                           548                 :          20470 :     splan->plan_id = list_length(root->glob->subplans);
                                549                 :                : 
                                550         [ +  + ]:          20470 :     if (isInitPlan)
                                551                 :           5211 :         root->init_plans = lappend(root->init_plans, splan);
                                552                 :                : 
                                553                 :                :     /*
                                554                 :                :      * A parameterless subplan (not initplan) should be prepared to handle
                                555                 :                :      * REWIND efficiently.  If it has direct parameters then there's no point
                                556                 :                :      * since it'll be reset on each scan anyway; and if it's an initplan then
                                557                 :                :      * there's no point since it won't get re-run without parameter changes
                                558                 :                :      * anyway.  The input of a hashed subplan doesn't need REWIND either.
                                559                 :                :      */
                                560   [ +  +  +  +  :          20470 :     if (splan->parParam == NIL && !isInitPlan && !splan->useHashTable)
                                              +  + ]
                                561                 :             21 :         root->glob->rewindPlanIDs = bms_add_member(root->glob->rewindPlanIDs,
                                562                 :                :                                                    splan->plan_id);
                                563                 :                : 
                                564                 :                :     /* Label the subplan for EXPLAIN purposes */
  536                           565         [ +  + ]:          20470 :     splan->plan_name = psprintf("%s %d",
                                566                 :                :                                 isInitPlan ? "InitPlan" : "SubPlan",
                                567                 :                :                                 splan->plan_id);
                                568                 :                : 
                                569                 :                :     /* Lastly, fill in the cost estimates for use later */
 6224                           570                 :          20470 :     cost_subplan(root, splan, plan);
                                571                 :                : 
 8320                           572                 :          20470 :     return result;
                                573                 :                : }
                                574                 :                : 
                                575                 :                : /*
                                576                 :                :  * generate_subquery_params: build a list of Params representing the output
                                577                 :                :  * columns of a sublink's sub-select, given the sub-select's targetlist.
                                578                 :                :  *
                                579                 :                :  * We also return an integer list of the paramids of the Params.
                                580                 :                :  */
                                581                 :                : static List *
 6442                           582                 :            362 : generate_subquery_params(PlannerInfo *root, List *tlist, List **paramIds)
                                583                 :                : {
                                584                 :                :     List       *result;
                                585                 :                :     List       *ids;
                                586                 :                :     ListCell   *lc;
                                587                 :                : 
                                588                 :            362 :     result = ids = NIL;
                                589   [ +  -  +  +  :            848 :     foreach(lc, tlist)
                                              +  + ]
                                590                 :                :     {
                                591                 :            486 :         TargetEntry *tent = (TargetEntry *) lfirst(lc);
                                592                 :                :         Param      *param;
                                593                 :                : 
                                594         [ +  + ]:            486 :         if (tent->resjunk)
                                595                 :              3 :             continue;
                                596                 :                : 
 2430                           597                 :            483 :         param = generate_new_exec_param(root,
                                598                 :            483 :                                         exprType((Node *) tent->expr),
                                599                 :            483 :                                         exprTypmod((Node *) tent->expr),
                                600                 :            483 :                                         exprCollation((Node *) tent->expr));
 6442                           601                 :            483 :         result = lappend(result, param);
                                602                 :            483 :         ids = lappend_int(ids, param->paramid);
                                603                 :                :     }
                                604                 :                : 
                                605                 :            362 :     *paramIds = ids;
                                606                 :            362 :     return result;
                                607                 :                : }
                                608                 :                : 
                                609                 :                : /*
                                610                 :                :  * generate_subquery_vars: build a list of Vars representing the output
                                611                 :                :  * columns of a sublink's sub-select, given the sub-select's targetlist.
                                612                 :                :  * The Vars have the specified varno (RTE index).
                                613                 :                :  */
                                614                 :                : static List *
                                615                 :           2208 : generate_subquery_vars(PlannerInfo *root, List *tlist, Index varno)
                                616                 :                : {
                                617                 :                :     List       *result;
                                618                 :                :     ListCell   *lc;
                                619                 :                : 
                                620                 :           2208 :     result = NIL;
                                621   [ +  -  +  +  :           4455 :     foreach(lc, tlist)
                                              +  + ]
                                622                 :                :     {
                                623                 :           2247 :         TargetEntry *tent = (TargetEntry *) lfirst(lc);
                                624                 :                :         Var        *var;
                                625                 :                : 
                                626         [ -  + ]:           2247 :         if (tent->resjunk)
 6442 tgl@sss.pgh.pa.us         627                 :UBC           0 :             continue;
                                628                 :                : 
 5489 peter_e@gmx.net           629                 :CBC        2247 :         var = makeVarFromTargetEntry(varno, tent);
 6442 tgl@sss.pgh.pa.us         630                 :           2247 :         result = lappend(result, var);
                                631                 :                :     }
                                632                 :                : 
                                633                 :           2208 :     return result;
                                634                 :                : }
                                635                 :                : 
                                636                 :                : /*
                                637                 :                :  * convert_testexpr: convert the testexpr given by the parser into
                                638                 :                :  * actually executable form.  This entails replacing PARAM_SUBLINK Params
                                639                 :                :  * with Params or Vars representing the results of the sub-select.  The
                                640                 :                :  * nodes to be substituted are passed in as the List result from
                                641                 :                :  * generate_subquery_params or generate_subquery_vars.
                                642                 :                :  */
                                643                 :                : static Node *
 6774                           644                 :           2642 : convert_testexpr(PlannerInfo *root,
                                645                 :                :                  Node *testexpr,
                                646                 :                :                  List *subst_nodes)
                                647                 :                : {
                                648                 :                :     convert_testexpr_context context;
                                649                 :                : 
                                650                 :           2642 :     context.root = root;
 6442                           651                 :           2642 :     context.subst_nodes = subst_nodes;
                                652                 :           2642 :     return convert_testexpr_mutator(testexpr, &context);
                                653                 :                : }
                                654                 :                : 
                                655                 :                : static Node *
 7192                           656                 :          12667 : convert_testexpr_mutator(Node *node,
                                657                 :                :                          convert_testexpr_context *context)
                                658                 :                : {
                                659         [ +  + ]:          12667 :     if (node == NULL)
                                660                 :             47 :         return NULL;
                                661         [ +  + ]:          12620 :     if (IsA(node, Param))
                                662                 :                :     {
 6912 bruce@momjian.us          663                 :           2746 :         Param      *param = (Param *) node;
                                664                 :                : 
 7192 tgl@sss.pgh.pa.us         665         [ +  + ]:           2746 :         if (param->paramkind == PARAM_SUBLINK)
                                666                 :                :         {
 6442                           667   [ +  -  -  + ]:           5486 :             if (param->paramid <= 0 ||
                                668                 :           2743 :                 param->paramid > list_length(context->subst_nodes))
 7192 tgl@sss.pgh.pa.us         669         [ #  # ]:UBC           0 :                 elog(ERROR, "unexpected PARAM_SUBLINK ID: %d", param->paramid);
                                670                 :                : 
                                671                 :                :             /*
                                672                 :                :              * We copy the list item to avoid having doubly-linked
                                673                 :                :              * substructure in the modified parse tree.  This is probably
                                674                 :                :              * unnecessary when it's a Param, but be safe.
                                675                 :                :              */
 6442 tgl@sss.pgh.pa.us         676                 :CBC        2743 :             return (Node *) copyObject(list_nth(context->subst_nodes,
                                677                 :                :                                                 param->paramid - 1));
                                678                 :                :         }
                                679                 :                :     }
 4288                           680         [ +  + ]:           9877 :     if (IsA(node, SubLink))
                                681                 :                :     {
                                682                 :                :         /*
                                683                 :                :          * If we come across a nested SubLink, it is neither necessary nor
                                684                 :                :          * correct to recurse into it: any PARAM_SUBLINKs we might find inside
                                685                 :                :          * belong to the inner SubLink not the outer. So just return it as-is.
                                686                 :                :          *
                                687                 :                :          * This reasoning depends on the assumption that nothing will pull
                                688                 :                :          * subexpressions into or out of the testexpr field of a SubLink, at
                                689                 :                :          * least not without replacing PARAM_SUBLINKs first.  If we did want
                                690                 :                :          * to do that we'd need to rethink the parser-output representation
                                691                 :                :          * altogether, since currently PARAM_SUBLINKs are only unique per
                                692                 :                :          * SubLink not globally across the query.  The whole point of
                                693                 :                :          * replacing them with Vars or PARAM_EXEC nodes is to make them
                                694                 :                :          * globally unique before they escape from the SubLink's testexpr.
                                695                 :                :          *
                                696                 :                :          * Note: this can't happen when called during SS_process_sublinks,
                                697                 :                :          * because that recursively processes inner SubLinks first.  It can
                                698                 :                :          * happen when called from convert_ANY_sublink_to_join, though.
                                699                 :                :          */
                                700                 :              6 :         return node;
                                701                 :                :     }
  282 peter@eisentraut.org      702                 :           9871 :     return expression_tree_mutator(node, convert_testexpr_mutator, context);
                                703                 :                : }
                                704                 :                : 
                                705                 :                : /*
                                706                 :                :  * subplan_is_hashable: can we implement an ANY subplan by hashing?
                                707                 :                :  *
                                708                 :                :  * This is not responsible for checking whether the combining testexpr
                                709                 :                :  * is suitable for hashing.  We only look at the subquery itself.
                                710                 :                :  */
                                711                 :                : static bool
 6224 tgl@sss.pgh.pa.us         712                 :           1066 : subplan_is_hashable(Plan *plan)
                                713                 :                : {
                                714                 :                :     double      subquery_size;
                                715                 :                : 
                                716                 :                :     /*
                                717                 :                :      * The estimated size of the subquery result must fit in hash_mem. (Note:
                                718                 :                :      * we use heap tuple overhead here even though the tuples will actually be
                                719                 :                :      * stored as MinimalTuples; this provides some fudge factor for hashtable
                                720                 :                :      * overhead.)
                                721                 :                :      */
 6771                           722                 :           1066 :     subquery_size = plan->plan_rows *
 3850                           723                 :           1066 :         (MAXALIGN(plan->plan_width) + MAXALIGN(SizeofHeapTupleHeader));
 1504                           724         [ -  + ]:           1066 :     if (subquery_size > get_hash_memory_limit())
 1805 tgl@sss.pgh.pa.us         725                 :UBC           0 :         return false;
                                726                 :                : 
 1805 tgl@sss.pgh.pa.us         727                 :CBC        1066 :     return true;
                                728                 :                : }
                                729                 :                : 
                                730                 :                : /*
                                731                 :                :  * subpath_is_hashable: can we implement an ANY subplan by hashing?
                                732                 :                :  *
                                733                 :                :  * Identical to subplan_is_hashable, but work from a Path for the subplan.
                                734                 :                :  */
                                735                 :                : static bool
                                736                 :            848 : subpath_is_hashable(Path *path)
                                737                 :                : {
                                738                 :                :     double      subquery_size;
                                739                 :                : 
                                740                 :                :     /*
                                741                 :                :      * The estimated size of the subquery result must fit in hash_mem. (Note:
                                742                 :                :      * we use heap tuple overhead here even though the tuples will actually be
                                743                 :                :      * stored as MinimalTuples; this provides some fudge factor for hashtable
                                744                 :                :      * overhead.)
                                745                 :                :      */
                                746                 :            848 :     subquery_size = path->rows *
                                747                 :            848 :         (MAXALIGN(path->pathtarget->width) + MAXALIGN(SizeofHeapTupleHeader));
 1504                           748         [ +  + ]:            848 :     if (subquery_size > get_hash_memory_limit())
 8275 tgl@sss.pgh.pa.us         749                 :GBC           3 :         return false;
                                750                 :                : 
 6224 tgl@sss.pgh.pa.us         751                 :CBC         845 :     return true;
                                752                 :                : }
                                753                 :                : 
                                754                 :                : /*
                                755                 :                :  * testexpr_is_hashable: is an ANY SubLink's test expression hashable?
                                756                 :                :  *
                                757                 :                :  * To identify LHS vs RHS of the hash expression, we must be given the
                                758                 :                :  * list of output Param IDs of the SubLink's subquery.
                                759                 :                :  */
                                760                 :                : static bool
 1849                           761                 :           1066 : testexpr_is_hashable(Node *testexpr, List *param_ids)
                                762                 :                : {
                                763                 :                :     /*
                                764                 :                :      * The testexpr must be a single OpExpr, or an AND-clause containing only
                                765                 :                :      * OpExprs, each of which satisfy test_opexpr_is_hashable().
                                766                 :                :      */
 6224                           767   [ +  -  +  + ]:           1066 :     if (testexpr && IsA(testexpr, OpExpr))
                                768                 :                :     {
 1849                           769         [ +  + ]:            633 :         if (test_opexpr_is_hashable((OpExpr *) testexpr, param_ids))
 6224                           770                 :            621 :             return true;
                                771                 :                :     }
 2412                           772         [ +  - ]:            433 :     else if (is_andclause(testexpr))
                                773                 :                :     {
                                774                 :                :         ListCell   *l;
                                775                 :                : 
 6224                           776   [ +  -  +  +  :           1299 :         foreach(l, ((BoolExpr *) testexpr)->args)
                                              +  + ]
                                777                 :                :         {
 6912 bruce@momjian.us          778                 :            866 :             Node       *andarg = (Node *) lfirst(l);
                                779                 :                : 
 7192 tgl@sss.pgh.pa.us         780         [ -  + ]:            866 :             if (!IsA(andarg, OpExpr))
 6224 tgl@sss.pgh.pa.us         781                 :UBC           0 :                 return false;
 1849 tgl@sss.pgh.pa.us         782         [ -  + ]:CBC         866 :             if (!test_opexpr_is_hashable((OpExpr *) andarg, param_ids))
 7192 tgl@sss.pgh.pa.us         783                 :UBC           0 :                 return false;
                                784                 :                :         }
 6224 tgl@sss.pgh.pa.us         785                 :CBC         433 :         return true;
                                786                 :                :     }
                                787                 :                : 
                                788                 :             12 :     return false;
                                789                 :                : }
                                790                 :                : 
                                791                 :                : static bool
 1849                           792                 :           1499 : test_opexpr_is_hashable(OpExpr *testexpr, List *param_ids)
                                793                 :                : {
                                794                 :                :     /*
                                795                 :                :      * The combining operator must be hashable and strict.  The need for
                                796                 :                :      * hashability is obvious, since we want to use hashing.  Without
                                797                 :                :      * strictness, behavior in the presence of nulls is too unpredictable.  We
                                798                 :                :      * actually must assume even more than plain strictness: it can't yield
                                799                 :                :      * NULL for non-null inputs, either (see nodeSubplan.c).  However, hash
                                800                 :                :      * indexes and hash joins assume that too.
                                801                 :                :      */
                                802         [ +  + ]:           1499 :     if (!hash_ok_operator(testexpr))
                                803                 :              6 :         return false;
                                804                 :                : 
                                805                 :                :     /*
                                806                 :                :      * The left and right inputs must belong to the outer and inner queries
                                807                 :                :      * respectively; hence Params that will be supplied by the subquery must
                                808                 :                :      * not appear in the LHS, and Vars of the outer query must not appear in
                                809                 :                :      * the RHS.  (Ordinarily, this must be true because of the way that the
                                810                 :                :      * parser builds an ANY SubLink's testexpr ... but inlining of functions
                                811                 :                :      * could have changed the expression's structure, so we have to check.
                                812                 :                :      * Such cases do not occur often enough to be worth trying to optimize, so
                                813                 :                :      * we don't worry about trying to commute the clause or anything like
                                814                 :                :      * that; we just need to be sure not to build an invalid plan.)
                                815                 :                :      */
                                816         [ -  + ]:           1493 :     if (list_length(testexpr->args) != 2)
 1849 tgl@sss.pgh.pa.us         817                 :UBC           0 :         return false;
 1849 tgl@sss.pgh.pa.us         818         [ +  + ]:CBC        1493 :     if (contain_exec_param((Node *) linitial(testexpr->args), param_ids))
                                819                 :              6 :         return false;
                                820         [ -  + ]:           1487 :     if (contain_var_clause((Node *) lsecond(testexpr->args)))
 1849 tgl@sss.pgh.pa.us         821                 :UBC           0 :         return false;
 1849 tgl@sss.pgh.pa.us         822                 :CBC        1487 :     return true;
                                823                 :                : }
                                824                 :                : 
                                825                 :                : /*
                                826                 :                :  * Check expression is hashable + strict
                                827                 :                :  *
                                828                 :                :  * We could use op_hashjoinable() and op_strict(), but do it like this to
                                829                 :                :  * avoid a redundant cache lookup.
                                830                 :                :  */
                                831                 :                : static bool
 7192                           832                 :           4627 : hash_ok_operator(OpExpr *expr)
                                833                 :                : {
                                834                 :           4627 :     Oid         opid = expr->opno;
                                835                 :                : 
                                836                 :                :     /* quick out if not a binary operator */
 6224                           837         [ -  + ]:           4627 :     if (list_length(expr->args) != 2)
 6224 tgl@sss.pgh.pa.us         838                 :UBC           0 :         return false;
 1329 tgl@sss.pgh.pa.us         839   [ +  -  +  + ]:CBC        4627 :     if (opid == ARRAY_EQ_OP ||
                                840                 :                :         opid == RECORD_EQ_OP)
                                841                 :                :     {
                                842                 :                :         /* these are strict, but must check input type to ensure hashable */
 5425                           843                 :              6 :         Node       *leftarg = linitial(expr->args);
                                844                 :                : 
                                845                 :              6 :         return op_hashjoinable(opid, exprType(leftarg));
                                846                 :                :     }
                                847                 :                :     else
                                848                 :                :     {
                                849                 :                :         /* else must look up the operator properties */
                                850                 :                :         HeapTuple   tup;
                                851                 :                :         Form_pg_operator optup;
                                852                 :                : 
                                853                 :           4621 :         tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(opid));
                                854         [ -  + ]:           4621 :         if (!HeapTupleIsValid(tup))
 5425 tgl@sss.pgh.pa.us         855         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for operator %u", opid);
 5425 tgl@sss.pgh.pa.us         856                 :CBC        4621 :         optup = (Form_pg_operator) GETSTRUCT(tup);
                                857   [ +  +  -  + ]:           4621 :         if (!optup->oprcanhash || !func_strict(optup->oprcode))
                                858                 :                :         {
                                859                 :            421 :             ReleaseSysCache(tup);
                                860                 :            421 :             return false;
                                861                 :                :         }
 8275                           862                 :           4200 :         ReleaseSysCache(tup);
 5425                           863                 :           4200 :         return true;
                                864                 :                :     }
                                865                 :                : }
                                866                 :                : 
                                867                 :                : 
                                868                 :                : /*
                                869                 :                :  * SS_process_ctes: process a query's WITH list
                                870                 :                :  *
                                871                 :                :  * Consider each CTE in the WITH list and either ignore it (if it's an
                                872                 :                :  * unreferenced SELECT), "inline" it to create a regular sub-SELECT-in-FROM,
                                873                 :                :  * or convert it to an initplan.
                                874                 :                :  *
                                875                 :                :  * A side effect is to fill in root->cte_plan_ids with a list that
                                876                 :                :  * parallels root->parse->cteList and provides the subplan ID for
                                877                 :                :  * each CTE's initplan, or a dummy ID (-1) if we didn't make an initplan.
                                878                 :                :  */
                                879                 :                : void
 6181                           880                 :           1431 : SS_process_ctes(PlannerInfo *root)
                                881                 :                : {
                                882                 :                :     ListCell   *lc;
                                883                 :                : 
                                884         [ -  + ]:           1431 :     Assert(root->cte_plan_ids == NIL);
                                885                 :                : 
                                886   [ +  -  +  +  :           3450 :     foreach(lc, root->parse->cteList)
                                              +  + ]
                                887                 :                :     {
                                888                 :           2022 :         CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
 5307                           889                 :           2022 :         CmdType     cmdType = ((Query *) cte->ctequery)->commandType;
                                890                 :                :         Query      *subquery;
                                891                 :                :         PlannerInfo *subroot;
                                892                 :                :         RelOptInfo *final_rel;
                                893                 :                :         Path       *best_path;
                                894                 :                :         Plan       *plan;
                                895                 :                :         SubPlan    *splan;
                                896                 :                :         int         paramid;
                                897                 :                : 
                                898                 :                :         /*
                                899                 :                :          * Ignore SELECT CTEs that are not actually referenced anywhere.
                                900                 :                :          */
                                901   [ +  +  +  + ]:           2022 :         if (cte->cterefcount == 0 && cmdType == CMD_SELECT)
                                902                 :                :         {
                                903                 :                :             /* Make a dummy entry in cte_plan_ids */
 6181                           904                 :             26 :             root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
                                905                 :            776 :             continue;
                                906                 :                :         }
                                907                 :                : 
                                908                 :                :         /*
                                909                 :                :          * Consider inlining the CTE (creating RTE_SUBQUERY RTE(s)) instead of
                                910                 :                :          * implementing it as a separately-planned CTE.
                                911                 :                :          *
                                912                 :                :          * We cannot inline if any of these conditions hold:
                                913                 :                :          *
                                914                 :                :          * 1. The user said not to (the CTEMaterializeAlways option).
                                915                 :                :          *
                                916                 :                :          * 2. The CTE is recursive.
                                917                 :                :          *
                                918                 :                :          * 3. The CTE has side-effects; this includes either not being a plain
                                919                 :                :          * SELECT, or containing volatile functions.  Inlining might change
                                920                 :                :          * the side-effects, which would be bad.
                                921                 :                :          *
                                922                 :                :          * 4. The CTE is multiply-referenced and contains a self-reference to
                                923                 :                :          * a recursive CTE outside itself.  Inlining would result in multiple
                                924                 :                :          * recursive self-references, which we don't support.
                                925                 :                :          *
                                926                 :                :          * Otherwise, we have an option whether to inline or not.  That should
                                927                 :                :          * always be a win if there's just a single reference, but if the CTE
                                928                 :                :          * is multiply-referenced then it's unclear: inlining adds duplicate
                                929                 :                :          * computations, but the ability to absorb restrictions from the outer
                                930                 :                :          * query level could outweigh that.  We do not have nearly enough
                                931                 :                :          * information at this point to tell whether that's true, so we let
                                932                 :                :          * the user express a preference.  Our default behavior is to inline
                                933                 :                :          * only singly-referenced CTEs, but a CTE marked CTEMaterializeNever
                                934                 :                :          * will be inlined even if multiply referenced.
                                935                 :                :          *
                                936                 :                :          * Note: we check for volatile functions last, because that's more
                                937                 :                :          * expensive than the other tests needed.
                                938                 :                :          */
 2394                           939         [ +  + ]:           1996 :         if ((cte->ctematerialized == CTEMaterializeNever ||
                                940         [ +  + ]:           1972 :              (cte->ctematerialized == CTEMaterializeDefault &&
                                941         [ +  + ]:           1871 :               cte->cterefcount == 1)) &&
                                942   [ +  +  +  + ]:           1364 :             !cte->cterecursive &&
                                943                 :            796 :             cmdType == CMD_SELECT &&
                                944         [ +  + ]:            796 :             !contain_dml(cte->ctequery) &&
 2342                           945         [ +  + ]:            792 :             (cte->cterefcount <= 1 ||
                                946         [ +  + ]:             18 :              !contain_outer_selfref(cte->ctequery)) &&
 2394                           947         [ +  + ]:            786 :             !contain_volatile_functions(cte->ctequery))
                                948                 :                :         {
                                949                 :            750 :             inline_cte(root, cte);
                                950                 :                :             /* Make a dummy entry in cte_plan_ids */
                                951                 :            750 :             root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
                                952                 :            750 :             continue;
                                953                 :                :         }
                                954                 :                : 
                                955                 :                :         /*
                                956                 :                :          * Copy the source Query node.  Probably not necessary, but let's keep
                                957                 :                :          * this similar to make_subplan.
                                958                 :                :          */
 6181                           959                 :           1246 :         subquery = (Query *) copyObject(cte->ctequery);
                                960                 :                : 
                                961                 :                :         /* plan_params should not be in use in current query level */
 4749                           962         [ -  + ]:           1246 :         Assert(root->plan_params == NIL);
                                963                 :                : 
                                964                 :                :         /*
                                965                 :                :          * Generate Paths for the CTE query.  Always plan for full retrieval
                                966                 :                :          * --- we don't have enough info to predict otherwise.
                                967                 :                :          */
  473 rhaas@postgresql.org      968                 :           1246 :         subroot = subquery_planner(root->glob, subquery, root,
                                969                 :           1246 :                                    cte->cterecursive, 0.0, NULL);
                                970                 :                : 
                                971                 :                :         /*
                                972                 :                :          * Since the current query level doesn't yet contain any RTEs, it
                                973                 :                :          * should not be possible for the CTE to have requested parameters of
                                974                 :                :          * this level.
                                975                 :                :          */
 4749 tgl@sss.pgh.pa.us         976         [ -  + ]:           1243 :         if (root->plan_params)
 4749 tgl@sss.pgh.pa.us         977         [ #  # ]:UBC           0 :             elog(ERROR, "unexpected outer reference in CTE query");
                                978                 :                : 
                                979                 :                :         /*
                                980                 :                :          * Select best Path and turn it into a Plan.  At least for now, there
                                981                 :                :          * seems no reason to postpone doing that.
                                982                 :                :          */
 3470 tgl@sss.pgh.pa.us         983                 :CBC        1243 :         final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
                                984                 :           1243 :         best_path = final_rel->cheapest_total_path;
                                985                 :                : 
                                986                 :           1243 :         plan = create_plan(subroot, best_path);
                                987                 :                : 
                                988                 :                :         /*
                                989                 :                :          * Make a SubPlan node for it.  This is just enough unlike
                                990                 :                :          * build_subplan that we can't share code.
                                991                 :                :          *
                                992                 :                :          * Note plan_id, plan_name, and cost fields are set further down.
                                993                 :                :          */
 6181                           994                 :           1243 :         splan = makeNode(SubPlan);
                                995                 :           1243 :         splan->subLinkType = CTE_SUBLINK;
                                996                 :           1243 :         splan->testexpr = NULL;
                                997                 :           1243 :         splan->paramIds = NIL;
 5251                           998                 :           1243 :         get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
                                999                 :                :                            &splan->firstColCollation);
 6181                          1000                 :           1243 :         splan->useHashTable = false;
                               1001                 :           1243 :         splan->unknownEqFalse = false;
                               1002                 :                : 
                               1003                 :                :         /*
                               1004                 :                :          * CTE scans are not considered for parallelism (cf
                               1005                 :                :          * set_rel_consider_parallel).
                               1006                 :                :          */
 3126 rhaas@postgresql.org     1007                 :           1243 :         splan->parallel_safe = false;
 6181 tgl@sss.pgh.pa.us        1008                 :           1243 :         splan->setParam = NIL;
                               1009                 :           1243 :         splan->parParam = NIL;
                               1010                 :           1243 :         splan->args = NIL;
                               1011                 :                : 
                               1012                 :                :         /*
                               1013                 :                :          * The node can't have any inputs (since it's an initplan), so the
                               1014                 :                :          * parParam and args lists remain empty.  (It could contain references
                               1015                 :                :          * to earlier CTEs' output param IDs, but CTE outputs are not
                               1016                 :                :          * propagated via the args list.)
                               1017                 :                :          */
                               1018                 :                : 
                               1019                 :                :         /*
                               1020                 :                :          * Assign a param ID to represent the CTE's output.  No ordinary
                               1021                 :                :          * "evaluation" of this param slot ever happens, but we use the param
                               1022                 :                :          * ID for setParam/chgParam signaling just as if the CTE plan were
                               1023                 :                :          * returning a simple scalar output.  (Also, the executor abuses the
                               1024                 :                :          * ParamExecData slot for this param ID for communication among
                               1025                 :                :          * multiple CteScan nodes that might be scanning this CTE.)
                               1026                 :                :          */
 2430                          1027                 :           1243 :         paramid = assign_special_exec_param(root);
 4749                          1028                 :           1243 :         splan->setParam = list_make1_int(paramid);
                               1029                 :                : 
                               1030                 :                :         /*
                               1031                 :                :          * Add the subplan, its path, and its PlannerInfo to the global lists.
                               1032                 :                :          */
 6181                          1033                 :           1243 :         root->glob->subplans = lappend(root->glob->subplans, plan);
  529                          1034                 :           1243 :         root->glob->subpaths = lappend(root->glob->subpaths, best_path);
 5117                          1035                 :           1243 :         root->glob->subroots = lappend(root->glob->subroots, subroot);
 6181                          1036                 :           1243 :         splan->plan_id = list_length(root->glob->subplans);
                               1037                 :                : 
                               1038                 :           1243 :         root->init_plans = lappend(root->init_plans, splan);
                               1039                 :                : 
                               1040                 :           1243 :         root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
                               1041                 :                : 
                               1042                 :                :         /* Label the subplan for EXPLAIN purposes */
 4346 peter_e@gmx.net          1043                 :           1243 :         splan->plan_name = psprintf("CTE %s", cte->ctename);
                               1044                 :                : 
                               1045                 :                :         /* Lastly, fill in the cost estimates for use later */
 6181 tgl@sss.pgh.pa.us        1046                 :           1243 :         cost_subplan(root, splan, plan);
                               1047                 :                :     }
                               1048                 :           1428 : }
                               1049                 :                : 
                               1050                 :                : /*
                               1051                 :                :  * contain_dml: is any subquery not a plain SELECT?
                               1052                 :                :  *
                               1053                 :                :  * We reject SELECT FOR UPDATE/SHARE as well as INSERT etc.
                               1054                 :                :  */
                               1055                 :                : static bool
 2394                          1056                 :            796 : contain_dml(Node *node)
                               1057                 :                : {
                               1058                 :            796 :     return contain_dml_walker(node, NULL);
                               1059                 :                : }
                               1060                 :                : 
                               1061                 :                : static bool
                               1062                 :          53598 : contain_dml_walker(Node *node, void *context)
                               1063                 :                : {
                               1064         [ +  + ]:          53598 :     if (node == NULL)
                               1065                 :          18656 :         return false;
                               1066         [ +  + ]:          34942 :     if (IsA(node, Query))
                               1067                 :                :     {
                               1068                 :           1470 :         Query      *query = (Query *) node;
                               1069                 :                : 
                               1070         [ +  - ]:           1470 :         if (query->commandType != CMD_SELECT ||
                               1071         [ +  + ]:           1470 :             query->rowMarks != NIL)
                               1072                 :              4 :             return true;
                               1073                 :                : 
                               1074                 :           1466 :         return query_tree_walker(query, contain_dml_walker, context, 0);
                               1075                 :                :     }
                               1076                 :          33472 :     return expression_tree_walker(node, contain_dml_walker, context);
                               1077                 :                : }
                               1078                 :                : 
                               1079                 :                : /*
                               1080                 :                :  * contain_outer_selfref: is there an external recursive self-reference?
                               1081                 :                :  */
                               1082                 :                : static bool
 2342                          1083                 :             18 : contain_outer_selfref(Node *node)
                               1084                 :                : {
                               1085                 :             18 :     Index       depth = 0;
                               1086                 :                : 
                               1087                 :                :     /*
                               1088                 :                :      * We should be starting with a Query, so that depth will be 1 while
                               1089                 :                :      * examining its immediate contents.
                               1090                 :                :      */
                               1091         [ -  + ]:             18 :     Assert(IsA(node, Query));
                               1092                 :                : 
                               1093                 :             18 :     return contain_outer_selfref_walker(node, &depth);
                               1094                 :                : }
                               1095                 :                : 
                               1096                 :                : static bool
                               1097                 :            405 : contain_outer_selfref_walker(Node *node, Index *depth)
                               1098                 :                : {
                               1099         [ +  + ]:            405 :     if (node == NULL)
                               1100                 :            243 :         return false;
                               1101         [ +  + ]:            162 :     if (IsA(node, RangeTblEntry))
                               1102                 :                :     {
                               1103                 :             15 :         RangeTblEntry *rte = (RangeTblEntry *) node;
                               1104                 :                : 
                               1105                 :                :         /*
                               1106                 :                :          * Check for a self-reference to a CTE that's above the Query that our
                               1107                 :                :          * search started at.
                               1108                 :                :          */
                               1109         [ +  + ]:             15 :         if (rte->rtekind == RTE_CTE &&
                               1110         [ +  - ]:              6 :             rte->self_reference &&
                               1111         [ +  - ]:              6 :             rte->ctelevelsup >= *depth)
                               1112                 :              6 :             return true;
                               1113                 :              9 :         return false;           /* allow range_table_walker to continue */
                               1114                 :                :     }
                               1115         [ +  + ]:            147 :     if (IsA(node, Query))
                               1116                 :                :     {
                               1117                 :                :         /* Recurse into subquery, tracking nesting depth properly */
                               1118                 :             21 :         Query      *query = (Query *) node;
                               1119                 :                :         bool        result;
                               1120                 :                : 
                               1121                 :             21 :         (*depth)++;
                               1122                 :                : 
                               1123                 :             21 :         result = query_tree_walker(query, contain_outer_selfref_walker,
                               1124                 :                :                                    depth, QTW_EXAMINE_RTES_BEFORE);
                               1125                 :                : 
                               1126                 :             21 :         (*depth)--;
                               1127                 :                : 
                               1128                 :             21 :         return result;
                               1129                 :                :     }
  282 peter@eisentraut.org     1130                 :            126 :     return expression_tree_walker(node, contain_outer_selfref_walker, depth);
                               1131                 :                : }
                               1132                 :                : 
                               1133                 :                : /*
                               1134                 :                :  * inline_cte: convert RTE_CTE references to given CTE into RTE_SUBQUERYs
                               1135                 :                :  */
                               1136                 :                : static void
 2394 tgl@sss.pgh.pa.us        1137                 :            750 : inline_cte(PlannerInfo *root, CommonTableExpr *cte)
                               1138                 :                : {
                               1139                 :                :     struct inline_cte_walker_context context;
                               1140                 :                : 
                               1141                 :            750 :     context.ctename = cte->ctename;
                               1142                 :                :     /* Start at levelsup = -1 because we'll immediately increment it */
                               1143                 :            750 :     context.levelsup = -1;
                               1144                 :            750 :     context.ctequery = castNode(Query, cte->ctequery);
                               1145                 :                : 
                               1146                 :            750 :     (void) inline_cte_walker((Node *) root->parse, &context);
                               1147                 :            750 : }
                               1148                 :                : 
                               1149                 :                : static bool
                               1150                 :         275949 : inline_cte_walker(Node *node, inline_cte_walker_context *context)
                               1151                 :                : {
                               1152         [ +  + ]:         275949 :     if (node == NULL)
                               1153                 :          74408 :         return false;
                               1154         [ +  + ]:         201541 :     if (IsA(node, Query))
                               1155                 :                :     {
                               1156                 :           5450 :         Query      *query = (Query *) node;
                               1157                 :                : 
                               1158                 :           5450 :         context->levelsup++;
                               1159                 :                : 
                               1160                 :                :         /*
                               1161                 :                :          * Visit the query's RTE nodes after their contents; otherwise
                               1162                 :                :          * query_tree_walker would descend into the newly inlined CTE query,
                               1163                 :                :          * which we don't want.
                               1164                 :                :          */
                               1165                 :           5450 :         (void) query_tree_walker(query, inline_cte_walker, context,
                               1166                 :                :                                  QTW_EXAMINE_RTES_AFTER);
                               1167                 :                : 
                               1168                 :           5450 :         context->levelsup--;
                               1169                 :                : 
                               1170                 :           5450 :         return false;
                               1171                 :                :     }
                               1172         [ +  + ]:         196091 :     else if (IsA(node, RangeTblEntry))
                               1173                 :                :     {
                               1174                 :          10249 :         RangeTblEntry *rte = (RangeTblEntry *) node;
                               1175                 :                : 
                               1176         [ +  + ]:          10249 :         if (rte->rtekind == RTE_CTE &&
                               1177         [ +  + ]:           3011 :             strcmp(rte->ctename, context->ctename) == 0 &&
                               1178         [ +  + ]:            765 :             rte->ctelevelsup == context->levelsup)
                               1179                 :                :         {
                               1180                 :                :             /*
                               1181                 :                :              * Found a reference to replace.  Generate a copy of the CTE query
                               1182                 :                :              * with appropriate level adjustment for outer references (e.g.,
                               1183                 :                :              * to other CTEs).
                               1184                 :                :              */
                               1185                 :            762 :             Query      *newquery = copyObject(context->ctequery);
                               1186                 :                : 
                               1187         [ +  + ]:            762 :             if (context->levelsup > 0)
                               1188                 :            481 :                 IncrementVarSublevelsUp((Node *) newquery, context->levelsup, 1);
                               1189                 :                : 
                               1190                 :                :             /*
                               1191                 :                :              * Convert the RTE_CTE RTE into a RTE_SUBQUERY.
                               1192                 :                :              *
                               1193                 :                :              * Historically, a FOR UPDATE clause has been treated as extending
                               1194                 :                :              * into views and subqueries, but not into CTEs.  We preserve this
                               1195                 :                :              * distinction by not trying to push rowmarks into the new
                               1196                 :                :              * subquery.
                               1197                 :                :              */
                               1198                 :            762 :             rte->rtekind = RTE_SUBQUERY;
                               1199                 :            762 :             rte->subquery = newquery;
                               1200                 :            762 :             rte->security_barrier = false;
                               1201                 :                : 
                               1202                 :                :             /* Zero out CTE-specific fields */
                               1203                 :            762 :             rte->ctename = NULL;
                               1204                 :            762 :             rte->ctelevelsup = 0;
                               1205                 :            762 :             rte->self_reference = false;
                               1206                 :            762 :             rte->coltypes = NIL;
                               1207                 :            762 :             rte->coltypmods = NIL;
                               1208                 :            762 :             rte->colcollations = NIL;
                               1209                 :                :         }
                               1210                 :                : 
                               1211                 :          10249 :         return false;
                               1212                 :                :     }
                               1213                 :                : 
                               1214                 :         185842 :     return expression_tree_walker(node, inline_cte_walker, context);
                               1215                 :                : }
                               1216                 :                : 
                               1217                 :                : /*
                               1218                 :                :  * Attempt to transform 'testexpr' over the VALUES subquery into
                               1219                 :                :  * a ScalarArrayOpExpr.  We currently support the transformation only when
                               1220                 :                :  * it ends up with a constant array.  Otherwise, the evaluation of non-hashed
                               1221                 :                :  * SAOP might be slower than the corresponding Hash Join with VALUES.
                               1222                 :                :  *
                               1223                 :                :  * Return transformed ScalarArrayOpExpr or NULL if transformation isn't
                               1224                 :                :  * allowed.
                               1225                 :                :  */
                               1226                 :                : ScalarArrayOpExpr *
  155 akorotkov@postgresql     1227                 :           2303 : convert_VALUES_to_ANY(PlannerInfo *root, Node *testexpr, Query *values)
                               1228                 :                : {
                               1229                 :                :     RangeTblEntry *rte;
                               1230                 :                :     Node       *leftop;
                               1231                 :                :     Node       *rightop;
                               1232                 :                :     Oid         opno;
                               1233                 :                :     ListCell   *lc;
                               1234                 :                :     Oid         inputcollid;
                               1235                 :           2303 :     List       *exprs = NIL;
                               1236                 :                : 
                               1237                 :                :     /*
                               1238                 :                :      * Check we have a binary operator over a single-column subquery with no
                               1239                 :                :      * joins and no LIMIT/OFFSET/ORDER BY clauses.
                               1240                 :                :      */
                               1241   [ +  +  +  - ]:           4553 :     if (!IsA(testexpr, OpExpr) ||
                               1242         [ +  - ]:           4500 :         list_length(((OpExpr *) testexpr)->args) != 2 ||
                               1243                 :           2250 :         list_length(values->targetList) > 1 ||
                               1244         [ +  + ]:           2250 :         values->limitCount != NULL ||
                               1245         [ +  + ]:           2244 :         values->limitOffset != NULL ||
                               1246   [ +  +  +  + ]:           4461 :         values->sortClause != NIL ||
                               1247                 :           2229 :         list_length(values->rtable) != 1)
                               1248                 :           1847 :         return NULL;
                               1249                 :                : 
                               1250                 :            456 :     rte = linitial_node(RangeTblEntry, values->rtable);
                               1251                 :            456 :     leftop = linitial(((OpExpr *) testexpr)->args);
                               1252                 :            456 :     rightop = lsecond(((OpExpr *) testexpr)->args);
                               1253                 :            456 :     opno = ((OpExpr *) testexpr)->opno;
                               1254                 :            456 :     inputcollid = ((OpExpr *) testexpr)->inputcollid;
                               1255                 :                : 
                               1256                 :                :     /*
                               1257                 :                :      * Also, check that only RTE corresponds to VALUES; the list of values has
                               1258                 :                :      * at least two items and no volatile functions.
                               1259                 :                :      */
                               1260   [ +  +  +  + ]:            522 :     if (rte->rtekind != RTE_VALUES ||
                               1261         [ -  + ]:            126 :         list_length(rte->values_lists) < 2 ||
                               1262                 :             60 :         contain_volatile_functions((Node *) rte->values_lists))
                               1263                 :            396 :         return NULL;
                               1264                 :                : 
                               1265   [ +  -  +  +  :            180 :     foreach(lc, rte->values_lists)
                                              +  + ]
                               1266                 :                :     {
                               1267                 :            138 :         List       *elem = lfirst(lc);
                               1268                 :            138 :         Node       *value = linitial(elem);
                               1269                 :                : 
                               1270                 :                :         /*
                               1271                 :                :          * Prepare an evaluation of the right side of the operator with
                               1272                 :                :          * substitution of the given value.
                               1273                 :                :          */
                               1274                 :            138 :         value = convert_testexpr(root, rightop, list_make1(value));
                               1275                 :                : 
                               1276                 :                :         /*
                               1277                 :                :          * Try to evaluate constant expressions.  We could get Const as a
                               1278                 :                :          * result.
                               1279                 :                :          */
                               1280                 :            138 :         value = eval_const_expressions(root, value);
                               1281                 :                : 
                               1282                 :                :         /*
                               1283                 :                :          * As we only support constant output arrays, all the items must also
                               1284                 :                :          * be constant.
                               1285                 :                :          */
                               1286         [ +  + ]:            138 :         if (!IsA(value, Const))
                               1287                 :             18 :             return NULL;
                               1288                 :                : 
                               1289                 :            120 :         exprs = lappend(exprs, value);
                               1290                 :                :     }
                               1291                 :                : 
                               1292                 :                :     /* Finally, build ScalarArrayOpExpr at the top of the 'exprs' list. */
                               1293                 :             42 :     return make_SAOP_expr(opno, leftop, exprType(rightop),
                               1294                 :             42 :                           linitial_oid(rte->colcollations), inputcollid,
                               1295                 :                :                           exprs, false);
                               1296                 :                : }
                               1297                 :                : 
                               1298                 :                : /*
                               1299                 :                :  * convert_ANY_sublink_to_join: try to convert an ANY SubLink to a join
                               1300                 :                :  *
                               1301                 :                :  * The caller has found an ANY SubLink at the top level of one of the query's
                               1302                 :                :  * qual clauses, but has not checked the properties of the SubLink further.
                               1303                 :                :  * Decide whether it is appropriate to process this SubLink in join style.
                               1304                 :                :  * If so, form a JoinExpr and return it.  Return NULL if the SubLink cannot
                               1305                 :                :  * be converted to a join.
                               1306                 :                :  *
                               1307                 :                :  * The only non-obvious input parameter is available_rels: this is the set
                               1308                 :                :  * of query rels that can safely be referenced in the sublink expression.
                               1309                 :                :  * (We must restrict this to avoid changing the semantics when a sublink
                               1310                 :                :  * is present in an outer join's ON qual.)  The conversion must fail if
                               1311                 :                :  * the converted qual would reference any but these parent-query relids.
                               1312                 :                :  *
                               1313                 :                :  * On success, the returned JoinExpr has larg = NULL and rarg = the jointree
                               1314                 :                :  * item representing the pulled-up subquery.  The caller must set larg to
                               1315                 :                :  * represent the relation(s) on the lefthand side of the new join, and insert
                               1316                 :                :  * the JoinExpr into the upper query's jointree at an appropriate place
                               1317                 :                :  * (typically, where the lefthand relation(s) had been).  Note that the
                               1318                 :                :  * passed-in SubLink must also be removed from its original position in the
                               1319                 :                :  * query quals, since the quals of the returned JoinExpr replace it.
                               1320                 :                :  * (Notionally, we replace the SubLink with a constant TRUE, then elide the
                               1321                 :                :  * redundant constant from the qual.)
                               1322                 :                :  *
                               1323                 :                :  * On success, the caller is also responsible for recursively applying
                               1324                 :                :  * pull_up_sublinks processing to the rarg and quals of the returned JoinExpr.
                               1325                 :                :  * (On failure, there is no need to do anything, since pull_up_sublinks will
                               1326                 :                :  * be applied when we recursively plan the sub-select.)
                               1327                 :                :  *
                               1328                 :                :  * Side effects of a successful conversion include adding the SubLink's
                               1329                 :                :  * subselect to the query's rangetable, so that it can be referenced in
                               1330                 :                :  * the JoinExpr's rarg.
                               1331                 :                :  */
                               1332                 :                : JoinExpr *
 6229 tgl@sss.pgh.pa.us        1333                 :           2264 : convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
                               1334                 :                :                             Relids available_rels)
                               1335                 :                : {
                               1336                 :                :     JoinExpr   *result;
 7398                          1337                 :           2264 :     Query      *parse = root->parse;
 8265                          1338                 :           2264 :     Query      *subselect = (Query *) sublink->subselect;
                               1339                 :                :     Relids      upper_varnos;
                               1340                 :                :     int         rtindex;
                               1341                 :                :     ParseNamespaceItem *nsitem;
                               1342                 :                :     RangeTblEntry *rte;
                               1343                 :                :     RangeTblRef *rtr;
                               1344                 :                :     List       *subquery_vars;
                               1345                 :                :     Node       *quals;
                               1346                 :                :     ParseState *pstate;
                               1347                 :                :     Relids      sub_ref_outer_relids;
                               1348                 :                :     bool        use_lateral;
                               1349                 :                : 
 6232                          1350         [ -  + ]:           2264 :     Assert(sublink->subLinkType == ANY_SUBLINK);
                               1351                 :                : 
                               1352                 :                :     /*
                               1353                 :                :      * If the sub-select contains any Vars of the parent query, we treat it as
                               1354                 :                :      * LATERAL.  (Vars from higher levels don't matter here.)
                               1355                 :                :      */
  569 akorotkov@postgresql     1356                 :           2264 :     sub_ref_outer_relids = pull_varnos_of_level(NULL, (Node *) subselect, 1);
                               1357                 :           2264 :     use_lateral = !bms_is_empty(sub_ref_outer_relids);
                               1358                 :                : 
                               1359                 :                :     /*
                               1360                 :                :      * Can't convert if the sub-select contains parent-level Vars of relations
                               1361                 :                :      * not in available_rels.
                               1362                 :                :      */
                               1363         [ +  + ]:           2264 :     if (!bms_is_subset(sub_ref_outer_relids, available_rels))
 6037 tgl@sss.pgh.pa.us        1364                 :              6 :         return NULL;
                               1365                 :                : 
                               1366                 :                :     /*
                               1367                 :                :      * The test expression must contain some Vars of the parent query, else
                               1368                 :                :      * it's not gonna be a join.  (Note that it won't have Vars referring to
                               1369                 :                :      * the subquery, rather Params.)
                               1370                 :                :      */
 1689                          1371                 :           2258 :     upper_varnos = pull_varnos(root, sublink->testexpr);
 6037                          1372         [ +  + ]:           2258 :     if (bms_is_empty(upper_varnos))
                               1373                 :              6 :         return NULL;
                               1374                 :                : 
                               1375                 :                :     /*
                               1376                 :                :      * However, it can't refer to anything outside available_rels.
                               1377                 :                :      */
                               1378         [ +  + ]:           2252 :     if (!bms_is_subset(upper_varnos, available_rels))
                               1379                 :             12 :         return NULL;
                               1380                 :                : 
                               1381                 :                :     /*
                               1382                 :                :      * The combining operators and left-hand expressions mustn't be volatile.
                               1383                 :                :      */
 7192                          1384         [ +  + ]:           2240 :     if (contain_volatile_functions(sublink->testexpr))
 6037                          1385                 :             32 :         return NULL;
                               1386                 :                : 
                               1387                 :                :     /* Create a dummy ParseState for addRangeTableEntryForSubquery */
 3832 rhaas@postgresql.org     1388                 :           2208 :     pstate = make_parsestate(NULL);
                               1389                 :                : 
                               1390                 :                :     /*
                               1391                 :                :      * Okay, pull up the sub-select into upper range table.
                               1392                 :                :      *
                               1393                 :                :      * We rely here on the assumption that the outer query has no references
                               1394                 :                :      * to the inner (necessarily true, other than the Vars that we build
                               1395                 :                :      * below). Therefore this is a lot easier than what pull_up_subqueries has
                               1396                 :                :      * to go through.
                               1397                 :                :      */
 2074 tgl@sss.pgh.pa.us        1398                 :           2208 :     nsitem = addRangeTableEntryForSubquery(pstate,
                               1399                 :                :                                            subselect,
                               1400                 :                :                                            makeAlias("ANY_subquery", NIL),
                               1401                 :                :                                            use_lateral,
                               1402                 :                :                                            false);
                               1403                 :           2208 :     rte = nsitem->p_rte;
 8265                          1404                 :           2208 :     parse->rtable = lappend(parse->rtable, rte);
 7769 neilc@samurai.com        1405                 :           2208 :     rtindex = list_length(parse->rtable);
                               1406                 :                : 
                               1407                 :                :     /*
                               1408                 :                :      * Form a RangeTblRef for the pulled-up sub-select.
                               1409                 :                :      */
 6229 tgl@sss.pgh.pa.us        1410                 :           2208 :     rtr = makeNode(RangeTblRef);
                               1411                 :           2208 :     rtr->rtindex = rtindex;
                               1412                 :                : 
                               1413                 :                :     /*
                               1414                 :                :      * Build a list of Vars representing the subselect outputs.
                               1415                 :                :      */
 6347                          1416                 :           2208 :     subquery_vars = generate_subquery_vars(root,
                               1417                 :                :                                            subselect->targetList,
                               1418                 :                :                                            rtindex);
                               1419                 :                : 
                               1420                 :                :     /*
                               1421                 :                :      * Build the new join's qual expression, replacing Params with these Vars.
                               1422                 :                :      */
 6037                          1423                 :           2208 :     quals = convert_testexpr(root, sublink->testexpr, subquery_vars);
                               1424                 :                : 
                               1425                 :                :     /*
                               1426                 :                :      * And finally, build the JoinExpr node.
                               1427                 :                :      */
                               1428                 :           2208 :     result = makeNode(JoinExpr);
                               1429                 :           2208 :     result->jointype = JOIN_SEMI;
                               1430                 :           2208 :     result->isNatural = false;
                               1431                 :           2208 :     result->larg = NULL;     /* caller must fill this in */
                               1432                 :           2208 :     result->rarg = (Node *) rtr;
 5896 peter_e@gmx.net          1433                 :           2208 :     result->usingClause = NIL;
 1620 peter@eisentraut.org     1434                 :           2208 :     result->join_using_alias = NULL;
 6037 tgl@sss.pgh.pa.us        1435                 :           2208 :     result->quals = quals;
                               1436                 :           2208 :     result->alias = NULL;
                               1437                 :           2208 :     result->rtindex = 0;     /* we don't need an RTE for it */
                               1438                 :                : 
                               1439                 :           2208 :     return result;
                               1440                 :                : }
                               1441                 :                : 
                               1442                 :                : /*
                               1443                 :                :  * convert_EXISTS_sublink_to_join: try to convert an EXISTS SubLink to a join
                               1444                 :                :  *
                               1445                 :                :  * The API of this function is identical to convert_ANY_sublink_to_join's,
                               1446                 :                :  * except that we also support the case where the caller has found NOT EXISTS,
                               1447                 :                :  * so we need an additional input parameter "under_not".
                               1448                 :                :  */
                               1449                 :                : JoinExpr *
 6232                          1450                 :           1848 : convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
                               1451                 :                :                                bool under_not, Relids available_rels)
                               1452                 :                : {
                               1453                 :                :     JoinExpr   *result;
                               1454                 :           1848 :     Query      *parse = root->parse;
                               1455                 :           1848 :     Query      *subselect = (Query *) sublink->subselect;
                               1456                 :                :     Node       *whereClause;
                               1457                 :                :     PlannerInfo subroot;
                               1458                 :                :     int         rtoffset;
                               1459                 :                :     int         varno;
                               1460                 :                :     Relids      clause_varnos;
                               1461                 :                :     Relids      upper_varnos;
                               1462                 :                : 
                               1463         [ -  + ]:           1848 :     Assert(sublink->subLinkType == EXISTS_SUBLINK);
                               1464                 :                : 
                               1465                 :                :     /*
                               1466                 :                :      * Can't flatten if it contains WITH.  (We could arrange to pull up the
                               1467                 :                :      * WITH into the parent query's cteList, but that risks changing the
                               1468                 :                :      * semantics, since a WITH ought to be executed once per associated query
                               1469                 :                :      * call.)  Note that convert_ANY_sublink_to_join doesn't have to reject
                               1470                 :                :      * this case, since it just produces a subquery RTE that doesn't have to
                               1471                 :                :      * get flattened into the parent query.
                               1472                 :                :      */
 5710                          1473         [ -  + ]:           1848 :     if (subselect->cteList)
 5710 tgl@sss.pgh.pa.us        1474                 :UBC           0 :         return NULL;
                               1475                 :                : 
                               1476                 :                :     /*
                               1477                 :                :      * Copy the subquery so we can modify it safely (see comments in
                               1478                 :                :      * make_subplan).
                               1479                 :                :      */
 3103 peter_e@gmx.net          1480                 :CBC        1848 :     subselect = copyObject(subselect);
                               1481                 :                : 
                               1482                 :                :     /*
                               1483                 :                :      * See if the subquery can be simplified based on the knowledge that it's
                               1484                 :                :      * being used in EXISTS().  If we aren't able to get rid of its
                               1485                 :                :      * targetlist, we have to fail, because the pullup operation leaves us
                               1486                 :                :      * with noplace to evaluate the targetlist.
                               1487                 :                :      */
 3941 tgl@sss.pgh.pa.us        1488         [ +  + ]:           1848 :     if (!simplify_EXISTS_query(root, subselect))
 6037                          1489                 :             16 :         return NULL;
                               1490                 :                : 
                               1491                 :                :     /*
                               1492                 :                :      * Separate out the WHERE clause.  (We could theoretically also remove
                               1493                 :                :      * top-level plain JOIN/ON clauses, but it's probably not worth the
                               1494                 :                :      * trouble.)
                               1495                 :                :      */
 6232                          1496                 :           1832 :     whereClause = subselect->jointree->quals;
                               1497                 :           1832 :     subselect->jointree->quals = NULL;
                               1498                 :                : 
                               1499                 :                :     /*
                               1500                 :                :      * The rest of the sub-select must not refer to any Vars of the parent
                               1501                 :                :      * query.  (Vars of higher levels should be okay, though.)
                               1502                 :                :      */
                               1503         [ -  + ]:           1832 :     if (contain_vars_of_level((Node *) subselect, 1))
 6037 tgl@sss.pgh.pa.us        1504                 :UBC           0 :         return NULL;
                               1505                 :                : 
                               1506                 :                :     /*
                               1507                 :                :      * On the other hand, the WHERE clause must contain some Vars of the
                               1508                 :                :      * parent query, else it's not gonna be a join.
                               1509                 :                :      */
 6232 tgl@sss.pgh.pa.us        1510         [ +  + ]:CBC        1832 :     if (!contain_vars_of_level(whereClause, 1))
 6037                          1511                 :             51 :         return NULL;
                               1512                 :                : 
                               1513                 :                :     /*
                               1514                 :                :      * We don't risk optimizing if the WHERE clause is volatile, either.
                               1515                 :                :      */
 6232                          1516         [ -  + ]:           1781 :     if (contain_volatile_functions(whereClause))
 6037 tgl@sss.pgh.pa.us        1517                 :UBC           0 :         return NULL;
                               1518                 :                : 
                               1519                 :                :     /*
                               1520                 :                :      * Scan the rangetable for relation RTEs and retrieve the necessary
                               1521                 :                :      * catalog information for each relation.  Using this information, clear
                               1522                 :                :      * the inh flag for any relation that has no children, collect not-null
                               1523                 :                :      * attribute numbers for any relation that has column not-null
                               1524                 :                :      * constraints, and expand virtual generated columns for any relation that
                               1525                 :                :      * contains them.
                               1526                 :                :      *
                               1527                 :                :      * Note: we construct up an entirely dummy PlannerInfo for use here.  This
                               1528                 :                :      * is fine because only the "glob" and "parse" links will be used in this
                               1529                 :                :      * case.
                               1530                 :                :      *
                               1531                 :                :      * Note: we temporarily assign back the WHERE clause so that any virtual
                               1532                 :                :      * generated column references within it can be expanded.  It should be
                               1533                 :                :      * separated out again afterward.
                               1534                 :                :      */
   46 rguo@postgresql.org      1535   [ +  -  +  -  :GNC      158509 :     MemSet(&subroot, 0, sizeof(subroot));
                                     +  -  +  -  +  
                                                 + ]
                               1536                 :           1781 :     subroot.type = T_PlannerInfo;
                               1537                 :           1781 :     subroot.glob = root->glob;
                               1538                 :           1781 :     subroot.parse = subselect;
                               1539                 :           1781 :     subselect->jointree->quals = whereClause;
                               1540                 :           1781 :     subselect = preprocess_relation_rtes(&subroot);
                               1541                 :                : 
                               1542                 :                :     /*
                               1543                 :                :      * Now separate out the WHERE clause again.
                               1544                 :                :      */
                               1545                 :           1781 :     whereClause = subselect->jointree->quals;
                               1546                 :           1781 :     subselect->jointree->quals = NULL;
                               1547                 :                : 
                               1548                 :                :     /*
                               1549                 :                :      * The subquery must have a nonempty jointree, but we can make it so.
                               1550                 :                :      */
 2413 tgl@sss.pgh.pa.us        1551                 :CBC        1781 :     replace_empty_jointree(subselect);
                               1552                 :                : 
                               1553                 :                :     /*
                               1554                 :                :      * Prepare to pull up the sub-select into top range table.
                               1555                 :                :      *
                               1556                 :                :      * We rely here on the assumption that the outer query has no references
                               1557                 :                :      * to the inner (necessarily true). Therefore this is a lot easier than
                               1558                 :                :      * what pull_up_subqueries has to go through.
                               1559                 :                :      *
                               1560                 :                :      * In fact, it's even easier than what convert_ANY_sublink_to_join has to
                               1561                 :                :      * do.  The machinations of simplify_EXISTS_query ensured that there is
                               1562                 :                :      * nothing interesting in the subquery except an rtable and jointree, and
                               1563                 :                :      * even the jointree FromExpr no longer has quals.  So we can just append
                               1564                 :                :      * the rtable to our own and use the FromExpr in our jointree. But first,
                               1565                 :                :      * adjust all level-zero varnos in the subquery to account for the rtable
                               1566                 :                :      * merger.
                               1567                 :                :      */
 6232                          1568                 :           1781 :     rtoffset = list_length(parse->rtable);
                               1569                 :           1781 :     OffsetVarNodes((Node *) subselect, rtoffset, 0);
                               1570                 :           1781 :     OffsetVarNodes(whereClause, rtoffset, 0);
                               1571                 :                : 
                               1572                 :                :     /*
                               1573                 :                :      * Upper-level vars in subquery will now be one level closer to their
                               1574                 :                :      * parent than before; in particular, anything that had been level 1
                               1575                 :                :      * becomes level zero.
                               1576                 :                :      */
                               1577                 :           1781 :     IncrementVarSublevelsUp((Node *) subselect, -1, 1);
                               1578                 :           1781 :     IncrementVarSublevelsUp(whereClause, -1, 1);
                               1579                 :                : 
                               1580                 :                :     /*
                               1581                 :                :      * Now that the WHERE clause is adjusted to match the parent query
                               1582                 :                :      * environment, we can easily identify all the level-zero rels it uses.
                               1583                 :                :      * The ones <= rtoffset belong to the upper query; the ones > rtoffset do
                               1584                 :                :      * not.
                               1585                 :                :      */
 1689                          1586                 :           1781 :     clause_varnos = pull_varnos(root, whereClause);
 6037                          1587                 :           1781 :     upper_varnos = NULL;
  919                          1588                 :           1781 :     varno = -1;
                               1589         [ +  + ]:           5379 :     while ((varno = bms_next_member(clause_varnos, varno)) >= 0)
                               1590                 :                :     {
 6232                          1591         [ +  + ]:           3598 :         if (varno <= rtoffset)
 6037                          1592                 :           1805 :             upper_varnos = bms_add_member(upper_varnos, varno);
                               1593                 :                :     }
 6232                          1594                 :           1781 :     bms_free(clause_varnos);
 6037                          1595         [ -  + ]:           1781 :     Assert(!bms_is_empty(upper_varnos));
                               1596                 :                : 
                               1597                 :                :     /*
                               1598                 :                :      * Now that we've got the set of upper-level varnos, we can make the last
                               1599                 :                :      * check: only available_rels can be referenced.
                               1600                 :                :      */
                               1601         [ +  + ]:           1781 :     if (!bms_is_subset(upper_varnos, available_rels))
                               1602                 :             22 :         return NULL;
                               1603                 :                : 
                               1604                 :                :     /*
                               1605                 :                :      * Now we can attach the modified subquery rtable to the parent. This also
                               1606                 :                :      * adds subquery's RTEPermissionInfos into the upper query.
                               1607                 :                :      */
 1005 alvherre@alvh.no-ip.     1608                 :           1759 :     CombineRangeTables(&parse->rtable, &parse->rteperminfos,
                               1609                 :                :                        subselect->rtable, subselect->rteperminfos);
                               1610                 :                : 
                               1611                 :                :     /*
                               1612                 :                :      * And finally, build the JoinExpr node.
                               1613                 :                :      */
 6037 tgl@sss.pgh.pa.us        1614                 :           1759 :     result = makeNode(JoinExpr);
                               1615         [ +  + ]:           1759 :     result->jointype = under_not ? JOIN_ANTI : JOIN_SEMI;
                               1616                 :           1759 :     result->isNatural = false;
                               1617                 :           1759 :     result->larg = NULL;     /* caller must fill this in */
                               1618                 :                :     /* flatten out the FromExpr node if it's useless */
                               1619         [ +  + ]:           1759 :     if (list_length(subselect->jointree->fromlist) == 1)
                               1620                 :           1750 :         result->rarg = (Node *) linitial(subselect->jointree->fromlist);
                               1621                 :                :     else
                               1622                 :              9 :         result->rarg = (Node *) subselect->jointree;
 5896 peter_e@gmx.net          1623                 :           1759 :     result->usingClause = NIL;
 1620 peter@eisentraut.org     1624                 :           1759 :     result->join_using_alias = NULL;
 6037 tgl@sss.pgh.pa.us        1625                 :           1759 :     result->quals = whereClause;
                               1626                 :           1759 :     result->alias = NULL;
                               1627                 :           1759 :     result->rtindex = 0;     /* we don't need an RTE for it */
                               1628                 :                : 
                               1629                 :           1759 :     return result;
                               1630                 :                : }
                               1631                 :                : 
                               1632                 :                : /*
                               1633                 :                :  * simplify_EXISTS_query: remove any useless stuff in an EXISTS's subquery
                               1634                 :                :  *
                               1635                 :                :  * The only thing that matters about an EXISTS query is whether it returns
                               1636                 :                :  * zero or more than zero rows.  Therefore, we can remove certain SQL features
                               1637                 :                :  * that won't affect that.  The only part that is really likely to matter in
                               1638                 :                :  * typical usage is simplifying the targetlist: it's a common habit to write
                               1639                 :                :  * "SELECT * FROM" even though there is no need to evaluate any columns.
                               1640                 :                :  *
                               1641                 :                :  * Note: by suppressing the targetlist we could cause an observable behavioral
                               1642                 :                :  * change, namely that any errors that might occur in evaluating the tlist
                               1643                 :                :  * won't occur, nor will other side-effects of volatile functions.  This seems
                               1644                 :                :  * unlikely to bother anyone in practice.
                               1645                 :                :  *
                               1646                 :                :  * Returns true if was able to discard the targetlist, else false.
                               1647                 :                :  */
                               1648                 :                : static bool
 3941                          1649                 :           4160 : simplify_EXISTS_query(PlannerInfo *root, Query *query)
                               1650                 :                : {
                               1651                 :                :     ListCell   *lc;
                               1652                 :                : 
                               1653                 :                :     /*
                               1654                 :                :      * We don't try to simplify at all if the query uses set operations,
                               1655                 :                :      * aggregates, grouping sets, SRFs, modifying CTEs, HAVING, OFFSET, or FOR
                               1656                 :                :      * UPDATE/SHARE; none of these seem likely in normal usage and their
                               1657                 :                :      * possible effects are complex.  (Note: we could ignore an "OFFSET 0"
                               1658                 :                :      * clause, but that traditionally is used as an optimization fence, so we
                               1659                 :                :      * don't.)
                               1660                 :                :      */
 6224                          1661         [ +  - ]:           4160 :     if (query->commandType != CMD_SELECT ||
                               1662         [ +  - ]:           4160 :         query->setOperations ||
                               1663         [ +  - ]:           4160 :         query->hasAggs ||
 3766 andres@anarazel.de       1664         [ +  - ]:           4160 :         query->groupingSets ||
 6096 tgl@sss.pgh.pa.us        1665         [ +  - ]:           4160 :         query->hasWindowFuncs ||
 3280                          1666         [ +  - ]:           4160 :         query->hasTargetSRFs ||
 5307                          1667         [ +  - ]:           4160 :         query->hasModifyingCTE ||
 6224                          1668         [ +  - ]:           4160 :         query->havingQual ||
                               1669         [ +  + ]:           4160 :         query->limitOffset ||
                               1670         [ +  + ]:           4148 :         query->rowMarks)
                               1671                 :             26 :         return false;
                               1672                 :                : 
                               1673                 :                :     /*
                               1674                 :                :      * LIMIT with a constant positive (or NULL) value doesn't affect the
                               1675                 :                :      * semantics of EXISTS, so let's ignore such clauses.  This is worth doing
                               1676                 :                :      * because people accustomed to certain other DBMSes may be in the habit
                               1677                 :                :      * of writing EXISTS(SELECT ... LIMIT 1) as an optimization.  If there's a
                               1678                 :                :      * LIMIT with anything else as argument, though, we can't simplify.
                               1679                 :                :      */
 3941                          1680         [ +  + ]:           4134 :     if (query->limitCount)
                               1681                 :                :     {
                               1682                 :                :         /*
                               1683                 :                :          * The LIMIT clause has not yet been through eval_const_expressions,
                               1684                 :                :          * so we have to apply that here.  It might seem like this is a waste
                               1685                 :                :          * of cycles, since the only case plausibly worth worrying about is
                               1686                 :                :          * "LIMIT 1" ... but what we'll actually see is "LIMIT int8(1::int4)",
                               1687                 :                :          * so we have to fold constants or we're not going to recognize it.
                               1688                 :                :          */
                               1689                 :             12 :         Node       *node = eval_const_expressions(root, query->limitCount);
                               1690                 :                :         Const      *limit;
                               1691                 :                : 
                               1692                 :                :         /* Might as well update the query if we simplified the clause. */
                               1693                 :             12 :         query->limitCount = node;
                               1694                 :                : 
                               1695         [ -  + ]:             12 :         if (!IsA(node, Const))
 3941 tgl@sss.pgh.pa.us        1696                 :UBC           0 :             return false;
                               1697                 :                : 
 3941 tgl@sss.pgh.pa.us        1698                 :CBC          12 :         limit = (Const *) node;
                               1699         [ -  + ]:             12 :         Assert(limit->consttype == INT8OID);
                               1700   [ +  +  +  + ]:             12 :         if (!limit->constisnull && DatumGetInt64(limit->constvalue) <= 0)
                               1701                 :              6 :             return false;
                               1702                 :                : 
                               1703                 :                :         /* Whether or not the targetlist is safe, we can drop the LIMIT. */
                               1704                 :              6 :         query->limitCount = NULL;
                               1705                 :                :     }
                               1706                 :                : 
                               1707                 :                :     /*
                               1708                 :                :      * Otherwise, we can throw away the targetlist, as well as any GROUP,
                               1709                 :                :      * WINDOW, DISTINCT, and ORDER BY clauses; none of those clauses will
                               1710                 :                :      * change a nonzero-rows result to zero rows or vice versa.  (Furthermore,
                               1711                 :                :      * since our parsetree representation of these clauses depends on the
                               1712                 :                :      * targetlist, we'd better throw them away if we drop the targetlist.)
                               1713                 :                :      */
 6224                          1714                 :           4128 :     query->targetList = NIL;
                               1715                 :           4128 :     query->groupClause = NIL;
 6096                          1716                 :           4128 :     query->windowClause = NIL;
 6224                          1717                 :           4128 :     query->distinctClause = NIL;
                               1718                 :           4128 :     query->sortClause = NIL;
                               1719                 :           4128 :     query->hasDistinctOn = false;
                               1720                 :                : 
                               1721                 :                :     /*
                               1722                 :                :      * Since we have thrown away the GROUP BY clauses, we'd better remove the
                               1723                 :                :      * RTE_GROUP RTE and clear the hasGroupRTE flag.
                               1724                 :                :      */
  316 rguo@postgresql.org      1725   [ +  +  +  +  :           8473 :     foreach(lc, query->rtable)
                                              +  + ]
                               1726                 :                :     {
                               1727                 :           4348 :         RangeTblEntry *rte = lfirst_node(RangeTblEntry, lc);
                               1728                 :                : 
                               1729                 :                :         /*
                               1730                 :                :          * Remove the RTE_GROUP RTE and clear the hasGroupRTE flag.  (Since
                               1731                 :                :          * we'll exit the foreach loop immediately, we don't bother with
                               1732                 :                :          * foreach_delete_current.)
                               1733                 :                :          */
                               1734         [ +  + ]:           4348 :         if (rte->rtekind == RTE_GROUP)
                               1735                 :                :         {
                               1736         [ -  + ]:              3 :             Assert(query->hasGroupRTE);
                               1737                 :              3 :             query->rtable = list_delete_cell(query->rtable, lc);
                               1738                 :              3 :             query->hasGroupRTE = false;
                               1739                 :              3 :             break;
                               1740                 :                :         }
                               1741                 :                :     }
                               1742                 :                : 
 6224 tgl@sss.pgh.pa.us        1743                 :           4128 :     return true;
                               1744                 :                : }
                               1745                 :                : 
                               1746                 :                : /*
                               1747                 :                :  * convert_EXISTS_to_ANY: try to convert EXISTS to a hashable ANY sublink
                               1748                 :                :  *
                               1749                 :                :  * The subselect is expected to be a fresh copy that we can munge up,
                               1750                 :                :  * and to have been successfully passed through simplify_EXISTS_query.
                               1751                 :                :  *
                               1752                 :                :  * On success, the modified subselect is returned, and we store a suitable
                               1753                 :                :  * upper-level test expression at *testexpr, plus a list of the subselect's
                               1754                 :                :  * output Params at *paramIds.  (The test expression is already Param-ified
                               1755                 :                :  * and hence need not go through convert_testexpr, which is why we have to
                               1756                 :                :  * deal with the Param IDs specially.)
                               1757                 :                :  *
                               1758                 :                :  * On failure, returns NULL.
                               1759                 :                :  */
                               1760                 :                : static Query *
                               1761                 :           1085 : convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
                               1762                 :                :                       Node **testexpr, List **paramIds)
                               1763                 :                : {
                               1764                 :                :     Node       *whereClause;
                               1765                 :                :     PlannerInfo subroot;
                               1766                 :                :     List       *leftargs,
                               1767                 :                :                *rightargs,
                               1768                 :                :                *opids,
                               1769                 :                :                *opcollations,
                               1770                 :                :                *newWhere,
                               1771                 :                :                *tlist,
                               1772                 :                :                *testlist,
                               1773                 :                :                *paramids;
                               1774                 :                :     ListCell   *lc,
                               1775                 :                :                *rc,
                               1776                 :                :                *oc,
                               1777                 :                :                *cc;
                               1778                 :                :     AttrNumber  resno;
                               1779                 :                : 
                               1780                 :                :     /*
                               1781                 :                :      * Query must not require a targetlist, since we have to insert a new one.
                               1782                 :                :      * Caller should have dealt with the case already.
                               1783                 :                :      */
                               1784         [ -  + ]:           1085 :     Assert(subselect->targetList == NIL);
                               1785                 :                : 
                               1786                 :                :     /*
                               1787                 :                :      * Separate out the WHERE clause.  (We could theoretically also remove
                               1788                 :                :      * top-level plain JOIN/ON clauses, but it's probably not worth the
                               1789                 :                :      * trouble.)
                               1790                 :                :      */
                               1791                 :           1085 :     whereClause = subselect->jointree->quals;
                               1792                 :           1085 :     subselect->jointree->quals = NULL;
                               1793                 :                : 
                               1794                 :                :     /*
                               1795                 :                :      * The rest of the sub-select must not refer to any Vars of the parent
                               1796                 :                :      * query.  (Vars of higher levels should be okay, though.)
                               1797                 :                :      *
                               1798                 :                :      * Note: we need not check for Aggrefs separately because we know the
                               1799                 :                :      * sub-select is as yet unoptimized; any uplevel Aggref must therefore
                               1800                 :                :      * contain an uplevel Var reference.  This is not the case below ...
                               1801                 :                :      */
                               1802         [ +  + ]:           1085 :     if (contain_vars_of_level((Node *) subselect, 1))
                               1803                 :              3 :         return NULL;
                               1804                 :                : 
                               1805                 :                :     /*
                               1806                 :                :      * We don't risk optimizing if the WHERE clause is volatile, either.
                               1807                 :                :      */
                               1808         [ -  + ]:           1082 :     if (contain_volatile_functions(whereClause))
 6224 tgl@sss.pgh.pa.us        1809                 :UBC           0 :         return NULL;
                               1810                 :                : 
                               1811                 :                :     /*
                               1812                 :                :      * Clean up the WHERE clause by doing const-simplification etc on it.
                               1813                 :                :      * Aside from simplifying the processing we're about to do, this is
                               1814                 :                :      * important for being able to pull chunks of the WHERE clause up into the
                               1815                 :                :      * parent query.  Since we are invoked partway through the parent's
                               1816                 :                :      * preprocess_expression() work, earlier steps of preprocess_expression()
                               1817                 :                :      * wouldn't get applied to the pulled-up stuff unless we do them here. For
                               1818                 :                :      * the parts of the WHERE clause that get put back into the child query,
                               1819                 :                :      * this work is partially duplicative, but it shouldn't hurt.
                               1820                 :                :      *
                               1821                 :                :      * Note: we do not run flatten_join_alias_vars.  This is OK because any
                               1822                 :                :      * parent aliases were flattened already, and we're not going to pull any
                               1823                 :                :      * child Vars (of any description) into the parent.
                               1824                 :                :      *
                               1825                 :                :      * Note: we construct up an entirely dummy PlannerInfo to pass to
                               1826                 :                :      * eval_const_expressions.  This is fine because only the "glob" and
                               1827                 :                :      * "parse" links are used by eval_const_expressions.
                               1828                 :                :      */
   46 rguo@postgresql.org      1829   [ +  -  +  -  :GNC       96298 :     MemSet(&subroot, 0, sizeof(subroot));
                                     +  -  +  -  +  
                                                 + ]
                               1830                 :           1082 :     subroot.type = T_PlannerInfo;
                               1831                 :           1082 :     subroot.glob = root->glob;
                               1832                 :           1082 :     subroot.parse = subselect;
                               1833                 :           1082 :     whereClause = eval_const_expressions(&subroot, whereClause);
 2736 tgl@sss.pgh.pa.us        1834                 :CBC        1082 :     whereClause = (Node *) canonicalize_qual((Expr *) whereClause, false);
 6224                          1835                 :           1082 :     whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
                               1836                 :                : 
                               1837                 :                :     /*
                               1838                 :                :      * We now have a flattened implicit-AND list of clauses, which we try to
                               1839                 :                :      * break apart into "outervar = innervar" hash clauses. Anything that
                               1840                 :                :      * can't be broken apart just goes back into the newWhere list.  Note that
                               1841                 :                :      * we aren't trying hard yet to ensure that we have only outer or only
                               1842                 :                :      * inner on each side; we'll check that if we get to the end.
                               1843                 :                :      */
 5285                          1844                 :           1082 :     leftargs = rightargs = opids = opcollations = newWhere = NIL;
 6224                          1845   [ +  -  +  +  :           4291 :     foreach(lc, (List *) whereClause)
                                              +  + ]
                               1846                 :                :     {
                               1847                 :           3209 :         OpExpr     *expr = (OpExpr *) lfirst(lc);
                               1848                 :                : 
                               1849   [ +  +  +  + ]:           5241 :         if (IsA(expr, OpExpr) &&
                               1850                 :           2032 :             hash_ok_operator(expr))
                               1851                 :                :         {
 5931 bruce@momjian.us         1852                 :           1611 :             Node       *leftarg = (Node *) linitial(expr->args);
                               1853                 :           1611 :             Node       *rightarg = (Node *) lsecond(expr->args);
                               1854                 :                : 
 6224 tgl@sss.pgh.pa.us        1855         [ +  + ]:           1611 :             if (contain_vars_of_level(leftarg, 1))
                               1856                 :                :             {
                               1857                 :            202 :                 leftargs = lappend(leftargs, leftarg);
                               1858                 :            202 :                 rightargs = lappend(rightargs, rightarg);
                               1859                 :            202 :                 opids = lappend_oid(opids, expr->opno);
 5285                          1860                 :            202 :                 opcollations = lappend_oid(opcollations, expr->inputcollid);
 6224                          1861                 :            202 :                 continue;
                               1862                 :                :             }
                               1863         [ +  + ]:           1409 :             if (contain_vars_of_level(rightarg, 1))
                               1864                 :                :             {
                               1865                 :                :                 /*
                               1866                 :                :                  * We must commute the clause to put the outer var on the
                               1867                 :                :                  * left, because the hashing code in nodeSubplan.c expects
                               1868                 :                :                  * that.  This probably shouldn't ever fail, since hashable
                               1869                 :                :                  * operators ought to have commutators, but be paranoid.
                               1870                 :                :                  */
                               1871                 :           1096 :                 expr->opno = get_commutator(expr->opno);
                               1872   [ +  -  +  - ]:           1096 :                 if (OidIsValid(expr->opno) && hash_ok_operator(expr))
                               1873                 :                :                 {
                               1874                 :           1096 :                     leftargs = lappend(leftargs, rightarg);
                               1875                 :           1096 :                     rightargs = lappend(rightargs, leftarg);
                               1876                 :           1096 :                     opids = lappend_oid(opids, expr->opno);
 5285                          1877                 :           1096 :                     opcollations = lappend_oid(opcollations, expr->inputcollid);
 6224                          1878                 :           1096 :                     continue;
                               1879                 :                :                 }
                               1880                 :                :                 /* If no commutator, no chance to optimize the WHERE clause */
 6224 tgl@sss.pgh.pa.us        1881                 :UBC           0 :                 return NULL;
                               1882                 :                :             }
                               1883                 :                :         }
                               1884                 :                :         /* Couldn't handle it as a hash clause */
 6224 tgl@sss.pgh.pa.us        1885                 :CBC        1911 :         newWhere = lappend(newWhere, expr);
                               1886                 :                :     }
                               1887                 :                : 
                               1888                 :                :     /*
                               1889                 :                :      * If we didn't find anything we could convert, fail.
                               1890                 :                :      */
                               1891         [ +  + ]:           1082 :     if (leftargs == NIL)
                               1892                 :            184 :         return NULL;
                               1893                 :                : 
                               1894                 :                :     /*
                               1895                 :                :      * There mustn't be any parent Vars or Aggs in the stuff that we intend to
                               1896                 :                :      * put back into the child query.  Note: you might think we don't need to
                               1897                 :                :      * check for Aggs separately, because an uplevel Agg must contain an
                               1898                 :                :      * uplevel Var in its argument.  But it is possible that the uplevel Var
                               1899                 :                :      * got optimized away by eval_const_expressions.  Consider
                               1900                 :                :      *
                               1901                 :                :      * SUM(CASE WHEN false THEN uplevelvar ELSE 0 END)
                               1902                 :                :      */
                               1903   [ +  +  -  + ]:           1746 :     if (contain_vars_of_level((Node *) newWhere, 1) ||
                               1904                 :            848 :         contain_vars_of_level((Node *) rightargs, 1))
                               1905                 :             50 :         return NULL;
                               1906   [ +  +  +  - ]:            869 :     if (root->parse->hasAggs &&
                               1907         [ -  + ]:             42 :         (contain_aggs_of_level((Node *) newWhere, 1) ||
                               1908                 :             21 :          contain_aggs_of_level((Node *) rightargs, 1)))
 6224 tgl@sss.pgh.pa.us        1909                 :UBC           0 :         return NULL;
                               1910                 :                : 
                               1911                 :                :     /*
                               1912                 :                :      * And there can't be any child Vars in the stuff we intend to pull up.
                               1913                 :                :      * (Note: we'd need to check for child Aggs too, except we know the child
                               1914                 :                :      * has no aggs at all because of simplify_EXISTS_query's check. The same
                               1915                 :                :      * goes for window functions.)
                               1916                 :                :      */
 6224 tgl@sss.pgh.pa.us        1917         [ -  + ]:CBC         848 :     if (contain_vars_of_level((Node *) leftargs, 0))
 6224 tgl@sss.pgh.pa.us        1918                 :UBC           0 :         return NULL;
                               1919                 :                : 
                               1920                 :                :     /*
                               1921                 :                :      * Also reject sublinks in the stuff we intend to pull up.  (It might be
                               1922                 :                :      * possible to support this, but doesn't seem worth the complication.)
                               1923                 :                :      */
 6224 tgl@sss.pgh.pa.us        1924         [ -  + ]:CBC         848 :     if (contain_subplans((Node *) leftargs))
 6224 tgl@sss.pgh.pa.us        1925                 :UBC           0 :         return NULL;
                               1926                 :                : 
                               1927                 :                :     /*
                               1928                 :                :      * Okay, adjust the sublevelsup in the stuff we're pulling up.
                               1929                 :                :      */
 6224 tgl@sss.pgh.pa.us        1930                 :CBC         848 :     IncrementVarSublevelsUp((Node *) leftargs, -1, 1);
                               1931                 :                : 
                               1932                 :                :     /*
                               1933                 :                :      * Put back any child-level-only WHERE clauses.
                               1934                 :                :      */
                               1935         [ +  + ]:            848 :     if (newWhere)
                               1936                 :            746 :         subselect->jointree->quals = (Node *) make_ands_explicit(newWhere);
                               1937                 :                : 
                               1938                 :                :     /*
                               1939                 :                :      * Build a new targetlist for the child that emits the expressions we
                               1940                 :                :      * need.  Concurrently, build a testexpr for the parent using Params to
                               1941                 :                :      * reference the child outputs.  (Since we generate Params directly here,
                               1942                 :                :      * there will be no need to convert the testexpr in build_subplan.)
                               1943                 :                :      */
                               1944                 :            848 :     tlist = testlist = paramids = NIL;
                               1945                 :            848 :     resno = 1;
 2382                          1946   [ +  -  +  +  :           2096 :     forfour(lc, leftargs, rc, rightargs, oc, opids, cc, opcollations)
                                     +  -  +  +  +  
                                     -  +  +  +  -  
                                     +  +  +  +  +  
                                     -  +  -  +  -  
                                              +  + ]
                               1947                 :                :     {
 6224                          1948                 :           1248 :         Node       *leftarg = (Node *) lfirst(lc);
                               1949                 :           1248 :         Node       *rightarg = (Node *) lfirst(rc);
                               1950                 :           1248 :         Oid         opid = lfirst_oid(oc);
 5285                          1951                 :           1248 :         Oid         opcollation = lfirst_oid(cc);
                               1952                 :                :         Param      *param;
                               1953                 :                : 
 2430                          1954                 :           1248 :         param = generate_new_exec_param(root,
                               1955                 :                :                                         exprType(rightarg),
                               1956                 :                :                                         exprTypmod(rightarg),
                               1957                 :                :                                         exprCollation(rightarg));
 6224                          1958                 :           1248 :         tlist = lappend(tlist,
                               1959                 :           1248 :                         makeTargetEntry((Expr *) rightarg,
                               1960                 :           1248 :                                         resno++,
                               1961                 :                :                                         NULL,
                               1962                 :                :                                         false));
                               1963                 :           1248 :         testlist = lappend(testlist,
                               1964                 :           1248 :                            make_opclause(opid, BOOLOID, false,
                               1965                 :                :                                          (Expr *) leftarg, (Expr *) param,
                               1966                 :                :                                          InvalidOid, opcollation));
                               1967                 :           1248 :         paramids = lappend_int(paramids, param->paramid);
                               1968                 :                :     }
                               1969                 :                : 
                               1970                 :                :     /* Put everything where it should go, and we're done */
                               1971                 :            848 :     subselect->targetList = tlist;
                               1972                 :            848 :     *testexpr = (Node *) make_ands_explicit(testlist);
                               1973                 :            848 :     *paramIds = paramids;
                               1974                 :                : 
                               1975                 :            848 :     return subselect;
                               1976                 :                : }
                               1977                 :                : 
                               1978                 :                : 
                               1979                 :                : /*
                               1980                 :                :  * Replace correlation vars (uplevel vars) with Params.
                               1981                 :                :  *
                               1982                 :                :  * Uplevel PlaceHolderVars, aggregates, GROUPING() expressions,
                               1983                 :                :  * MergeSupportFuncs, and ReturningExprs are replaced, too.
                               1984                 :                :  *
                               1985                 :                :  * Note: it is critical that this runs immediately after SS_process_sublinks.
                               1986                 :                :  * Since we do not recurse into the arguments of uplevel PHVs and aggregates,
                               1987                 :                :  * they will get copied to the appropriate subplan args list in the parent
                               1988                 :                :  * query with uplevel vars not replaced by Params, but only adjusted in level
                               1989                 :                :  * (see replace_outer_placeholdervar and replace_outer_agg).  That's exactly
                               1990                 :                :  * what we want for the vars of the parent level --- but if a PHV's or
                               1991                 :                :  * aggregate's argument contains any further-up variables, they have to be
                               1992                 :                :  * replaced with Params in their turn. That will happen when the parent level
                               1993                 :                :  * runs SS_replace_correlation_vars.  Therefore it must do so after expanding
                               1994                 :                :  * its sublinks to subplans.  And we don't want any steps in between, else
                               1995                 :                :  * those steps would never get applied to the argument expressions, either in
                               1996                 :                :  * the parent or the child level.
                               1997                 :                :  *
                               1998                 :                :  * Another fairly tricky thing going on here is the handling of SubLinks in
                               1999                 :                :  * the arguments of uplevel PHVs/aggregates.  Those are not touched inside the
                               2000                 :                :  * intermediate query level, either.  Instead, SS_process_sublinks recurses on
                               2001                 :                :  * them after copying the PHV or Aggref expression into the parent plan level
                               2002                 :                :  * (this is actually taken care of in build_subplan).
                               2003                 :                :  */
                               2004                 :                : Node *
 6774                          2005                 :          78383 : SS_replace_correlation_vars(PlannerInfo *root, Node *expr)
                               2006                 :                : {
                               2007                 :                :     /* No setup needed for tree walk, so away we go */
                               2008                 :          78383 :     return replace_correlation_vars_mutator(expr, root);
                               2009                 :                : }
                               2010                 :                : 
                               2011                 :                : static Node *
                               2012                 :         684657 : replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
                               2013                 :                : {
 9509                          2014         [ +  + ]:         684657 :     if (node == NULL)
                               2015                 :          31545 :         return NULL;
                               2016         [ +  + ]:         653112 :     if (IsA(node, Var))
                               2017                 :                :     {
                               2018         [ +  + ]:         176674 :         if (((Var *) node)->varlevelsup > 0)
 6774                          2019                 :          26646 :             return (Node *) replace_outer_var(root, (Var *) node);
                               2020                 :                :     }
 4914                          2021         [ +  + ]:         626466 :     if (IsA(node, PlaceHolderVar))
                               2022                 :                :     {
                               2023         [ +  + ]:             51 :         if (((PlaceHolderVar *) node)->phlevelsup > 0)
                               2024                 :             30 :             return (Node *) replace_outer_placeholdervar(root,
                               2025                 :                :                                                          (PlaceHolderVar *) node);
                               2026                 :                :     }
 8128                          2027         [ +  + ]:         626436 :     if (IsA(node, Aggref))
                               2028                 :                :     {
                               2029         [ +  + ]:           4341 :         if (((Aggref *) node)->agglevelsup > 0)
 6774                          2030                 :             26 :             return (Node *) replace_outer_agg(root, (Aggref *) node);
                               2031                 :                :     }
 3766 andres@anarazel.de       2032         [ +  + ]:         626410 :     if (IsA(node, GroupingFunc))
                               2033                 :                :     {
                               2034         [ +  + ]:             45 :         if (((GroupingFunc *) node)->agglevelsup > 0)
                               2035                 :             32 :             return (Node *) replace_outer_grouping(root, (GroupingFunc *) node);
                               2036                 :                :     }
  538 dean.a.rasheed@gmail     2037         [ +  + ]:         626378 :     if (IsA(node, MergeSupportFunc))
                               2038                 :                :     {
                               2039         [ +  + ]:             18 :         if (root->parse->commandType != CMD_MERGE)
                               2040                 :              3 :             return (Node *) replace_outer_merge_support(root,
                               2041                 :                :                                                         (MergeSupportFunc *) node);
                               2042                 :                :     }
  233                          2043         [ +  + ]:         626375 :     if (IsA(node, ReturningExpr))
                               2044                 :                :     {
                               2045         [ +  - ]:              9 :         if (((ReturningExpr *) node)->retlevelsup > 0)
                               2046                 :              9 :             return (Node *) replace_outer_returning(root,
                               2047                 :                :                                                     (ReturningExpr *) node);
                               2048                 :                :     }
  282 peter@eisentraut.org     2049                 :         626366 :     return expression_tree_mutator(node, replace_correlation_vars_mutator, root);
                               2050                 :                : }
                               2051                 :                : 
                               2052                 :                : /*
                               2053                 :                :  * Expand SubLinks to SubPlans in the given expression.
                               2054                 :                :  *
                               2055                 :                :  * The isQual argument tells whether or not this expression is a WHERE/HAVING
                               2056                 :                :  * qualifier expression.  If it is, any sublinks appearing at top level need
                               2057                 :                :  * not distinguish FALSE from UNKNOWN return values.
                               2058                 :                :  */
                               2059                 :                : Node *
 6774 tgl@sss.pgh.pa.us        2060                 :          52525 : SS_process_sublinks(PlannerInfo *root, Node *expr, bool isQual)
                               2061                 :                : {
                               2062                 :                :     process_sublinks_context context;
                               2063                 :                : 
                               2064                 :          52525 :     context.root = root;
                               2065                 :          52525 :     context.isTopQual = isQual;
                               2066                 :          52525 :     return process_sublinks_mutator(expr, &context);
                               2067                 :                : }
                               2068                 :                : 
                               2069                 :                : static Node *
 6505 bruce@momjian.us         2070                 :         685219 : process_sublinks_mutator(Node *node, process_sublinks_context *context)
                               2071                 :                : {
                               2072                 :                :     process_sublinks_context locContext;
                               2073                 :                : 
 6774 tgl@sss.pgh.pa.us        2074                 :         685219 :     locContext.root = context->root;
                               2075                 :                : 
 9509                          2076         [ +  + ]:         685219 :     if (node == NULL)
 9867 bruce@momjian.us         2077                 :          29770 :         return NULL;
 9509 tgl@sss.pgh.pa.us        2078         [ +  + ]:         655449 :     if (IsA(node, SubLink))
                               2079                 :                :     {
 9278 bruce@momjian.us         2080                 :          19625 :         SubLink    *sublink = (SubLink *) node;
                               2081                 :                :         Node       *testexpr;
                               2082                 :                : 
                               2083                 :                :         /*
                               2084                 :                :          * First, recursively process the lefthand-side expressions, if any.
                               2085                 :                :          * They're not top-level anymore.
                               2086                 :                :          */
 6774 tgl@sss.pgh.pa.us        2087                 :          19625 :         locContext.isTopQual = false;
                               2088                 :          19625 :         testexpr = process_sublinks_mutator(sublink->testexpr, &locContext);
                               2089                 :                : 
                               2090                 :                :         /*
                               2091                 :                :          * Now build the SubPlan node and make the expr to return.
                               2092                 :                :          */
                               2093                 :          19625 :         return make_subplan(context->root,
 6224                          2094                 :          19625 :                             (Query *) sublink->subselect,
                               2095                 :                :                             sublink->subLinkType,
                               2096                 :                :                             sublink->subLinkId,
                               2097                 :                :                             testexpr,
 6774                          2098                 :          19625 :                             context->isTopQual);
                               2099                 :                :     }
                               2100                 :                : 
                               2101                 :                :     /*
                               2102                 :                :      * Don't recurse into the arguments of an outer PHV, Aggref, GroupingFunc,
                               2103                 :                :      * or ReturningExpr here.  Any SubLinks in the arguments have to be dealt
                               2104                 :                :      * with at the outer query level; they'll be handled when build_subplan
                               2105                 :                :      * collects the PHV, Aggref, GroupingFunc, or ReturningExpr into the
                               2106                 :                :      * arguments to be passed down to the current subplan.
                               2107                 :                :      */
 4914                          2108         [ +  + ]:         635824 :     if (IsA(node, PlaceHolderVar))
                               2109                 :                :     {
                               2110         [ +  + ]:            119 :         if (((PlaceHolderVar *) node)->phlevelsup > 0)
                               2111                 :              6 :             return node;
                               2112                 :                :     }
                               2113         [ +  + ]:         635705 :     else if (IsA(node, Aggref))
                               2114                 :                :     {
 5978                          2115         [ +  + ]:            300 :         if (((Aggref *) node)->agglevelsup > 0)
                               2116                 :              9 :             return node;
                               2117                 :                :     }
 1265                          2118         [ +  + ]:         635405 :     else if (IsA(node, GroupingFunc))
                               2119                 :                :     {
                               2120         [ +  + ]:             80 :         if (((GroupingFunc *) node)->agglevelsup > 0)
                               2121                 :             18 :             return node;
                               2122                 :                :     }
  233 dean.a.rasheed@gmail     2123         [ +  + ]:         635325 :     else if (IsA(node, ReturningExpr))
                               2124                 :                :     {
                               2125         [ +  + ]:             99 :         if (((ReturningExpr *) node)->retlevelsup > 0)
                               2126                 :              3 :             return node;
                               2127                 :                :     }
                               2128                 :                : 
                               2129                 :                :     /*
                               2130                 :                :      * We should never see a SubPlan expression in the input (since this is
                               2131                 :                :      * the very routine that creates 'em to begin with).  We shouldn't find
                               2132                 :                :      * ourselves invoked directly on a Query, either.
                               2133                 :                :      */
 6224 tgl@sss.pgh.pa.us        2134         [ -  + ]:         635788 :     Assert(!IsA(node, SubPlan));
                               2135         [ -  + ]:         635788 :     Assert(!IsA(node, AlternativeSubPlan));
 8268                          2136         [ -  + ]:         635788 :     Assert(!IsA(node, Query));
                               2137                 :                : 
                               2138                 :                :     /*
                               2139                 :                :      * Because make_subplan() could return an AND or OR clause, we have to
                               2140                 :                :      * take steps to preserve AND/OR flatness of a qual.  We assume the input
                               2141                 :                :      * has been AND/OR flattened and so we need no recursion here.
                               2142                 :                :      *
                               2143                 :                :      * (Due to the coding here, we will not get called on the List subnodes of
                               2144                 :                :      * an AND; and the input is *not* yet in implicit-AND format.  So no check
                               2145                 :                :      * is needed for a bare List.)
                               2146                 :                :      *
                               2147                 :                :      * Anywhere within the top-level AND/OR clause structure, we can tell
                               2148                 :                :      * make_subplan() that NULL and FALSE are interchangeable.  So isTopQual
                               2149                 :                :      * propagates down in both cases.  (Note that this is unlike the meaning
                               2150                 :                :      * of "top level qual" used in most other places in Postgres.)
                               2151                 :                :      */
 2412                          2152         [ +  + ]:         635788 :     if (is_andclause(node))
                               2153                 :                :     {
 7678 bruce@momjian.us         2154                 :           9941 :         List       *newargs = NIL;
                               2155                 :                :         ListCell   *l;
                               2156                 :                : 
                               2157                 :                :         /* Still at qual top-level */
 6774 tgl@sss.pgh.pa.us        2158                 :           9941 :         locContext.isTopQual = context->isTopQual;
                               2159                 :                : 
 7908                          2160   [ +  -  +  +  :          36296 :         foreach(l, ((BoolExpr *) node)->args)
                                              +  + ]
                               2161                 :                :         {
                               2162                 :                :             Node       *newarg;
                               2163                 :                : 
 6774                          2164                 :          26355 :             newarg = process_sublinks_mutator(lfirst(l), &locContext);
 2412                          2165         [ -  + ]:          26355 :             if (is_andclause(newarg))
 7769 neilc@samurai.com        2166                 :UBC           0 :                 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
                               2167                 :                :             else
 7908 tgl@sss.pgh.pa.us        2168                 :CBC       26355 :                 newargs = lappend(newargs, newarg);
                               2169                 :                :         }
                               2170                 :           9941 :         return (Node *) make_andclause(newargs);
                               2171                 :                :     }
                               2172                 :                : 
 2412                          2173         [ +  + ]:         625847 :     if (is_orclause(node))
                               2174                 :                :     {
 7678 bruce@momjian.us         2175                 :           1128 :         List       *newargs = NIL;
                               2176                 :                :         ListCell   *l;
                               2177                 :                : 
                               2178                 :                :         /* Still at qual top-level */
 6226 tgl@sss.pgh.pa.us        2179                 :           1128 :         locContext.isTopQual = context->isTopQual;
                               2180                 :                : 
 7908                          2181   [ +  -  +  +  :           4038 :         foreach(l, ((BoolExpr *) node)->args)
                                              +  + ]
                               2182                 :                :         {
                               2183                 :                :             Node       *newarg;
                               2184                 :                : 
 6774                          2185                 :           2910 :             newarg = process_sublinks_mutator(lfirst(l), &locContext);
 2412                          2186         [ -  + ]:           2910 :             if (is_orclause(newarg))
 7769 neilc@samurai.com        2187                 :UBC           0 :                 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
                               2188                 :                :             else
 7908 tgl@sss.pgh.pa.us        2189                 :CBC        2910 :                 newargs = lappend(newargs, newarg);
                               2190                 :                :         }
                               2191                 :           1128 :         return (Node *) make_orclause(newargs);
                               2192                 :                :     }
                               2193                 :                : 
                               2194                 :                :     /*
                               2195                 :                :      * If we recurse down through anything other than an AND or OR node, we
                               2196                 :                :      * are definitely not at top qual level anymore.
                               2197                 :                :      */
 6226                          2198                 :         624719 :     locContext.isTopQual = false;
                               2199                 :                : 
 9509                          2200                 :         624719 :     return expression_tree_mutator(node,
                               2201                 :                :                                    process_sublinks_mutator,
                               2202                 :                :                                    &locContext);
                               2203                 :                : }
                               2204                 :                : 
                               2205                 :                : /*
                               2206                 :                :  * SS_identify_outer_params - identify the Params available from outer levels
                               2207                 :                :  *
                               2208                 :                :  * This must be run after SS_replace_correlation_vars and SS_process_sublinks
                               2209                 :                :  * processing is complete in a given query level as well as all of its
                               2210                 :                :  * descendant levels (which means it's most practical to do it at the end of
                               2211                 :                :  * processing the query level).  We compute the set of paramIds that outer
                               2212                 :                :  * levels will make available to this level+descendants, and record it in
                               2213                 :                :  * root->outer_params for use while computing extParam/allParam sets in final
                               2214                 :                :  * plan cleanup.  (We can't just compute it then, because the upper levels'
                               2215                 :                :  * plan_params lists are transient and will be gone by then.)
                               2216                 :                :  */
                               2217                 :                : void
 3679                          2218                 :         256862 : SS_identify_outer_params(PlannerInfo *root)
                               2219                 :                : {
                               2220                 :                :     Bitmapset  *outer_params;
                               2221                 :                :     PlannerInfo *proot;
                               2222                 :                :     ListCell   *l;
                               2223                 :                : 
                               2224                 :                :     /*
                               2225                 :                :      * If no parameters have been assigned anywhere in the tree, we certainly
                               2226                 :                :      * don't need to do anything here.
                               2227                 :                :      */
 2854 rhaas@postgresql.org     2228         [ +  + ]:         256862 :     if (root->glob->paramExecTypes == NIL)
 3679 tgl@sss.pgh.pa.us        2229                 :         172965 :         return;
                               2230                 :                : 
                               2231                 :                :     /*
                               2232                 :                :      * Scan all query levels above this one to see which parameters are due to
                               2233                 :                :      * be available from them, either because lower query levels have
                               2234                 :                :      * requested them (via plan_params) or because they will be available from
                               2235                 :                :      * initPlans of those levels.
                               2236                 :                :      */
                               2237                 :          83897 :     outer_params = NULL;
 4749                          2238         [ +  + ]:         112323 :     for (proot = root->parent_root; proot != NULL; proot = proot->parent_root)
                               2239                 :                :     {
                               2240                 :                :         /*
                               2241                 :                :          * Include ordinary Var/PHV/Aggref/GroupingFunc/ReturningExpr params.
                               2242                 :                :          */
                               2243   [ +  +  +  +  :          51486 :         foreach(l, proot->plan_params)
                                              +  + ]
                               2244                 :                :         {
                               2245                 :          23060 :             PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
                               2246                 :                : 
 3679                          2247                 :          23060 :             outer_params = bms_add_member(outer_params, pitem->paramId);
                               2248                 :                :         }
                               2249                 :                :         /* Include any outputs of outer-level initPlans */
 4749                          2250   [ +  +  +  +  :          31537 :         foreach(l, proot->init_plans)
                                              +  + ]
                               2251                 :                :         {
                               2252                 :           3111 :             SubPlan    *initsubplan = (SubPlan *) lfirst(l);
                               2253                 :                :             ListCell   *l2;
                               2254                 :                : 
                               2255   [ +  -  +  +  :           6222 :             foreach(l2, initsubplan->setParam)
                                              +  + ]
                               2256                 :                :             {
 3679                          2257                 :           3111 :                 outer_params = bms_add_member(outer_params, lfirst_int(l2));
                               2258                 :                :             }
                               2259                 :                :         }
                               2260                 :                :         /* Include worktable ID, if a recursive query is being planned */
 4749                          2261         [ +  + ]:          28426 :         if (proot->wt_param_id >= 0)
 3679                          2262                 :           1618 :             outer_params = bms_add_member(outer_params, proot->wt_param_id);
                               2263                 :                :     }
                               2264                 :          83897 :     root->outer_params = outer_params;
                               2265                 :                : }
                               2266                 :                : 
                               2267                 :                : /*
                               2268                 :                :  * SS_charge_for_initplans - account for initplans in Path costs & parallelism
                               2269                 :                :  *
                               2270                 :                :  * If any initPlans have been created in the current query level, they will
                               2271                 :                :  * get attached to the Plan tree created from whichever Path we select from
                               2272                 :                :  * the given rel.  Increment all that rel's Paths' costs to account for them,
                               2273                 :                :  * and if any of the initPlans are parallel-unsafe, mark all the rel's Paths
                               2274                 :                :  * parallel-unsafe as well.
                               2275                 :                :  *
                               2276                 :                :  * This is separate from SS_attach_initplans because we might conditionally
                               2277                 :                :  * create more initPlans during create_plan(), depending on which Path we
                               2278                 :                :  * select.  However, Paths that would generate such initPlans are expected
                               2279                 :                :  * to have included their cost and parallel-safety effects already.
                               2280                 :                :  */
                               2281                 :                : void
 3470                          2282                 :         256862 : SS_charge_for_initplans(PlannerInfo *root, RelOptInfo *final_rel)
                               2283                 :                : {
                               2284                 :                :     Cost        initplan_cost;
                               2285                 :                :     bool        unsafe_initplans;
                               2286                 :                :     ListCell   *lc;
                               2287                 :                : 
                               2288                 :                :     /* Nothing to do if no initPlans */
                               2289         [ +  + ]:         256862 :     if (root->init_plans == NIL)
                               2290                 :         251018 :         return;
                               2291                 :                : 
                               2292                 :                :     /*
                               2293                 :                :      * Compute the cost increment just once, since it will be the same for all
                               2294                 :                :      * Paths.  Also check for parallel-unsafe initPlans.
                               2295                 :                :      */
  786                          2296                 :           5844 :     SS_compute_initplan_cost(root->init_plans,
                               2297                 :                :                              &initplan_cost, &unsafe_initplans);
                               2298                 :                : 
                               2299                 :                :     /*
                               2300                 :                :      * Now adjust the costs and parallel_safe flags.
                               2301                 :                :      */
 3470                          2302   [ +  -  +  +  :          11775 :     foreach(lc, final_rel->pathlist)
                                              +  + ]
                               2303                 :                :     {
                               2304                 :           5931 :         Path       *path = (Path *) lfirst(lc);
                               2305                 :                : 
                               2306                 :           5931 :         path->startup_cost += initplan_cost;
                               2307                 :           5931 :         path->total_cost += initplan_cost;
  786                          2308         [ +  + ]:           5931 :         if (unsafe_initplans)
                               2309                 :           3523 :             path->parallel_safe = false;
                               2310                 :                :     }
                               2311                 :                : 
                               2312                 :                :     /*
                               2313                 :                :      * Adjust partial paths' costs too, or forget them entirely if we must
                               2314                 :                :      * consider the rel parallel-unsafe.
                               2315                 :                :      */
                               2316         [ +  + ]:           5844 :     if (unsafe_initplans)
                               2317                 :                :     {
                               2318                 :           3483 :         final_rel->partial_pathlist = NIL;
                               2319                 :           3483 :         final_rel->consider_parallel = false;
                               2320                 :                :     }
                               2321                 :                :     else
                               2322                 :                :     {
                               2323   [ +  +  +  +  :           2367 :         foreach(lc, final_rel->partial_pathlist)
                                              +  + ]
                               2324                 :                :         {
                               2325                 :              6 :             Path       *path = (Path *) lfirst(lc);
                               2326                 :                : 
                               2327                 :              6 :             path->startup_cost += initplan_cost;
                               2328                 :              6 :             path->total_cost += initplan_cost;
                               2329                 :                :         }
                               2330                 :                :     }
                               2331                 :                : 
                               2332                 :                :     /* We needn't do set_cheapest() here, caller will do it */
                               2333                 :                : }
                               2334                 :                : 
                               2335                 :                : /*
                               2336                 :                :  * SS_compute_initplan_cost - count up the cost delta for some initplans
                               2337                 :                :  *
                               2338                 :                :  * The total cost returned in *initplan_cost_p should be added to both the
                               2339                 :                :  * startup and total costs of the plan node the initplans get attached to.
                               2340                 :                :  * We also report whether any of the initplans are not parallel-safe.
                               2341                 :                :  *
                               2342                 :                :  * The primary user of this is SS_charge_for_initplans, but it's also
                               2343                 :                :  * used in adjusting costs when we move initplans to another plan node.
                               2344                 :                :  */
                               2345                 :                : void
                               2346                 :           5963 : SS_compute_initplan_cost(List *init_plans,
                               2347                 :                :                          Cost *initplan_cost_p,
                               2348                 :                :                          bool *unsafe_initplans_p)
                               2349                 :                : {
                               2350                 :                :     Cost        initplan_cost;
                               2351                 :                :     bool        unsafe_initplans;
                               2352                 :                :     ListCell   *lc;
                               2353                 :                : 
                               2354                 :                :     /*
                               2355                 :                :      * We assume each initPlan gets run once during top plan startup.  This is
                               2356                 :                :      * a conservative overestimate, since in fact an initPlan might be
                               2357                 :                :      * executed later than plan startup, or even not at all.
                               2358                 :                :      */
                               2359                 :           5963 :     initplan_cost = 0;
                               2360                 :           5963 :     unsafe_initplans = false;
                               2361   [ +  +  +  +  :          12447 :     foreach(lc, init_plans)
                                              +  + ]
                               2362                 :                :     {
                               2363                 :           6484 :         SubPlan    *initsubplan = lfirst_node(SubPlan, lc);
                               2364                 :                : 
                               2365                 :           6484 :         initplan_cost += initsubplan->startup_cost + initsubplan->per_call_cost;
                               2366         [ +  + ]:           6484 :         if (!initsubplan->parallel_safe)
                               2367                 :           3980 :             unsafe_initplans = true;
                               2368                 :                :     }
                               2369                 :           5963 :     *initplan_cost_p = initplan_cost;
                               2370                 :           5963 :     *unsafe_initplans_p = unsafe_initplans;
                               2371                 :           5963 : }
                               2372                 :                : 
                               2373                 :                : /*
                               2374                 :                :  * SS_attach_initplans - attach initplans to topmost plan node
                               2375                 :                :  *
                               2376                 :                :  * Attach any initplans created in the current query level to the specified
                               2377                 :                :  * plan node, which should normally be the topmost node for the query level.
                               2378                 :                :  * (In principle the initPlans could go in any node at or above where they're
                               2379                 :                :  * referenced; but there seems no reason to put them any lower than the
                               2380                 :                :  * topmost node, so we don't bother to track exactly where they came from.)
                               2381                 :                :  *
                               2382                 :                :  * We do not touch the plan node's cost or parallel_safe flag.  The initplans
                               2383                 :                :  * must have been accounted for in SS_charge_for_initplans, or by any later
                               2384                 :                :  * code that adds initplans via SS_make_initplan_from_plan.
                               2385                 :                :  */
                               2386                 :                : void
 3470                          2387                 :         256226 : SS_attach_initplans(PlannerInfo *root, Plan *plan)
                               2388                 :                : {
                               2389                 :         256226 :     plan->initPlan = root->init_plans;
 8245                          2390                 :         256226 : }
                               2391                 :                : 
                               2392                 :                : /*
                               2393                 :                :  * SS_finalize_plan - do final parameter processing for a completed Plan.
                               2394                 :                :  *
                               2395                 :                :  * This recursively computes the extParam and allParam sets for every Plan
                               2396                 :                :  * node in the given plan tree.  (Oh, and RangeTblFunction.funcparams too.)
                               2397                 :                :  *
                               2398                 :                :  * We assume that SS_finalize_plan has already been run on any initplans or
                               2399                 :                :  * subplans the plan tree could reference.
                               2400                 :                :  */
                               2401                 :                : void
 3679                          2402                 :          96979 : SS_finalize_plan(PlannerInfo *root, Plan *plan)
                               2403                 :                : {
                               2404                 :                :     /* No setup needed, just recurse through plan tree. */
 2929                          2405                 :          96979 :     (void) finalize_plan(root, plan, -1, root->outer_params, NULL);
 3679                          2406                 :          96979 : }
                               2407                 :                : 
                               2408                 :                : /*
                               2409                 :                :  * Recursive processing of all nodes in the plan tree
                               2410                 :                :  *
                               2411                 :                :  * gather_param is the rescan_param of an ancestral Gather/GatherMerge,
                               2412                 :                :  * or -1 if there is none.
                               2413                 :                :  *
                               2414                 :                :  * valid_params is the set of param IDs supplied by outer plan levels
                               2415                 :                :  * that are valid to reference in this plan node or its children.
                               2416                 :                :  *
                               2417                 :                :  * scan_params is a set of param IDs to force scan plan nodes to reference.
                               2418                 :                :  * This is for EvalPlanQual support, and is always NULL at the top of the
                               2419                 :                :  * recursion.
                               2420                 :                :  *
                               2421                 :                :  * The return value is the computed allParam set for the given Plan node.
                               2422                 :                :  * This is just an internal notational convenience: we can add a child
                               2423                 :                :  * plan's allParams to the set of param IDs of interest to this level
                               2424                 :                :  * in the same statement that recurses to that child.
                               2425                 :                :  *
                               2426                 :                :  * Do not scribble on caller's values of valid_params or scan_params!
                               2427                 :                :  *
                               2428                 :                :  * Note: although we attempt to deal with initPlans anywhere in the tree, the
                               2429                 :                :  * logic is not really right.  The problem is that a plan node might return an
                               2430                 :                :  * output Param of its initPlan as a targetlist item, in which case it's valid
                               2431                 :                :  * for the parent plan level to reference that same Param; the parent's usage
                               2432                 :                :  * will be converted into a Var referencing the child plan node by setrefs.c.
                               2433                 :                :  * But this function would see the parent's reference as out of scope and
                               2434                 :                :  * complain about it.  For now, this does not matter because the planner only
                               2435                 :                :  * attaches initPlans to the topmost plan node in a query level, so the case
                               2436                 :                :  * doesn't arise.  If we ever merge this processing into setrefs.c, maybe it
                               2437                 :                :  * can be handled more cleanly.
                               2438                 :                :  */
                               2439                 :                : static Bitmapset *
 2929                          2440                 :         731690 : finalize_plan(PlannerInfo *root, Plan *plan,
                               2441                 :                :               int gather_param,
                               2442                 :                :               Bitmapset *valid_params,
                               2443                 :                :               Bitmapset *scan_params)
                               2444                 :                : {
                               2445                 :                :     finalize_primnode_context context;
                               2446                 :                :     int         locally_added_param;
                               2447                 :                :     Bitmapset  *nestloop_params;
                               2448                 :                :     Bitmapset  *initExtParam;
                               2449                 :                :     Bitmapset  *initSetParam;
                               2450                 :                :     Bitmapset  *child_params;
                               2451                 :                :     ListCell   *l;
                               2452                 :                : 
10054 bruce@momjian.us         2453         [ +  + ]:         731690 :     if (plan == NULL)
 8245 tgl@sss.pgh.pa.us        2454                 :         425804 :         return NULL;
                               2455                 :                : 
 6771                          2456                 :         305886 :     context.root = root;
 8245                          2457                 :         305886 :     context.paramids = NULL;    /* initialize set to empty */
 5794                          2458                 :         305886 :     locally_added_param = -1;   /* there isn't one */
 5535                          2459                 :         305886 :     nestloop_params = NULL;     /* there aren't any */
                               2460                 :                : 
                               2461                 :                :     /*
                               2462                 :                :      * Examine any initPlans to determine the set of external params they
                               2463                 :                :      * reference and the set of output params they supply.  (We assume
                               2464                 :                :      * SS_finalize_plan was run on them already.)
                               2465                 :                :      */
 3679                          2466                 :         305886 :     initExtParam = initSetParam = NULL;
                               2467   [ +  +  +  +  :         312540 :     foreach(l, plan->initPlan)
                                              +  + ]
                               2468                 :                :     {
                               2469                 :           6654 :         SubPlan    *initsubplan = (SubPlan *) lfirst(l);
                               2470                 :           6654 :         Plan       *initplan = planner_subplan_get_plan(root, initsubplan);
                               2471                 :                :         ListCell   *l2;
                               2472                 :                : 
                               2473                 :           6654 :         initExtParam = bms_add_members(initExtParam, initplan->extParam);
                               2474   [ +  -  +  +  :          13332 :         foreach(l2, initsubplan->setParam)
                                              +  + ]
                               2475                 :                :         {
                               2476                 :           6678 :             initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
                               2477                 :                :         }
                               2478                 :                :     }
                               2479                 :                : 
                               2480                 :                :     /* Any setParams are validly referenceable in this node and children */
                               2481         [ +  + ]:         305886 :     if (initSetParam)
                               2482                 :           6020 :         valid_params = bms_union(valid_params, initSetParam);
                               2483                 :                : 
                               2484                 :                :     /*
                               2485                 :                :      * When we call finalize_primnode, context.paramids sets are automatically
                               2486                 :                :      * merged together.  But when recursing to self, we have to do it the hard
                               2487                 :                :      * way.  We want the paramids set to include params in subplans as well as
                               2488                 :                :      * at this level.
                               2489                 :                :      */
                               2490                 :                : 
                               2491                 :                :     /* Find params in targetlist and qual */
 8245                          2492                 :         305886 :     finalize_primnode((Node *) plan->targetlist, &context);
                               2493                 :         305886 :     finalize_primnode((Node *) plan->qual, &context);
                               2494                 :                : 
                               2495                 :                :     /*
                               2496                 :                :      * If it's a parallel-aware scan node, mark it as dependent on the parent
                               2497                 :                :      * Gather/GatherMerge's rescan Param.
                               2498                 :                :      */
 2929                          2499         [ +  + ]:         305886 :     if (plan->parallel_aware)
                               2500                 :                :     {
                               2501         [ -  + ]:           1267 :         if (gather_param < 0)
 2929 tgl@sss.pgh.pa.us        2502         [ #  # ]:UBC           0 :             elog(ERROR, "parallel-aware plan node is not below a Gather");
 2929 tgl@sss.pgh.pa.us        2503                 :CBC        1267 :         context.paramids =
                               2504                 :           1267 :             bms_add_member(context.paramids, gather_param);
                               2505                 :                :     }
                               2506                 :                : 
                               2507                 :                :     /* Check additional node-type-specific fields */
10067 vadim4o@yahoo.com        2508   [ +  +  +  +  :         305886 :     switch (nodeTag(plan))
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                     +  +  +  -  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                           +  +  - ]
                               2509                 :                :     {
                               2510                 :          35563 :         case T_Result:
 9427 tgl@sss.pgh.pa.us        2511                 :          35563 :             finalize_primnode(((Result *) plan)->resconstantqual,
                               2512                 :                :                               &context);
10067 vadim4o@yahoo.com        2513                 :          35563 :             break;
                               2514                 :                : 
 5794 tgl@sss.pgh.pa.us        2515                 :          45580 :         case T_SeqScan:
 3696                          2516                 :          45580 :             context.paramids = bms_add_members(context.paramids, scan_params);
                               2517                 :          45580 :             break;
                               2518                 :                : 
 3767 simon@2ndQuadrant.co     2519                 :             52 :         case T_SampleScan:
 3696 tgl@sss.pgh.pa.us        2520                 :             52 :             finalize_primnode((Node *) ((SampleScan *) plan)->tablesample,
                               2521                 :                :                               &context);
 5794                          2522                 :             52 :             context.paramids = bms_add_members(context.paramids, scan_params);
                               2523                 :             52 :             break;
                               2524                 :                : 
 8512                          2525                 :          48487 :         case T_IndexScan:
 7439                          2526                 :          48487 :             finalize_primnode((Node *) ((IndexScan *) plan)->indexqual,
                               2527                 :                :                               &context);
 5392                          2528                 :          48487 :             finalize_primnode((Node *) ((IndexScan *) plan)->indexorderby,
                               2529                 :                :                               &context);
                               2530                 :                : 
                               2531                 :                :             /*
                               2532                 :                :              * we need not look at indexqualorig, since it will have the same
                               2533                 :                :              * param references as indexqual.  Likewise, we can ignore
                               2534                 :                :              * indexorderbyorig.
                               2535                 :                :              */
 5794                          2536                 :          48487 :             context.paramids = bms_add_members(context.paramids, scan_params);
 8512                          2537                 :          48487 :             break;
                               2538                 :                : 
 5079                          2539                 :           3760 :         case T_IndexOnlyScan:
                               2540                 :           3760 :             finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexqual,
                               2541                 :                :                               &context);
 1342                          2542                 :           3760 :             finalize_primnode((Node *) ((IndexOnlyScan *) plan)->recheckqual,
                               2543                 :                :                               &context);
 5079                          2544                 :           3760 :             finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexorderby,
                               2545                 :                :                               &context);
                               2546                 :                : 
                               2547                 :                :             /*
                               2548                 :                :              * we need not look at indextlist, since it cannot contain Params.
                               2549                 :                :              */
                               2550                 :           3760 :             context.paramids = bms_add_members(context.paramids, scan_params);
                               2551                 :           3760 :             break;
                               2552                 :                : 
 7445                          2553                 :           3782 :         case T_BitmapIndexScan:
 7439                          2554                 :           3782 :             finalize_primnode((Node *) ((BitmapIndexScan *) plan)->indexqual,
                               2555                 :                :                               &context);
                               2556                 :                : 
                               2557                 :                :             /*
                               2558                 :                :              * we need not look at indexqualorig, since it will have the same
                               2559                 :                :              * param references as indexqual.
                               2560                 :                :              */
 7445                          2561                 :           3782 :             break;
                               2562                 :                : 
                               2563                 :           3584 :         case T_BitmapHeapScan:
                               2564                 :           3584 :             finalize_primnode((Node *) ((BitmapHeapScan *) plan)->bitmapqualorig,
                               2565                 :                :                               &context);
 5794                          2566                 :           3584 :             context.paramids = bms_add_members(context.paramids, scan_params);
 7445                          2567                 :           3584 :             break;
                               2568                 :                : 
 8512                          2569                 :            306 :         case T_TidScan:
 7224                          2570                 :            306 :             finalize_primnode((Node *) ((TidScan *) plan)->tidquals,
                               2571                 :                :                               &context);
 5794                          2572                 :            306 :             context.paramids = bms_add_members(context.paramids, scan_params);
10067 vadim4o@yahoo.com        2573                 :            306 :             break;
                               2574                 :                : 
 1652 drowley@postgresql.o     2575                 :             17 :         case T_TidRangeScan:
                               2576                 :             17 :             finalize_primnode((Node *) ((TidRangeScan *) plan)->tidrangequals,
                               2577                 :                :                               &context);
                               2578                 :             17 :             context.paramids = bms_add_members(context.paramids, scan_params);
                               2579                 :             17 :             break;
                               2580                 :                : 
 9102 tgl@sss.pgh.pa.us        2581                 :           9508 :         case T_SubqueryScan:
                               2582                 :                :             {
 3679                          2583                 :           9508 :                 SubqueryScan *sscan = (SubqueryScan *) plan;
                               2584                 :                :                 RelOptInfo *rel;
                               2585                 :                :                 Bitmapset  *subquery_params;
                               2586                 :                : 
                               2587                 :                :                 /* We must run finalize_plan on the subquery */
                               2588                 :           9508 :                 rel = find_base_rel(root, sscan->scan.scanrelid);
 2734 rhaas@postgresql.org     2589                 :           9508 :                 subquery_params = rel->subroot->outer_params;
                               2590         [ +  + ]:           9508 :                 if (gather_param >= 0)
                               2591                 :             12 :                     subquery_params = bms_add_member(bms_copy(subquery_params),
                               2592                 :                :                                                      gather_param);
                               2593                 :           9508 :                 finalize_plan(rel->subroot, sscan->subplan, gather_param,
                               2594                 :                :                               subquery_params, NULL);
                               2595                 :                : 
                               2596                 :                :                 /* Now we can add its extParams to the parent's params */
 3679 tgl@sss.pgh.pa.us        2597                 :          19016 :                 context.paramids = bms_add_members(context.paramids,
                               2598                 :           9508 :                                                    sscan->subplan->extParam);
                               2599                 :                :                 /* We need scan_params too, though */
                               2600                 :           9508 :                 context.paramids = bms_add_members(context.paramids,
                               2601                 :                :                                                    scan_params);
                               2602                 :                :             }
 9102                          2603                 :           9508 :             break;
                               2604                 :                : 
 8512                          2605                 :          11933 :         case T_FunctionScan:
                               2606                 :                :             {
 4307                          2607                 :          11933 :                 FunctionScan *fscan = (FunctionScan *) plan;
                               2608                 :                :                 ListCell   *lc;
                               2609                 :                : 
                               2610                 :                :                 /*
                               2611                 :                :                  * Call finalize_primnode independently on each function
                               2612                 :                :                  * expression, so that we can record which params are
                               2613                 :                :                  * referenced in each, in order to decide which need
                               2614                 :                :                  * re-evaluating during rescan.
                               2615                 :                :                  */
                               2616   [ +  -  +  +  :          23977 :                 foreach(lc, fscan->functions)
                                              +  + ]
                               2617                 :                :                 {
                               2618                 :          12044 :                     RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
                               2619                 :                :                     finalize_primnode_context funccontext;
                               2620                 :                : 
                               2621                 :          12044 :                     funccontext = context;
                               2622                 :          12044 :                     funccontext.paramids = NULL;
                               2623                 :                : 
                               2624                 :          12044 :                     finalize_primnode(rtfunc->funcexpr, &funccontext);
                               2625                 :                : 
                               2626                 :                :                     /* remember results for execution */
                               2627                 :          12044 :                     rtfunc->funcparams = funccontext.paramids;
                               2628                 :                : 
                               2629                 :                :                     /* add the function's params to the overall set */
                               2630                 :          12044 :                     context.paramids = bms_add_members(context.paramids,
                               2631                 :          12044 :                                                        funccontext.paramids);
                               2632                 :                :                 }
                               2633                 :                : 
                               2634                 :          11933 :                 context.paramids = bms_add_members(context.paramids,
                               2635                 :                :                                                    scan_params);
                               2636                 :                :             }
 8512                          2637                 :          11933 :             break;
                               2638                 :                : 
 3104 alvherre@alvh.no-ip.     2639                 :            117 :         case T_TableFuncScan:
                               2640                 :            117 :             finalize_primnode((Node *) ((TableFuncScan *) plan)->tablefunc,
                               2641                 :                :                               &context);
                               2642                 :            117 :             context.paramids = bms_add_members(context.paramids, scan_params);
                               2643                 :            117 :             break;
                               2644                 :                : 
 6975 mail@joeconway.com       2645                 :           2854 :         case T_ValuesScan:
 6774 tgl@sss.pgh.pa.us        2646                 :           2854 :             finalize_primnode((Node *) ((ValuesScan *) plan)->values_lists,
                               2647                 :                :                               &context);
 5794                          2648                 :           2854 :             context.paramids = bms_add_members(context.paramids, scan_params);
 6975 mail@joeconway.com       2649                 :           2854 :             break;
                               2650                 :                : 
 6181 tgl@sss.pgh.pa.us        2651                 :           2116 :         case T_CteScan:
                               2652                 :                :             {
                               2653                 :                :                 /*
                               2654                 :                :                  * You might think we should add the node's cteParam to
                               2655                 :                :                  * paramids, but we shouldn't because that param is just a
                               2656                 :                :                  * linkage mechanism for multiple CteScan nodes for the same
                               2657                 :                :                  * CTE; it is never used for changed-param signaling.  What we
                               2658                 :                :                  * have to do instead is to find the referenced CTE plan and
                               2659                 :                :                  * incorporate its external paramids, so that the correct
                               2660                 :                :                  * things will happen if the CTE references outer-level
                               2661                 :                :                  * variables.  See test cases for bug #4902.  (We assume
                               2662                 :                :                  * SS_finalize_plan was run on the CTE plan already.)
                               2663                 :                :                  */
 5671 bruce@momjian.us         2664                 :           2116 :                 int         plan_id = ((CteScan *) plan)->ctePlanId;
                               2665                 :                :                 Plan       *cteplan;
                               2666                 :                : 
                               2667                 :                :                 /* so, do this ... */
 5906 tgl@sss.pgh.pa.us        2668   [ +  -  -  + ]:           2116 :                 if (plan_id < 1 || plan_id > list_length(root->glob->subplans))
 5906 tgl@sss.pgh.pa.us        2669         [ #  # ]:UBC           0 :                     elog(ERROR, "could not find plan for CteScan referencing plan ID %d",
                               2670                 :                :                          plan_id);
 5906 tgl@sss.pgh.pa.us        2671                 :CBC        2116 :                 cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
                               2672                 :           2116 :                 context.paramids =
                               2673                 :           2116 :                     bms_add_members(context.paramids, cteplan->extParam);
                               2674                 :                : 
                               2675                 :                : #ifdef NOT_USED
                               2676                 :                :                 /* ... but not this */
                               2677                 :                :                 context.paramids =
                               2678                 :                :                     bms_add_member(context.paramids,
                               2679                 :                :                                    ((CteScan *) plan)->cteParam);
                               2680                 :                : #endif
                               2681                 :                : 
 5794                          2682                 :           2116 :                 context.paramids = bms_add_members(context.paramids,
                               2683                 :                :                                                    scan_params);
                               2684                 :                :             }
 6181                          2685                 :           2116 :             break;
                               2686                 :                : 
                               2687                 :            463 :         case T_WorkTableScan:
                               2688                 :            463 :             context.paramids =
                               2689                 :            463 :                 bms_add_member(context.paramids,
                               2690                 :                :                                ((WorkTableScan *) plan)->wtParam);
 5794                          2691                 :            463 :             context.paramids = bms_add_members(context.paramids, scan_params);
 6181                          2692                 :            463 :             break;
                               2693                 :                : 
 3081 kgrittn@postgresql.o     2694                 :            206 :         case T_NamedTuplestoreScan:
                               2695                 :            206 :             context.paramids = bms_add_members(context.paramids, scan_params);
                               2696                 :            206 :             break;
                               2697                 :                : 
 5312 tgl@sss.pgh.pa.us        2698                 :            410 :         case T_ForeignScan:
                               2699                 :                :             {
 3614 rhaas@postgresql.org     2700                 :            410 :                 ForeignScan *fscan = (ForeignScan *) plan;
                               2701                 :                : 
                               2702                 :            410 :                 finalize_primnode((Node *) fscan->fdw_exprs,
                               2703                 :                :                                   &context);
                               2704                 :            410 :                 finalize_primnode((Node *) fscan->fdw_recheck_quals,
                               2705                 :                :                                   &context);
                               2706                 :                : 
                               2707                 :                :                 /* We assume fdw_scan_tlist cannot contain Params */
                               2708                 :            410 :                 context.paramids = bms_add_members(context.paramids,
                               2709                 :                :                                                    scan_params);
                               2710                 :                :             }
 5312 tgl@sss.pgh.pa.us        2711                 :            410 :             break;
                               2712                 :                : 
 3956 rhaas@postgresql.org     2713                 :UBC           0 :         case T_CustomScan:
                               2714                 :                :             {
 3725                          2715                 :              0 :                 CustomScan *cscan = (CustomScan *) plan;
                               2716                 :                :                 ListCell   *lc;
                               2717                 :                : 
                               2718                 :              0 :                 finalize_primnode((Node *) cscan->custom_exprs,
                               2719                 :                :                                   &context);
                               2720                 :                :                 /* We assume custom_scan_tlist cannot contain Params */
                               2721                 :              0 :                 context.paramids =
                               2722                 :              0 :                     bms_add_members(context.paramids, scan_params);
                               2723                 :                : 
                               2724                 :                :                 /* child nodes if any */
 3696 tgl@sss.pgh.pa.us        2725   [ #  #  #  #  :              0 :                 foreach(lc, cscan->custom_plans)
                                              #  # ]
                               2726                 :                :                 {
 3725 rhaas@postgresql.org     2727                 :              0 :                     context.paramids =
                               2728                 :              0 :                         bms_add_members(context.paramids,
                               2729                 :              0 :                                         finalize_plan(root,
                               2730                 :              0 :                                                       (Plan *) lfirst(lc),
                               2731                 :                :                                                       gather_param,
                               2732                 :                :                                                       valid_params,
                               2733                 :                :                                                       scan_params));
                               2734                 :                :                 }
                               2735                 :                :             }
 3956                          2736                 :              0 :             break;
                               2737                 :                : 
 5810 tgl@sss.pgh.pa.us        2738                 :CBC       42882 :         case T_ModifyTable:
                               2739                 :                :             {
 5794                          2740                 :          42882 :                 ModifyTable *mtplan = (ModifyTable *) plan;
                               2741                 :                : 
                               2742                 :                :                 /* Force descendant scan nodes to reference epqParam */
                               2743                 :          42882 :                 locally_added_param = mtplan->epqParam;
                               2744                 :          42882 :                 valid_params = bms_add_member(bms_copy(valid_params),
                               2745                 :                :                                               locally_added_param);
                               2746                 :          42882 :                 scan_params = bms_add_member(bms_copy(scan_params),
                               2747                 :                :                                              locally_added_param);
                               2748                 :          42882 :                 finalize_primnode((Node *) mtplan->returningLists,
                               2749                 :                :                                   &context);
 3774 andres@anarazel.de       2750                 :          42882 :                 finalize_primnode((Node *) mtplan->onConflictSet,
                               2751                 :                :                                   &context);
                               2752                 :          42882 :                 finalize_primnode((Node *) mtplan->onConflictWhere,
                               2753                 :                :                                   &context);
                               2754                 :                :                 /* exclRelTlist contains only Vars, doesn't need examination */
                               2755                 :                :             }
 5810 tgl@sss.pgh.pa.us        2756                 :          42882 :             break;
                               2757                 :                : 
 8512                          2758                 :           5206 :         case T_Append:
                               2759                 :                :             {
 7773 neilc@samurai.com        2760   [ +  -  +  +  :          18016 :                 foreach(l, ((Append *) plan)->appendplans)
                                              +  + ]
                               2761                 :                :                 {
                               2762                 :          12810 :                     context.paramids =
                               2763                 :          12810 :                         bms_add_members(context.paramids,
 6771 tgl@sss.pgh.pa.us        2764                 :          12810 :                                         finalize_plan(root,
                               2765                 :          12810 :                                                       (Plan *) lfirst(l),
                               2766                 :                :                                                       gather_param,
                               2767                 :                :                                                       valid_params,
                               2768                 :                :                                                       scan_params));
                               2769                 :                :                 }
                               2770                 :                :             }
10067 vadim4o@yahoo.com        2771                 :           5206 :             break;
                               2772                 :                : 
 5441 tgl@sss.pgh.pa.us        2773                 :             72 :         case T_MergeAppend:
                               2774                 :                :             {
                               2775   [ +  -  +  +  :            297 :                 foreach(l, ((MergeAppend *) plan)->mergeplans)
                                              +  + ]
                               2776                 :                :                 {
                               2777                 :            225 :                     context.paramids =
                               2778                 :            225 :                         bms_add_members(context.paramids,
                               2779                 :            225 :                                         finalize_plan(root,
                               2780                 :            225 :                                                       (Plan *) lfirst(l),
                               2781                 :                :                                                       gather_param,
                               2782                 :                :                                                       valid_params,
                               2783                 :                :                                                       scan_params));
                               2784                 :                :                 }
                               2785                 :                :             }
                               2786                 :             72 :             break;
                               2787                 :                : 
 7445                          2788                 :             85 :         case T_BitmapAnd:
                               2789                 :                :             {
                               2790   [ +  -  +  +  :            255 :                 foreach(l, ((BitmapAnd *) plan)->bitmapplans)
                                              +  + ]
                               2791                 :                :                 {
                               2792                 :            170 :                     context.paramids =
                               2793                 :            170 :                         bms_add_members(context.paramids,
 6771                          2794                 :            170 :                                         finalize_plan(root,
                               2795                 :            170 :                                                       (Plan *) lfirst(l),
                               2796                 :                :                                                       gather_param,
                               2797                 :                :                                                       valid_params,
                               2798                 :                :                                                       scan_params));
                               2799                 :                :                 }
                               2800                 :                :             }
 7445                          2801                 :             85 :             break;
                               2802                 :                : 
                               2803                 :            113 :         case T_BitmapOr:
                               2804                 :                :             {
                               2805   [ +  -  +  +  :            339 :                 foreach(l, ((BitmapOr *) plan)->bitmapplans)
                                              +  + ]
                               2806                 :                :                 {
                               2807                 :            226 :                     context.paramids =
                               2808                 :            226 :                         bms_add_members(context.paramids,
 6771                          2809                 :            226 :                                         finalize_plan(root,
                               2810                 :            226 :                                                       (Plan *) lfirst(l),
                               2811                 :                :                                                       gather_param,
                               2812                 :                :                                                       valid_params,
                               2813                 :                :                                                       scan_params));
                               2814                 :                :                 }
                               2815                 :                :             }
 7445                          2816                 :            113 :             break;
                               2817                 :                : 
 9125                          2818                 :          33576 :         case T_NestLoop:
                               2819                 :                :             {
 5535                          2820                 :          33576 :                 finalize_primnode((Node *) ((Join *) plan)->joinqual,
                               2821                 :                :                                   &context);
                               2822                 :                :                 /* collect set of params that will be passed to right child */
                               2823   [ +  +  +  +  :          58548 :                 foreach(l, ((NestLoop *) plan)->nestParams)
                                              +  + ]
                               2824                 :                :                 {
                               2825                 :          24972 :                     NestLoopParam *nlp = (NestLoopParam *) lfirst(l);
                               2826                 :                : 
                               2827                 :          24972 :                     nestloop_params = bms_add_member(nestloop_params,
                               2828                 :                :                                                      nlp->paramno);
                               2829                 :                :                 }
                               2830                 :                :             }
 9125                          2831                 :          33576 :             break;
                               2832                 :                : 
10067 vadim4o@yahoo.com        2833                 :           2099 :         case T_MergeJoin:
 9125 tgl@sss.pgh.pa.us        2834                 :           2099 :             finalize_primnode((Node *) ((Join *) plan)->joinqual,
                               2835                 :                :                               &context);
 9427                          2836                 :           2099 :             finalize_primnode((Node *) ((MergeJoin *) plan)->mergeclauses,
                               2837                 :                :                               &context);
10067 vadim4o@yahoo.com        2838                 :           2099 :             break;
                               2839                 :                : 
                               2840                 :           9262 :         case T_HashJoin:
 9125 tgl@sss.pgh.pa.us        2841                 :           9262 :             finalize_primnode((Node *) ((Join *) plan)->joinqual,
                               2842                 :                :                               &context);
 9427                          2843                 :           9262 :             finalize_primnode((Node *) ((HashJoin *) plan)->hashclauses,
                               2844                 :                :                               &context);
10067 vadim4o@yahoo.com        2845                 :           9262 :             break;
                               2846                 :                : 
  809 tgl@sss.pgh.pa.us        2847                 :           9262 :         case T_Hash:
                               2848                 :           9262 :             finalize_primnode((Node *) ((Hash *) plan)->hashkeys,
                               2849                 :                :                               &context);
                               2850                 :           9262 :             break;
                               2851                 :                : 
 7788                          2852                 :           1137 :         case T_Limit:
                               2853                 :           1137 :             finalize_primnode(((Limit *) plan)->limitOffset,
                               2854                 :                :                               &context);
                               2855                 :           1137 :             finalize_primnode(((Limit *) plan)->limitCount,
                               2856                 :                :                               &context);
                               2857                 :           1137 :             break;
                               2858                 :                : 
 6181                          2859                 :            463 :         case T_RecursiveUnion:
                               2860                 :                :             /* child nodes are allowed to reference wtParam */
 5794                          2861                 :            463 :             locally_added_param = ((RecursiveUnion *) plan)->wtParam;
                               2862                 :            463 :             valid_params = bms_add_member(bms_copy(valid_params),
                               2863                 :                :                                           locally_added_param);
                               2864                 :                :             /* wtParam does *not* get added to scan_params */
                               2865                 :            463 :             break;
                               2866                 :                : 
                               2867                 :           3854 :         case T_LockRows:
                               2868                 :                :             /* Force descendant scan nodes to reference epqParam */
                               2869                 :           3854 :             locally_added_param = ((LockRows *) plan)->epqParam;
                               2870                 :           3854 :             valid_params = bms_add_member(bms_copy(valid_params),
                               2871                 :                :                                           locally_added_param);
                               2872                 :           3854 :             scan_params = bms_add_member(bms_copy(scan_params),
                               2873                 :                :                                          locally_added_param);
                               2874                 :           3854 :             break;
                               2875                 :                : 
 3300                          2876                 :           5121 :         case T_Agg:
                               2877                 :                :             {
                               2878                 :           5121 :                 Agg        *agg = (Agg *) plan;
                               2879                 :                : 
                               2880                 :                :                 /*
                               2881                 :                :                  * AGG_HASHED plans need to know which Params are referenced
                               2882                 :                :                  * in aggregate calls.  Do a separate scan to identify them.
                               2883                 :                :                  */
                               2884         [ +  + ]:           5121 :                 if (agg->aggstrategy == AGG_HASHED)
                               2885                 :                :                 {
                               2886                 :                :                     finalize_primnode_context aggcontext;
                               2887                 :                : 
                               2888                 :            642 :                     aggcontext.root = root;
                               2889                 :            642 :                     aggcontext.paramids = NULL;
                               2890                 :            642 :                     finalize_agg_primnode((Node *) agg->plan.targetlist,
                               2891                 :                :                                           &aggcontext);
                               2892                 :            642 :                     finalize_agg_primnode((Node *) agg->plan.qual,
                               2893                 :                :                                           &aggcontext);
                               2894                 :            642 :                     agg->aggParams = aggcontext.paramids;
                               2895                 :                :                 }
                               2896                 :                :             }
                               2897                 :           5121 :             break;
                               2898                 :                : 
 5685                          2899                 :             61 :         case T_WindowAgg:
                               2900                 :             61 :             finalize_primnode(((WindowAgg *) plan)->startOffset,
                               2901                 :                :                               &context);
                               2902                 :             61 :             finalize_primnode(((WindowAgg *) plan)->endOffset,
                               2903                 :                :                               &context);
                               2904                 :             61 :             break;
                               2905                 :                : 
 2929                          2906                 :            478 :         case T_Gather:
                               2907                 :                :             /* child nodes are allowed to reference rescan_param, if any */
                               2908                 :            478 :             locally_added_param = ((Gather *) plan)->rescan_param;
                               2909         [ +  + ]:            478 :             if (locally_added_param >= 0)
                               2910                 :                :             {
                               2911                 :            475 :                 valid_params = bms_add_member(bms_copy(valid_params),
                               2912                 :                :                                               locally_added_param);
                               2913                 :                : 
                               2914                 :                :                 /*
                               2915                 :                :                  * We currently don't support nested Gathers.  The issue so
                               2916                 :                :                  * far as this function is concerned would be how to identify
                               2917                 :                :                  * which child nodes depend on which Gather.
                               2918                 :                :                  */
                               2919         [ -  + ]:            475 :                 Assert(gather_param < 0);
                               2920                 :                :                 /* Pass down rescan_param to child parallel-aware nodes */
                               2921                 :            475 :                 gather_param = locally_added_param;
                               2922                 :                :             }
                               2923                 :                :             /* rescan_param does *not* get added to scan_params */
                               2924                 :            478 :             break;
                               2925                 :                : 
                               2926                 :            171 :         case T_GatherMerge:
                               2927                 :                :             /* child nodes are allowed to reference rescan_param, if any */
                               2928                 :            171 :             locally_added_param = ((GatherMerge *) plan)->rescan_param;
                               2929         [ +  - ]:            171 :             if (locally_added_param >= 0)
                               2930                 :                :             {
                               2931                 :            171 :                 valid_params = bms_add_member(bms_copy(valid_params),
                               2932                 :                :                                               locally_added_param);
                               2933                 :                : 
                               2934                 :                :                 /*
                               2935                 :                :                  * We currently don't support nested Gathers.  The issue so
                               2936                 :                :                  * far as this function is concerned would be how to identify
                               2937                 :                :                  * which child nodes depend on which Gather.
                               2938                 :                :                  */
                               2939         [ -  + ]:            171 :                 Assert(gather_param < 0);
                               2940                 :                :                 /* Pass down rescan_param to child parallel-aware nodes */
                               2941                 :            171 :                 gather_param = locally_added_param;
                               2942                 :                :             }
                               2943                 :                :             /* rescan_param does *not* get added to scan_params */
                               2944                 :            171 :             break;
                               2945                 :                : 
 1515 drowley@postgresql.o     2946                 :           1014 :         case T_Memoize:
                               2947                 :           1014 :             finalize_primnode((Node *) ((Memoize *) plan)->param_exprs,
                               2948                 :                :                               &context);
 1618                          2949                 :           1014 :             break;
                               2950                 :                : 
 3153 andres@anarazel.de       2951                 :          22292 :         case T_ProjectSet:
                               2952                 :                :         case T_Material:
                               2953                 :                :         case T_Sort:
                               2954                 :                :         case T_IncrementalSort:
                               2955                 :                :         case T_Unique:
                               2956                 :                :         case T_SetOp:
                               2957                 :                :         case T_Group:
                               2958                 :                :             /* no node-type-specific fields need fixing */
10067 vadim4o@yahoo.com        2959                 :          22292 :             break;
                               2960                 :                : 
10067 vadim4o@yahoo.com        2961                 :UBC           0 :         default:
 8079 tgl@sss.pgh.pa.us        2962         [ #  # ]:              0 :             elog(ERROR, "unrecognized node type: %d",
                               2963                 :                :                  (int) nodeTag(plan));
                               2964                 :                :     }
                               2965                 :                : 
                               2966                 :                :     /* Process left and right child plans, if any */
 5535 tgl@sss.pgh.pa.us        2967                 :CBC      305886 :     child_params = finalize_plan(root,
                               2968                 :         305886 :                                  plan->lefttree,
                               2969                 :                :                                  gather_param,
                               2970                 :                :                                  valid_params,
                               2971                 :                :                                  scan_params);
                               2972                 :         305886 :     context.paramids = bms_add_members(context.paramids, child_params);
                               2973                 :                : 
                               2974         [ +  + ]:         305886 :     if (nestloop_params)
                               2975                 :                :     {
                               2976                 :                :         /* right child can reference nestloop_params as well as valid_params */
                               2977                 :          22507 :         child_params = finalize_plan(root,
                               2978                 :          22507 :                                      plan->righttree,
                               2979                 :                :                                      gather_param,
                               2980                 :                :                                      bms_union(nestloop_params, valid_params),
                               2981                 :                :                                      scan_params);
                               2982                 :                :         /* ... and they don't count as parameters used at my level */
                               2983                 :          22507 :         child_params = bms_difference(child_params, nestloop_params);
                               2984                 :          22507 :         bms_free(nestloop_params);
                               2985                 :                :     }
                               2986                 :                :     else
                               2987                 :                :     {
                               2988                 :                :         /* easy case */
                               2989                 :         283379 :         child_params = finalize_plan(root,
                               2990                 :         283379 :                                      plan->righttree,
                               2991                 :                :                                      gather_param,
                               2992                 :                :                                      valid_params,
                               2993                 :                :                                      scan_params);
                               2994                 :                :     }
                               2995                 :         305886 :     context.paramids = bms_add_members(context.paramids, child_params);
                               2996                 :                : 
                               2997                 :                :     /*
                               2998                 :                :      * Any locally generated parameter doesn't count towards its generating
                               2999                 :                :      * plan node's external dependencies.  (Note: if we changed valid_params
                               3000                 :                :      * and/or scan_params, we leak those bitmapsets; not worth the notational
                               3001                 :                :      * trouble to clean them up.)
                               3002                 :                :      */
 5794                          3003         [ +  + ]:         305886 :     if (locally_added_param >= 0)
                               3004                 :                :     {
 6181                          3005                 :          47845 :         context.paramids = bms_del_member(context.paramids,
                               3006                 :                :                                           locally_added_param);
                               3007                 :                :     }
                               3008                 :                : 
                               3009                 :                :     /* Now we have all the paramids referenced in this node and children */
                               3010                 :                : 
 8245                          3011         [ -  + ]:         305886 :     if (!bms_is_subset(context.paramids, valid_params))
 8079 tgl@sss.pgh.pa.us        3012         [ #  # ]:UBC           0 :         elog(ERROR, "plan should not reference subplan's variable");
                               3013                 :                : 
                               3014                 :                :     /*
                               3015                 :                :      * The plan node's allParam and extParam fields should include all its
                               3016                 :                :      * referenced paramids, plus contributions from any child initPlans.
                               3017                 :                :      * However, any setParams of the initPlans should not be present in the
                               3018                 :                :      * parent node's extParams, only in its allParams.  (It's possible that
                               3019                 :                :      * some initPlans have extParams that are setParams of other initPlans.)
                               3020                 :                :      */
                               3021                 :                : 
                               3022                 :                :     /* allParam must include initplans' extParams and setParams */
 3679 tgl@sss.pgh.pa.us        3023                 :CBC      305886 :     plan->allParam = bms_union(context.paramids, initExtParam);
                               3024                 :         305886 :     plan->allParam = bms_add_members(plan->allParam, initSetParam);
                               3025                 :                :     /* extParam must include any initplan extParams */
                               3026                 :         305886 :     plan->extParam = bms_union(context.paramids, initExtParam);
                               3027                 :                :     /* but not any initplan setParams */
                               3028                 :         305886 :     plan->extParam = bms_del_members(plan->extParam, initSetParam);
                               3029                 :                : 
 8245                          3030                 :         305886 :     return plan->allParam;
                               3031                 :                : }
                               3032                 :                : 
                               3033                 :                : /*
                               3034                 :                :  * finalize_primnode: add IDs of all PARAM_EXEC params that appear (or will
                               3035                 :                :  * appear) in the given expression tree to the result set.
                               3036                 :                :  */
                               3037                 :                : static bool
 8065 bruce@momjian.us         3038                 :        5157097 : finalize_primnode(Node *node, finalize_primnode_context *context)
                               3039                 :                : {
 8302 tgl@sss.pgh.pa.us        3040         [ +  + ]:        5157097 :     if (node == NULL)
                               3041                 :         608006 :         return false;
                               3042         [ +  + ]:        4549091 :     if (IsA(node, Param))
                               3043                 :                :     {
                               3044         [ +  + ]:          63950 :         if (((Param *) node)->paramkind == PARAM_EXEC)
                               3045                 :                :         {
 7077                          3046                 :          62883 :             int         paramid = ((Param *) node)->paramid;
                               3047                 :                : 
 8245                          3048                 :          62883 :             context->paramids = bms_add_member(context->paramids, paramid);
                               3049                 :                :         }
 8302                          3050                 :          63950 :         return false;           /* no more to do here */
                               3051                 :                :     }
  786                          3052         [ +  + ]:        4485141 :     else if (IsA(node, Aggref))
                               3053                 :                :     {
                               3054                 :                :         /*
                               3055                 :                :          * Check to see if the aggregate will be replaced by a Param
                               3056                 :                :          * referencing a subquery output during setrefs.c.  If so, we must
                               3057                 :                :          * account for that Param here.  (For various reasons, it's not
                               3058                 :                :          * convenient to perform that substitution earlier than setrefs.c, nor
                               3059                 :                :          * to perform this processing after setrefs.c.  Thus we need a wart
                               3060                 :                :          * here.)
                               3061                 :                :          */
                               3062                 :           6762 :         Aggref     *aggref = (Aggref *) node;
                               3063                 :                :         Param      *aggparam;
                               3064                 :                : 
                               3065                 :           6762 :         aggparam = find_minmax_agg_replacement_param(context->root, aggref);
                               3066         [ +  + ]:           6762 :         if (aggparam != NULL)
                               3067                 :            278 :             context->paramids = bms_add_member(context->paramids,
                               3068                 :                :                                                aggparam->paramid);
                               3069                 :                :         /* Fall through to examine the agg's arguments */
                               3070                 :                :     }
                               3071         [ +  + ]:        4478379 :     else if (IsA(node, SubPlan))
                               3072                 :                :     {
 8069 bruce@momjian.us         3073                 :          18248 :         SubPlan    *subplan = (SubPlan *) node;
 6771 tgl@sss.pgh.pa.us        3074                 :          18248 :         Plan       *plan = planner_subplan_get_plan(context->root, subplan);
                               3075                 :                :         ListCell   *lc;
                               3076                 :                :         Bitmapset  *subparamids;
                               3077                 :                : 
                               3078                 :                :         /* Recurse into the testexpr, but not into the Plan */
 6267                          3079                 :          18248 :         finalize_primnode(subplan->testexpr, context);
                               3080                 :                : 
                               3081                 :                :         /*
                               3082                 :                :          * Remove any param IDs of output parameters of the subplan that were
                               3083                 :                :          * referenced in the testexpr.  These are not interesting for
                               3084                 :                :          * parameter change signaling since we always re-evaluate the subplan.
                               3085                 :                :          * Note that this wouldn't work too well if there might be uses of the
                               3086                 :                :          * same param IDs elsewhere in the plan, but that can't happen because
                               3087                 :                :          * generate_new_exec_param never tries to merge params.
                               3088                 :                :          */
                               3089   [ +  +  +  +  :          19861 :         foreach(lc, subplan->paramIds)
                                              +  + ]
                               3090                 :                :         {
                               3091                 :           1613 :             context->paramids = bms_del_member(context->paramids,
                               3092                 :                :                                                lfirst_int(lc));
                               3093                 :                :         }
                               3094                 :                : 
                               3095                 :                :         /* Also examine args list */
                               3096                 :          18248 :         finalize_primnode((Node *) subplan->args, context);
                               3097                 :                : 
                               3098                 :                :         /*
                               3099                 :                :          * Add params needed by the subplan to paramids, but excluding those
                               3100                 :                :          * we will pass down to it.  (We assume SS_finalize_plan was run on
                               3101                 :                :          * the subplan already.)
                               3102                 :                :          */
                               3103                 :          18248 :         subparamids = bms_copy(plan->extParam);
                               3104   [ +  +  +  +  :          44223 :         foreach(lc, subplan->parParam)
                                              +  + ]
                               3105                 :                :         {
                               3106                 :          25975 :             subparamids = bms_del_member(subparamids, lfirst_int(lc));
                               3107                 :                :         }
                               3108                 :          18248 :         context->paramids = bms_join(context->paramids, subparamids);
                               3109                 :                : 
                               3110                 :          18248 :         return false;           /* no more to do here */
                               3111                 :                :     }
  282 peter@eisentraut.org     3112                 :        4466893 :     return expression_tree_walker(node, finalize_primnode, context);
                               3113                 :                : }
                               3114                 :                : 
                               3115                 :                : /*
                               3116                 :                :  * finalize_agg_primnode: find all Aggref nodes in the given expression tree,
                               3117                 :                :  * and add IDs of all PARAM_EXEC params appearing within their aggregated
                               3118                 :                :  * arguments to the result set.
                               3119                 :                :  */
                               3120                 :                : static bool
 3300 tgl@sss.pgh.pa.us        3121                 :           5011 : finalize_agg_primnode(Node *node, finalize_primnode_context *context)
                               3122                 :                : {
                               3123         [ +  + ]:           5011 :     if (node == NULL)
                               3124                 :            681 :         return false;
                               3125         [ +  + ]:           4330 :     if (IsA(node, Aggref))
                               3126                 :                :     {
                               3127                 :            572 :         Aggref     *agg = (Aggref *) node;
                               3128                 :                : 
                               3129                 :                :         /* we should not consider the direct arguments, if any */
                               3130                 :            572 :         finalize_primnode((Node *) agg->args, context);
                               3131                 :            572 :         finalize_primnode((Node *) agg->aggfilter, context);
                               3132                 :            572 :         return false;           /* there can't be any Aggrefs below here */
                               3133                 :                :     }
  282 peter@eisentraut.org     3134                 :           3758 :     return expression_tree_walker(node, finalize_agg_primnode, context);
                               3135                 :                : }
                               3136                 :                : 
                               3137                 :                : /*
                               3138                 :                :  * SS_make_initplan_output_param - make a Param for an initPlan's output
                               3139                 :                :  *
                               3140                 :                :  * The plan is expected to return a scalar value of the given type/collation.
                               3141                 :                :  *
                               3142                 :                :  * Note that in some cases the initplan may not ever appear in the finished
                               3143                 :                :  * plan tree.  If that happens, we'll have wasted a PARAM_EXEC slot, which
                               3144                 :                :  * is no big deal.
                               3145                 :                :  */
                               3146                 :                : Param *
 3470 tgl@sss.pgh.pa.us        3147                 :            223 : SS_make_initplan_output_param(PlannerInfo *root,
                               3148                 :                :                               Oid resulttype, int32 resulttypmod,
                               3149                 :                :                               Oid resultcollation)
                               3150                 :                : {
 2430                          3151                 :            223 :     return generate_new_exec_param(root, resulttype,
                               3152                 :                :                                    resulttypmod, resultcollation);
                               3153                 :                : }
                               3154                 :                : 
                               3155                 :                : /*
                               3156                 :                :  * SS_make_initplan_from_plan - given a plan tree, make it an InitPlan
                               3157                 :                :  *
                               3158                 :                :  * We build an EXPR_SUBLINK SubPlan node and put it into the initplan
                               3159                 :                :  * list for the outer query level.  A Param that represents the initplan's
                               3160                 :                :  * output has already been assigned using SS_make_initplan_output_param.
                               3161                 :                :  */
                               3162                 :                : void
 3679                          3163                 :            200 : SS_make_initplan_from_plan(PlannerInfo *root,
                               3164                 :                :                            PlannerInfo *subroot, Plan *plan,
                               3165                 :                :                            Param *prm)
                               3166                 :                : {
                               3167                 :                :     SubPlan    *node;
                               3168                 :                : 
                               3169                 :                :     /*
                               3170                 :                :      * Add the subplan and its PlannerInfo, as well as a dummy path entry, to
                               3171                 :                :      * the global lists.  Ideally we'd save a real path, but right now our
                               3172                 :                :      * sole caller doesn't build a path that exactly matches the plan.  Since
                               3173                 :                :      * we're not currently going to need the path for an initplan, it's not
                               3174                 :                :      * worth requiring construction of such a path.
                               3175                 :                :      */
 5117                          3176                 :            200 :     root->glob->subplans = lappend(root->glob->subplans, plan);
  529                          3177                 :            200 :     root->glob->subpaths = lappend(root->glob->subpaths, NULL);
 3679                          3178                 :            200 :     root->glob->subroots = lappend(root->glob->subroots, subroot);
                               3179                 :                : 
                               3180                 :                :     /*
                               3181                 :                :      * Create a SubPlan node and add it to the outer list of InitPlans. Note
                               3182                 :                :      * it has to appear after any other InitPlans it might depend on (see
                               3183                 :                :      * comments in ExecReScan).
                               3184                 :                :      */
 7453                          3185                 :            200 :     node = makeNode(SubPlan);
                               3186                 :            200 :     node->subLinkType = EXPR_SUBLINK;
 3470                          3187                 :            200 :     node->plan_id = list_length(root->glob->subplans);
  536                          3188                 :            200 :     node->plan_name = psprintf("InitPlan %d", node->plan_id);
 5285                          3189                 :            200 :     get_first_col_type(plan, &node->firstColType, &node->firstColTypmod,
                               3190                 :                :                        &node->firstColCollation);
  786                          3191                 :            200 :     node->parallel_safe = plan->parallel_safe;
 3470                          3192                 :            200 :     node->setParam = list_make1_int(prm->paramid);
                               3193                 :                : 
 6774                          3194                 :            200 :     root->init_plans = lappend(root->init_plans, node);
                               3195                 :                : 
                               3196                 :                :     /*
                               3197                 :                :      * The node can't have any inputs (since it's an initplan), so the
                               3198                 :                :      * parParam and args lists remain empty.
                               3199                 :                :      */
                               3200                 :                : 
                               3201                 :                :     /* Set costs of SubPlan using info from the plan tree */
 3679                          3202                 :            200 :     cost_subplan(subroot, node, plan);
 7453                          3203                 :            200 : }
        

Generated by: LCOV version 2.4-beta