Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tk4Lf-00HCAa-Cd for pgsql-general@arkaria.postgresql.org; Mon, 17 Feb 2025 16:51:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tk4Ld-000M5X-N0 for pgsql-general@arkaria.postgresql.org; Mon, 17 Feb 2025 16:51:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tk4Ld-000M5O-B3 for pgsql-general@lists.postgresql.org; Mon, 17 Feb 2025 16:51:57 +0000 Received: from gaoxing.magnetkern.de ([167.235.225.147]) by magus.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1tk4La-001O2x-2v for pgsql-general@lists.postgresql.org; Mon, 17 Feb 2025 16:51:57 +0000 Received: from titanium.fritz.box (p200300c26f10eb00264bfefffe54b09c.dip0.t-ipconnect.de [IPv6:2003:c2:6f10:eb00:264b:feff:fe54:b09c]) by gaoxing.magnetkern.de (Postfix) with ESMTPSA id C4DA67BF1F for ; Mon, 17 Feb 2025 17:53:27 +0100 (CET) Date: Mon, 17 Feb 2025 17:51:48 +0100 From: Jan Behrens To: pgsql-general@lists.postgresql.org Subject: Calling set-returning functions in a non-set-expecting context Message-Id: <20250217175148.70e8df2414f6b47a462b418c@magnetkern.de> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; amd64-portbld-freebsd14.1) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, I wonder if it is guaranteed that when calling a set-returning function in a non-set-expecting context, the used row is guaranteed to be the first row returned. I.e. if I have the following function definition CREATE FUNCTION foo() RETURNS INTEGER RETURN generate_series(1, 10); is it then guaranteed, that foo() always returns 1? And if so, is that documented somewhere? I didn't find it. I know that generate_series creates an ordered result, so that's not my concern, but I'm not sure whether the first row will be picked. There is something written here: https://www.postgresql.org/docs/17/xfunc-sql.html#XFUNC-SQL-FUNCTIONS-RETURNING-SET "SQL functions execute an arbitrary list of SQL statements, returning the result of the last query in the list. In the simple (non-set) case, the first row of the last query's result will be returned. (Bear in mind that 'the first row' of a multirow result is not well-defined unless you use ORDER BY.) If the last query happens to return no rows at all, the null value will be returned." But this part explicitly mentions queries. Using the "RETURN" statement, I don't give a query but an expression. So does the "first row gets used" rule also apply in my context, and why so? For example, the following command results in an error: SELECT (SELECT generate_series(1, 10)); ERROR: more than one row returned by a subquery used as an expression Kind regards and thanks for your advice, Jan Behrens