@@ -1318,6 +1318,7 @@ create_append_plan(PlannerInfo *root, AppendPath *best_path, int flags)
13181318 Oid * sortOperators ;
13191319 Oid * collations ;
13201320 bool * nullsFirst ;
1321+ int presorted_keys ;
13211322
13221323 /*
13231324 * Compute sort column info, and adjust subplan's tlist as needed.
@@ -1353,14 +1354,38 @@ create_append_plan(PlannerInfo *root, AppendPath *best_path, int flags)
13531354 numsortkeys * sizeof (bool )) == 0 );
13541355
13551356 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1356- if (!pathkeys_contained_in (pathkeys , subpath -> pathkeys ))
1357+ if (!pathkeys_count_contained_in (pathkeys , subpath -> pathkeys ,
1358+ & presorted_keys ))
13571359 {
1358- Sort * sort = make_sort (subplan , numsortkeys ,
1360+ Plan * sort_plan ;
1361+
1362+ /*
1363+ * We choose to use incremental sort if it is enabled and
1364+ * there are presorted keys; otherwise we use full sort.
1365+ */
1366+ if (enable_incremental_sort && presorted_keys > 0 )
1367+ {
1368+ sort_plan = (Plan * )
1369+ make_incrementalsort (subplan , numsortkeys , presorted_keys ,
13591370 sortColIdx , sortOperators ,
13601371 collations , nullsFirst );
13611372
1362- label_sort_with_costsize (root , sort , best_path -> limit_tuples );
1363- subplan = (Plan * ) sort ;
1373+ label_incrementalsort_with_costsize (root ,
1374+ (IncrementalSort * ) sort_plan ,
1375+ pathkeys ,
1376+ best_path -> limit_tuples );
1377+ }
1378+ else
1379+ {
1380+ sort_plan = (Plan * ) make_sort (subplan , numsortkeys ,
1381+ sortColIdx , sortOperators ,
1382+ collations , nullsFirst );
1383+
1384+ label_sort_with_costsize (root , (Sort * ) sort_plan ,
1385+ best_path -> limit_tuples );
1386+ }
1387+
1388+ subplan = sort_plan ;
13641389 }
13651390 }
13661391
@@ -1491,6 +1516,7 @@ create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path,
14911516 Oid * sortOperators ;
14921517 Oid * collations ;
14931518 bool * nullsFirst ;
1519+ int presorted_keys ;
14941520
14951521 /* Build the child plan */
14961522 /* Must insist that all children return the same tlist */
@@ -1525,14 +1551,38 @@ create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path,
15251551 numsortkeys * sizeof (bool )) == 0 );
15261552
15271553 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
1528- if (!pathkeys_contained_in (pathkeys , subpath -> pathkeys ))
1554+ if (!pathkeys_count_contained_in (pathkeys , subpath -> pathkeys ,
1555+ & presorted_keys ))
15291556 {
1530- Sort * sort = make_sort (subplan , numsortkeys ,
1557+ Plan * sort_plan ;
1558+
1559+ /*
1560+ * We choose to use incremental sort if it is enabled and there
1561+ * are presorted keys; otherwise we use full sort.
1562+ */
1563+ if (enable_incremental_sort && presorted_keys > 0 )
1564+ {
1565+ sort_plan = (Plan * )
1566+ make_incrementalsort (subplan , numsortkeys , presorted_keys ,
15311567 sortColIdx , sortOperators ,
15321568 collations , nullsFirst );
15331569
1534- label_sort_with_costsize (root , sort , best_path -> limit_tuples );
1535- subplan = (Plan * ) sort ;
1570+ label_incrementalsort_with_costsize (root ,
1571+ (IncrementalSort * ) sort_plan ,
1572+ pathkeys ,
1573+ best_path -> limit_tuples );
1574+ }
1575+ else
1576+ {
1577+ sort_plan = (Plan * ) make_sort (subplan , numsortkeys ,
1578+ sortColIdx , sortOperators ,
1579+ collations , nullsFirst );
1580+
1581+ label_sort_with_costsize (root , (Sort * ) sort_plan ,
1582+ best_path -> limit_tuples );
1583+ }
1584+
1585+ subplan = sort_plan ;
15361586 }
15371587
15381588 subplans = lappend (subplans , subplan );
0 commit comments