From: Tom Lane <tgl@sss.pgh.pa.us>
To: Marius Lorek <marius.lorek@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: TableOID in description of inlined function
Date: Thu, 01 Feb 2024 10:15:10 -0500
Message-ID: <2772879.1706800510@sss.pgh.pa.us> (raw)
In-Reply-To: <CAAajOOkyBWi2GkPx=e+=Oms+P47r4nv1WH9ZcGxTQ6uk1CXvEw@mail.gmail.com>
References: <CAAajOOkyBWi2GkPx=e+=Oms+P47r4nv1WH9ZcGxTQ6uk1CXvEw@mail.gmail.com>
Marius Lorek <marius.lorek@gmail.com> writes:
> If I have a table
> create table my_table(id int primary key);
> and a function
> create function my_function() returns table(id int) stable as $$ select *
> from my_table $$ language sql;
> then Postgres knows that selecting from the function is really just
> selecting from the table:
> explain select * from my_function();
> QUERY PLAN
> ------------------------------------------------------------
> Seq Scan on my_table (cost=0.00..35.50 rows=2550 width=4)
> (1 row)
What happened there is that the planner "inlined" the function
while building a plan.
> But if you prepare the same select statement and ask Postgres for a
> description of it, then in the response the column "id" will have a
> TableOID of 0 - even though we know, on some level, that it's going to be
> selected straight from a table.
The external properties of the prepared statement are determined
long before that inlining happens; in particular they do not depend
on the contents of the function. So the output column is merely
perceived as coming from the declaration "returns table(id int)"
of the function, which has no associated table.
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, marius.lorek@gmail.com
Subject: Re: TableOID in description of inlined function
In-Reply-To: <2772879.1706800510@sss.pgh.pa.us>
References: <CAAajOOkyBWi2GkPx=e+=Oms+P47r4nv1WH9ZcGxTQ6uk1CXvEw@mail.gmail.com> <2772879.1706800510@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox