pgsql: postgres_fdw: push down FUNCTION RTE into foreign joins

From: Alexander Korotkov <akorotkov(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: postgres_fdw: push down FUNCTION RTE into foreign joins
Date: 2026-08-19 10:49:25
Message-ID: E1wwdrI-00000001CbA-30di@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers pgsql-hackers

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: https://postgr.es/m/e3af56f9b2f1bd9fdf12aff6ca25b18d%40postgrespro.ru
Author: Alexander Pyhalov <a(dot)pyhalov(at)postgrespro(dot)ru>
Reviewed-by: Solaimurugan Vellaipandiyan <drsolaimurugan(dot)v(at)gmail(dot)com>
Reviewed-by: Alexander Korotkov <aekorotkov(at)gmail(dot)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(-)

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Alexander Korotkov 2026-08-19 14:31:08 pgsql: Fix compilation warning in 0ee83dd4a994
Previous Message Álvaro Herrera 2026-08-19 10:27:16 pgsql: Tighten ACL check in repack_is_permitted_for_relation()

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2026-08-19 11:09:16 Re: Introduce XID age based replication slot invalidation
Previous Message Jakub Wartak 2026-08-19 10:39:08 Re: MPTCP - multiplexing many TCP connections through one socket to get better bandwidth