From 7da24355a2993721bf9bc54224b3bb3e601530ce Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 1 Sep 2026 16:27:18 -0500 Subject: [PATCH] pg_dump: Fix performance regression in attribute statistics query. After adding pg_stats.tableid, we taught pg_dump to use it in the attribute statistics query for servers running v19 or later. Notably, we did not give this new form of the query a "redundant filter clause" like we did the pre-v19 form. Testing indicates that produces a significant performance regression, and that adding a similar filter clause to the v19-or-later form of the query fixes it. Oversight in commit 4b5ba0c4ca. Reviewed-by: Sami Imseih Reviewed-by: Michael Paquier Discussion: https://postgr.es/m/apQ9nvLW_zFdarYd%40nathan Backpatch-through: 19 --- src/bin/pg_dump/pg_dump.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 358ac1d390e..9cba2aae380 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -11342,12 +11342,9 @@ dumpRelationStats_dumper(Archive *fout, const void *userArg, const TocEntry *te) /* * The results must be in the order of the relations supplied in the * parameters to ensure we remain in sync as we walk through the TOC. - * - * For v9.4 through v18, the redundant filter clause on s.tablename = - * ANY(...) seems sufficient to convince the planner to use - * pg_class_relname_nsp_index, which avoids a full scan of pg_stats. - * In newer versions, pg_stats returns the table OIDs, eliminating the - * need for that hack. + * The redundant filter clause seems sufficient to convince the + * planner to use pg_class_relname_nsp_index, which avoids a full scan + * of pg_stats. This may not work for all versions. * * Our query for retrieving statistics for multiple relations uses * WITH ORDINALITY and multi-argument UNNEST(), both of which were @@ -11359,6 +11356,7 @@ dumpRelationStats_dumper(Archive *fout, const void *userArg, const TocEntry *te) "FROM pg_catalog.pg_stats s " "JOIN unnest($1) WITH ORDINALITY AS u (tableid, ord) " "ON s.tableid = u.tableid " + "WHERE s.tableid = ANY($1) " "ORDER BY u.ord, s.attname, s.inherited"); else if (fout->remoteVersion >= 90400) appendPQExpBufferStr(query,