Hi,

I noticed that pg_restore_relation|attribute_stats() can restore statistics
for foreign tables, but pg_dump --statistics-only doesn't include them.
Is there a reason why pg_dump skips statistics for foreign tables?
Are there any risks or concerns around including them?

Sorry if this has already been discussed. I tried searching but couldn't
find the discussion. If there is one, I'd appreciate it if you could point
me to it.

In case we decide it's reasonable to allow dumping statistics for foreign
tables, I've attached a patch that implements this behavior.

Regards,

--
Fujii Masao
NTT DATA Japan Corporation
From 27992ef9e7335473f673c30c24f8d0344857518c Mon Sep 17 00:00:00 2001
From: Fujii Masao <fu...@postgresql.org>
Date: Fri, 13 Jun 2025 15:35:44 +0900
Subject: [PATCH v1] pg_dump: Allow pg_dump to dump the statistics for foreign
 tables.

---
 doc/src/sgml/ref/pg_dump.sgml | 3 ++-
 src/bin/pg_dump/pg_dump.c     | 4 +++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml
index d7595a7e546..8ef624d961c 100644
--- a/doc/src/sgml/ref/pg_dump.sgml
+++ b/doc/src/sgml/ref/pg_dump.sgml
@@ -1359,7 +1359,8 @@ PostgreSQL documentation
       <listitem>
        <para>
         Dump only the statistics, not the schema (data definitions) or data.
-        Statistics for tables, materialized views, and indexes are dumped.
+        Statistics for tables, materialized views, foreign tables,
+        and indexes are dumped.
        </para>
       </listitem>
      </varlistentry>
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 37432e66efd..8fb2b17cc3a 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -6890,7 +6890,8 @@ getRelationStatistics(Archive *fout, DumpableObject *rel, 
int32 relpages,
                (relkind == RELKIND_PARTITIONED_TABLE) ||
                (relkind == RELKIND_INDEX) ||
                (relkind == RELKIND_PARTITIONED_INDEX) ||
-               (relkind == RELKIND_MATVIEW))
+               (relkind == RELKIND_MATVIEW ||
+                relkind == RELKIND_FOREIGN_TABLE))
        {
                RelStatsInfo *info = pg_malloc0(sizeof(RelStatsInfo));
                DumpableObject *dobj = &info->dobj;
@@ -6929,6 +6930,7 @@ getRelationStatistics(Archive *fout, DumpableObject *rel, 
int32 relpages,
                        case RELKIND_RELATION:
                        case RELKIND_PARTITIONED_TABLE:
                        case RELKIND_MATVIEW:
+                       case RELKIND_FOREIGN_TABLE:
                                info->section = SECTION_DATA;
                                break;
                        case RELKIND_INDEX:
-- 
2.49.0

Reply via email to