Re: Several issues with postgres_fdw stats import

From: Etsuro Fujita <etsuro(dot)fujita(at)gmail(dot)com>
To: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>
Cc: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Several issues with postgres_fdw stats import
Date: 2026-09-11 17:11:52
Message-ID: CAPmGK16bNY6-1yExMOg8g5VZFq6U1icvtAodaY9MTSg-pt2O0w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Sep 11, 2026 at 12:46 AM Corey Huinker <corey(dot)huinker(at)gmail(dot)com> wrote:
> On Thu, Sep 10, 2026 at 11:02 AM Etsuro Fujita <etsuro(dot)fujita(at)gmail(dot)com> wrote:
>> On Thu, Sep 10, 2026 at 3:59 PM Fujii Masao <masao(dot)fujii(at)gmail(dot)com> wrote:
>> > postgres_fdw stats import seems to have several potential issues.

>> > (2) COLLATE is not supported by old remote servers
>> >
>> > Stats import sends COLLATE "C" to the remote server without checking
>> > its version, but COLLATE is supported only in v9.1 and later.
>> > The comment in deparse.c explicitly mentions this compatibility issue, and
>> > IMPORT FOREIGN SCHEMA disables collation import for remote servers older
>> > than v9.1.
>> >
>> > So, it seems stats import should handle this issue as well, e.g., either by
>> > avoiding COLLATE or by falling back to sampling.
>>
>> Good catch! I feel like just falling back to sampling.
>
> In other areas (pg_dump, for example) we're walking the minimum supported version to v10, so I don't feel like we want to spend much effort accommodating machines that went out of support > 12 years ago.

+1

> The query will error, it will fall back to sampling, and that's the right thing to do in these cases, in my opinion.

No, it won't fall back; the syntax error on the remote server will
lead to an error on the local server, actually. That isn't great, so
I modified postgres_fdw to do the fallback. Patch attached.

> The obvious counter-example are databases that are forks of postgres from the 8.x era (Vertica, Redshift), neither of which have a pg_stats view, so the queries are dead ends anyway. I could envision a future where those databases are encouraged to add a pg_stats view with the stats translated/synthesized to FDW-friendly values.

Me too.

>> > (3) n_distinct is ignored
>> >
>> > Stats import ignores the foreign table column's n_distinct option, whereas
>> > normal ANALYZE applies it after collecting statistics.
>> >
>> > IMO, n_distinct should also be applied to imported stats.
>>
>> Rather than making the code complicated for that, I feel like just
>> copying the remote table's stats as-proposed, as in most cases, those
>> stats are generated on the remote server under the correct settings of
>> parameters like n_distcint. How about adding a note about that?
>
> I agree. The user trusts the remote to have good stats, otherwise they wouldn't use this option.

I modified postgres-fdw.sgml as well to mention that that option is
ignored, tweaking a phrase a bit.

Best regards,
Etsuro Fujita

Attachment Content-Type Size
disallow-stats-import-from-old-servers.patch application/octet-stream 4.5 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Melanie Plageman 2026-09-11 17:24:17 Re: eliminate xl_heap_visible to reduce WAL (and eventually set VM on-access)
Previous Message Bharath Rupireddy 2026-09-11 17:07:37 Re: Support for 8-byte TOAST values, round two