Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ioEf6-0004hP-PK for pgsql-general@arkaria.postgresql.org; Sun, 05 Jan 2020 22:46:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ioEf5-0002A9-6u for pgsql-general@arkaria.postgresql.org; Sun, 05 Jan 2020 22:46:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ioEf4-0002A0-Kh for pgsql-general@lists.postgresql.org; Sun, 05 Jan 2020 22:46:18 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ioEex-0007az-6a for pgsql-general@lists.postgresql.org; Sun, 05 Jan 2020 22:46:16 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 005Mk61l028322; Sun, 5 Jan 2020 17:46:06 -0500 From: Tom Lane To: Gerald Britton cc: Andrew Gierth , pgsql-general@lists.postgresql.org Subject: Re: Are PostgreSQL functions that return sets or tables evaluated lazily or eagerly? In-reply-to: References: <87ftgwklbx.fsf@news-spur.riddles.org.uk> Comments: In-reply-to Gerald Britton message dated "Sun, 05 Jan 2020 15:31:10 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <28320.1578264366.1@sss.pgh.pa.us> Date: Sun, 05 Jan 2020 17:46:06 -0500 Message-ID: <28321.1578264366@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Gerald Britton writes: > Back to where I started in my top post: I became interested in this due to > the doc note on returning a cursor and that it can be an efficient way to > handle large result sets. I suppose that implies lazy evaluation. Does > that mean that if I need plpgsql for a function for he language's power yet > want the results to be returned lazily, a cursor is the (only?) way to go? Nope. The docs' reference to a cursor only suggests that if you can express the function's result as a single SQL query, then opening a cursor for that query and returning the cursor name will work. But if you need plpgsql to express the computation, that's not a terribly helpful suggestion. If you'd like to see some actual movement on the missing feature about lazy evaluation in FROM, you could help test/review the pending patch about it: https://commitfest.postgresql.org/26/2372/ However, that still is only half of the problem, because you also need a PL that is prepared to cooperate, which I don't believe plpgsql is. I think (might be wrong) that a plpython function using "yield" can be made to compute its results lazily. regards, tom lane