Hello David,

Saturday, September 13, 2025, 7:26:21 PM, you wrote:

> On Saturday, September 13, 2025, PG Doc comments form 
> <nore...@postgresql.org> 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 “except” clause that
> you’ve ignored which makes it true for exactly this reason.  The
> fact you are comparing a set-returning function to one that doesn’t
> return a set has invalidated the test.
> The fundamental issue here is “select (composite_func()).*” where
> the function is not set-returning if known to be broken - the “*”
> 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’t 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’t
> seem like it would make any difference.
> David J.

Thanks for more information on this. You and Tom both pointed me to `RETURNS 
SET OF` part. I agree with Tom that documentation is not the place to teach 
user how to write SQL.
But I would appeal that the documentation should be meaning full. And for me 
the part after "except" looks the same as it would be written in Arabic "إرجاع 
مجموعة من" (RETURNS SETOF).

The question from David: How it could be done better? is good. Let me describe 
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 official 
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
 
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/queries-table-expressions.html#QUERIES-TABLEFUNCTIONS

The latest one my my mind should be reffered from 
https://www.postgresql.org/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 effectively 
>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 information 
and extend "7.2.1.4 Table Functions for more information" with the information
that "in certain circumstances RETURNS TABLE is a subject for optimizer and 
could be inlined 
https://wiki.postgresql.org/wiki/Inlining_of_SQL_functions#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 
https://dbfiddle.uk/xfy-qw75 

Without this information and statement that these two: OUT and RETURNS TABLE 
are just taste of syntax, users will use either without knowing consequences. 
(like me until I benchmarked it)

--
Best regards,
Eugen Konkov 

-- 
Best regards,
Eugen Konkov



Reply via email to