Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7418767

Browse files
committed
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
1 parent 541e8f1 commit 7418767

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/bin/pg_dump/pg_dump.c

+27-3
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,26 @@ selectDumpablePublicationObject(DumpableObject *dobj, Archive *fout)
20462046
DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
20472047
}
20482048

2049+
/*
2050+
* selectDumpableStatisticsObject: policy-setting subroutine
2051+
* Mark an extended statistics object as to be dumped or not
2052+
*
2053+
* We dump an extended statistics object if the schema it's in and the table
2054+
* it's for are being dumped. (This'll need more thought if statistics
2055+
* objects ever support cross-table stats.)
2056+
*/
2057+
static void
2058+
selectDumpableStatisticsObject(StatsExtInfo *sobj, Archive *fout)
2059+
{
2060+
if (checkExtensionMembership(&sobj->dobj, fout))
2061+
return; /* extension membership overrides all else */
2062+
2063+
sobj->dobj.dump = sobj->dobj.namespace->dobj.dump_contains;
2064+
if (sobj->stattable == NULL ||
2065+
!(sobj->stattable->dobj.dump & DUMP_COMPONENT_DEFINITION))
2066+
sobj->dobj.dump = DUMP_COMPONENT_NONE;
2067+
}
2068+
20492069
/*
20502070
* selectDumpableObject: policy-setting subroutine
20512071
* Mark a generic dumpable object as to be dumped or not
@@ -7291,6 +7311,7 @@ getExtendedStatistics(Archive *fout)
72917311
int i_stxname;
72927312
int i_stxnamespace;
72937313
int i_stxowner;
7314+
int i_stxrelid;
72947315
int i_stattarget;
72957316
int i;
72967317

@@ -7302,11 +7323,11 @@ getExtendedStatistics(Archive *fout)
73027323

73037324
if (fout->remoteVersion < 130000)
73047325
appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, "
7305-
"stxnamespace, stxowner, (-1) AS stxstattarget "
7326+
"stxnamespace, stxowner, stxrelid, (-1) AS stxstattarget "
73067327
"FROM pg_catalog.pg_statistic_ext");
73077328
else
73087329
appendPQExpBufferStr(query, "SELECT tableoid, oid, stxname, "
7309-
"stxnamespace, stxowner, stxstattarget "
7330+
"stxnamespace, stxowner, stxrelid, stxstattarget "
73107331
"FROM pg_catalog.pg_statistic_ext");
73117332

73127333
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
@@ -7318,6 +7339,7 @@ getExtendedStatistics(Archive *fout)
73187339
i_stxname = PQfnumber(res, "stxname");
73197340
i_stxnamespace = PQfnumber(res, "stxnamespace");
73207341
i_stxowner = PQfnumber(res, "stxowner");
7342+
i_stxrelid = PQfnumber(res, "stxrelid");
73217343
i_stattarget = PQfnumber(res, "stxstattarget");
73227344

73237345
statsextinfo = (StatsExtInfo *) pg_malloc(ntups * sizeof(StatsExtInfo));
@@ -7332,10 +7354,12 @@ getExtendedStatistics(Archive *fout)
73327354
statsextinfo[i].dobj.namespace =
73337355
findNamespace(atooid(PQgetvalue(res, i, i_stxnamespace)));
73347356
statsextinfo[i].rolname = getRoleName(PQgetvalue(res, i, i_stxowner));
7357+
statsextinfo[i].stattable =
7358+
findTableByOid(atooid(PQgetvalue(res, i, i_stxrelid)));
73357359
statsextinfo[i].stattarget = atoi(PQgetvalue(res, i, i_stattarget));
73367360

73377361
/* Decide whether we want to dump it */
7338-
selectDumpableObject(&(statsextinfo[i].dobj), fout);
7362+
selectDumpableStatisticsObject(&(statsextinfo[i]), fout);
73397363
}
73407364

73417365
PQclear(res);

src/bin/pg_dump/pg_dump.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ typedef struct _indexAttachInfo
423423
typedef struct _statsExtInfo
424424
{
425425
DumpableObject dobj;
426-
const char *rolname;
426+
const char *rolname; /* owner */
427+
TableInfo *stattable; /* link to table the stats are for */
427428
int stattarget; /* statistics target */
428429
} StatsExtInfo;
429430

src/bin/pg_dump/t/002_pg_dump.pl

+3-2
Original file line numberDiff line numberDiff line change
@@ -3742,14 +3742,15 @@
37423742
'CREATE STATISTICS extended_stats_no_options' => {
37433743
create_order => 97,
37443744
create_sql => 'CREATE STATISTICS dump_test.test_ext_stats_no_options
3745-
ON col1, col2 FROM dump_test.test_fifth_table',
3745+
ON col1, col2 FROM dump_test.test_table',
37463746
regexp => qr/^
3747-
\QCREATE STATISTICS dump_test.test_ext_stats_no_options ON col1, col2 FROM dump_test.test_fifth_table;\E
3747+
\QCREATE STATISTICS dump_test.test_ext_stats_no_options ON col1, col2 FROM dump_test.test_table;\E
37483748
/xms,
37493749
like =>
37503750
{ %full_runs, %dump_test_schema_runs, section_post_data => 1, },
37513751
unlike => {
37523752
exclude_dump_test_schema => 1,
3753+
exclude_test_table => 1,
37533754
only_dump_measurement => 1,
37543755
},
37553756
},

0 commit comments

Comments
 (0)