Re: Order of rows in simple "select r from table_fn()" - Mailing list pgsql-general

From David G. Johnston
Subject Re: Order of rows in simple "select r from table_fn()"
Date
Msg-id CAKFQuwbc_wjNYjs1Oq7yqy8xPrB68_5-EkQd0H1TY4_MgGdLNg@mail.gmail.com
Whole thread Raw
In response to Order of rows in simple "select r from table_fn()"  (Bryn Llewellyn <bryn@yugabyte.com>)
Responses Re: Order of rows in simple "select r from table_fn()"  (Bryn Llewellyn <bryn@yugabyte.com>)
List pgsql-general
On Tue, Feb 14, 2023 at 4:49 PM Bryn Llewellyn <bryn@yugabyte.com> wrote:
I've found that a table function with "returns table(r text)" provides a convenient way to write a nicely formatted report using psql that can be easily directed to a file with the "\o" metacommand. In general, for cases like this, I can't write a useful "order by r" because the values of "r" interleave, for example, rule-offs between sections of the report, various sub-headings, and actual query results. The required order is exactly the order in which my code produces the rows.

Seems safe enough to rely upon if the query is indeed: SELECT * FROM fn();  The system has to consume the output of the function call in its serial order and has no reason to then reorder things prior to producing the final result set.  Though I'd probably still consider adding a "report line number" column to the output for end-user usability or if they want to sort the report and then return to the physical order.

I am curious whether a user-defined set-returning function is allowed to specify "WITH ORDINALITY" like the built-in UNNEST function does to produce the output row numbers external to the function body and signature.

David J.

pgsql-general by date:

Previous
From: Bryn Llewellyn
Date:
Subject: Order of rows in simple "select r from table_fn()"
Next
From: David Rowley
Date:
Subject: Re: Query plan for "id IS NULL" on PK