Re: postgres_fdw: statistics import can silently fall back to sampling across encodings

From: Etsuro Fujita <etsuro(dot)fujita(at)gmail(dot)com>
To: Baji Shaik <baji(dot)pgdev(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: postgres_fdw: statistics import can silently fall back to sampling across encodings
Date: 2026-12-02 11:31:40
Message-ID: CAPmGK14QTMujSRcQRBGHhf-vS6QqEh1j87pf784-t4GioMRGWA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello Baji,

On Tue, Nov 24, 2026 at 7:41 AM Baji Shaik <baji(dot)pgdev(at)gmail(dot)com> wrote:
> The attached patch makes postgres_fdw detect that case and fall back
> on purpose, rather than by accident.

The patch looks good to me overall. Here are a few minor comments:

* The patch checks if any remote_attnames have non-ASCII bytes before
the server-encoding check, but in most cases the local/remote
encodings would match because using UTF-8 for Postgres databases has
become the standard practice today, so I think it's better to do the
server-encoding check first, for efficiency.

* I think this warning message would be confusing when remote_encoding
is unknown:

+ ereport(WARNING,
+ errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("could not import statistics for
foreign table \"%s.%s\" --- remote server encoding \"%s\" differs from
local encoding \"%s\"",
+ local_schemaname, local_relname,
+ remote_encoding ? remote_encoding :
"unknown",
+ local_encoding));

I think it's fragile to proceed in such a state in the first place, so
I propose just falling back to sampling in that case. Otherwise, I
think it's better to add to the message the foreign server name as
well as an attribute name containing non-ASCII bytes.

* If we fall back to sampling, sorting the RemoteAttributeMapping
array is useless; to avoid that, I propose delaying the sort until
after the above checks, which also avoids issues where the attribute
name chosen varies depending on the sort order.

* I think the test case starting with this comment is redundant as
it's covered by the stats-import test cases in postgres_fdw.sql:

+# Sanity check: a same-encoding loopback must still import normally,
+# with no such warning.

To save cycles, I propose just removing it.

* To test "A remote/local encoding mismatch with plain ASCII column
names is safe", the patch creates
another database, but to save cycles, I propose reusing the EUC_JP
database to test it.

* IMO I think the file name "encoding_mismatch.pl" is vague about what
it tests. How about renaming it to "stats_import.pl"?

I updated the patch as above. Attached is an updated version of the
patch, in which I also updated some comments and the documentation.

Thanks for working on this issue!

Best regards,
Etsuro Fujita

Attachment Content-Type Size
0002-postgres_fdw-Fall-back-to-sampling-when-remote-encoding-efujita.patch application/octet-stream 8.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Rajesh Kumar Thandapani 2026-12-02 11:37:45 Re: Add aborted_txns and aborted_bytes to pg_stat_replication_slots
Previous Message Yao Feng 2026-12-02 11:21:00 Re: nbtree: opt-in leaf key prefix compression