11diff --git a/contrib/Makefile b/contrib/Makefile
2- indexf27e458482..0c62191904 100644
2+ indexf27e458482e..0c621919045 100644
33--- a/contrib/Makefile
44+++ b/contrib/Makefile
55@@ -7,6 +7,7 @@ include $(top_builddir)/src/Makefile.global
@@ -11,7 +11,7 @@ index f27e458482..0c62191904 100644
1111 auto_explain\
1212 bloom\
1313diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
14- index70551522da..958529fbab 100644
14+ index70551522dac..958529fbab4 100644
1515--- a/src/backend/commands/explain.c
1616+++ b/src/backend/commands/explain.c
1717@@ -24,6 +24,7 @@
@@ -57,7 +57,7 @@ index 70551522da..958529fbab 100644
5757 if (es->format == EXPLAIN_FORMAT_TEXT)
5858 appendStringInfoChar(es->str, '\n');
5959diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
60- index4d9746d54a..6fa85d1c71 100644
60+ index4d9746d54a0..6fa85d1c71f 100644
6161--- a/src/backend/nodes/copyfuncs.c
6262+++ b/src/backend/nodes/copyfuncs.c
6363@@ -132,6 +132,7 @@ CopyPlanFields(const Plan *from, Plan *newnode)
@@ -69,35 +69,31 @@ index 4d9746d54a..6fa85d1c71 100644
6969
7070 /*
7171diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
72- index58c2590698..1e06738a13 100644
72+ index58c2590698c..b9f39d36e03 100644
7373--- a/src/backend/nodes/outfuncs.c
7474+++ b/src/backend/nodes/outfuncs.c
7575@@ -342,6 +342,7 @@ _outPlanInfo(StringInfo str, const Plan *node)
7676 WRITE_NODE_FIELD(initPlan);
7777 WRITE_BITMAPSET_FIELD(extParam);
7878 WRITE_BITMAPSET_FIELD(allParam);
79- + /* WRITE_NODE_FIELD(ext_nodes); */
79+ + WRITE_NODE_FIELD(ext_nodes);
8080 }
8181
8282 /*
8383diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c
84- indexeaa51c5c06..6ad8b78c7d 100644
84+ indexeaa51c5c062..65741a86a05 100644
8585--- a/src/backend/nodes/readfuncs.c
8686+++ b/src/backend/nodes/readfuncs.c
87- @@ -1628,6 +1628,11 @@ ReadCommonPlan(Plan *local_node)
87+ @@ -1628,6 +1628,7 @@ ReadCommonPlan(Plan *local_node)
8888 READ_NODE_FIELD(initPlan);
8989 READ_BITMAPSET_FIELD(extParam);
9090 READ_BITMAPSET_FIELD(allParam);
91- + local_node->ext_nodes = NIL;
92- + /* READ_NODE_FIELD(ext_nodes);
93- + * Don't serialize this field. It is required to serialize RestrictInfo and
94- + * EqualenceClass.
95- + */
91+ + READ_NODE_FIELD(ext_nodes);
9692 }
9793
9894 /*
9995diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
100- index006f91f0a8..ef9c8ec581 100644
96+ index006f91f0a87..ef9c8ec5817 100644
10197--- a/src/backend/optimizer/path/costsize.c
10298+++ b/src/backend/optimizer/path/costsize.c
10399@@ -98,6 +98,11 @@
@@ -362,7 +358,7 @@ index 006f91f0a8..ef9c8ec581 100644
362358 {
363359 doubleparallel_divisor = path->parallel_workers;
364360diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
365- index0ed858f305..9d4a6c5903 100644
361+ index0ed858f305a..9d4a6c59030 100644
366362--- a/src/backend/optimizer/plan/createplan.c
367363+++ b/src/backend/optimizer/plan/createplan.c
368364@@ -71,6 +71,7 @@
@@ -393,7 +389,7 @@ index 0ed858f305..9d4a6c5903 100644
393389
394390 /*
395391diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
396- index70899e5430..34075cc87b 100644
392+ index5da863d85de..5b21ffd0667 100644
397393--- a/src/backend/optimizer/plan/planner.c
398394+++ b/src/backend/optimizer/plan/planner.c
399395@@ -143,7 +143,8 @@ static List *extract_rollup_sets(List *groupingSets);
@@ -406,7 +402,7 @@ index 70899e5430..34075cc87b 100644
406402 grouping_sets_data *gd,
407403 List *target_list);
408404 static RelOptInfo *create_grouping_paths(PlannerInfo *root,
409- @@ -3151 ,7 +3152 ,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
405+ @@ -3145 ,7 +3146 ,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
410406 */
411407 static double
412408 get_number_of_groups(PlannerInfo *root,
@@ -416,7 +412,7 @@ index 70899e5430..34075cc87b 100644
416412 grouping_sets_data *gd,
417413 List *target_list)
418414 {
419- @@ -3188 ,7 +3190 ,7 @@ get_number_of_groups(PlannerInfo *root,
415+ @@ -3182 ,7 +3184 ,7 @@ get_number_of_groups(PlannerInfo *root,
420416 GroupingSetData *gs = lfirst_node(GroupingSetData, lc2);
421417 doublenumGroups = estimate_num_groups(root,
422418 groupExprs,
@@ -425,7 +421,7 @@ index 70899e5430..34075cc87b 100644
425421 &gset,
426422 NULL);
427423
428- @@ -3214 ,7 +3216 ,7 @@ get_number_of_groups(PlannerInfo *root,
424+ @@ -3208 ,7 +3210 ,7 @@ get_number_of_groups(PlannerInfo *root,
429425 GroupingSetData *gs = lfirst_node(GroupingSetData, lc2);
430426 doublenumGroups = estimate_num_groups(root,
431427 groupExprs,
@@ -434,7 +430,7 @@ index 70899e5430..34075cc87b 100644
434430 &gset,
435431 NULL);
436432
437- @@ -3231 ,8 +3233 ,8 @@ get_number_of_groups(PlannerInfo *root,
433+ @@ -3225 ,8 +3227 ,8 @@ get_number_of_groups(PlannerInfo *root,
438434 groupExprs = get_sortgrouplist_exprs(parse->groupClause,
439435 target_list);
440436
@@ -445,7 +441,7 @@ index 70899e5430..34075cc87b 100644
445441 }
446442 }
447443 else if (parse->groupingSets)
448- @@ -3619 ,7 +3621 ,8 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
444+ @@ -3613 ,7 +3615 ,8 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
449445 * Estimate number of groups.
450446 */
451447 dNumGroups = get_number_of_groups(root,
@@ -455,7 +451,7 @@ index 70899e5430..34075cc87b 100644
455451 gd,
456452 extra->targetList);
457453
458- @@ -6425 ,13 +6428 ,15 @@ create_partial_grouping_paths(PlannerInfo *root,
454+ @@ -6419 ,13 +6422 ,15 @@ create_partial_grouping_paths(PlannerInfo *root,
459455 if (cheapest_total_path != NULL)
460456 dNumPartialGroups =
461457 get_number_of_groups(root,
@@ -474,7 +470,7 @@ index 70899e5430..34075cc87b 100644
474470 extra->targetList);
475471
476472diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c
477- indexe105a4d5f1..c5bcc9d1d1 100644
473+ indexe105a4d5f1d..c5bcc9d1d15 100644
478474--- a/src/backend/optimizer/util/relnode.c
479475+++ b/src/backend/optimizer/util/relnode.c
480476@@ -258,6 +258,7 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
@@ -540,7 +536,7 @@ index e105a4d5f1..c5bcc9d1d1 100644
540536
541537 return ppi;
542538diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
543- index87879c9ddc..1aad8c43d9 100644
539+ index87879c9ddc8..1aad8c43d92 100644
544540--- a/src/backend/utils/adt/selfuncs.c
545541+++ b/src/backend/utils/adt/selfuncs.c
546542@@ -143,6 +143,7 @@
@@ -573,7 +569,7 @@ index 87879c9ddc..1aad8c43d9 100644
573569 * estimate_num_groups- Estimate number of groups in a grouped query
574570 *
575571diff --git a/src/include/commands/explain.h b/src/include/commands/explain.h
576- indexe94d9e49cf..49236ced77 100644
572+ indexe94d9e49cf6..49236ced77c 100644
577573--- a/src/include/commands/explain.h
578574+++ b/src/include/commands/explain.h
579575@@ -75,6 +75,18 @@ extern PGDLLIMPORT ExplainOneQuery_hook_type ExplainOneQuery_hook;
@@ -596,7 +592,7 @@ index e94d9e49cf..49236ced77 100644
596592 extern void ExplainQuery(ParseState *pstate, ExplainStmt *stmt,
597593 ParamListInfo params, DestReceiver *dest);
598594diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h
599- indexe370a01141..9f2f1628f5 100644
595+ index3c034fa3c5e..e441674970c 100644
600596--- a/src/include/nodes/pathnodes.h
601597+++ b/src/include/nodes/pathnodes.h
602598@@ -756,6 +756,10 @@ typedef struct RelOptInfo
@@ -635,7 +631,7 @@ index e370a01141..9f2f1628f5 100644
635631
636632
637633diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h
638- index2308c80dde..a933afa483 100644
634+ index1c9357f6a77..58c005c1a9b 100644
639635--- a/src/include/nodes/plannodes.h
640636+++ b/src/include/nodes/plannodes.h
641637@@ -158,6 +158,9 @@ typedef struct Plan
@@ -649,7 +645,7 @@ index 2308c80dde..a933afa483 100644
649645
650646 /* ----------------
651647diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h
652- index2113bc82de..bcc2520cec 100644
648+ index2113bc82de0..bcc2520cec5 100644
653649--- a/src/include/optimizer/cost.h
654650+++ b/src/include/optimizer/cost.h
655651@@ -39,6 +39,37 @@ typedef enum
@@ -733,7 +729,7 @@ index 2113bc82de..bcc2520cec 100644
733729
734730 #endif/* COST_H */
735731diff --git a/src/include/optimizer/pathnode.h b/src/include/optimizer/pathnode.h
736- index2922c0cdc1..c59dce6989 100644
732+ index2922c0cdc14..c59dce6989e 100644
737733--- a/src/include/optimizer/pathnode.h
738734+++ b/src/include/optimizer/pathnode.h
739735@@ -18,6 +18,10 @@
@@ -748,7 +744,7 @@ index 2922c0cdc1..c59dce6989 100644
748744 * prototypes for pathnode.c
749745 */
750746diff --git a/src/include/optimizer/planmain.h b/src/include/optimizer/planmain.h
751- indexbf1adfc52a..9c78e0f4e0 100644
747+ indexbf1adfc52ac..9c78e0f4e02 100644
752748--- a/src/include/optimizer/planmain.h
753749+++ b/src/include/optimizer/planmain.h
754750@@ -24,6 +24,12 @@ extern double cursor_tuple_fraction;
@@ -765,7 +761,7 @@ index bf1adfc52a..9c78e0f4e0 100644
765761 * prototypes for plan/planmain.c
766762 */
767763diff --git a/src/include/utils/selfuncs.h b/src/include/utils/selfuncs.h
768- index9dd444e1ff..cfaae98aa2 100644
764+ index9dd444e1ff5..b0b5a656185 100644
769765--- a/src/include/utils/selfuncs.h
770766+++ b/src/include/utils/selfuncs.h
771767@@ -144,6 +144,13 @@ typedef bool (*get_index_stats_hook_type) (PlannerInfo *root,
@@ -782,13 +778,13 @@ index 9dd444e1ff..cfaae98aa2 100644
782778
783779 /* Functions in selfuncs.c */
784780
785- @@ -213 ,6 +220 ,9 @@ extern void mergejoinscansel(PlannerInfo *root, Node *clause,
786- extern double estimate_num_groups(PlannerInfo *root, List *groupExprs ,
787- double input_rows, List **pgset,
788- EstimationInfo *estinfo);
781+ @@ -210 ,6 +217 ,9 @@ extern void mergejoinscansel(PlannerInfo *root, Node *clause,
782+ Selectivity *leftstart, Selectivity *leftend ,
783+ Selectivity *rightstart, Selectivity *rightend);
784+
789785+ extern double estimate_num_groups_ext(PlannerInfo *root, List *groupExprs,
790786+ Path *subpath, RelOptInfo *grouped_rel,
791787+ List **pgset, EstimationInfo *estinfo);
792-
793- extern void estimate_hash_bucket_stats(PlannerInfo *root ,
794- Node *hashkey, double nbuckets,
788+ extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
789+ double input_rows, List **pgset ,
790+ EstimationInfo *estinfo);