Subject: nested loop joins

From: Greg Rychlewski <[email-masked]>
Date: Tue, 26 Oct 2021 14:47:50 -0400

Hi,

I'm looking to understand how nested loop joins with limits work. Say I
have this query

SELECT *
FROM a  INNER JOIN b ON a.id = b.id
LIMIT 10;

Say this was done as a nested loop join where the rows of "a" are the outer
loop.

In this scenario, would all the rows of "a" be materialized or are they
lazily evaluated?

If they are lazily evaluated is it done using cursors?

Thanks

---

From: Tom Lane <[email-masked]>
Date: Tue, 26 Oct 2021 14:56:12 -0400

> In this scenario, would all the rows of "a" be materialized or are they

Evaluation of the join will stop as soon as it's produced 10 rows.
Only as much of "a" will be read as necessary to get that.
(Potentially, not all of "b" would be read either, if 10 matches
to the first "a" row exist in "b".)

			regards, tom lane