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 1inZWS-0003q5-1c for pgsql-general@arkaria.postgresql.org; Sat, 04 Jan 2020 02:50:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1inZWQ-0003Sw-Bc for pgsql-general@arkaria.postgresql.org; Sat, 04 Jan 2020 02:50:38 +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 1inZWP-0003So-UJ for pgsql-general@lists.postgresql.org; Sat, 04 Jan 2020 02:50:38 +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 1inZWH-0003Zu-F5 for pgsql-general@lists.postgresql.org; Sat, 04 Jan 2020 02:50:36 +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 0042oQPY025230; Fri, 3 Jan 2020 21:50:26 -0500 From: Tom Lane To: Gerald Britton cc: pgsql-general@lists.postgresql.org Subject: Re: Are PostgreSQL functions that return sets or tables evaluated lazily or eagerly? In-reply-to: References: Comments: In-reply-to Gerald Britton message dated "Fri, 03 Jan 2020 21:21:54 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <25228.1578106226.1@sss.pgh.pa.us> Date: Fri, 03 Jan 2020 21:50:26 -0500 Message-ID: <25229.1578106226@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Gerald Britton writes: > I'm learning to write functions in PostgreSQL. When I got to the > documentation chapter on cursors, I came across this interesting comment: > A more interesting usage is to return a reference to a cursor that a > function has created, allowing the caller to read the rows. This provides > an efficient way to return large row sets from functions. This is more or less unrelated to your question, or at least to your example, because you didn't use a cursor. > I made up a little test function and call: > CREATE FUNCTION foo() RETURNS SETOF customers > LANGUAGE SQL AS $$ > SELECT c.* FROM customers c > CROSS JOIN customers x > CROSS JOIN customers y;$$; > SELECT * FROM foo() LIMIT 1; At the moment, set-returning functions in FROM are generally evaluated "eagerly", ie run to completion before the calling query does anything else. There's been some work recently towards improving that, but it's not done yet. This particular example could be improved, because SQL-language functions can potentially be inlined. Yours wasn't because it's volatile by default, and for somewhat arcane semantic reasons we won't inline volatiles. But if you were to mark that function stable it'd get inlined, and then it'd respond promptly to the LIMIT 1. Another point is that the rules are different for set-returning functions in a targetlist. For example, select foo() limit 1; is fast with your example despite the lack of inlining. That's because that case actually is done row-by-row, with the function yielding control after each row. So we have the infrastructure for that, it just hasn't been exploited in the function-in-FROM case. And, if you weren't confused yet: all of this depends on the particular PL that you're considering. Of the PLs included with core PG, I think only SQL-language functions can do the above --- the other ones are strictly execute-to-completion. I don't know offhand about third-party PLs. regards, tom lane