pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Several issues with postgres_fdw stats import
27+ messages / 7 participants
[nested] [flat]

Commits from this thread:
  2026-09-26 7d47e41238 18c04668f7  Fix privilege handling in postgres_fdw statistics import.
* Several issues with postgres_fdw stats import
@ 2026-09-10 06:58 Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 10:24 ` Re: Several issues with postgres_fdw stats import Osama Abdul Qader <osamaabdulqader.cs@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 2 replies; 27+ messages in thread

From: Fujii Masao @ 2026-09-10 06:58 UTC (permalink / raw)
  To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi,

postgres_fdw stats import seems to have several potential issues.


(1) User-defined functions may be executed with unexpected privileges

ANALYZE on a foreign table with import_stats disabled invokes
user-defined functions (e.g., domain constraints) as the foreign table's
owner. But, with import_stats enabled, they are invoked as the user
running ANALYZE. So, if that user is a superuser, those functions would
be run with superuser privileges. Could this be a security issue?


(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.


(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.


(4) Imported relpages may use different block sizes

Stats import stores the remote relpages value unchanged, whereas the
normal ANALYZE uses pg_relation_size() divided by the local BLCKSZ to
handle the case where the block sizes differ between the local and remote
servers.

We should convert the imported page count to local block units, for example?

Regards,

-- 
Fujii Masao






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
@ 2026-09-10 10:24 ` Osama Abdul Qader <osamaabdulqader.cs@gmail.com>
  2026-09-10 15:14   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  1 sibling, 1 reply; 27+ messages in thread

From: Osama Abdul Qader @ 2026-09-10 10:24 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi,

While reviewing the postgres_fdw statistics import implementation, I
noticed several potential issues:

   1. *User-defined functions and privileges:* With stats import enabled,
   some user-defined functions may be executed as the user running ANALYZE
   rather than as the foreign table owner. This could potentially result in
   functions being executed with unintended privileges, particularly when
   ANALYZE is run by a superuser.
   2. *COLLATE and old remote servers:* The generated query may send COLLATE
   "C" to remote servers without checking their version. Since COLLATE is
   only supported from PostgreSQL 9.1, this could break statistics import
   against older servers. IMPORT FOREIGN SCHEMA already handles this
   compatibility issue.
   3. *n_distinct** is ignored:* The imported statistics path appears not
   to apply the foreign table column's n_distinct option, whereas normal
   ANALYZE applies it after collecting statistics.
   4. *relpages** and different block sizes:* The remote relpages value
   appears to be stored unchanged. If the local and remote servers use
   different BLCKSZ values, this would result in an incorrect local page
   count. The value should presumably be converted to local block units,
   similar to how normal ANALYZE handles it.

Could these be addressed in the statistics import implementation?

On Thu, Sep 10, 2026 at 12:29 PM Fujii Masao <masao.fujii@gmail.com> wrote:

> Hi,
>
> postgres_fdw stats import seems to have several potential issues.
>
>
> (1) User-defined functions may be executed with unexpected privileges
>
> ANALYZE on a foreign table with import_stats disabled invokes
> user-defined functions (e.g., domain constraints) as the foreign table's
> owner. But, with import_stats enabled, they are invoked as the user
> running ANALYZE. So, if that user is a superuser, those functions would
> be run with superuser privileges. Could this be a security issue?
>
>
> (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.
>
>
> (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.
>
>
> (4) Imported relpages may use different block sizes
>
> Stats import stores the remote relpages value unchanged, whereas the
> normal ANALYZE uses pg_relation_size() divided by the local BLCKSZ to
> handle the case where the block sizes differ between the local and remote
> servers.
>
> We should convert the imported page count to local block units, for
> example?
>
> Regards,
>
> --
> Fujii Masao
>
>
>

^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 10:24 ` Re: Several issues with postgres_fdw stats import Osama Abdul Qader <osamaabdulqader.cs@gmail.com>
@ 2026-09-10 15:14   ` Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 0 replies; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-10 15:14 UTC (permalink / raw)
  To: Osama Abdul Qader <osamaabdulqader.cs@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Thu, Sep 10, 2026 at 7:55 PM Osama Abdul Qader
<osamaabdulqader.cs@gmail.com> wrote:
> While reviewing the postgres_fdw statistics import implementation, I noticed several potential issues:

[snip]

> Could these be addressed in the statistics import implementation?

I noticed that your comments are exactly the same as Fujii-san.  You
both used the same LLM for this?

Anyway, please check the email I sent just before.  Thanks for reviewing!

Best regards,
Etsuro Fujita





^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
@ 2026-09-10 15:01 ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  1 sibling, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-10 15:01 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Thu, Sep 10, 2026 at 3:59 PM Fujii Masao <masao.fujii@gmail.com> wrote:
> postgres_fdw stats import seems to have several potential issues.

> (1) User-defined functions may be executed with unexpected privileges
>
> ANALYZE on a foreign table with import_stats disabled invokes
> user-defined functions (e.g., domain constraints) as the foreign table's
> owner. But, with import_stats enabled, they are invoked as the user
> running ANALYZE. So, if that user is a superuser, those functions would
> be run with superuser privileges. Could this be a security issue?

Sorry, I don't follow this.  Could you elaborate on it using an example?

> (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.

> (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?

> (4) Imported relpages may use different block sizes
>
> Stats import stores the remote relpages value unchanged, whereas the
> normal ANALYZE uses pg_relation_size() divided by the local BLCKSZ to
> handle the case where the block sizes differ between the local and remote
> servers.
>
> We should convert the imported page count to local block units, for example?

Good point!  Actually, this is a known issue in the normal ANALYZE:

/*
 * Construct SELECT statement to acquire size in blocks of given relation.
 *
 * Note: we use local definition of block size, not remote definition.
 * This is perhaps debatable.
 *
 * Note: pg_relation_size() exists in 8.1 and later.
 */
void
deparseAnalyzeSizeSql(StringInfo buf, Relation rel)

From a cost calculation perspective, I think it's appropriate to use
the imported relpages as-is, as it's used to estimate the cost for
remote operations, not local ones, and thus we should actually instead
fix the normal-ANALYZE handling to calculate the size based on the
remote definition of block size.

Thanks for the report!

Best regards,
Etsuro Fujita





^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-10 15:46   ` Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-22 11:35     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-22 11:38     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 3 replies; 27+ messages in thread

From: Corey Huinker @ 2026-09-10 15:46 UTC (permalink / raw)
  To: Etsuro Fujita <etsuro.fujita@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Thu, Sep 10, 2026 at 11:02 AM Etsuro Fujita <etsuro.fujita@gmail.com>
wrote:

> On Thu, Sep 10, 2026 at 3:59 PM Fujii Masao <masao.fujii@gmail.com> wrote:
> > postgres_fdw stats import seems to have several potential issues.
>
> > (1) User-defined functions may be executed with unexpected privileges
> >
> > ANALYZE on a foreign table with import_stats disabled invokes
> > user-defined functions (e.g., domain constraints) as the foreign table's
> > owner. But, with import_stats enabled, they are invoked as the user
> > running ANALYZE. So, if that user is a superuser, those functions would
> > be run with superuser privileges. Could this be a security issue?
>
> Sorry, I don't follow this.  Could you elaborate on it using an example?
>

Me either. I think he's referring to generated columns, in which case the
generated column wouldn't have a corresponding source column, hence would
fail match_attrmap() and already falls back to sampling.


>
> > (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. The query
will error, it will fall back to sampling, and that's the right thing to do
in these cases, in my opinion.

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.

All of these things should already fall back to sampling.


>
> > (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.


>
> From a cost calculation perspective, I think it's appropriate to use
> the imported relpages as-is, as it's used to estimate the cost for
> remote operations, not local ones, and thus we should actually instead
> fix the normal-ANALYZE handling to calculate the size based on the
> remote definition of block size.
>

+1

^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
@ 2026-09-11 17:11     ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2 siblings, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-11 17:11 UTC (permalink / raw)
  To: Corey Huinker <corey.huinker@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Fri, Sep 11, 2026 at 12:46 AM Corey Huinker <corey.huinker@gmail.com> wrote:
> On Thu, Sep 10, 2026 at 11:02 AM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:
>> On Thu, Sep 10, 2026 at 3:59 PM Fujii Masao <masao.fujii@gmail.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

Attachments:

disallow-stats-import-from-old-servers.patch (application/octet-stream, 4.5K) download diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index adfdb91cc20..1c0a1d25dcc 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -596,6 +596,7 @@ static bool fetch_remote_statistics(Relation relation, const char *local_schemaname, const char *local_relname, ForeignTable *table, + ForeignServer *server, RemoteStatsResults *remstats, RemoteAttributeMapping **p_remattrmap, int *p_attrcnt); @@ -5763,7 +5764,7 @@ postgresImportForeignStatistics(Relation relation, List *va_cols, int elevel) starttime = GetCurrentTimestamp(); ok = fetch_remote_statistics(relation, va_cols, - schemaname, relname, table, + schemaname, relname, table, server, &remstats, &remattrmap, &attrcnt); if (ok) @@ -5796,6 +5797,7 @@ fetch_remote_statistics(Relation relation, const char *local_schemaname, const char *local_relname, ForeignTable *table, + ForeignServer *server, RemoteStatsResults *remstats, RemoteAttributeMapping **p_remattrmap, int *p_attrcnt) @@ -5912,6 +5914,19 @@ fetch_remote_statistics(Relation relation, /* Try to get attribute stats if needed. */ if (attrcnt > 0) { + /* + * The fetch_attstats query sends COLLATE "C" to the remote + * server; if it hasn't got it, fallback to sampling. + */ + if (server_version_num < 90100) + { + ereport(WARNING, + errmsg("could not import statistics for foreign table \"%s.%s\" --- foreign server \"%s\" is too old to support attribute statistics import", + local_schemaname, local_relname, + server->servername)); + goto fetch_cleanup; + } + /* Fetch attribute stats. */ remstats->att = attstats = fetch_attstats(conn, server_version_num, @@ -5987,6 +6002,9 @@ fetch_attstats(PGconn *conn, int server_version_num, StringInfoData sql; PGresult *res; + /* The caller guaranttees the remote server is v9.1 or later. */ + Assert(server_version_num >= 90100); + initStringInfo(&sql); appendStringInfoString(&sql, "SELECT DISTINCT ON (attname COLLATE \"C\") attname," @@ -6030,18 +6048,11 @@ fetch_attstats(PGconn *conn, int server_version_num, column_list); /* - * inherited is supported since Postgres 9.0 - * - * Note that this is okay because for now, we support the case where the - * remote table is partitioned, but not the case where it is inherited - * (see fetch_remote_statistics()). + * inherited and COLLATE are supported since Postgres 9.0 and 9.1, + * respectively. */ - if (server_version_num >= 90000) - appendStringInfoString(&sql, - " ORDER BY attname COLLATE \"C\", inherited DESC"); - else - appendStringInfoString(&sql, - " ORDER BY attname COLLATE \"C\""); + appendStringInfoString(&sql, + " ORDER BY attname COLLATE \"C\", inherited DESC"); res = pgfdw_exec_query(conn, sql.data, NULL); if (PQresultStatus(res) != PGRES_TUPLES_OK) diff --git a/doc/src/sgml/postgres-fdw.sgml b/doc/src/sgml/postgres-fdw.sgml index 80f79c017d6..fe4e6478e28 100644 --- a/doc/src/sgml/postgres-fdw.sgml +++ b/doc/src/sgml/postgres-fdw.sgml @@ -370,14 +370,15 @@ OPTIONS (ADD password_required 'false'); <para> This option, which can be specified for a foreign table or a foreign server, determines if <command>ANALYZE</command> on a foreign table - will instead attempt to fetch the existing statistics for the foreign - table on the remote server, and import those statistics directly to - the local server. If the attempt failed, statistics are collected by - row sampling on the foreign table. + will instead attempt to fetch the existing statistics for the remote + table, and import those statistics directly to the local server + (without applying the foreign table columns' + <literal>n_distinct</literal> option). If the attempt failed, + statistics are collected by row sampling on the foreign table. This option is only useful if the remote table is one that can have regular statistics (tables, not inherited, and materialized views). When using this option, <emphasis>it is the user's responsibility - </emphasis> to ensure that the existing statistics for the foreign + </emphasis> to ensure that the existing statistics for the remote table are up-to-date. The default is <literal>false</literal>. </para>
^ permalink raw reply [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-11 20:53       ` Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Corey Huinker @ 2026-09-11 20:53 UTC (permalink / raw)
  To: Etsuro Fujita <etsuro.fujita@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

>
>
> 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.
>

My mistake. The patch looks good. Clearly there's no sufficiently venerable
buildfarm animal that could have detected this, or else we would have found
it sooner, and it's hard to justify supporting it if we cant test it.


>
> > 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.
>

It's a bummer that this change will make it slightly harder to do so, but
the chance of redshift or vertica doing such a thing is very low, and if
they did they'd probably bump their server_version_number along with the
change.


> e 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.
>
>
+1. Applies clean. Passes tests tho obviously the test we needed all along
would have been on a very old buildfarm animal. The wording of the
documentation change is clear.

^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
@ 2026-09-14 11:12         ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-14 11:12 UTC (permalink / raw)
  To: Corey Huinker <corey.huinker@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Sat, Sep 12, 2026 at 5:53 AM Corey Huinker <corey.huinker@gmail.com> wrote:

>> 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.
>
> My mistake. The patch looks good. Clearly there's no sufficiently venerable buildfarm animal that could have detected this, or else we would have found it sooner, and it's hard to justify supporting it if we cant test it.

>> e 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.
>
> +1. Applies clean. Passes tests tho obviously the test we needed all along would have been on a very old buildfarm animal. The wording of the documentation change is clear.

Pushed/backpatched.

The regression test just uses a loopback server, so it only tests this
feature against the same version...  I think it would be nice if we
could do so against old versions, but that would need a new testing
framework that can also support other features like IMPORT FOREIGN
SCHEMA, so I'd like to leave that for future work.

Thanks for looking!

Best regards,
Etsuro Fujita






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-15 08:52           ` Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Fujii Masao @ 2026-09-15 08:52 UTC (permalink / raw)
  To: Etsuro Fujita <etsuro.fujita@gmail.com>; +Cc: Corey Huinker <corey.huinker@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Mon, Sep 14, 2026 at 8:12 PM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:
> Pushed/backpatched.

Thanks for working on this issue!

When I used v11 as the remote server, stats import via postgres_fdw
resulted in the following error. I think this should be addressed as well.

ERROR:  collations are not supported by type name
CONTEXT:  remote SQL command: SELECT DISTINCT ON (attname COLLATE "C")
attname, null_frac, avg_width, n_distinct, most_common_vals,
most_common_freqs, histogram_bounds, correlation, most_common_elems,
most_common_elem_freqs, elem_count_histogram, NULL, NULL, NULL FROM
pg_catalog.pg_stats WHERE schemaname = 'public' AND tablename = 't'
AND attname = ANY(ARRAY['i', 'j']) ORDER BY attname COLLATE "C",
inherited DESC

Regards,

-- 
Fujii Masao






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
@ 2026-09-15 11:31             ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-15 11:31 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: Corey Huinker <corey.huinker@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Tue, Sep 15, 2026 at 5:52 PM Fujii Masao <masao.fujii@gmail.com> wrote:
> When I used v11 as the remote server, stats import via postgres_fdw
> resulted in the following error. I think this should be addressed as well.
>
> ERROR:  collations are not supported by type name
> CONTEXT:  remote SQL command: SELECT DISTINCT ON (attname COLLATE "C")
> attname, null_frac, avg_width, n_distinct, most_common_vals,
> most_common_freqs, histogram_bounds, correlation, most_common_elems,
> most_common_elem_freqs, elem_count_histogram, NULL, NULL, NULL FROM
> pg_catalog.pg_stats WHERE schemaname = 'public' AND tablename = 't'
> AND attname = ANY(ARRAY['i', 'j']) ORDER BY attname COLLATE "C",
> inherited DESC

Reproduced here.  Will fix.

I was thinking this lacks testing against old servers, so thanks for
the testing and report!

Best regards,
Etsuro Fujita






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-16 10:25               ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-16 10:25 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: Corey Huinker <corey.huinker@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Tue, Sep 15, 2026 at 8:31 PM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:
> On Tue, Sep 15, 2026 at 5:52 PM Fujii Masao <masao.fujii@gmail.com> wrote:
> > When I used v11 as the remote server, stats import via postgres_fdw
> > resulted in the following error. I think this should be addressed as well.
> >
> > ERROR:  collations are not supported by type name
> > CONTEXT:  remote SQL command: SELECT DISTINCT ON (attname COLLATE "C")
> > attname, null_frac, avg_width, n_distinct, most_common_vals,
> > most_common_freqs, histogram_bounds, correlation, most_common_elems,
> > most_common_elem_freqs, elem_count_histogram, NULL, NULL, NULL FROM
> > pg_catalog.pg_stats WHERE schemaname = 'public' AND tablename = 't'
> > AND attname = ANY(ARRAY['i', 'j']) ORDER BY attname COLLATE "C",
> > inherited DESC
>
> Reproduced here.  Will fix.

As the error message says, the cause of this is that the name type
isn't collatable in v11.  It was made so in v12, so I fixed this by
just s/attname COLLATE "C"/attname::text COLLATE "C"/ to the query
generated for v11 or older.

Also, I fixed another bug in the same function: a typo in the if-test
to check whether the remote server is v9.2 or later.  The if-test in
Corey's original version was correct, so that's my fault when updating
it to the current version.  :palmface:

Attached is a patch for that.

Best regards,
Etsuro Fujita

Attachments:

fix-oversights-in-fetch_attstats.patch (application/octet-stream, 1.9K) download diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index d890bc02b77..3ea0682af44 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -6007,8 +6007,16 @@ fetch_attstats(PGconn *conn, int server_version_num, Assert(server_version_num >= 90100); initStringInfo(&sql); + + /* Type name is collatable since Postgres 12 */ + if (server_version_num >= 120000) + appendStringInfoString(&sql, + "SELECT DISTINCT ON (attname COLLATE \"C\") attname,"); + else + appendStringInfoString(&sql, + "SELECT DISTINCT ON (attname::text COLLATE \"C\") attname,"); + appendStringInfoString(&sql, - "SELECT DISTINCT ON (attname COLLATE \"C\") attname," " null_frac," " avg_width," " n_distinct," @@ -6018,7 +6026,7 @@ fetch_attstats(PGconn *conn, int server_version_num, " correlation,"); /* Elements stats are supported since Postgres 9.2 */ - if (server_version_num >= 92000) + if (server_version_num >= 90200) appendStringInfoString(&sql, " most_common_elems," " most_common_elem_freqs," @@ -6049,11 +6057,15 @@ fetch_attstats(PGconn *conn, int server_version_num, column_list); /* - * inherited and COLLATE are supported since Postgres 9.0 and 9.1, - * respectively. + * Type name is collatable since Postgres 12 (inherited and COLLATE are + * supported since Postgres 9.0 and 9.1, respectively) */ - appendStringInfoString(&sql, - " ORDER BY attname COLLATE \"C\", inherited DESC"); + if (server_version_num >= 120000) + appendStringInfoString(&sql, + " ORDER BY attname COLLATE \"C\", inherited DESC"); + else + appendStringInfoString(&sql, + " ORDER BY attname::text COLLATE \"C\", inherited DESC"); res = pgfdw_exec_query(conn, sql.data, NULL); if (PQresultStatus(res) != PGRES_TUPLES_OK)
^ permalink raw reply [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-16 17:58                 ` Corey Huinker <corey.huinker@gmail.com>
  2026-09-18 09:23                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Corey Huinker @ 2026-09-16 17:58 UTC (permalink / raw)
  To: Etsuro Fujita <etsuro.fujita@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Wed, Sep 16, 2026 at 6:25 AM Etsuro Fujita <etsuro.fujita@gmail.com>
wrote:

> On Tue, Sep 15, 2026 at 8:31 PM Etsuro Fujita <etsuro.fujita@gmail.com>
> wrote:
> > On Tue, Sep 15, 2026 at 5:52 PM Fujii Masao <masao.fujii@gmail.com>
> wrote:
> > > When I used v11 as the remote server, stats import via postgres_fdw
> > > resulted in the following error. I think this should be addressed as
> well.
> > >
> > > ERROR:  collations are not supported by type name
> > > CONTEXT:  remote SQL command: SELECT DISTINCT ON (attname COLLATE "C")
> > > attname, null_frac, avg_width, n_distinct, most_common_vals,
> > > most_common_freqs, histogram_bounds, correlation, most_common_elems,
> > > most_common_elem_freqs, elem_count_histogram, NULL, NULL, NULL FROM
> > > pg_catalog.pg_stats WHERE schemaname = 'public' AND tablename = 't'
> > > AND attname = ANY(ARRAY['i', 'j']) ORDER BY attname COLLATE "C",
> > > inherited DESC
> >
> > Reproduced here.  Will fix.
>
> As the error message says, the cause of this is that the name type
> isn't collatable in v11.  It was made so in v12, so I fixed this by
> just s/attname COLLATE "C"/attname::text COLLATE "C"/ to the query
> generated for v11 or older.
>


+1


>
> Also, I fixed another bug in the same function: a typo in the if-test
> to check whether the remote server is v9.2 or later.  The if-test in
> Corey's original version was correct, so that's my fault when updating
> it to the current version.  :palmface:
>

+1. This is a good argument for #define-ing the relevant server version
numbers going forward.

^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
@ 2026-09-18 09:23                   ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-18 17:37                     ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-18 09:23 UTC (permalink / raw)
  To: Corey Huinker <corey.huinker@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Thu, Sep 17, 2026 at 2:58 AM Corey Huinker <corey.huinker@gmail.com> wrote:
> On Wed, Sep 16, 2026 at 6:25 AM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:

>> As the error message says, the cause of this is that the name type
>> isn't collatable in v11.  It was made so in v12, so I fixed this by
>> just s/attname COLLATE "C"/attname::text COLLATE "C"/ to the query
>> generated for v11 or older.
>
> +1

>> Also, I fixed another bug in the same function: a typo in the if-test
>> to check whether the remote server is v9.2 or later.

> +1.

Pushed/backpatched.

> This is a good argument for #define-ing the relevant server version numbers going forward.

Seams like a good idea.

Thanks for looking!

Best regards,
Etsuro Fujita






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-18 09:23                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-18 17:37                     ` Nathan Bossart <nathandbossart@gmail.com>
  2026-09-19 10:32                       ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Nathan Bossart @ 2026-09-18 17:37 UTC (permalink / raw)
  To: Etsuro Fujita <etsuro.fujita@gmail.com>; +Cc: Corey Huinker <corey.huinker@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Fri, Sep 18, 2026 at 06:23:04PM +0900, Etsuro Fujita wrote:
> Pushed/backpatched.

Can the open item for this one be marked as resolved?

-- 
nathan






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-18 09:23                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-18 17:37                     ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
@ 2026-09-19 10:32                       ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-22 11:26                         ` Re: Several issues with postgres_fdw stats import Pavel Luzanov <p.luzanov@postgrespro.ru>
  0 siblings, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-19 10:32 UTC (permalink / raw)
  To: Nathan Bossart <nathandbossart@gmail.com>; +Cc: Corey Huinker <corey.huinker@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Sat, Sep 19, 2026 at 2:37 AM Nathan Bossart <nathandbossart@gmail.com> wrote:
> Can the open item for this one be marked as resolved?

Sorry, I forgot to mention about that.  We have resolved two of the
four reported issues, but these are still open:

(1) User-defined functions may be executed with unexpected privileges
(4) Imported relpages may use different block sizes

For #1, I will address it with a patch shared by Noah off-list.  For
#4, I will propose a document-only patch to address it.  I will send
the proposals for these separately early next week.

Best regards,
Etsuro Fujita






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-18 09:23                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-18 17:37                     ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  2026-09-19 10:32                       ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-22 11:26                         ` Pavel Luzanov <p.luzanov@postgrespro.ru>
  2026-09-22 11:50                           ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Pavel Luzanov @ 2026-09-22 11:26 UTC (permalink / raw)
  To: Etsuro Fujita <etsuro.fujita@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; +Cc: Corey Huinker <corey.huinker@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi,

Commit 9a08585e8 renamed option "restore_stats" to "import_stats".
But I still see the "restore_stats" option in the v19 documentation. [1]

Is it intended only for v20?

1. 
https://www.postgresql.org/docs/19/postgres-fdw.html#POSTGRES-FDW-OPTIONS-COST-ESTIMATION 


-- 
Pavel Luzanov
Postgres Professional: https://postgrespro.com







^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-18 09:23                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-18 17:37                     ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  2026-09-19 10:32                       ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-22 11:26                         ` Re: Several issues with postgres_fdw stats import Pavel Luzanov <p.luzanov@postgrespro.ru>
@ 2026-09-22 11:50                           ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-23 15:11                             ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-22 11:50 UTC (permalink / raw)
  To: Pavel Luzanov <p.luzanov@postgrespro.ru>; +Cc: Nathan Bossart <nathandbossart@gmail.com>; Corey Huinker <corey.huinker@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Tue, Sep 22, 2026 at 8:26 PM Pavel Luzanov <p.luzanov@postgrespro.ru> wrote:
> Commit 9a08585e8 renamed option "restore_stats" to "import_stats".
> But I still see the "restore_stats" option in the v19 documentation. [1]
>
> Is it intended only for v20?

The change was made for beta4, released this week, but the
documentation is still for beta3.

Best regards,
Etsuro Fujita






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-18 09:23                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-18 17:37                     ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  2026-09-19 10:32                       ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-22 11:26                         ` Re: Several issues with postgres_fdw stats import Pavel Luzanov <p.luzanov@postgrespro.ru>
  2026-09-22 11:50                           ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-23 15:11                             ` Nathan Bossart <nathandbossart@gmail.com>
  2026-09-23 23:58                               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-28 11:18                               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 2 replies; 27+ messages in thread

From: Nathan Bossart @ 2026-09-23 15:11 UTC (permalink / raw)
  To: Etsuro Fujita <etsuro.fujita@gmail.com>; +Cc: Pavel Luzanov <p.luzanov@postgrespro.ru>; Corey Huinker <corey.huinker@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Tue, Sep 22, 2026 at 08:50:46PM +0900, Etsuro Fujita wrote:
> On Tue, Sep 22, 2026 at 8:26 PM Pavel Luzanov <p.luzanov@postgrespro.ru> wrote:
>> Commit 9a08585e8 renamed option "restore_stats" to "import_stats".
>> But I still see the "restore_stats" option in the v19 documentation. [1]
>>
>> Is it intended only for v20?
> 
> The change was made for beta4, released this week, but the
> documentation is still for beta3.

Can the open item for this one be marked resolved?

-- 
nathan






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-18 09:23                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-18 17:37                     ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  2026-09-19 10:32                       ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-22 11:26                         ` Re: Several issues with postgres_fdw stats import Pavel Luzanov <p.luzanov@postgrespro.ru>
  2026-09-22 11:50                           ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-23 15:11                             ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
@ 2026-09-23 23:58                               ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-25 21:25                                 ` Re: Several issues with postgres_fdw stats import Matheus Alcantara <matheusssilv97@gmail.com>
  1 sibling, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-23 23:58 UTC (permalink / raw)
  To: Nathan Bossart <nathandbossart@gmail.com>; +Cc: Pavel Luzanov <p.luzanov@postgrespro.ru>; Corey Huinker <corey.huinker@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Thu, Sep 24, 2026 at 12:11 AM Nathan Bossart
<nathandbossart@gmail.com> wrote:
> Can the open item for this one be marked resolved?

Yes, I think so; I will push two remaining patches by this weekend at
the latest.

Best regards,
Etsuro Fujita






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-18 09:23                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-18 17:37                     ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  2026-09-19 10:32                       ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-22 11:26                         ` Re: Several issues with postgres_fdw stats import Pavel Luzanov <p.luzanov@postgrespro.ru>
  2026-09-22 11:50                           ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-23 15:11                             ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  2026-09-23 23:58                               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-25 21:25                                 ` Matheus Alcantara <matheusssilv97@gmail.com>
  2026-09-26 14:59                                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Matheus Alcantara @ 2026-09-25 21:25 UTC (permalink / raw)
  To: Etsuro Fujita <etsuro.fujita@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; +Cc: Pavel Luzanov <p.luzanov@postgrespro.ru>; Corey Huinker <corey.huinker@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi all,

On 23/09/26 20:58, Etsuro Fujita wrote:
> On Thu, Sep 24, 2026 at 12:11 AM Nathan Bossart
> <nathandbossart@gmail.com> wrote:
>> Can the open item for this one be marked resolved?
> 
> Yes, I think so; I will push two remaining patches by this weekend at
> the latest.
> 

While testing the postgres_fdw statistics import feature, I also ran
into issue (1), "User-defined functions may be executed with unexpected
privileges".

I've tested Noah's patch that Etsuro shared, and it looks good to me. It
fixes both the domain-function case and the data disclosure case in all
the scenarios I tried.

Since the patch doesn't include regression tests, I'm attaching a
patch with two tests added:

- The first is based on Noah's example: a superuser runs ANALYZE on a
  foreign table owned by a non-superuser that has no user mapping. It
  now fails with "user mapping not found" instead of importing
  statistics the owner can't read.

- The second covers the case Fujii described: a domain CHECK constraint
  on a foreign table column calls a function that reports current_user
  and search_path. It now runs as the table owner with search_path set
  to "pg_catalog, pg_temp".

I also added a paragraph to the ImportForeignStatistics section on
fdwhandler.sgml saying that the callback runs as the foreign table's
owner in a security-restricted operation, so FDWs should use the owner's
user mapping. This is now part of the callback's contract, and it
differs from AnalyzeForeignTable, which is still called as the user
running ANALYZE, so I think it's worth documenting for FDW authors.

Noah's code changes are unchanged; the attached patch adds only the
tests, the docs and a commit message.

--
Matheus Alcantara
EDB: https://www.enterprisedb.com

Attachments:

0001-postgres_fdw-Import-statistics-as-the-foreign-table-.patch (text/plain, 11.4K) download From a5ee7e6e16336a016947130df4177c716e0bf1d6 Mon Sep 17 00:00:00 2001 From: Matheus Alcantara <mths.dev@pm.me> Date: Fri, 25 Sep 2026 17:59:44 -0300 Subject: [PATCH] postgres_fdw: Import statistics as the foreign table's owner. ANALYZE called the ImportForeignStatistics callback before switching to the table owner's userid, so the callback ran as the user executing ANALYZE, outside of a security-restricted operation. This had two consequences when postgres_fdw's import_stats option was enabled: - User-defined functions invoked while importing the statistics, such as a domain's CHECK constraint on a foreign table column, were executed with the privileges of the user running ANALYZE. If that user was a superuser, the owner of the foreign table could get code run with superuser privileges. - postgres_fdw accessed the remote server using the user mapping of the user running ANALYZE rather than that of the table's owner, as is done in the sampling case. The imported statistics, such as most common values, could then disclose remote data to the owner of the foreign table that the owner itself has no privilege to read. To fix, switch to the table owner's userid before calling ImportForeignStatistics, lock down security-restricted operations, and restrict search_path, as do_analyze_rel() already does for the sampling case. postgres_fdw's statistics import now uses the owner's user mapping, which makes the identity handling consistent between the sampling and import methods. Also document this in the FDW API docs, and add regression tests for both issues. Author: Noah Misch <noah@leadboat.com> Co-Authored-by: Matheus Alcantara <mths.dev@pm.me>> Reported-by: Fujii Masao <masao.fujii@gmail.com> Reported-by: Noah Misch <noah@leadboat.com> Reviewed-by: Etsuro Fujita <etsuro.fujita@gmail.com> Discussion: https://postgr.es/m/CAHGQGwH8+uDTwfohpck9v_9L3nwSWACbmC36JYRyqHd5xQN_OA@mail.gmail.com Backpatch-through: 19 --- .../postgres_fdw/expected/postgres_fdw.out | 49 +++++++++++++++++++ contrib/postgres_fdw/postgres_fdw.c | 9 ++-- contrib/postgres_fdw/sql/postgres_fdw.sql | 44 +++++++++++++++++ doc/src/sgml/fdwhandler.sgml | 13 +++++ src/backend/commands/analyze.c | 30 ++++++++++-- 5 files changed, 136 insertions(+), 9 deletions(-) diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index 739f43af7bb..fbbacbb7734 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -13816,6 +13816,50 @@ WHERE schemaname = 'public' AND tablename = 'dtest_ftable'; ---------+-----------+-----------+-----------+------------+-----+-------------------+----+------------- (0 rows) +-- Statistics import must access the remote server as the foreign table's +-- owner, not as the user running ANALYZE +CREATE ROLE regress_simport_owner NOSUPERUSER; +GRANT USAGE ON FOREIGN SERVER loopback TO regress_simport_owner; +GRANT CREATE ON SCHEMA public TO regress_simport_owner; +CREATE TABLE simport_secret (s text); +REVOKE ALL ON simport_secret FROM PUBLIC; +INSERT INTO simport_secret SELECT 'hunter2' FROM generate_series(1, 10); +ANALYZE simport_secret; +SET ROLE regress_simport_owner; +CREATE FOREIGN TABLE simport_fsecret (s text) + SERVER loopback OPTIONS (table_name 'simport_secret', import_stats 'true'); +RESET ROLE; +ANALYZE simport_fsecret; -- should fail, owner has no user mapping +ERROR: user mapping not found for user "regress_simport_owner", server "loopback" +SELECT most_common_vals FROM pg_stats WHERE tablename = 'simport_fsecret'; + most_common_vals +------------------ +(0 rows) + +-- Functions invoked during statistics import (here a domain's CHECK +-- constraint) must run as the foreign table's owner, with a restricted +-- search_path, as in the sampling case +CREATE USER MAPPING FOR regress_simport_owner SERVER loopback + OPTIONS (password_required 'false'); +CREATE TABLE simport_dtable (c1 int); +INSERT INTO simport_dtable VALUES (1), (1), (2), (2); +ANALYZE simport_dtable; +SET ROLE regress_simport_owner; +CREATE FUNCTION simport_check(int) RETURNS bool LANGUAGE plpgsql AS $$ +BEGIN + RAISE NOTICE 'current_user: %, search_path: %', + current_user, current_setting('search_path'); + RETURN true; +END $$; +CREATE DOMAIN simport_dom AS int CHECK (simport_check(VALUE)); +CREATE FOREIGN TABLE simport_fdtable (c1 simport_dom) + SERVER loopback OPTIONS (table_name 'simport_dtable', import_stats 'true'); +RESET ROLE; +ANALYZE VERBOSE simport_fdtable; -- should work +INFO: importing statistics for foreign table "public.simport_fdtable" +NOTICE: current_user: regress_simport_owner, search_path: pg_catalog, pg_temp +NOTICE: current_user: regress_simport_owner, search_path: pg_catalog, pg_temp +INFO: finished importing statistics for foreign table "public.simport_fdtable" -- cleanup DROP FOREIGN TABLE simport_ftable; DROP FOREIGN TABLE simport_fview; @@ -13825,6 +13869,11 @@ DROP FOREIGN TABLE simport_fpt; DROP TABLE simport_pt; DROP FOREIGN TABLE dtest_ftable; DROP TABLE dtest_table; +DROP TABLE simport_secret; +DROP TABLE simport_dtable; +DROP USER MAPPING FOR regress_simport_owner SERVER loopback; +DROP OWNED BY regress_simport_owner; +DROP ROLE regress_simport_owner; -- =================================================================== -- test for postgres_fdw_get_connections function with check_conn = true -- =================================================================== diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index fbe01fdde85..42a704344b8 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -5832,11 +5832,10 @@ fetch_remote_statistics(Relation relation, } /* - * Get connection to the foreign server. Connection manager will - * establish new connection if necessary. - * - * Note that unlike the sampling case, we only query pg_class and - * pg_stats, so we do the remote access as the current user. + * Get the connection to use. We do the remote access as the table's + * owner. Note that unlike AnalyzeForeignTable(), the core code would + * already have switched us to the table's owner, before we are called + * from ImportForeignStatistics(). */ user = GetUserMapping(GetUserId(), table->serverid); conn = GetConnection(user, false, NULL); diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index f1ca3204382..21558282c35 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -4956,6 +4956,45 @@ SELECT attname, inherited, null_frac, avg_width, n_distinct, FROM pg_stats WHERE schemaname = 'public' AND tablename = 'dtest_ftable'; +-- Statistics import must access the remote server as the foreign table's +-- owner, not as the user running ANALYZE +CREATE ROLE regress_simport_owner NOSUPERUSER; +GRANT USAGE ON FOREIGN SERVER loopback TO regress_simport_owner; +GRANT CREATE ON SCHEMA public TO regress_simport_owner; +CREATE TABLE simport_secret (s text); +REVOKE ALL ON simport_secret FROM PUBLIC; +INSERT INTO simport_secret SELECT 'hunter2' FROM generate_series(1, 10); +ANALYZE simport_secret; +SET ROLE regress_simport_owner; +CREATE FOREIGN TABLE simport_fsecret (s text) + SERVER loopback OPTIONS (table_name 'simport_secret', import_stats 'true'); +RESET ROLE; + +ANALYZE simport_fsecret; -- should fail, owner has no user mapping +SELECT most_common_vals FROM pg_stats WHERE tablename = 'simport_fsecret'; + +-- Functions invoked during statistics import (here a domain's CHECK +-- constraint) must run as the foreign table's owner, with a restricted +-- search_path, as in the sampling case +CREATE USER MAPPING FOR regress_simport_owner SERVER loopback + OPTIONS (password_required 'false'); +CREATE TABLE simport_dtable (c1 int); +INSERT INTO simport_dtable VALUES (1), (1), (2), (2); +ANALYZE simport_dtable; +SET ROLE regress_simport_owner; +CREATE FUNCTION simport_check(int) RETURNS bool LANGUAGE plpgsql AS $$ +BEGIN + RAISE NOTICE 'current_user: %, search_path: %', + current_user, current_setting('search_path'); + RETURN true; +END $$; +CREATE DOMAIN simport_dom AS int CHECK (simport_check(VALUE)); +CREATE FOREIGN TABLE simport_fdtable (c1 simport_dom) + SERVER loopback OPTIONS (table_name 'simport_dtable', import_stats 'true'); +RESET ROLE; + +ANALYZE VERBOSE simport_fdtable; -- should work + -- cleanup DROP FOREIGN TABLE simport_ftable; DROP FOREIGN TABLE simport_fview; @@ -4965,6 +5004,11 @@ DROP FOREIGN TABLE simport_fpt; DROP TABLE simport_pt; DROP FOREIGN TABLE dtest_ftable; DROP TABLE dtest_table; +DROP TABLE simport_secret; +DROP TABLE simport_dtable; +DROP USER MAPPING FOR regress_simport_owner SERVER loopback; +DROP OWNED BY regress_simport_owner; +DROP ROLE regress_simport_owner; -- =================================================================== -- test for postgres_fdw_get_connections function with check_conn = true diff --git a/doc/src/sgml/fdwhandler.sgml b/doc/src/sgml/fdwhandler.sgml index 502441fefcb..77cb4d2c143 100644 --- a/doc/src/sgml/fdwhandler.sgml +++ b/doc/src/sgml/fdwhandler.sgml @@ -1433,6 +1433,19 @@ ImportForeignStatistics(Relation relation, called on the foreign table to generate statistics locally, if supported. </para> + <para> + <function>ImportForeignStatistics</function> is called with the current + user switched to the owner of the foreign table, within a + security-restricted operation, and with + <xref linkend="guc-search-path"/> temporarily changed to + <literal>pg_catalog, pg_temp</literal>, as is done when statistics are + calculated locally. Thus, if the FDW needs to access a remote server, + it should use the user mapping for the current user (that is, the owner + of the foreign table), as returned by <function>GetUserId</function>. + Note that <function>AnalyzeForeignTable</function> is not called in + this environment. + </para> + <para> For reference, the logic for calculating statistics in <productname>PostgreSQL</productname> is found in diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 4fea106bafd..58370c0a2c5 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -228,10 +228,32 @@ analyze_rel(Oid relid, RangeVar *relation, fdwroutine = GetFdwRoutineForRelation(onerel, false); - if (fdwroutine->ImportForeignStatistics != NULL && - fdwroutine->ImportForeignStatistics(onerel, va_cols, elevel)) - stats_imported = true; - else + if (fdwroutine->ImportForeignStatistics != NULL) + { + Oid save_userid; + int save_sec_context; + int save_nestlevel; + + /* + * Switch to the table owner's userid, as in the sampling method. + * Also lock down security-restricted operations and arrange to + * make GUC variable changes local to this command. + */ + GetUserIdAndSecContext(&save_userid, &save_sec_context); + SetUserIdAndSecContext(onerel->rd_rel->relowner, + save_sec_context | SECURITY_RESTRICTED_OPERATION); + save_nestlevel = NewGUCNestLevel(); + RestrictSearchPath(); + + stats_imported = fdwroutine->ImportForeignStatistics(onerel, + va_cols, + elevel); + + AtEOXact_GUC(false, save_nestlevel); + SetUserIdAndSecContext(save_userid, save_sec_context); + } + + if (!stats_imported) { bool ok = false; -- 2.50.1 (Apple Git-155)
^ permalink raw reply [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-18 09:23                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-18 17:37                     ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  2026-09-19 10:32                       ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-22 11:26                         ` Re: Several issues with postgres_fdw stats import Pavel Luzanov <p.luzanov@postgrespro.ru>
  2026-09-22 11:50                           ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-23 15:11                             ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  2026-09-23 23:58                               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-25 21:25                                 ` Re: Several issues with postgres_fdw stats import Matheus Alcantara <matheusssilv97@gmail.com>
@ 2026-09-26 14:59                                   ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-26 15:10                                     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-26 14:59 UTC (permalink / raw)
  To: Matheus Alcantara <matheusssilv97@gmail.com>; +Cc: Nathan Bossart <nathandbossart@gmail.com>; Pavel Luzanov <p.luzanov@postgrespro.ru>; Corey Huinker <corey.huinker@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Sat, Sep 26, 2026 at 6:25 AM Matheus Alcantara
<matheusssilv97@gmail.com> wrote:
> While testing the postgres_fdw statistics import feature, I also ran
> into issue (1), "User-defined functions may be executed with unexpected
> privileges".
>
> I've tested Noah's patch that Etsuro shared, and it looks good to me. It
> fixes both the domain-function case and the data disclosure case in all
> the scenarios I tried.

Thanks for the testing!

> Since the patch doesn't include regression tests, I'm attaching a
> patch with two tests added:
>
> - The first is based on Noah's example: a superuser runs ANALYZE on a
>   foreign table owned by a non-superuser that has no user mapping. It
>   now fails with "user mapping not found" instead of importing
>   statistics the owner can't read.
>
> - The second covers the case Fujii described: a domain CHECK constraint
>   on a foreign table column calls a function that reports current_user
>   and search_path. It now runs as the table owner with search_path set
>   to "pg_catalog, pg_temp".

I think it's a good idea to extend these to test the sampling path as
well.  Note that we don't have any test cases to test that path's priv
handling.

> I also added a paragraph to the ImportForeignStatistics section on
> fdwhandler.sgml saying that the callback runs as the foreign table's
> owner in a security-restricted operation, so FDWs should use the owner's
> user mapping. This is now part of the callback's contract, and it
> differs from AnalyzeForeignTable, which is still called as the user
> running ANALYZE, so I think it's worth documenting for FDW authors.

I think the contract that the FDW should use the foreign table owner's
user mapping applies to the sampling path as well, as
AcquireSampleRowsFunc is run as the owner in that path.  Also note
that postgresAnalyzeForeignTable follows the contract by doing this:

    /*
     * Get the connection to use.  We do the remote access as the table's
     * owner, even if the ANALYZE was started by some other user.
     */
    table = GetForeignTable(RelationGetRelid(relation));
    user = GetUserMapping(relation->rd_rel->relowner, table->serverid);
    conn = GetConnection(user, false, NULL);

I'm not sure we really need to add a note about this, but if we do so,
I think it's better to add it as a common contract for both
sampling/import paths.

Also, I recommend making this as an improvement for both paths, not
just for the import path, maybe in v20.

> Noah's code changes are unchanged; the attached patch adds only the
> tests, the docs and a commit message.

I pushed Noah's patch.

Best regards,
Etsuro Fujita






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-18 09:23                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-18 17:37                     ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  2026-09-19 10:32                       ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-22 11:26                         ` Re: Several issues with postgres_fdw stats import Pavel Luzanov <p.luzanov@postgrespro.ru>
  2026-09-22 11:50                           ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-23 15:11                             ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  2026-09-23 23:58                               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-25 21:25                                 ` Re: Several issues with postgres_fdw stats import Matheus Alcantara <matheusssilv97@gmail.com>
  2026-09-26 14:59                                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-26 15:10                                     ` Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 0 replies; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-26 15:10 UTC (permalink / raw)
  To: Matheus Alcantara <matheusssilv97@gmail.com>; +Cc: Nathan Bossart <nathandbossart@gmail.com>; Pavel Luzanov <p.luzanov@postgrespro.ru>; Corey Huinker <corey.huinker@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Sat, Sep 26, 2026 at 11:59 PM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:
> On Sat, Sep 26, 2026 at 6:25 AM Matheus Alcantara
> <matheusssilv97@gmail.com> wrote:
> > Noah's code changes are unchanged; the attached patch adds only the
> > tests, the docs and a commit message.
>
> I pushed Noah's patch.

I forgot to say thank you for the patch.  Thank you!

Best regards,
Etsuro Fujita






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-11 17:11     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-11 20:53       ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-14 11:12         ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-15 08:52           ` Re: Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-15 11:31             ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 10:25               ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-16 17:58                 ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-18 09:23                   ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-18 17:37                     ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
  2026-09-19 10:32                       ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-22 11:26                         ` Re: Several issues with postgres_fdw stats import Pavel Luzanov <p.luzanov@postgrespro.ru>
  2026-09-22 11:50                           ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-23 15:11                             ` Re: Several issues with postgres_fdw stats import Nathan Bossart <nathandbossart@gmail.com>
@ 2026-09-28 11:18                               ` Etsuro Fujita <etsuro.fujita@gmail.com>
  1 sibling, 0 replies; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-28 11:18 UTC (permalink / raw)
  To: Nathan Bossart <nathandbossart@gmail.com>; +Cc: Pavel Luzanov <p.luzanov@postgrespro.ru>; Corey Huinker <corey.huinker@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Thu, Sep 24, 2026 at 12:11 AM Nathan Bossart
<nathandbossart@gmail.com> wrote:
> Can the open item for this one be marked resolved?

I think Matheus's additional proposal downthread is an improvement, so
I marked the open item as resolved.

Best regards,
Etsuro Fujita






^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
@ 2026-09-22 11:35     ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-26 11:54       ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2 siblings, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-22 11:35 UTC (permalink / raw)
  To: Corey Huinker <corey.huinker@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Noah Misch <noah@leadboat.com>

I added Noah in CC.

On Fri, Sep 11, 2026 at 12:46 AM Corey Huinker <corey.huinker@gmail.com> wrote:
> On Thu, Sep 10, 2026 at 11:02 AM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:
>> On Thu, Sep 10, 2026 at 3:59 PM Fujii Masao <masao.fujii@gmail.com> wrote:
>> > postgres_fdw stats import seems to have several potential issues.
>>
>> > (1) User-defined functions may be executed with unexpected privileges
>> >
>> > ANALYZE on a foreign table with import_stats disabled invokes
>> > user-defined functions (e.g., domain constraints) as the foreign table's
>> > owner. But, with import_stats enabled, they are invoked as the user
>> > running ANALYZE. So, if that user is a superuser, those functions would
>> > be run with superuser privileges. Could this be a security issue?
>>
>> Sorry, I don't follow this.  Could you elaborate on it using an example?
>
> Me either. I think he's referring to generated columns, in which case the generated column wouldn't have a corresponding source column, hence would fail match_attrmap() and already falls back to sampling.

Here is an example provided by Noah, which doesn't involve
user-defined functions, but causes a security issue:

CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw OPTIONS
(dbname 'postgres');
CREATE USER MAPPING FOR CURRENT_USER SERVER loopback;
DROP ROLE IF EXISTS regress_ftowner;
CREATE ROLE regress_ftowner NOSUPERUSER;
GRANT USAGE ON FOREIGN SERVER loopback TO regress_ftowner;
GRANT CREATE ON SCHEMA public TO regress_ftowner;
CREATE TABLE secret (s text);
REVOKE ALL ON secret FROM PUBLIC;
INSERT INTO secret SELECT 'hunter2' FROM generate_series(1, 10);
ANALYZE secret;
SET ROLE regress_ftowner;
CREATE FOREIGN TABLE ft_secret (s text) SERVER loopback
    OPTIONS (table_name 'secret', import_stats 'true');

-- the owner can neither read secret nor reach the remote server
SELECT * FROM ft_secret;
ERROR:  user mapping not found for user "regress_ftowner", server "loopback"

-- which is good, BUT import_stats discloses data the owner cannot read
RESET ROLE;
ANALYZE ft_secret;
SET ROLE regress_ftowner;
SELECT most_common_vals FROM pg_stats WHERE tablename = 'ft_secret';
 most_common_vals
------------------
 {hunter2}
(1 row)

He provided a fix for this as well, which I'm attaching.  The fix
addresses it by switching the userid to the foreign-table owner's
userid in analyze.c before calling ImportForeignStatistics().  I think
the fix is also reasonable in that it makes the identity handling
consistent between the sampling/import methods.  So I will push and
back-patch the fix if no objections from others.

Best regards,
Etsuro Fujita

Attachments:

fix-stats-import-priv-handling.patch (application/octet-stream, 2.2K) download diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index fbe01fdde85..42a704344b8 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -5832,11 +5832,10 @@ fetch_remote_statistics(Relation relation, } /* - * Get connection to the foreign server. Connection manager will - * establish new connection if necessary. - * - * Note that unlike the sampling case, we only query pg_class and - * pg_stats, so we do the remote access as the current user. + * Get the connection to use. We do the remote access as the table's + * owner. Note that unlike AnalyzeForeignTable(), the core code would + * already have switched us to the table's owner, before we are called + * from ImportForeignStatistics(). */ user = GetUserMapping(GetUserId(), table->serverid); conn = GetConnection(user, false, NULL); diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index c05f9f50e43..40b0e23ca2e 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -228,10 +228,32 @@ analyze_rel(Oid relid, RangeVar *relation, fdwroutine = GetFdwRoutineForRelation(onerel, false); - if (fdwroutine->ImportForeignStatistics != NULL && - fdwroutine->ImportForeignStatistics(onerel, va_cols, elevel)) - stats_imported = true; - else + if (fdwroutine->ImportForeignStatistics != NULL) + { + Oid save_userid; + int save_sec_context; + int save_nestlevel; + + /* + * Switch to the table owner's userid, as in the sampling method. + * Also lock down security-restricted operations and arrange to + * make GUC variable changes local to this command. + */ + GetUserIdAndSecContext(&save_userid, &save_sec_context); + SetUserIdAndSecContext(onerel->rd_rel->relowner, + save_sec_context | SECURITY_RESTRICTED_OPERATION); + save_nestlevel = NewGUCNestLevel(); + RestrictSearchPath(); + + stats_imported = fdwroutine->ImportForeignStatistics(onerel, + va_cols, + elevel); + + AtEOXact_GUC(false, save_nestlevel); + SetUserIdAndSecContext(save_userid, save_sec_context); + } + + if (!stats_imported) { bool ok = false;
^ permalink raw reply [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-22 11:35     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-26 11:54       ` Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 0 replies; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-26 11:54 UTC (permalink / raw)
  To: Corey Huinker <corey.huinker@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Noah Misch <noah@leadboat.com>

On Tue, Sep 22, 2026 at 8:35 PM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:
> He provided a fix for this as well, which I'm attaching.  The fix
> addresses it by switching the userid to the foreign-table owner's
> userid in analyze.c before calling ImportForeignStatistics().  I think
> the fix is also reasonable in that it makes the identity handling
> consistent between the sampling/import methods.  So I will push and
> back-patch the fix if no objections from others.

Done.  (I know Matheus updated the patch, but what he is proposing
would be rather an improvement, so I will work on it separately.)

Best regards,
Etsuro Fujita





^ permalink  raw  reply  [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
@ 2026-09-22 11:38     ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-26 11:55       ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2 siblings, 1 reply; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-22 11:38 UTC (permalink / raw)
  To: Corey Huinker <corey.huinker@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Fri, Sep 11, 2026 at 12:46 AM Corey Huinker <corey.huinker@gmail.com> wrote:
> On Thu, Sep 10, 2026 at 11:02 AM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:

>> From a cost calculation perspective, I think it's appropriate to use
>> the imported relpages as-is, as it's used to estimate the cost for
>> remote operations, not local ones, and thus we should actually instead
>> fix the normal-ANALYZE handling to calculate the size based on the
>> remote definition of block size.
>
> +1

I expanded a comment to mention this.  Attached is a patch for that.
I will push and back-patch the patch if no objections.

Best regards,
Etsuro Fujita

Attachments:

improve-stats-import-comments.patch (application/octet-stream, 710B) download diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index 42a704344b8..3bbc4493925 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -5954,7 +5954,10 @@ fetch_remote_statistics(Relation relation, /* * If the remote table is partitioned, import relpages = 0, to match the - * sampling case. + * sampling case. Otherwise, import the relpages value as-is even if the + * remote definition of block size was different from the local one. Note + * that this is fine because it's only used for costing remote operations + * on the foreign table. */ if (relkind == RELKIND_PARTITIONED_TABLE) remstats->relpages = 0;
^ permalink raw reply [nested|flat] 27+ messages in thread

* Re: Several issues with postgres_fdw stats import
  2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
  2026-09-10 15:01 ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
  2026-09-10 15:46   ` Re: Several issues with postgres_fdw stats import Corey Huinker <corey.huinker@gmail.com>
  2026-09-22 11:38     ` Re: Several issues with postgres_fdw stats import Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2026-09-26 11:55       ` Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 0 replies; 27+ messages in thread

From: Etsuro Fujita @ 2026-09-26 11:55 UTC (permalink / raw)
  To: Corey Huinker <corey.huinker@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Tue, Sep 22, 2026 at 8:38 PM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:
> I expanded a comment to mention this.  Attached is a patch for that.
> I will push and back-patch the patch if no objections.

Done.

Best regards,
Etsuro Fujita






^ permalink  raw  reply  [nested|flat] 27+ messages in thread


end of thread, other threads:[~2026-09-28 11:18 UTC | newest]

Thread overview: 27+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-10 06:58 Several issues with postgres_fdw stats import Fujii Masao <masao.fujii@gmail.com>
2026-09-10 10:24 ` Osama Abdul Qader <osamaabdulqader.cs@gmail.com>
2026-09-10 15:14   ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-10 15:01 ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-10 15:46   ` Corey Huinker <corey.huinker@gmail.com>
2026-09-11 17:11     ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-11 20:53       ` Corey Huinker <corey.huinker@gmail.com>
2026-09-14 11:12         ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-15 08:52           ` Fujii Masao <masao.fujii@gmail.com>
2026-09-15 11:31             ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-16 10:25               ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-16 17:58                 ` Corey Huinker <corey.huinker@gmail.com>
2026-09-18 09:23                   ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-18 17:37                     ` Nathan Bossart <nathandbossart@gmail.com>
2026-09-19 10:32                       ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-22 11:26                         ` Pavel Luzanov <p.luzanov@postgrespro.ru>
2026-09-22 11:50                           ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-23 15:11                             ` Nathan Bossart <nathandbossart@gmail.com>
2026-09-23 23:58                               ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-25 21:25                                 ` Matheus Alcantara <matheusssilv97@gmail.com>
2026-09-26 14:59                                   ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-26 15:10                                     ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-28 11:18                               ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-22 11:35     ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-26 11:54       ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-22 11:38     ` Etsuro Fujita <etsuro.fujita@gmail.com>
2026-09-26 11:55       ` Etsuro Fujita <etsuro.fujita@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox