Allow simplification of EXISTS() subqueries containing LIMIT.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 23 Nov 2014 00:12:38 +0000 (19:12 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 23 Nov 2014 00:12:38 +0000 (19:12 -0500)
commitb62f94c60386796fd88256c5b7b1e8301c345166
treebdc238f5b1cd5d4c4b8d551f1b90e8a58b3de93f
parent9c58101117d25f174c8d8013befdc33c632922d0
Allow simplification of EXISTS() subqueries containing LIMIT.

The locution "EXISTS(SELECT ... LIMIT 1)" seems to be rather common among
people who don't realize that the database already performs optimizations
equivalent to putting LIMIT 1 in the sub-select.  Unfortunately, this was
actually making things worse, because it prevented us from optimizing such
EXISTS clauses into semi or anti joins.  Teach simplify_EXISTS_query() to
suppress constant-positive LIMIT clauses.  That fixes the semi/anti-join
case, and may help marginally even for cases that have to be left as
sub-SELECTs.

Marti Raudsepp, reviewed by David Rowley
src/backend/optimizer/plan/subselect.c
src/test/regress/expected/subselect.out
src/test/regress/sql/subselect.sql