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

From: Etsuro Fujita <etsuro(dot)fujita(at)gmail(dot)com>
To: Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>
Cc: baji(dot)pgdev(at)gmail(dot)com, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: postgres_fdw: statistics import can silently fall back to sampling across encodings
Date: 2026-11-24 10:20:46
Message-ID: CAPmGK14n3LN9Cx6bxEtDwZKCrcxgVS0f-raJH4kjBUzZ=6fAuQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello Horiguchi-san,

On Tue, Nov 24, 2026 at 1:10 PM Kyotaro Horiguchi
<horikyota(dot)ntt(at)gmail(dot)com> wrote:
> At Mon, 23 Nov 2026 16:41:17 -0600, Baji Shaik <baji(dot)pgdev(at)gmail(dot)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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message shveta malik 2026-11-24 10:36:03 Re: [PATCH] Report changes discarded for relations not in the subscription
Previous Message vignesh C 2026-11-24 09:50:16 Re: Fix a wrong comment in UpdateDeadTupleRetentionStatus()