postgres/src
David Rowley 5543677ec9 Use Limit instead of Unique to implement DISTINCT, when possible
When all of the query's DISTINCT pathkeys have been marked as redundant
due to EquivalenceClasses existing which contain constants, we can just
implement the DISTINCT operation on a query by just limiting the number of
returned rows to 1 instead of performing a Unique on all of the matching
(duplicate) rows.

This applies in cases such as:

SELECT DISTINCT col,col2 FROM tab WHERE col = 1 AND col2 = 10;

If there are any matching rows, then they must all be {1,10}.  There's no
point in fetching all of those and running a Unique operator on them to
leave only a single row.  Here we effectively just find the first row and
then stop.  We are obviously unable to apply this optimization if either
the col = 1 or col2 = 10 were missing from the WHERE clause or if there
were any additional columns in the SELECT clause.

Such queries are probably not all that common, but detecting when we can
apply this optimization amounts to checking if the distinct_pathkeys are
NULL, which is very cheap indeed.

Nothing is done here to check if the query already has a LIMIT clause.  If
it does then the plan may end up with 2 Limits nodes.  There's no harm in
that and it's probably not worth the complexity to unify them into a
single Limit node.

Author: David Rowley
Reviewed-by: Richard Guo
Discussion: https://postgr.es/m/CAApHDvqS0j8RUWRUSgCAXxOqnYjHUXmKwspRj4GzVfOO25ByHA@mail.gmail.com
Discussion: https://postgr.es/m/MEYPR01MB7101CD5DA0A07C9DE2B74850A4239@MEYPR01MB7101.ausprd01.prod.outlook.com
2022-10-28 23:04:38 +13:00
..
backend Use Limit instead of Unique to implement DISTINCT, when possible 2022-10-28 23:04:38 +13:00
bin Improve tab completion for ALTER STATISTICS <name> SET in psql 2022-10-24 15:46:42 +09:00
common Remove AssertArg and AssertState 2022-10-28 09:19:06 +02:00
fe_utils meson: Add support for building with precompiled headers 2022-10-06 17:19:30 -07:00
include Remove AssertArg and AssertState 2022-10-28 09:19:06 +02:00
interfaces libpq: Reset singlerow flag correctly in pipeline mode 2022-10-14 19:06:26 +02:00
makefiles autoconf: Rely on ar supporting index creation 2022-10-07 11:53:39 -07:00
pl Store GUC data in a memory context, instead of using malloc(). 2022-10-14 12:10:48 -04:00
port Fix unlink() for STATUS_DELETE_PENDING on Windows. 2022-10-25 16:26:58 +13:00
template Move darwin sysroot determination into separate file 2022-09-01 16:54:19 -07:00
test Use Limit instead of Unique to implement DISTINCT, when possible 2022-10-28 23:04:38 +13:00
timezone meson: Add windows resource files 2022-10-05 09:56:05 -07:00
tools Remove pgpid_t type, use pid_t instead 2022-10-22 10:45:19 +02:00
tutorial Update copyright for 2022 2022-01-07 19:04:57 -05:00
.gitignore
DEVELOPERS
Makefile
Makefile.global.in Make finding openssl program a configure or meson option 2022-10-20 21:05:42 +02:00
Makefile.shlib autoconf: Rely on ar supporting index creation 2022-10-07 11:53:39 -07:00
meson.build meson: Add initial version of meson based build system 2022-09-21 22:37:17 -07:00
nls-global.mk Fix for make unportability 2022-07-13 09:15:01 +02:00