Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mfRcT-0006cK-0T for pgsql-novice@arkaria.postgresql.org; Tue, 26 Oct 2021 18:56:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mfRcR-0001zd-8E for pgsql-novice@arkaria.postgresql.org; Tue, 26 Oct 2021 18:56:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mfRcR-0001zT-0p for pgsql-novice@lists.postgresql.org; Tue, 26 Oct 2021 18:56:19 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mfRcM-0005u9-Q7 for pgsql-novice@lists.postgresql.org; Tue, 26 Oct 2021 18:56:18 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 19QIuCRR110342; Tue, 26 Oct 2021 14:56:12 -0400 From: Tom Lane To: Greg Rychlewski cc: pgsql-novice@lists.postgresql.org Subject: Re: nested loop joins In-reply-to: References: Comments: In-reply-to Greg Rychlewski message dated "Tue, 26 Oct 2021 14:47:50 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <110340.1635274572.1@sss.pgh.pa.us> Date: Tue, 26 Oct 2021 14:56:12 -0400 Message-ID: <110341.1635274572@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Greg Rychlewski writes: > 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? 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