summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2023-12-29 10:57:11 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2023-12-29 10:57:11 -0500
commit1e0841426e968f184277e7638227b16fb3cdca7a (patch)
tree0769210a129150ed8bffac015f90c6c600f778e2
parent296128a53646126fc33394ca859df5ab000c4798 (diff)
In pg_dump, don't dump a stats object unless dumping underlying table.
If the underlying table isn't being dumped, it's useless to dump an extended statistics object; it'll just cause errors at restore. We have always applied similar policies to, say, indexes. (When and if we get cross-table stats objects, it might be profitable to think a little harder about what to do with them. But for now there seems no point in considering a stats object as anything but an appendage of its table.) Rian McGuire and Tom Lane, per report from Rian McGuire. Back-patch to supported branches. Discussion: https://postgr.es/m/7075d3aa-3f05-44a5-b68f-47dc6a8a0550@buildkite.com
-rw-r--r--src/bin/pg_dump/pg_dump.c30
-rw-r--r--src/bin/pg_dump/pg_dump.h3
-rw-r--r--src/bin/pg_dump/t/002_pg_dump.pl6
3 files changed, 32 insertions, 7 deletions
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index b69bf8d9e54..cf34bfdba80 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -1934,6 +1934,26 @@ selectDumpablePublicationObject(DumpableObject *dobj, Archive *fout)
}
/*
+ * selectDumpableStatisticsObject: policy-setting subroutine
+ * Mark an extended statistics object as to be dumped or not
+ *
+ * We dump an extended statistics object if the schema it's in and the table
+ * it's for are being dumped. (This'll need more thought if statistics
+ * objects ever support cross-table stats.)
+ */
+static void
+selectDumpableStatisticsObject(StatsExtInfo *sobj, Archive *fout)
+{
+ if (checkExtensionMembership(&sobj->dobj, fout))
+ return; /* extension membership overrides all else */
+
+ sobj->dobj.dump = sobj->dobj.namespace->dobj.dump_contains;
+ if (sobj->stattable == NULL ||
+ !(sobj->stattable->dobj.dump & DUMP_COMPONENT_DEFINITION))
+ sobj->dobj.dump = DUMP_COMPONENT_NONE;
+}
+
+/*
* selectDumpableObject: policy-setting subroutine
* Mark a generic dumpable object as to be dumped or not
*
@@ -7096,6 +7116,7 @@ getExtendedStatistics(Archive *fout)
int i_stxname;
int i_stxnamespace;
int i_stxowner;
+ int i_stxrelid;
int i_stattarget;
int i;
@@ -7107,11 +7128,11 @@ getExtendedStatistics(Archive *fout)
if (fout->remoteVersion < 130000)
appendPQExpBuffer(query, "SELECT tableoid, oid, stxname, "
- "stxnamespace, stxowner, (-1) AS stxstattarget "
+ "stxnamespace, stxowner, stxrelid, (-1) AS stxstattarget "
"FROM pg_catalog.pg_statistic_ext");
else
appendPQExpBuffer(query, "SELECT tableoid, oid, stxname, "
- "stxnamespace, stxowner, stxstattarget "
+ "stxnamespace, stxowner, stxrelid, stxstattarget "
"FROM pg_catalog.pg_statistic_ext");
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
@@ -7123,6 +7144,7 @@ getExtendedStatistics(Archive *fout)
i_stxname = PQfnumber(res, "stxname");
i_stxnamespace = PQfnumber(res, "stxnamespace");
i_stxowner = PQfnumber(res, "stxowner");
+ i_stxrelid = PQfnumber(res, "stxrelid");
i_stattarget = PQfnumber(res, "stxstattarget");
statsextinfo = (StatsExtInfo *) pg_malloc(ntups * sizeof(StatsExtInfo));
@@ -7137,10 +7159,12 @@ getExtendedStatistics(Archive *fout)
statsextinfo[i].dobj.namespace =
findNamespace(atooid(PQgetvalue(res, i, i_stxnamespace)));
statsextinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_stxowner));
+ statsextinfo[i].stattable =
+ findTableByOid(atooid(PQgetvalue(res, i, i_stxrelid)));
statsextinfo[i].stattarget = atoi(PQgetvalue(res, i, i_stattarget));
/* Decide whether we want to dump it */
- selectDumpableObject(&(statsextinfo[i].dobj), fout);
+ selectDumpableStatisticsObject(&(statsextinfo[i]), fout);
}
PQclear(res);
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index 0cdcc010317..18c951735f3 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -418,7 +418,8 @@ typedef struct _indexAttachInfo
typedef struct _statsExtInfo
{
DumpableObject dobj;
- const char *rolname;
+ const char *rolname; /* owner */
+ TableInfo *stattable; /* link to table the stats are for */
int stattarget; /* statistics target */
} StatsExtInfo;
diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl
index 6006276b89b..d012b748ffc 100644
--- a/src/bin/pg_dump/t/002_pg_dump.pl
+++ b/src/bin/pg_dump/t/002_pg_dump.pl
@@ -3136,13 +3136,13 @@ my %tests = (
'CREATE STATISTICS extended_stats_no_options' => {
create_order => 97,
create_sql => 'CREATE STATISTICS dump_test.test_ext_stats_no_options
- ON col1, col2 FROM dump_test.test_fifth_table',
+ ON col1, col2 FROM dump_test.test_table',
regexp => qr/^
- \QCREATE STATISTICS dump_test.test_ext_stats_no_options ON col1, col2 FROM dump_test.test_fifth_table;\E
+ \QCREATE STATISTICS dump_test.test_ext_stats_no_options ON col1, col2 FROM dump_test.test_table;\E
/xms,
like =>
{ %full_runs, %dump_test_schema_runs, section_post_data => 1, },
- unlike => { exclude_dump_test_schema => 1, },
+ unlike => { exclude_dump_test_schema => 1, exclude_test_table => 1, },
},
'CREATE STATISTICS extended_stats_options' => {