Have the planner consider Incremental Sort for DISTINCT
authorDavid Rowley <drowley@postgresql.org>
Tue, 10 Jan 2023 21:25:43 +0000 (10:25 +1300)
committerDavid Rowley <drowley@postgresql.org>
Tue, 10 Jan 2023 21:25:43 +0000 (10:25 +1300)
commit3c6fc58209f24b959ee18f5d19ef96403d08f15c
tree6c27d7704fa2bc60554f1383c787ada232c6837c
parente5b8a4c098ad6add39626a14475148872cd687e0
Have the planner consider Incremental Sort for DISTINCT

Prior to this, we only considered a full sort on the cheapest input path
and uniquifying any path which was already sorted in the required sort
order.  Here we adjust create_final_distinct_paths() so that it also
adds an Incremental Sort path on any path which has presorted keys.

Additionally, this adjusts the parallel distinct code so that we now
consider sorting the cheapest partial path and incrementally sorting any
partial paths with presorted keys.  Previously we didn't consider any
sorting for parallel distinct and only added a unique path atop any path
which had the required pathkeys already.

Author: David Rowley
Reviewed-by: Richard Guo
Discussion: https://postgr.es/m/CAApHDvo8Lz2H=42urBbfP65LTcEUOh288MT7DsG2_EWtW1AXHQ@mail.gmail.com
src/backend/optimizer/plan/planner.c
src/test/regress/expected/incremental_sort.out
src/test/regress/expected/select_distinct.out
src/test/regress/expected/window.out
src/test/regress/sql/select_distinct.sql