pgsql: postgres_fdw: Fix issues with statistics import.

From: Etsuro Fujita <efujita(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: postgres_fdw: Fix issues with statistics import.
Date: 2026-09-04 09:42:10
Message-ID: E1x2QQy-00000003R1l-3y8w@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

postgres_fdw: Fix issues with statistics import.

When analyzing a foreign table pointing to a partitioned or inherited
table on the remote server, this method just imported relpages and
reltuples for the parent table from the remote pg_class, causing a
discrepancy or an error in results compared to the sampling method.

* In the partitioning case, while the sampling method sets relpages=0,
this method set relpages=-1. (For reltuples, the results of both
methods match.)
* In the inheritance case, while the sampling method sets
reltuples=(# of rows in the parent and child tables), this method set
reltuples=(# of rows in the parent table). (For relpages, the results
of both methods match.)

As relpages and reltuples are used for costing foreign paths, these
would lead to incorrect plans or plan changes depending on the method.
To fix, do the following:

* For the partitioning case, modify postgres_fdw to set relpages=0 to
match the sampling method.
* For the inheritance case, we could also modify it to set the correct
reltuples value by fetching reltuples for the child tables as well,
but 1) the change isn't small, and 2) table inheritance hasn't been
used that much these days, so fix by disabling this method.

While at it, adjust comments and docs slightly.

Oversight in commit 28972b6fc.

Author: Etsuro Fujita <etsuro(dot)fujita(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAPmGK173b2Xvyoeq%3DY7L09H%2BajgMgxC2fYe%3D7CCunZz8tGTfFA%40mail.gmail.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/6a857156827b6a938bd02b9bf7281e7b71dfd088

Modified Files
--------------
contrib/postgres_fdw/deparse.c | 11 ++--
contrib/postgres_fdw/expected/postgres_fdw.out | 27 +++++++++
contrib/postgres_fdw/postgres_fdw.c | 79 ++++++++++++++++++--------
contrib/postgres_fdw/sql/postgres_fdw.sql | 17 ++++++
doc/src/sgml/postgres-fdw.sgml | 5 +-
5 files changed, 108 insertions(+), 31 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Daniel Gustafsson 2026-09-04 11:46:42 pgsql: ssl: Fix expected output for OpenSSL 4
Previous Message Michael Paquier 2026-09-04 07:56:02 pgsql: Add pgstat test for reset of dead/live counters with table trunc