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 1uyeKk-00Asyu-E2 for pgsql-docs@arkaria.postgresql.org; Tue, 16 Sep 2025 22:39:34 +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 1uyeKi-00332z-9D for pgsql-docs@arkaria.postgresql.org; Tue, 16 Sep 2025 22:39:33 +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 1uyeKi-00332q-1F for pgsql-docs@lists.postgresql.org; Tue, 16 Sep 2025 22:39:32 +0000 Received: from forward501a.mail.yandex.net ([2a02:6b8:c0e:500:1:45:d181:d501]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1uyeKf-000nRy-0o for pgsql-docs@lists.postgresql.org; Tue, 16 Sep 2025 22:39:31 +0000 Received: from mail-nwsmtp-smtp-production-main-95.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-95.vla.yp-c.yandex.net [IPv6:2a02:6b8:c18:43a9:0:640:86ff:0]) by forward501a.mail.yandex.net (Yandex) with ESMTPS id 67CA780451; Wed, 17 Sep 2025 01:39:26 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-95.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id MdZXPW3LuCg0-WpEuSPsC; Wed, 17 Sep 2025 01:39:25 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1758062365; bh=Y8aQNQAPqqZskzLeXipGmhLEpYgzY3wOxJleguToR28=; h=In-Reply-To:Message-ID:Subject:References:To:From:Date; b=WQHFy7kWnyhfCVAnAG44B0aD7QXIamIcW0Dj88Qa5OMkJ2nRafh9YjWIyi39MrwtK Lyaq3TMhaGpt9QwzEkWOJki7Ip1NdO4n+QprtDSR7iWLRfFeEnuBAAKplrlzFaX2ku 92UOWU+uIuWCX/fOoAS5g4CK+ASkjxPIIEQVSSC4= Authentication-Results: mail-nwsmtp-smtp-production-main-95.vla.yp-c.yandex.net; dkim=pass header.i=@yandex.ru Date: Tue, 16 Sep 2025 18:39:22 -0400 From: Eugen Konkov Message-ID: <1513108534.20250916183922@yandex.ru> To: "David G. Johnston" , "pgsql-docs@lists.postgresql.org" Subject: Re: The document contains false statements In-Reply-To: References: <175779557876.861.12514976006917547591@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello David, Saturday, September 13, 2025, 7:26:21 PM, you wrote: > On Saturday, September 13, 2025, PG Doc comments form wrote: > The following documentation comment has been logged on the website: > Page: https://www.postgresql.org/docs/17/sql-createfunction.html > Description: >> This is effectively another way of declaring a named OUT parameter > Actually OUT works twice slower in compare to RETURNS TABLE > https://stackoverflow.com/q/79763947/4632019 > DB<>fiddle for [`OUT`](https://dbfiddle.uk/fz9L_wm0) and [`RETURNS > TABLE`](https://dbfiddle.uk/uTkU1MT8) cases. > *I hope after the fix, data centers will consume 2 times less electricity > The statement is not false - it contains an =E2=80=9Cexcept=E2=80=9D clau= se that > you=E2=80=99ve ignored which makes it true for exactly this reason. The > fact you are comparing a set-returning function to one that doesn=E2=80= =99t > return a set has invalidated the test. > The fundamental issue here is =E2=80=9Cselect (composite_func()).*=E2=80= =9D where > the function is not set-returning if known to be broken - the =E2=80=9C*= =E2=80=9D > expansion during planning results in the function being executed > multiple times once for each output column. (I may be missing some > nuances here as, since the inclusion of lateral joins, this almost never = comes up anymore.) > Non-trivial function calls should be placed in the FROM clause of a > query; in part to ensure avoidance of this problematic behavior. > This is not at all limited to RLS. > In short, I don=E2=80=99t know how to improve the documentation to prevent > people from writing bad queries of this type. Concrete suggestions > are welcome, but removing this sentence, or re-wording it, doesn=E2=80=99t > seem like it would make any difference. > David J. Thanks for more information on this. You and Tom both pointed me to `RETURN= S SET OF` part. I agree with Tom that documentation is not the place to tea= ch user how to write SQL. But I would appeal that the documentation should be meaning full. And for m= e the part after "except" looks the same as it would be written in Arabic "= =D8=A5=D8=B1=D8=AC=D8=A7=D8=B9 =D9=85=D8=AC=D9=85=D9=88=D8=B9=D8=A9 =D9=85= =D9=86" (RETURNS SETOF). The question from David: How it could be done better? is good. Let me descr= ibe how I see it from my point of view and experience. The documentation above highlights as RETURNS SETOF as something special. I= never used SETOF. I tried to google and find almost nothing in the officia= l documentation, except these two https://www.postgresql.org/docs/current/xfunc-sql.html#XFUNC-SQL-FUNCTIONS-= RETURNING-SET https://www.postgresql.org/docs/current/xfunc-sql.html#XFUNC-SQL-FUNCTIONS-= RETURNING-TABLE=20 with just a mention that it can return 0, 1 or more rows. And the most informative one is https://www.postgresql.org/docs/current/que= ries-table-expressions.html#QUERIES-TABLEFUNCTIONS The latest one my my mind should be reffered from https://www.postgresql.or= g/docs/current/xfunc-sql.html#XFUNC-SQL-FUNCTIONS-RETURNING-TABLE , Eg. Please read 7.2.1.4 Table Functions for more information. For the original problem: >The name of an output column in the RETURNS TABLE syntax. This is effectiv= ely another way of declaring a named OUT parameter, > except that RETURNS TABLE also implies RETURNS SETOF. My proposition is to add link to 7.2.1.4 Table Functions for more informati= on and extend "7.2.1.4 Table Functions for more information" with the infor= mation that "in certain circumstances RETURNS TABLE is a subject for optimizer and= could be inlined https://wiki.postgresql.org/wiki/Inlining_of_SQL_function= s#Inlining_conditions_for_table_functions". Having this it would be clear that `RETURNS TABLE` and `RETURNS SETOF` are= sort of fuctions which are called Table functions. Probably official documentation should have in future a section somewhere "= How to optimize your queries" and links to benchmarks like this I did in my= question on SO: https://pastebin.com/n3sxBxt6=20 https://dbfiddle.uk/xfy-qw75=20 Without this information and statement that these two: OUT and RETURNS TABL= E are just taste of syntax, users will use either without knowing consequen= ces. (like me until I benchmarked it) -- Best regards, Eugen Konkov=20 --=20 Best regards, Eugen Konkov