[PG19] pg_dump --statistics restores extended statistics by attribute number

From: shihao zhong <zhong950419(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz>, tomas(at)vondra(dot)me
Subject: [PG19] pg_dump --statistics restores extended statistics by attribute number
Date: 2026-10-13 04:06:06
Message-ID: CAGRkXqQh9vifL1JP1je4ZEGmkKmQNG6JEd7djcdPYhCfKA_b1A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

This is Medium 5 from my list in [1].

Since c32fb29e979, pg_dump --statistics dumps extended statistics.
n_distinct and dependencies are written with the attribute numbers of
the source table. The restored table can number its columns in another
way.

create table d (junk int, a int, b int, c int);
alter table d drop column junk;
insert into d select g % 10, g % 10, g % 100
from generate_series(1, 2000) g;
create statistics d_s (ndistinct) on a, b, c from d;
analyze d;
explain select a, b from d group by a, b;

This estimates 10 rows. Restoring the dump into an empty database
prints

WARNING: could not validate "pg_ndistinct" object:
invalid attribute number 4 found

and the same explain estimates 100 rows there.

It is silent when the numbers are still valid but mean other columns.
That happens for an inheritance child whose columns are in another
order than the parent, because pg_dump recreates it with the order of
the parent. The statistics then land on other columns.

0001 adds two optional arguments to pg_restore_extended_stats, the
source column numbers and the column names of the statistics object,
and maps the numbers by name. 0002 makes pg_dump pass them. 0003 adds
tests. The MCV list is stored by column position, so 0001 skips it
with a warning when the order differs.

The function takes name and value pairs, so this needs no catalog
change, and dumps without the new arguments still load. I put the
mapping in the restore function because pg_dump would have to guess
the column numbers of the restored table. The patches apply to master
and REL_19_STABLE.

[1]
https://postgr.es/m/CAGRkXqSFdjqZP_kwRz7dv6LHX-CSrkDnynNXgntRsCEnQTKxmg@mail.gmail.com

Thanks,
Shihao

Attachment Content-Type Size
v1-0003-Test-pg_restore_extended_stats-with-shifted-attri.patch application/octet-stream 7.1 KB
v1-0002-pg_dump-pass-source-key-numbers-with-extended-sta.patch application/octet-stream 3.3 KB
v1-0001-Let-pg_restore_extended_stats-translate-attribute.patch application/octet-stream 9.2 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message shveta malik 2026-10-13 04:09:42 Re: Proposal: Conflict log history table for Logical Replication
Previous Message jian he 2026-10-13 04:02:57 Re: [PG19] Bugs found comparing 19 with 18, list before I open threads