Now that the bms_add_range boundary protections are gone, some
alternative ones are needed in a few places.
Author: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
Discussion: https://postgr.es/m/
3437ccf8-a144-55ff-1e2f-
fc16b437823b@lab.ntt.co.jp
 
                {
                        /* We'll need to initialize all subplans */
                        nplans = list_length(node->appendplans);
+                       Assert(nplans > 0);
                        validsubplans = bms_add_range(NULL, 0, nplans - 1);
                }
 
                 * immediately, preventing later calls to ExecFindMatchingSubPlans.
                 */
                if (!prunestate->do_exec_prune)
+               {
+                       Assert(nplans > 0);
                        appendstate->as_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
+               }
        }
        else
        {
                 * When run-time partition pruning is not enabled we can just mark all
                 * subplans as valid; they must also all be initialized.
                 */
+               Assert(nplans > 0);
                appendstate->as_valid_subplans = validsubplans =
                        bms_add_range(NULL, 0, nplans - 1);
                appendstate->as_prune_state = NULL;
 
                {
                        /* We'll need to initialize all subplans */
                        nplans = list_length(node->mergeplans);
+                       Assert(nplans > 0);
                        validsubplans = bms_add_range(NULL, 0, nplans - 1);
                }
 
                 * immediately, preventing later calls to ExecFindMatchingSubPlans.
                 */
                if (!prunestate->do_exec_prune)
+               {
+                       Assert(nplans > 0);
                        mergestate->ms_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
+               }
        }
        else
        {
                 * When run-time partition pruning is not enabled we can just mark all
                 * subplans as valid; they must also all be initialized.
                 */
+               Assert(nplans > 0);
                mergestate->ms_valid_subplans = validsubplans =
                        bms_add_range(NULL, 0, nplans - 1);
                mergestate->ms_prune_state = NULL;
 
 
        /* If there are no pruning steps then all partitions match. */
        if (num_steps == 0)
+       {
+               Assert(context->nparts > 0);
                return bms_add_range(NULL, 0, context->nparts - 1);
+       }
 
        /*
         * Allocate space for individual pruning steps to store its result.  Each
                                bms_make_singleton(rowHash % greatest_modulus);
        }
        else
+       {
+               /* Getting here means at least one hash partition exists. */
+               Assert(boundinfo->ndatums > 0);
                result->bound_offsets = bms_add_range(NULL, 0,
                                                                                          boundinfo->ndatums - 1);
+       }
 
        /*
         * There is neither a special hash null partition or the default hash
         */
        if (nvalues == 0)
        {
+               Assert(boundinfo->ndatums > 0);
                result->bound_offsets = bms_add_range(NULL, 0,
                                                                                          boundinfo->ndatums - 1);
                result->scan_default = partition_bound_has_default(boundinfo);
                /*
                 * First match to all bounds.  We'll remove any matching datums below.
                 */
+               Assert(boundinfo->ndatums > 0);
                result->bound_offsets = bms_add_range(NULL, 0,
                                                                                          boundinfo->ndatums - 1);
 
                        break;
        }
 
+       Assert(minoff >= 0 && maxoff >= 0);
        result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
        return result;
 }
                        maxoff--;
 
                result->scan_default = partition_bound_has_default(boundinfo);
+               Assert(minoff >= 0 && maxoff >= 0);
                result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
 
                return result;