postgres_fdw: statistics import can silently fall back to sampling across encodings
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
Hi,
After an offline chat with Etsuro, starting a discussion here
on an issue in postgres_fdw's statistics import (28972b6fc3d).
It matches up a remote table's columns with the local ones by name to
copy over their stats. When the remote and local databases use
different encodings and a column name has a non-ASCII character, that
match can miss, because the same local name can correspond to more
than one distinct byte sequence on the remote side once encoding
conversion is involved [1]Example: an EUC_JP remote and a UTF-8 local database, table with a column named 日付. Unpatched: WARNING: no attribute statistics found for column "日付" ANALYZE falls back to sampling even though the remote has stats for that column, with no hint as to why. Patched: WARNING: could not import statistics for foreign table "public.注文" --- remote server encoding "EUC_JP" differs from local encoding "UTF8". When that happens, postgres_fdw quietly
falls back to sampling instead of importing the remote's stats, with
no indication that it happened for this reason.
The attached patch makes postgres_fdw detect that case and fall back
on purpose, rather than by accident.
Thoughts?
[1]: Example: an EUC_JP remote and a UTF-8 local database, table with a column named 日付. Unpatched: WARNING: no attribute statistics found for column "日付" ANALYZE falls back to sampling even though the remote has stats for that column, with no hint as to why. Patched: WARNING: could not import statistics for foreign table "public.注文" --- remote server encoding "EUC_JP" differs from local encoding "UTF8"
column named 日付.
Unpatched: WARNING: no attribute statistics found for column "日付"
ANALYZE falls back to sampling even though the remote has stats
for that column, with no hint as to why.
Patched: WARNING: could not import statistics for foreign table
"public.注文" --- remote server encoding "EUC_JP" differs from
local encoding "UTF8"
Regards,
Baji Shaik
Hello,
At Mon, 23 Nov 2026 16:41:17 -0600, Baji Shaik <baji.pgdev@gmail.com> wrote in
The attached patch makes postgres_fdw detect that case and fall back
on purpose, rather than by accident.Thoughts?
It seems to me that the issue here is simply that the sort order of
column names differs between the remote and local databases. If so,
re-sorting the results from the remote server in byte order in the
local encoding should be sufficient. If the non-injectivity of
encoding conversion itself is a concern, it should affect not only
column names but also the contents of the imported statistics.
On the other hand, the target columns are selected on the remote side
using the column names sent from the local side. If distinct remote
column names are converted to the same local name, there will be
unmatched result rows. Conversely, if distinct local column names are
converted to the same remote name, at least one of them will not
survive the round trip unchanged. In either case, the existing
matching logic should detect the mismatch. The resulting error message
might not be particularly helpful, but I don't think this is a problem
in practice.
Therefore, as far as column name matching is concerned, I don't think
we need any special handling for the non-injectivity of encoding
conversion. Dealing with the difference in sort order should be
sufficient.
We could simply build an array of result row indexes and sort it
locally by column name before applying the existing matching logic in
match_attrmap().
rowmap = palloc_array(int, numrows);
for (int i = 0; i < numrows; i++)
rowmap[i] = i;
qsort_arg(rowmap, numrows, sizeof(int), attstats_row_cmp, res);
(Then, use rowmap[row] instead of row when accessing the result set.)
Regards,
--
Kyotaro Horiguchi
NTT Open Source Software Center
Hello Horiguchi-san,
On Tue, Nov 24, 2026 at 1:10 PM Kyotaro Horiguchi
<horikyota.ntt@gmail.com> wrote:
At Mon, 23 Nov 2026 16:41:17 -0600, Baji Shaik <baji.pgdev@gmail.com> wrote in
The attached patch makes postgres_fdw detect that case and fall back
on purpose, rather than by accident.
It seems to me that the issue here is simply that the sort order of
column names differs between the remote and local databases. If so,
re-sorting the results from the remote server in byte order in the
local encoding should be sufficient. If the non-injectivity of
encoding conversion itself is a concern, it should affect not only
column names but also the contents of the imported statistics.
Our concern is the non-injectivity issue, but I don't follow the last
part, so could you elaborate a bit more on it?
On the other hand, the target columns are selected on the remote side
using the column names sent from the local side. If distinct remote
column names are converted to the same local name, there will be
unmatched result rows. Conversely, if distinct local column names are
converted to the same remote name, at least one of them will not
survive the round trip unchanged. In either case, the existing
matching logic should detect the mismatch. The resulting error message
might not be particularly helpful, but I don't think this is a problem
in practice.
I think we could detect it, but I suggested to him just falling back
to sampling, rather than complicating the code for that, because I
think the use case where this issue occurs is rather rare, as 1)
today, using UTF-8 for Postgres databases has become the standard
practice, and 2) IMO it's not that common to use non-ASCII characters
for database column names. (Having said that, if there is a need to
support the case, I'd like to work on that as an improvement, rather
than a fix.)
Therefore, as far as column name matching is concerned, I don't think
we need any special handling for the non-injectivity of encoding
conversion. Dealing with the difference in sort order should be
sufficient.We could simply build an array of result row indexes and sort it
locally by column name before applying the existing matching logic in
match_attrmap().rowmap = palloc_array(int, numrows);
for (int i = 0; i < numrows; i++)
rowmap[i] = i;qsort_arg(rowmap, numrows, sizeof(int), attstats_row_cmp, res);
(Then, use rowmap[row] instead of row when accessing the result set.)
Actually, we discussed this idea off-list, but didn't use it, for the
reasons mentioned above.
Thanks for the comments!
Best regards,
Etsuro Fujita