Re: Import Statistics in postgres_fdw before resorting to sampling.
Corey Huinker <corey.huinker@gmail.com>
From: Corey Huinker <corey.huinker@gmail.com>
To: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Cc: Etsuro Fujita <etsuro.fujita@gmail.com>,
Michael Paquier <michael@paquier.xyz>, pgsql-hackers@postgresql.org, jkatz@postgresql.org, nathandbossart@gmail.com
Date: 2026-01-22T22:20:12Z
Lists: pgsql-hackers
Attachments
- v12-0003-Add-remote_analyze-to-postgres_fdw-remote-statis.patch (text/x-patch) patch v12-0003
- v12-0002-Add-remote-statistics-fetching-to-postgres_fdw.patch (text/x-patch) patch v12-0002
- v12-0001-Add-FDW-functions-for-importing-optimizer-statis.patch (text/x-patch) patch v12-0001
On Thu, Jan 22, 2026 at 5:16 AM Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> wrote: > On Thu, Jan 22, 2026 at 2:21 AM Corey Huinker <corey.huinker@gmail.com> > wrote: > >> > >> Changes in this release, aside from rebasing: > >> > >> - The generic analyze and fdw.h changes are in their own patch (0001) > that ignores contrib/postgres_fdw entirely. > >> - The option for remote_analyze has been moved to its own patch (0003). > >> - The errors raised are now warnings, to ensure that we can always fall > back to row sampling. > >> - All local attributes with attstatarget > 0 must get matching remote > statistics or the import is considered a failure. > >> - The pg_restore_attribute_stats() call has been turned into a prepared > statement, for clarity and some minor parsing savings. > >> - The calls to pg_restore_relation_stats() are parameterized, but not > prepared as this is rarely called more than once. > >> - postgresStatisticsAreImportable will now disqualify a table if has > extended statistics objects, because we can't compute those without a row > sample. > > > > Thanks Corey for breaking down these patches. It makes reviewing easier. > > analyze_rel() and acquire_inherited_sample_rows() both call > fdwroutine->AnalyzeForeignTable() but only the first one uses the > statistics import facility. Is that intentional? Typical use case of > sharding will create a partitioned table with foreign tables as > partitions. The partitions will be analyzed by the second function. > Thus a big use case of postgres_fdw won't be able to use the import > statistics facility. That seems like a major drawback of this patch. > Thinking more about it, acquire_inherited_sample_rows() accumulates > the sample rows from the child tables and extracts statistics from > those rows and then updates corresponding pg_statistics rows. Doing > that through import statistics seems a bit tricky since we need to be > able to combine statistics from multiple relations. Can we do that? > We can't synthesize sample rows from imported statistics, no. > There's an advantage if we can combine stats across multiple relations > - we don't have to sample children twice when analyzing the parent > without ONLY. Instead we could produce parent statistics by combining > statistics across children and the parent. To me this looks like > altogether a different beast just like partial aggregates. > I think this patch is only ever going to get us out of 1 of the 2 samples, which isn't ideal but it is a savings. > > It will be good to fix this drawback. If not, at least we should > figure out (plan/POC) how to deal with the child tables? We need to at > least document this drawback - the documentation in the current patch > reads as if all foreign tables will use this facility when available. > Yes, we will have to note the limitation. I have made that note, as well as the documentation fix attached.
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
postgres_fdw: Replace buffers in RemoteAttributeMapping with pointers.
- aa1f93a3387a 19 (unreleased) landed
-
Add support for importing statistics from remote servers.
- 28972b6fc3dc 19 (unreleased) landed