@@ -126,6 +126,7 @@ static void subquery_push_qual(Query *subquery,
126126static void recurse_push_qual (Node * setOp , Query * topquery ,
127127 RangeTblEntry * rte , Index rti , Node * qual );
128128static void remove_unused_subquery_outputs (Query * subquery , RelOptInfo * rel );
129+ static int compute_parallel_worker (RelOptInfo * rel , BlockNumber pages );
129130
130131
131132/*
@@ -678,49 +679,7 @@ create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
678679{
679680 int parallel_workers ;
680681
681- /*
682- * If the user has set the parallel_workers reloption, use that; otherwise
683- * select a default number of workers.
684- */
685- if (rel -> rel_parallel_workers != -1 )
686- parallel_workers = rel -> rel_parallel_workers ;
687- else
688- {
689- int parallel_threshold ;
690-
691- /*
692- * If this relation is too small to be worth a parallel scan, just
693- * return without doing anything ... unless it's an inheritance child.
694- * In that case, we want to generate a parallel path here anyway. It
695- * might not be worthwhile just for this relation, but when combined
696- * with all of its inheritance siblings it may well pay off.
697- */
698- if (rel -> pages < (BlockNumber ) min_parallel_relation_size &&
699- rel -> reloptkind == RELOPT_BASEREL )
700- return ;
701-
702- /*
703- * Select the number of workers based on the log of the size of the
704- * relation. This probably needs to be a good deal more
705- * sophisticated, but we need something here for now. Note that the
706- * upper limit of the min_parallel_relation_size GUC is chosen to
707- * prevent overflow here.
708- */
709- parallel_workers = 1 ;
710- parallel_threshold = Max (min_parallel_relation_size , 1 );
711- while (rel -> pages >= (BlockNumber ) (parallel_threshold * 3 ))
712- {
713- parallel_workers ++ ;
714- parallel_threshold *= 3 ;
715- if (parallel_threshold > INT_MAX / 3 )
716- break ; /* avoid overflow */
717- }
718- }
719-
720- /*
721- * In no case use more than max_parallel_workers_per_gather workers.
722- */
723- parallel_workers = Min (parallel_workers , max_parallel_workers_per_gather );
682+ parallel_workers = compute_parallel_worker (rel , rel -> pages );
724683
725684 /* If any limit was set to zero, the user doesn't want a parallel scan. */
726685 if (parallel_workers <= 0 )
@@ -2908,6 +2867,64 @@ remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel)
29082867 }
29092868}
29102869
2870+ /*
2871+ * Compute the number of parallel workers that should be used to scan a
2872+ * relation. "pages" is the number of pages from the relation that we
2873+ * expect to scan.
2874+ */
2875+ static int
2876+ compute_parallel_worker (RelOptInfo * rel , BlockNumber pages )
2877+ {
2878+ int parallel_workers ;
2879+
2880+ /*
2881+ * If the user has set the parallel_workers reloption, use that; otherwise
2882+ * select a default number of workers.
2883+ */
2884+ if (rel -> rel_parallel_workers != -1 )
2885+ parallel_workers = rel -> rel_parallel_workers ;
2886+ else
2887+ {
2888+ int parallel_threshold ;
2889+
2890+ /*
2891+ * If this relation is too small to be worth a parallel scan, just
2892+ * return without doing anything ... unless it's an inheritance child.
2893+ * In that case, we want to generate a parallel path here anyway. It
2894+ * might not be worthwhile just for this relation, but when combined
2895+ * with all of its inheritance siblings it may well pay off.
2896+ */
2897+ if (pages < (BlockNumber ) min_parallel_relation_size &&
2898+ rel -> reloptkind == RELOPT_BASEREL )
2899+ return 0 ;
2900+
2901+ /*
2902+ * Select the number of workers based on the log of the size of the
2903+ * relation. This probably needs to be a good deal more
2904+ * sophisticated, but we need something here for now. Note that the
2905+ * upper limit of the min_parallel_relation_size GUC is chosen to
2906+ * prevent overflow here.
2907+ */
2908+ parallel_workers = 1 ;
2909+ parallel_threshold = Max (min_parallel_relation_size , 1 );
2910+ while (pages >= (BlockNumber ) (parallel_threshold * 3 ))
2911+ {
2912+ parallel_workers ++ ;
2913+ parallel_threshold *= 3 ;
2914+ if (parallel_threshold > INT_MAX / 3 )
2915+ break ; /* avoid overflow */
2916+ }
2917+ }
2918+
2919+ /*
2920+ * In no case use more than max_parallel_workers_per_gather workers.
2921+ */
2922+ parallel_workers = Min (parallel_workers , max_parallel_workers_per_gather );
2923+
2924+ return parallel_workers ;
2925+ }
2926+
2927+
29112928/*****************************************************************************
29122929 * DEBUG SUPPORT
29132930 *****************************************************************************/
0 commit comments