@@ -1442,8 +1442,8 @@ ExecSetupPartitionPruneState(PlanState *planstate, List *partitionpruneinfo)
14421442 int n_steps ;
14431443
14441444 /*
1445- * We must make a copy of this rather than pointing directly to the
1446- * plan's version as we may end up making modifications to it later.
1445+ * We must copy the subplan_map rather than pointing directly to the
1446+ * plan's version, as we may end up making modifications to it later.
14471447 */
14481448 pprune -> subplan_map = palloc (sizeof (int ) * pinfo -> nparts );
14491449 memcpy (pprune -> subplan_map , pinfo -> subplan_map ,
@@ -1589,8 +1589,8 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubplans)
15891589 int newidx ;
15901590
15911591 /*
1592- * First we must build an array which we can use to adjust the
1593- * existing subplan_map so that it contains the new subplan indexes .
1592+ * First we must build a temporary array which maps old subplan
1593+ * indexes to new ones .
15941594 */
15951595 new_subplan_indexes = (int * ) palloc (sizeof (int ) * nsubplans );
15961596 newidx = 0 ;
@@ -1603,79 +1603,55 @@ ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubplans)
16031603 }
16041604
16051605 /*
1606- * Now we can re-sequence each PartitionPruneInfo's subplan_map so
1607- * that they point to the new index of the subplan.
1606+ * Now we can update each PartitionPruneInfo's subplan_map with new
1607+ * subplan indexes. We must also recompute its present_parts bitmap.
1608+ * We perform this loop in back-to-front order so that we determine
1609+ * present_parts for the lowest-level partitioned tables first. This
1610+ * way we can tell whether a sub-partitioned table's partitions were
1611+ * entirely pruned so we can exclude that from 'present_parts'.
16081612 */
1609- for (i = 0 ; i < prunestate -> num_partprunedata ; i ++ )
1613+ for (i = prunestate -> num_partprunedata - 1 ; i >= 0 ; i -- )
16101614 {
16111615 int nparts ;
16121616 int j ;
16131617
16141618 pprune = & prunestate -> partprunedata [i ];
16151619 nparts = pprune -> context .nparts ;
1616-
1617- /*
1618- * We also need to reset the present_parts field so that it only
1619- * contains partition indexes that we actually still have subplans
1620- * for. It seems easier to build a fresh one, rather than trying
1621- * to update the existing one.
1622- */
1620+ /* We just rebuild present_parts from scratch */
16231621 bms_free (pprune -> present_parts );
16241622 pprune -> present_parts = NULL ;
16251623
16261624 for (j = 0 ; j < nparts ; j ++ )
16271625 {
16281626 int oldidx = pprune -> subplan_map [j ];
1627+ int subidx ;
16291628
16301629 /*
16311630 * If this partition existed as a subplan then change the old
16321631 * subplan index to the new subplan index. The new index may
16331632 * become -1 if the partition was pruned above, or it may just
16341633 * come earlier in the subplan list due to some subplans being
1635- * removed earlier in the list.
1634+ * removed earlier in the list. If it's a subpartition, add
1635+ * it to present_parts unless it's entirely pruned.
16361636 */
16371637 if (oldidx >= 0 )
16381638 {
1639+ Assert (oldidx < nsubplans );
16391640 pprune -> subplan_map [j ] = new_subplan_indexes [oldidx ];
16401641
16411642 if (new_subplan_indexes [oldidx ] >= 0 )
16421643 pprune -> present_parts =
16431644 bms_add_member (pprune -> present_parts , j );
16441645 }
1645- }
1646- }
1647-
1648- /*
1649- * Now we must determine which sub-partitioned tables still have
1650- * unpruned partitions. The easiest way to do this is to simply loop
1651- * over each PartitionPruningData again checking if there are any
1652- * 'present_parts' in the sub-partitioned table. We needn't bother
1653- * doing this if there are no sub-partitioned tables.
1654- */
1655- if (prunestate -> num_partprunedata > 1 )
1656- {
1657- for (i = 0 ; i < prunestate -> num_partprunedata ; i ++ )
1658- {
1659- int nparts ;
1660- int j ;
1661-
1662- pprune = & prunestate -> partprunedata [i ];
1663- nparts = pprune -> context .nparts ;
1664-
1665- for (j = 0 ; j < nparts ; j ++ )
1646+ else if ((subidx = pprune -> subpart_map [j ]) >= 0 )
16661647 {
1667- int subidx = pprune -> subpart_map [j ];
1668-
1669- if (subidx >= 0 )
1670- {
1671- PartitionPruningData * subprune ;
1648+ PartitionPruningData * subprune ;
16721649
1673- subprune = & prunestate -> partprunedata [subidx ];
1650+ subprune = & prunestate -> partprunedata [subidx ];
16741651
1675- if (!bms_is_empty (subprune -> present_parts ))
1676- pprune -> present_parts =
1677- bms_add_member (pprune -> present_parts , j );
1678- }
1652+ if (!bms_is_empty (subprune -> present_parts ))
1653+ pprune -> present_parts =
1654+ bms_add_member (pprune -> present_parts , j );
16791655 }
16801656 }
16811657 }
0 commit comments