Recently I'm getting familiar with Clojure and I am amused with an idea of lazy sequence evaluation which compute values only when it is necessary.
I work a lot with PostgreSQL DB and I experienced different performance of queries when LIMIT clause is used. For example query
SELECT * FROM(
SELECT id FROM foo1
INTERSECT
SELECT id FROM foo2) AS subquery
LIMIT 50
will have the same execution time like
SELECT id FROM foo1
INTERSECT
SELECT id FROM foo2.
This suggests that Postgres firstly evaluates the whole result and then just get first 50 rows. This behaviour is in opposite to the idea of laziness, because DB process data that it is not required to get final answer. But on the other hand query
SELECT * FROM foo1 INNER JOIN foo2 ON foo1.id=foo2.id LIMIT 50
performs much better than
SELECT * FROM foo1 INNER JOIN foo2 ON foo1.id=foo2.id.
Does somebody know which Postgres operations supports such LIMIT laziness?