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
