diff --git a/contrib/postgres_fdw/meson.build b/contrib/postgres_fdw/meson.build index 3e2ed06b766..2543213e747 100644 --- a/contrib/postgres_fdw/meson.build +++ b/contrib/postgres_fdw/meson.build @@ -52,6 +52,7 @@ tests += { 'tests': [ 't/001_auth_scram.pl', 't/010_subscription.pl', + 't/020_stats_import.pl', ], }, } diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index ccc97d53286..c9b3d10a734 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -26,6 +26,7 @@ #include "executor/instrument.h" #include "foreign/fdwapi.h" #include "funcapi.h" +#include "mb/pg_wchar.h" #include "miscadmin.h" #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" @@ -609,6 +610,7 @@ static RemoteAttributeMapping *build_remattrmap(Relation relation, List *va_cols int *p_attrcnt, StringInfo column_list); static void free_remattrmap(RemoteAttributeMapping *map, int len); static bool attname_in_list(const char *attname, List *va_cols); +static bool attname_ascii_only(const char *attname); static int remattrmap_cmp(const void *v1, const void *v2); static bool match_attrmap(PGresult *res, const char *local_schemaname, @@ -5915,6 +5917,9 @@ fetch_remote_statistics(Relation relation, /* Try to get attribute stats if needed. */ if (attrcnt > 0) { + const char *remote_encoding = NULL; + const char *local_encoding = NULL; + /* * The fetch_attstats query sends COLLATE "C" to the remote * server; if it hasn't got it, fallback to sampling. @@ -5928,6 +5933,57 @@ fetch_remote_statistics(Relation relation, goto fetch_cleanup; } + /* + * If the remote server's encoding is unknown, fallback to + * sampling. + */ + remote_encoding = PQparameterStatus(conn, "server_encoding"); + if (remote_encoding == NULL) + { + ereport(WARNING, + errmsg("could not import statistics for foreign table \"%s.%s\" --- the encoding of foreign server \"%s\" is unknown", + local_schemaname, local_relname, + server->servername)); + goto fetch_cleanup; + } + + /* + * When the fetch_attstats query is executed below, attribute + * names come back encoded per client_encoding, which we set to + * our own encoding in connection.c. If any of them have + * non-ASCII bytes and the remote server's encoding differs from + * ours, that conversion isn't guaranteed to be one-to-one (e.g. + * two distinct EUC_JP code points can both map to the same UTF8 + * one), so two differently-named remote columns could arrive with + * identical bytes (Also, even if that doesn't happen, remote + * sorting of such columns with COLLATE "C" might still produce + * different results than local sorting with strcmp().) Matching + * statistics to columns by attribute name isn't safe then, so + * fallback to sampling. Note that plain ASCII names aren't at + * risk: every server encoding we support is an ASCII-compatible + * superset. + */ + local_encoding = GetDatabaseEncodingName(); + if (strcmp(remote_encoding, local_encoding) != 0) + for (int mapidx = 0; mapidx < attrcnt; mapidx++) + if (!attname_ascii_only(remattrmap[mapidx].remote_attname)) + { + ereport(WARNING, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("could not import statistics for foreign table \"%s.%s\" --- unable to fetch attribute statistics for non-ASCII named column \"%s\" of remote table \"%s.%s\" beccause encoding \"%s\" of foreign server \"%s\" differs from local encoding \"%s\"", + local_schemaname, local_relname, + remattrmap[mapidx].remote_attname, + remote_schemaname, remote_relname, + remote_encoding, server->servername, + local_encoding)); + goto fetch_cleanup; + } + + /* Sort the mappings by remote_attname if needed. */ + if (attrcnt > 1) + qsort(remattrmap, attrcnt, sizeof(RemoteAttributeMapping), + remattrmap_cmp); + /* Fetch attribute stats. */ remstats->att = attstats = fetch_attstats(conn, server_version_num, @@ -6144,10 +6200,6 @@ build_remattrmap(Relation relation, List *va_cols, } appendStringInfoChar(column_list, ']'); - /* Sort the mappings by remote_attname if needed. */ - if (attrcnt > 1) - qsort(remattrmap, attrcnt, sizeof(RemoteAttributeMapping), remattrmap_cmp); - *p_attrcnt = attrcnt; return remattrmap; } @@ -6195,6 +6247,20 @@ attname_in_list(const char *attname, List *va_cols) return false; } +/* + * Test if an attribute name is ASCII-only. + */ +static bool +attname_ascii_only(const char *attname) +{ + for (const unsigned char *p = (const unsigned char *) attname; *p; p++) + { + if (*p > 0x7F) + return false; + } + return true; +} + /* * Compare two RemoteAttributeMappings for sorting. */ diff --git a/contrib/postgres_fdw/t/020_stats_import.pl b/contrib/postgres_fdw/t/020_stats_import.pl new file mode 100644 index 00000000000..bd3cc9cbf40 --- /dev/null +++ b/contrib/postgres_fdw/t/020_stats_import.pl @@ -0,0 +1,96 @@ +# Copyright (c) 2024-2026, PostgreSQL Global Development Group + +# Test cross-encoding statistics import. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +my $node = PostgreSQL::Test::Cluster->new('node'); +$node->init; +$node->start; + +$node->safe_psql('postgres', <safe_psql('postgres', <safe_psql('remote_eucjp', <port; +my $host = $node->host; +$node->safe_psql('postgres', <psql('postgres', <psql('postgres', <stop; +done_testing(); diff --git a/doc/src/sgml/postgres-fdw.sgml b/doc/src/sgml/postgres-fdw.sgml index a2c1ca80544..168db894aa0 100644 --- a/doc/src/sgml/postgres-fdw.sgml +++ b/doc/src/sgml/postgres-fdw.sgml @@ -393,8 +393,11 @@ OPTIONS (ADD password_required 'false'); Note that this option doesn't currently support the case where the - remote table is (or was once) inherited. This is an implementation - restriction that might be fixed in a future release. + remote table is (or was once) inherited. Also note that if the foreign + server's encoding doesn't match the local one, it doesn't currently + support remote column names containing non-ASCII characters either. + These are implementation restrictions that might be fixed in future + releases.