Re: Several issues with postgres_fdw stats import
Re: Several issues with postgres_fdw stats import
От:
Corey Huinker <corey.huinker@gmail.com>
Дата:
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
Re: Several issues with postgres_fdw stats import
От:
Etsuro Fujita <etsuro.fujita@gmail.com>
Дата:
On Sat, Sep 12, 2026 at 5:53 AM Corey Huinker 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
Re: Several issues with postgres_fdw stats import
On Tue, Sep 15, 2026 at 8:31 PM Etsuro Fujita wrote: > On Tue, Sep 15, 2026 at 5:52 PM Fujii Masao 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
Re: Several issues with postgres_fdw stats import
От:
Corey Huinker <corey.huinker@gmail.com>
Дата:
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.
Re: Several issues with postgres_fdw stats import
От:
Etsuro Fujita <etsuro.fujita@gmail.com>
Дата:
On Thu, Sep 17, 2026 at 2:58 AM Corey Huinker wrote: > On Wed, Sep 16, 2026 at 6:25 AM Etsuro Fujita 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
Re: Several issues with postgres_fdw stats import
От:
Nathan Bossart <nathandbossart@gmail.com>
Дата:
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
Re: Several issues with postgres_fdw stats import
От:
Etsuro Fujita <etsuro.fujita@gmail.com>
Дата:
On Sat, Sep 19, 2026 at 2:37 AM Nathan Bossart 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
Re: Several issues with postgres_fdw stats import
От:
Pavel Luzanov <p.luzanov@postgrespro.ru>
Дата:
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
Re: Several issues with postgres_fdw stats import
I added Noah in CC.
On Fri, Sep 11, 2026 at 12:46 AM Corey Huinker wrote:
> On Thu, Sep 10, 2026 at 11:02 AM Etsuro Fujita wrote:
>> On Thu, Sep 10, 2026 at 3:59 PM Fujii Masao 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
Re: Several issues with postgres_fdw stats import
On Fri, Sep 11, 2026 at 12:46 AM Corey Huinker wrote: > On Thu, Sep 10, 2026 at 11:02 AM Etsuro Fujita 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
Re: Several issues with postgres_fdw stats import
От:
Etsuro Fujita <etsuro.fujita@gmail.com>
Дата:
On Tue, Sep 22, 2026 at 8:26 PM Pavel Luzanov 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
Re: Several issues with postgres_fdw stats import
От:
Nathan Bossart <nathandbossart@gmail.com>
Дата:
On Tue, Sep 22, 2026 at 08:50:46PM +0900, Etsuro Fujita wrote: > On Tue, Sep 22, 2026 at 8:26 PM Pavel Luzanov 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
Re: Several issues with postgres_fdw stats import
От:
Etsuro Fujita <etsuro.fujita@gmail.com>
Дата:
On Thu, Sep 24, 2026 at 12:11 AM Nathan Bossart 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
Re: Several issues with postgres_fdw stats import
Hi all, On 23/09/26 20:58, Etsuro Fujita wrote: > On Thu, Sep 24, 2026 at 12:11 AM Nathan Bossart > 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
Re: Several issues with postgres_fdw stats import
От:
Etsuro Fujita <etsuro.fujita@gmail.com>
Дата:
On Tue, Sep 22, 2026 at 8:35 PM Etsuro Fujita 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
Re: Several issues with postgres_fdw stats import
От:
Etsuro Fujita <etsuro.fujita@gmail.com>
Дата:
On Tue, Sep 22, 2026 at 8:38 PM Etsuro Fujita 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
Re: Several issues with postgres_fdw stats import
От:
Etsuro Fujita <etsuro.fujita@gmail.com>
Дата:
On Sat, Sep 26, 2026 at 6:25 AM Matheus Alcantara
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
Re: Several issues with postgres_fdw stats import
От:
Etsuro Fujita <etsuro.fujita@gmail.com>
Дата:
On Sat, Sep 26, 2026 at 11:59 PM Etsuro Fujita wrote: > On Sat, Sep 26, 2026 at 6:25 AM Matheus Alcantara > 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
Re: Several issues with postgres_fdw stats import
От:
Etsuro Fujita <etsuro.fujita@gmail.com>
Дата:
On Thu, Sep 24, 2026 at 12:11 AM Nathan Bossart 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