Subject: Re: RowDescription for a function does not include table OID

From: Maxwell Dreytser <[email-masked]>
Date: Fri, 21 Jun 2024 15:03:58 +0000

On Friday, June 21, 2024 10:48 AM David G. Johnston <[email-masked]>wrote:

>Yes, but the bug is yours.  The definition you want is:  RETURNS SETOF physical_table (not tested though)
>What you did was produce a one-column table whose column type is a composite (and whose name is like - what with case-folding of unquoted identifiers).  Since that table doesn't exist anywhere in the catalogs it has no TableOID.

SETOF also does not return correct RowDescription data. Table OID and column number are still both 0.
Both versions have the exact same pg_proc.prorettype. If I join this onto pg_type, the pg_type.typrelid = 'physical_table'::regclass.

Regards,
Maxwell

---

From: "David G. Johnston" <[email-masked]>
Date: Fri, 21 Jun 2024 08:28:56 -0700

On Fri, Jun 21, 2024 at 8:04 AM Maxwell Dreytser <
[email-masked]> wrote:

> >What you did was produce a one-column table whose column type is a
> composite (and whose name is like - what with case-folding of unquoted
> identifiers).  Since that table doesn't exist anywhere in the catalogs it
> has no TableOID.
>
> Both versions have the exact same pg_proc.prorettype. If I join this onto
> pg_type, the pg_type.typrelid = 'physical_table'::regclass.
>
>
Interesting, then I suppose it is semantics.  There is no table involved -
you are referencing the type of that name, not the table - so no TableOID.
There is no guarantee the row you are holding came from a table - and I'd
interpret the current behavior as conveying that fact.  Though the current
wording: "If the field can be identified as a column of a specific table,
the object ID of the table; otherwise zero."; and the observation that at
least a human "can identify" a related column, leads one to reasonably
infer the system should be able to make such an identification as well.

I would expect you'd be able to find the pg_type.oid value somewhere in the
RowDescription given those specifications, but not the pg_type.typrelid
value.  But since the header has no allowance for a row type oid this
information does seem to be missing.

In short, the system doesn't generate the information you need, where you
need it, to tie these pieces together.  Modifying existing elements of the
backend protocol is not presently in the cards.

David J.

---

From: Maxwell Dreytser <[email-masked]>
Date: Fri, 21 Jun 2024 15:41:09 +0000

On Friday, June 21, 2024 11:28 AM David G. Johnston <[email-masked]> wrote:

> Interesting, then I suppose it is semantics.  There is no table involved - you are referencing the type of that name, not the table - so no TableOID.  There is no guarantee the row you are holding came from a table - and I'd interpret the current behavior as conveying that fact.  Though the current wording: "If the field can be identified as a column of a specific table, the object ID of the table; otherwise zero."; and the observation that at least a human "can identify" a related column, leads one to reasonably infer the system should be able to make such an identification as well.

This is exactly my point. If the return type of the function is strongly linked (directly in the function schema) to the table according to pg_catalog, the field can obviously be tied to a specific column of that specific table. The RowDescription not having that value filled in is a violation of that promise.

> In short, the system doesn't generate the information you need, where you need it, to tie these pieces together.  Modifying existing elements of the backend protocol is not presently in the cards.

>From my perspective this is clearly a bug as there is no way to define a function in a way that provides enough data to the reader.

Regards,
Maxwell.

---

From: "David G. Johnston" <[email-masked]>
Date: Fri, 21 Jun 2024 08:52:00 -0700

On Fri, Jun 21, 2024 at 8:41 AM Maxwell Dreytser <
[email-masked]> wrote:

> > In short, the system doesn't generate the information you need, where
> you need it, to tie these pieces together.  Modifying existing elements of
> the backend protocol is not presently in the cards.
>
> From my perspective this is clearly a bug as there is no way to define a
> function in a way that provides enough data to the reader.
>

Quick search turned up this prior thread:

https://www.postgresql.org/message-id/19323.1245720832%40sss.pgh.pa.us

Based upon that unargued point the only bug here is in the documentation,
leaving the reader to assume that some effort will be made to chain
together a function returns clause to a physical table through that table's
automatically-generated composite type.  We don't and never will modify the
existing protocol message semantics in that respect.

David J.

---

From: Tom Lane <[email-masked]>
Date: Fri, 21 Jun 2024 12:12:43 -0400

"David G. Johnston" <[email-masked]> writes:
> automatically-generated composite type.

Hmm, I read the documentation as making minimal promises about how
much effort will be expended, not maximal ones.

But in any case, I repeat the point that you can't open this can of
worms without having a lot of definitional slipperiness wriggle out.
Here is an example:

regression=# create table foo(a int, b int);
CREATE TABLE
regression=# create table bar(x int, y int, z int);
CREATE TABLE
regression=# create function f(int) returns setof foo stable
begin atomic select y, z from bar where x = $1; end;
CREATE FUNCTION

What labeling would you expect for "select * from f(...)",
and on what grounds?  It is by no stretch of the imagination a
select from table foo.  Moreover, the system has fully enough
information to perceive the query as a select from bar after
inlining the function call:

regression=# explain verbose select * from f(42);
                         QUERY PLAN                         
------------------------------------------------------------
 Seq Scan on public.bar  (cost=0.00..35.50 rows=10 width=8)
   Output: bar.y, bar.z
   Filter: (bar.x = 42)
(3 rows)

In fact, if we implemented this labeling at the tail end of
planning rather than early in parsing, it'd be fairly hard
to avoid labeling the output columns as bar.* rather than
foo.*.  But we don't, and I'm not seeing an upside to
redefining how that works.

I've long forgotten the alleged JDBC connection that David
mentions, but it's surely just the tip of the iceberg of
client-side code that we could break if we change how this
works.

			regards, tom lane