@@ -1334,9 +1334,9 @@ adjust_partition_tlist(List *tlist, TupleConversionMap *map)
13341334 * Run-Time Partition Pruning Support.
13351335 *
13361336 * The following series of functions exist to support the removal of unneeded
1337- * subnodes for queries against partitioned tables. The supporting functions
1338- * here are designed to work with any node type which supports an arbitrary
1339- * number of subnodes , e.g. Append, MergeAppend.
1337+ * subplans for queries against partitioned tables. The supporting functions
1338+ * here are designed to work with any plan type which supports an arbitrary
1339+ * number of subplans , e.g. Append, MergeAppend.
13401340 *
13411341 * When pruning involves comparison of a partition key to a constant, it's
13421342 * done by the planner. However, if we have a comparison to a non-constant
@@ -1346,85 +1346,83 @@ adjust_partition_tlist(List *tlist, TupleConversionMap *map)
13461346 *
13471347 * We must distinguish expressions containing PARAM_EXEC Params from
13481348 * expressions that don't contain those. Even though a PARAM_EXEC Param is
1349- * considered to be a stable expression, it can change value from one node
1350- * scan to the next during query execution. Stable comparison expressions
1351- * that don't involve such Params allow partition pruning to be done once
1352- * during executor startup. Expressions that do involve such Params require
1353- * us to prune separately for each scan of the parent plan node.
1349+ * considered to be a stable expression, it can change value from one plan
1350+ * node scan to the next during query execution. Stable comparison
1351+ * expressions that don't involve such Params allow partition pruning to be
1352+ * done once during executor startup. Expressions that do involve such Params
1353+ * require us to prune separately for each scan of the parent plan node.
13541354 *
1355- * Note that pruning away unneeded subnodes during executor startup has the
1356- * added benefit of not having to initialize the unneeded subnodes at all.
1355+ * Note that pruning away unneeded subplans during executor startup has the
1356+ * added benefit of not having to initialize the unneeded subplans at all.
13571357 *
13581358 *
13591359 * Functions:
13601360 *
13611361 * ExecSetupPartitionPruneState:
1362- * This must be called by nodes before any partition pruning is
1363- * attempted. Normally executor startup is a good time. This function
1364- * creates the PartitionPruneState details which are required by each
1365- * of the two pruning functions, details include information about
1366- * how to map the partition index details which are returned by the
1367- * planner's partition prune function into subnode indexes.
1362+ * Creates the PartitionPruneState required by each of the two pruning
1363+ * functions. Details stored include how to map the partition index
1364+ * returned by the partition pruning code into subplan indexes.
13681365 *
13691366 * ExecFindInitialMatchingSubPlans:
1370- * Returns indexes of matching subnodes . Partition pruning is attempted
1367+ * Returns indexes of matching subplans . Partition pruning is attempted
13711368 * without any evaluation of expressions containing PARAM_EXEC Params.
1372- * This function must be called during executor startup for the given
1373- * node before the subnodes themselves are initialized. Subnodes which
1374- * are found not to match by this function must not be included in the
1375- * node 's list of subnodes as this function performs a remap of the
1376- * partition index to subplan index map and the newly created map
1377- * provides indexes only for subnodes which remain after calling this
1378- * function.
1369+ * This function must be called during executor startup for the parent
1370+ * plan before the subplans themselves are initialized. Subplans which
1371+ * are found not to match by this function must be removed from the
1372+ * plan 's list of subplans during execution, as this function performs a
1373+ * remap of the partition index to subplan index map and the newly
1374+ * created map provides indexes only for subplans which remain after
1375+ * calling this function.
13791376 *
13801377 * ExecFindMatchingSubPlans:
1381- * Returns indexes of matching subnodes after evaluating all available
1382- * expressions. This function can only be called while the executor is
1383- * running.
1378+ * Returns indexes of matching subplans after evaluating all available
1379+ * expressions. This function can only be called during execution and
1380+ * must be called again each time the value of a Param listed in
1381+ * PartitionPruneState's 'execparamids' changes.
13841382 *-------------------------------------------------------------------------
13851383 */
13861384
13871385/*
13881386 * ExecSetupPartitionPruneState
1389- * Setup the required data structure which is required for calling
1387+ * Set up the data structure required for calling
13901388 * ExecFindInitialMatchingSubPlans and ExecFindMatchingSubPlans.
13911389 *
1390+ * 'planstate' is the parent plan node's execution state.
1391+ *
13921392 * 'partitionpruneinfo' is a List of PartitionPruneInfos as generated by
1393- * make_partition_pruneinfo. Here we build a PartitionPruneContext for each
1394- * item in the List. These contexts can be re-used each time we re-evaulate
1395- * which partitions match the pruning steps provided in each
1396- * PartitionPruneInfo.
1393+ * make_partition_pruneinfo. Here we build a PartitionPruneState containing a
1394+ * PartitionPruningData for each item in that List. This data can be re-used
1395+ * each time we re-evaluate which partitions match the pruning steps provided
1396+ * in each PartitionPruneInfo.
13971397 */
13981398PartitionPruneState *
13991399ExecSetupPartitionPruneState (PlanState * planstate , List * partitionpruneinfo )
14001400{
1401- PartitionPruningData * prunedata ;
14021401 PartitionPruneState * prunestate ;
1402+ PartitionPruningData * prunedata ;
14031403 ListCell * lc ;
14041404 int i ;
14051405
14061406 Assert (partitionpruneinfo != NIL );
14071407
1408+ /*
1409+ * Allocate the data structure
1410+ */
14081411 prunestate = (PartitionPruneState * ) palloc (sizeof (PartitionPruneState ));
14091412 prunedata = (PartitionPruningData * )
14101413 palloc (sizeof (PartitionPruningData ) * list_length (partitionpruneinfo ));
14111414
1412- /*
1413- * The first item in the array contains the details for the query's target
1414- * partition, so record that as the root of the partition hierarchy.
1415- */
14161415 prunestate -> partprunedata = prunedata ;
14171416 prunestate -> num_partprunedata = list_length (partitionpruneinfo );
14181417 prunestate -> do_initial_prune = false; /* may be set below */
14191418 prunestate -> do_exec_prune = false; /* may be set below */
14201419 prunestate -> execparamids = NULL ;
14211420
14221421 /*
1423- * Create a sub memory context which we'll use when making calls to the
1424- * query planner's function to determine which partitions will match. The
1425- * planner is not too careful about freeing memory, so we'll ensure we
1426- * call the function in this context to avoid any memory leaking in the
1427- * executor's memory context.
1422+ * Create a short-term memory context which we'll use when making calls to
1423+ * the partition pruning functions. This avoids possible memory leaks,
1424+ * since the pruning functions call comparison functions that aren't under
1425+ * our control.
14281426 */
14291427 prunestate -> prune_context =
14301428 AllocSetContextCreate (CurrentMemoryContext ,
@@ -1448,8 +1446,8 @@ ExecSetupPartitionPruneState(PlanState *planstate, List *partitionpruneinfo)
14481446 * We must make a copy of this rather than pointing directly to the
14491447 * plan's version as we may end up making modifications to it later.
14501448 */
1451- pprune -> subnode_map = palloc (sizeof (int ) * pinfo -> nparts );
1452- memcpy (pprune -> subnode_map , pinfo -> subnode_map ,
1449+ pprune -> subplan_map = palloc (sizeof (int ) * pinfo -> nparts );
1450+ memcpy (pprune -> subplan_map , pinfo -> subplan_map ,
14531451 sizeof (int ) * pinfo -> nparts );
14541452
14551453 /* We can use the subpart_map verbatim, since we never modify it */
@@ -1525,7 +1523,7 @@ ExecSetupPartitionPruneState(PlanState *planstate, List *partitionpruneinfo)
15251523
15261524 /*
15271525 * Accumulate the IDs of all PARAM_EXEC Params affecting the
1528- * partitioning decisions at this node.
1526+ * partitioning decisions at this plan node.
15291527 */
15301528 prunestate -> execparamids = bms_add_members (prunestate -> execparamids ,
15311529 pinfo -> execparamids );
@@ -1540,22 +1538,19 @@ ExecSetupPartitionPruneState(PlanState *planstate, List *partitionpruneinfo)
15401538
15411539/*
15421540 * ExecFindInitialMatchingSubPlans
1543- * Determine which subset of subplan nodes we need to initialize based
1544- * on the details stored in 'prunestate'. Here we only determine the
1545- * matching partitions using values known during plan startup, which
1546- * excludes any expressions containing PARAM_EXEC Params.
1541+ * Identify the set of subplans that cannot be eliminated by initial
1542+ * pruning (disregarding any pruning constraints involving PARAM_EXEC
1543+ * Params). Also re-map the translation matrix which allows conversion
1544+ * of partition indexes into subplan indexes to account for the unneeded
1545+ * subplans having been removed.
15471546 *
1548- * It is expected that callers of this function do so only once during their
1549- * init plan. The caller must only initialize the subnodes which are returned
1550- * by this function. The remaining subnodes should be discarded. Once this
1551- * function has been called, future calls to ExecFindMatchingSubPlans will
1552- * return its matching subnode indexes assuming that the caller discarded
1553- * the original non-matching subnodes.
1547+ * Must only be called once per 'prunestate', and only if initial pruning
1548+ * is required.
15541549 *
1555- * 'nsubnodes ' must be passed as the total number of unpruned subnodes .
1550+ * 'nsubplans ' must be passed as the total number of unpruned subplans .
15561551 */
15571552Bitmapset *
1558- ExecFindInitialMatchingSubPlans (PartitionPruneState * prunestate , int nsubnodes )
1553+ ExecFindInitialMatchingSubPlans (PartitionPruneState * prunestate , int nsubplans )
15591554{
15601555 PartitionPruningData * pprune ;
15611556 MemoryContext oldcontext ;
@@ -1584,33 +1579,33 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubnodes)
15841579 ResetExprContext (pprune -> context .planstate -> ps_ExprContext );
15851580
15861581 /*
1587- * If any subnodes were pruned, we must re-sequence the subnode indexes so
1582+ * If any subplans were pruned, we must re-sequence the subplan indexes so
15881583 * that ExecFindMatchingSubPlans properly returns the indexes from the
1589- * subnodes which will remain after execution of this function.
1584+ * subplans which will remain after execution of this function.
15901585 */
1591- if (bms_num_members (result ) < nsubnodes )
1586+ if (bms_num_members (result ) < nsubplans )
15921587 {
1593- int * new_subnode_indexes ;
1588+ int * new_subplan_indexes ;
15941589 int i ;
15951590 int newidx ;
15961591
15971592 /*
15981593 * First we must build an array which we can use to adjust the
1599- * existing subnode_map so that it contains the new subnode indexes.
1594+ * existing subplan_map so that it contains the new subplan indexes.
16001595 */
1601- new_subnode_indexes = (int * ) palloc (sizeof (int ) * nsubnodes );
1596+ new_subplan_indexes = (int * ) palloc (sizeof (int ) * nsubplans );
16021597 newidx = 0 ;
1603- for (i = 0 ; i < nsubnodes ; i ++ )
1598+ for (i = 0 ; i < nsubplans ; i ++ )
16041599 {
16051600 if (bms_is_member (i , result ))
1606- new_subnode_indexes [i ] = newidx ++ ;
1601+ new_subplan_indexes [i ] = newidx ++ ;
16071602 else
1608- new_subnode_indexes [i ] = -1 ; /* Newly pruned */
1603+ new_subplan_indexes [i ] = -1 ; /* Newly pruned */
16091604 }
16101605
16111606 /*
1612- * Now we can re-sequence each PartitionPruneInfo's subnode_map so
1613- * that they point to the new index of the subnode .
1607+ * Now we can re-sequence each PartitionPruneInfo's subplan_map so
1608+ * that they point to the new index of the subplan .
16141609 */
16151610 for (i = 0 ; i < prunestate -> num_partprunedata ; i ++ )
16161611 {
@@ -1622,7 +1617,7 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubnodes)
16221617
16231618 /*
16241619 * We also need to reset the present_parts field so that it only
1625- * contains partition indexes that we actually still have subnodes
1620+ * contains partition indexes that we actually still have subplans
16261621 * for. It seems easier to build a fresh one, rather than trying
16271622 * to update the existing one.
16281623 */
@@ -1631,20 +1626,20 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubnodes)
16311626
16321627 for (j = 0 ; j < nparts ; j ++ )
16331628 {
1634- int oldidx = pprune -> subnode_map [j ];
1629+ int oldidx = pprune -> subplan_map [j ];
16351630
16361631 /*
1637- * If this partition existed as a subnode then change the old
1638- * subnode index to the new subnode index. The new index may
1632+ * If this partition existed as a subplan then change the old
1633+ * subplan index to the new subplan index. The new index may
16391634 * become -1 if the partition was pruned above, or it may just
1640- * come earlier in the subnode list due to some subnodes being
1635+ * come earlier in the subplan list due to some subplans being
16411636 * removed earlier in the list.
16421637 */
16431638 if (oldidx >= 0 )
16441639 {
1645- pprune -> subnode_map [j ] = new_subnode_indexes [oldidx ];
1640+ pprune -> subplan_map [j ] = new_subplan_indexes [oldidx ];
16461641
1647- if (new_subnode_indexes [oldidx ] >= 0 )
1642+ if (new_subplan_indexes [oldidx ] >= 0 )
16481643 pprune -> present_parts =
16491644 bms_add_member (pprune -> present_parts , j );
16501645 }
@@ -1686,7 +1681,7 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubnodes)
16861681 }
16871682 }
16881683
1689- pfree (new_subnode_indexes );
1684+ pfree (new_subplan_indexes );
16901685 }
16911686
16921687 return result ;
@@ -1695,7 +1690,7 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubnodes)
16951690/*
16961691 * ExecFindMatchingSubPlans
16971692 * Determine which subplans match the pruning steps detailed in
1698- * 'pprune ' for the current comparison expression values.
1693+ * 'prunestate ' for the current comparison expression values.
16991694 *
17001695 * Here we assume we may evaluate PARAM_EXEC Params.
17011696 */
@@ -1767,28 +1762,24 @@ find_matching_subplans_recurse(PartitionPruneState *prunestate,
17671762 partset = pprune -> present_parts ;
17681763 }
17691764
1770- /* Translate partset into subnode indexes */
1765+ /* Translate partset into subplan indexes */
17711766 i = -1 ;
17721767 while ((i = bms_next_member (partset , i )) >= 0 )
17731768 {
1774- if (pprune -> subnode_map [i ] >= 0 )
1769+ if (pprune -> subplan_map [i ] >= 0 )
17751770 * validsubplans = bms_add_member (* validsubplans ,
1776- pprune -> subnode_map [i ]);
1771+ pprune -> subplan_map [i ]);
17771772 else
17781773 {
17791774 int partidx = pprune -> subpart_map [i ];
17801775
1781- if (partidx != -1 )
1776+ if (partidx >= 0 )
17821777 find_matching_subplans_recurse (prunestate ,
17831778 & prunestate -> partprunedata [partidx ],
17841779 initial_prune , validsubplans );
17851780 else
17861781 {
1787- /*
1788- * This could only happen if clauses used in planning where
1789- * more restrictive than those used here, or if the maps are
1790- * somehow corrupt.
1791- */
1782+ /* Shouldn't happen */
17921783 elog (ERROR , "partition missing from subplans" );
17931784 }
17941785 }
0 commit comments