pgsql: postgres_fdw: push down FUNCTION RTE into foreign joins

Started by Alexander Korotkov3 months ago3 messageshackerscommitters

Thread notes

1 note#performance#moderate#enhancement
hackorum-bot#performance#moderate#enhancement
hackorum-bot
2 days ago

@global #performance #moderate #enhancement

#performance#moderate#enhancement
Jump to latest
#1Alexander Korotkov
akorotkov@postgresql.org
hackerscommitters

postgres_fdw: push down FUNCTION RTE into foreign joins

A foreign join planning hook now considers a (foreign-table x function-RTE)
INNER join as a push-down candidate when the function expression is
IMMUTABLE and otherwise shippable. The remote query absorbs the function
call as a FROM-list item (e.g. unnest(...) AS f<rti>(c1, c2, ...)), so the
foreign side returns only rows that match the function-produced set and the
join executes entirely on the remote.

An IMMUTABLE function gives the same result on any server, so the same
function RTE can be a push-down candidate for several distinct foreign
servers without semantic risk. To keep the planner state consistent
across those independent attempts, the per-call stub fpinfo for the
function side lives on the joinrel's PgFdwRelationInfo (new
outer_func_fpinfo / inner_func_fpinfo), never on the function rel itself,
and the function side is detected via rtekind rather than fdw_private.

set_foreign_rel_properties() propagates fdwroutine onto a joinrel that
pairs a foreign rel with an RTE_FUNCTION rel so GetForeignJoinPaths gets
called; the FDW retains full control over whether to actually generate a
path. deparseRangeTblRef and deparseColumnRef gain a FUNCTION-RTE branch
that emits the function expression and resolves Vars to the generated
column aliases.

Discussion: /messages/by-id/e3af56f9b2f1bd9fdf12aff6ca25b18d@postgrespro.ru
Author: Alexander Pyhalov <a.pyhalov@postgrespro.ru>
Reviewed-by: Solaimurugan Vellaipandiyan <drsolaimurugan.v@gmail.com>
Reviewed-by: Alexander Korotkov <aekorotkov@gmail.com>

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/0ee83dd4a9940883b5241fc9c80fd27f63f2e47f

Modified Files
--------------
contrib/postgres_fdw/deparse.c | 157 +++++-
contrib/postgres_fdw/expected/postgres_fdw.out | 627 ++++++++++++++++++++++++
contrib/postgres_fdw/postgres_fdw.c | 642 +++++++++++++++++++++++--
contrib/postgres_fdw/postgres_fdw.h | 12 +
contrib/postgres_fdw/sql/postgres_fdw.sql | 274 +++++++++++
doc/src/sgml/postgres-fdw.sgml | 18 +
src/backend/optimizer/util/relnode.c | 25 +
7 files changed, 1698 insertions(+), 57 deletions(-)

#2Erik Rijkers
er@xs4all.nl
In reply to: Alexander Korotkov (#1)
hackerscommitters
typo in postgres-fdw.sgml - Re: pgsql: postgres_fdw: push down FUNCTION RTE into foreign joins

Op 8/19/26 om 12:49 schreef Alexander Korotkov:

postgres_fdw: push down FUNCTION RTE into foreign joins

A foreign join planning hook now considers a (foreign-table x function-RTE)
INNER join as a push-down candidate when the function >

...

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/0ee83dd4a9940883b5241fc9c80fd27f63f2e47f

Modified Files
--------------
contrib/postgres_fdw/deparse.c | 157 +++++-
contrib/postgres_fdw/expected/postgres_fdw.out | 627 ++++++++++++++++++++++++
contrib/postgres_fdw/postgres_fdw.c | 642 +++++++++++++++++++++++--
contrib/postgres_fdw/postgres_fdw.h | 12 +
contrib/postgres_fdw/sql/postgres_fdw.sql | 274 +++++++++++
doc/src/sgml/postgres-fdw.sgml | 18 +
src/backend/optimizer/util/relnode.c | 25 +
7 files changed, 1698 insertions(+), 57 deletions(-)

Hi,

in: doc/src/sgml/postgres-fdw.sgml, I believe this sentence:

"and none of the following disqualifying conditions applies:"

should be:

"and none of the following disqualifying conditions apply:"

Please, can a native English speaker confirm?

Thanks,

Erik

#3Robert Treat
rob@xzilla.net
In reply to: Erik Rijkers (#2)
hackerscommitters
Re: typo in postgres-fdw.sgml - Re: pgsql: postgres_fdw: push down FUNCTION RTE into foreign joins

On Wed, Aug 19, 2026 at 8:32 PM Erik Rijkers <er@xs4all.nl> wrote:

Op 8/19/26 om 12:49 schreef Alexander Korotkov:

postgres_fdw: push down FUNCTION RTE into foreign joins

A foreign join planning hook now considers a (foreign-table x function-RTE)
INNER join as a push-down candidate when the function >

...

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/0ee83dd4a9940883b5241fc9c80fd27f63f2e47f

Modified Files
--------------
contrib/postgres_fdw/deparse.c | 157 +++++-
contrib/postgres_fdw/expected/postgres_fdw.out | 627 ++++++++++++++++++++++++
contrib/postgres_fdw/postgres_fdw.c | 642 +++++++++++++++++++++++--
contrib/postgres_fdw/postgres_fdw.h | 12 +
contrib/postgres_fdw/sql/postgres_fdw.sql | 274 +++++++++++
doc/src/sgml/postgres-fdw.sgml | 18 +
src/backend/optimizer/util/relnode.c | 25 +
7 files changed, 1698 insertions(+), 57 deletions(-)

Hi,

in: doc/src/sgml/postgres-fdw.sgml, I believe this sentence:

"and none of the following disqualifying conditions applies:"

should be:

"and none of the following disqualifying conditions apply:"

Please, can a native English speaker confirm?

+1. to the above. FWIW, the rest of the original patch seems fine.

Robert Treat
https://xzilla.net