Skip to content

Commit f0d0083

Browse files
pg_dump: Fix query for gathering attribute stats on older versions.
Commit 9c02e3a taught pg_dump to retrieve attribute statistics for 64 relations at a time. pg_dump supports dumping from v9.2 and newer versions, but our query for retrieving statistics for multiple relations uses WITH ORDINALITY and multi-argument UNNEST(), both of which were introduced in v9.4. To fix, we resort to gathering statistics for a single relation at a time on versions older than v9.4. Per buildfarm member crake. Author: Corey Huinker <corey.huinker@gmail.com> Discussion: https://postgr.es/m/Z_BcWVMvlUIJ_iuZ%40nathan
1 parent 43b8e6c commit f0d0083

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10571,6 +10571,16 @@ fetchAttributeStats(Archive *fout)
1057110571
PGresult *res = NULL;
1057210572
static TocEntry *te;
1057310573
static bool restarted;
10574+
int max_rels = MAX_ATTR_STATS_RELS;
10575+
10576+
/*
10577+
* Our query for retrieving statistics for multiple relations uses WITH
10578+
* ORDINALITY and multi-argument UNNEST(), both of which were introduced
10579+
* in v9.4. For older versions, we resort to gathering statistics for a
10580+
* single relation at a time.
10581+
*/
10582+
if (fout->remoteVersion < 90400)
10583+
max_rels = 1;
1057410584

1057510585
/* If we're just starting, set our TOC pointer. */
1057610586
if (!te)
@@ -10596,7 +10606,7 @@ fetchAttributeStats(Archive *fout)
1059610606
* This is perhaps not the sturdiest assumption, so we verify it matches
1059710607
* reality in dumpRelationStats_dumper().
1059810608
*/
10599-
for (; te != AH->toc && count < MAX_ATTR_STATS_RELS; te = te->next)
10609+
for (; te != AH->toc && count < max_rels; te = te->next)
1060010610
{
1060110611
if ((te->reqs & REQ_STATS) != 0 &&
1060210612
strcmp(te->desc, "STATISTICS DATA") == 0)
@@ -10709,14 +10719,26 @@ dumpRelationStats_dumper(Archive *fout, const void *userArg, const TocEntry *te)
1070910719
* sufficient to convince the planner to use
1071010720
* pg_class_relname_nsp_index, which avoids a full scan of pg_stats.
1071110721
* This may not work for all versions.
10722+
*
10723+
* Our query for retrieving statistics for multiple relations uses
10724+
* WITH ORDINALITY and multi-argument UNNEST(), both of which were
10725+
* introduced in v9.4. For older versions, we resort to gathering
10726+
* statistics for a single relation at a time.
1071210727
*/
10713-
appendPQExpBufferStr(query,
10714-
"FROM pg_catalog.pg_stats s "
10715-
"JOIN unnest($1, $2) WITH ORDINALITY AS u (schemaname, tablename, ord) "
10716-
"ON s.schemaname = u.schemaname "
10717-
"AND s.tablename = u.tablename "
10718-
"WHERE s.tablename = ANY($2) "
10719-
"ORDER BY u.ord, s.attname, s.inherited");
10728+
if (fout->remoteVersion >= 90400)
10729+
appendPQExpBufferStr(query,
10730+
"FROM pg_catalog.pg_stats s "
10731+
"JOIN unnest($1, $2) WITH ORDINALITY AS u (schemaname, tablename, ord) "
10732+
"ON s.schemaname = u.schemaname "
10733+
"AND s.tablename = u.tablename "
10734+
"WHERE s.tablename = ANY($2) "
10735+
"ORDER BY u.ord, s.attname, s.inherited");
10736+
else
10737+
appendPQExpBufferStr(query,
10738+
"FROM pg_catalog.pg_stats s "
10739+
"WHERE s.schemaname = $1[1] "
10740+
"AND s.tablename = $2[1] "
10741+
"ORDER BY s.attname, s.inherited");
1072010742

1072110743
ExecuteSqlStatement(fout, query->data);
1072210744

0 commit comments

Comments
 (0)