[PG19] pg_dump marks extended statistics of pre-v15 partitioned tables as non-inherited
Hello hackers,
Since c32fb29e979, pg_dump --statistics dumps extended statistics
data. For servers older than v15, where pg_stats_ext has no inherited
column, the query hard-codes the flag to false. The commit message
says "In v14 and older versions, inherited extended statistics are not
supported". That is true for the catalog, but not for the data: since
20b9fa308eb, back-patched down to 10.20, 11.15, 12.10, 13.6 and 14.2,
these versions build inherited extended stats on partitioned tables.
The planner always searches for inherited extended statistics on
partitioned tables, so after restore, data dumped as non-inherited is
never used. pg_upgrade from v10-v14 is affected the same way, as it
transfers statistics by default.
Reproduction on 14.24:
CREATE TABLE p (k int, a int, b int) PARTITION BY RANGE (k);
CREATE TABLE p1 PARTITION OF p FOR VALUES FROM (0) TO (50000);
CREATE TABLE p2 PARTITION OF p FOR VALUES FROM (50000) TO (100000);
INSERT INTO p SELECT i, i % 100, i % 100 FROM generate_series(0, 99999) i;
CREATE STATISTICS p_ab (ndistinct) ON a, b FROM p;
ANALYZE p;
On 14 the GROUP BY estimate is 100 rows:
SET max_parallel_workers_per_gather = 0; -- keep plan shapes the same
EXPLAIN SELECT a, b FROM p GROUP BY a, b;
QUERY PLAN
------------------------------------------------------------------------
HashAggregate (cost=2542.00..2543.00 rows=100 width=8)
Group Key: p.a, p.b
-> Append (cost=0.00..2042.00 rows=100000 width=8)
-> Seq Scan on p1 p_1 (cost=0.00..771.00 rows=50000 width=8)
-> Seq Scan on p2 p_2 (cost=0.00..771.00 rows=50000 width=8)
After pg_dump --statistics and restoring into REL_19_STABLE the
planner falls back to the product of per-column n_distinct:
SET max_parallel_workers_per_gather = 0;
EXPLAIN SELECT a, b FROM p GROUP BY a, b;
QUERY PLAN
------------------------------------------------------------------------
HashAggregate (cost=2701.48..2801.48 rows=10000 width=8)
Group Key: p.a, p.b
-> Append (cost=0.00..2170.11 rows=106274 width=8)
-> Seq Scan on p1 p_1 (cost=0.00..819.37 rows=53137 width=8)
-> Seq Scan on p2 p_2 (cost=0.00..819.37 rows=53137 width=8)
SELECT statistics_name, inherited FROM pg_stats_ext;
statistics_name | inherited
-----------------+-----------
p_ab | f
The attached patch deduces the inherited flag from the relkind of the
table for pre-v15 servers, the same way 20b9fa308eb chooses which data
to build: true for partitioned tables, false for all other relations.
pg_dump already has the relkind in TableInfo, so I did it in C rather
than in the SQL query. The patch applies to master (5ffb8c74093) and
REL_19_STABLE (2dc899f780f).
--
Artem Gavrilov
Senior Software Engineer, Percona
artem.gavrilov@percona.com
percona.com
On Thu, Oct 22, 2026 at 11:12 AM Artem Gavrilov <artem.gavrilov@percona.com>
wrote:
Hello hackers,
Since c32fb29e979, pg_dump --statistics dumps extended statistics
data. For servers older than v15, where pg_stats_ext has no inherited
column, the query hard-codes the flag to false. The commit message
says "In v14 and older versions, inherited extended statistics are not
supported". That is true for the catalog, but not for the data: since
20b9fa308eb, back-patched down to 10.20, 11.15, 12.10, 13.6 and 14.2,
these versions build inherited extended stats on partitioned tables.
That's an odd one, indeed.
The attached patch deduces the inherited flag from the relkind of the
table for pre-v15 servers, the same way 20b9fa308eb chooses which data
to build: true for partitioned tables, false for all other relations.
pg_dump already has the relkind in TableInfo, so I did it in C rather
than in the SQL query. The patch applies to master (5ffb8c74093) and
REL_19_STABLE (2dc899f780f).
My inclination was also to handle it in SQL but there isn't an elegant way
to do that, so the C route is definitely better.
I've also confirmed that it applies to master (d073ddd5180) and
REL_19_STABLE. It passes tests, but it isn't really exercised by the
regular test suite, so we'll have to rely on the buildfarm and even then
I'm not sure there will be coverage.
On Fri, Nov 06, 2026 at 05:11:07PM -0500, Corey Huinker wrote:
On Thu, Oct 22, 2026 at 11:12 AM Artem Gavrilov <artem.gavrilov@percona.com> wrote:
Since c32fb29e979, pg_dump --statistics dumps extended statistics
data. For servers older than v15, where pg_stats_ext has no inherited
column, the query hard-codes the flag to false. The commit message
says "In v14 and older versions, inherited extended statistics are not
supported". That is true for the catalog, but not for the data: since
20b9fa308eb, back-patched down to 10.20, 11.15, 12.10, 13.6 and 14.2,
these versions build inherited extended stats on partitioned tables.That's an odd one, indeed.
And clearly a mistake. Good catch.
My inclination was also to handle it in SQL but there isn't an elegant way
to do that, so the C route is definitely better.
The patch seems fine per-se. It's sad that it did not make it for
RC2, but let's get it done for the dot-0 release.
It is also worth mentioning 269b532aef55 (introduction of stxdinherit
in pg_statistic_ext_data, affecting v15~), that has removed the
special path that 20b9fa308ebf (affecting ~v14) has added in
do_analyze_rel() for partitioned tables.
--
Michael
Hi Corey, Michael,
Thanks for the review!
It passes tests, but it isn't really exercised by the regular test suite, so we'll have to rely on the buildfarm and even then I'm not sure there will be coverage.
I was unsure how to automate testing for this scenario. I checked
similar patches and didn't find any good examples.
It is also worth mentioning 269b532aef55 (introduction of stxdinherit
in pg_statistic_ext_data, affecting v15~), that has removed the
special path that 20b9fa308ebf (affecting ~v14) has added in
do_analyze_rel() for partitioned tables.
I added a mention of this to the commit message and also rebased onto
REL_19_STABLE tip (dffcca2e3c8) and verified for master (952bab8b74d).
v2 patch is attached.
--
Artem Gavrilov
Senior Software Engineer, Percona
artem.gavrilov@percona.com
percona.com
On Tue, Nov 10, 2026 at 04:01:13PM +0200, Artem Gavrilov wrote:
I added a mention of this to the commit message and also rebased onto
REL_19_STABLE tip (dffcca2e3c8) and verified for master (952bab8b74d).
v2 patch is attached.
Thanks for that. Adjusted a bit the whole, tested the thing with some
v14 and v15 instances, checked that it was correct, and finally
applied down to v19 as of 35def3385998.
--
Michael