If you want to check for availability, I usually use the LIMIT / FETCH FIRST 1 ROW ONLY query:
SELECT .. FROM some_table WHERE some_boolean_expression FETCH FIRST 1 ROW ONLY
This usually stops execution after the first press.
The same method can be applied using LATERAL for each row (group) from another table.
SELECT * FROM (SELECT something FROM some_table GROUP BY something ) t1 LEFT JOIN LATERAL (SELECT ... FROM ... WHERE ... FETCH FIRST 1 ROW ONLY) t2 ON (true)
In t2 you can use the WHERE that matches any row for the group. It was executed only once for each group and was interrupted as soon as the first hit was found. However, whether it works better or worse, of course, depends on your search and indexing predicates.
source share