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 1tk501-00HIip-8J for pgsql-general@arkaria.postgresql.org; Mon, 17 Feb 2025 17:33:41 +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 1tk4zz-000k2Y-Vn for pgsql-general@arkaria.postgresql.org; Mon, 17 Feb 2025 17:33:39 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tk4zz-000k2O-L9 for pgsql-general@lists.postgresql.org; Mon, 17 Feb 2025 17:33:39 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tk4zx-001MOZ-2H for pgsql-general@lists.postgresql.org; Mon, 17 Feb 2025 17:33:38 +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 51HHXah8849268; Mon, 17 Feb 2025 12:33:36 -0500 From: Tom Lane To: Jan Behrens cc: pgsql-general@lists.postgresql.org Subject: Re: Calling set-returning functions in a non-set-expecting context In-reply-to: <20250217175148.70e8df2414f6b47a462b418c@magnetkern.de> References: <20250217175148.70e8df2414f6b47a462b418c@magnetkern.de> Comments: In-reply-to Jan Behrens message dated "Mon, 17 Feb 2025 17:51:48 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <849266.1739813616.1@sss.pgh.pa.us> Date: Mon, 17 Feb 2025 12:33:36 -0500 Message-ID: <849267.1739813616@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Jan Behrens writes: > 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. In general, I'd expect either we'd use the first row or throw an error. We're not 100% consistent about which rule applies, but I can't think of a reason for anything to do something else. > 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'd say that using the first row (and not throwing an error) is guaranteed for the specific case of SQL-language functions by the same text you quote: > "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." The reason for the parenthetical weasel-wording is that a query might require joins, aggregation, etc, and some of our implementations of those things don't preserve row order. If we're talking about a simple invocation of another set-returning function, it's just going to take whatever that function returns first. regards, tom lane