pgsql: Reject incorrect range_bounds_histograms in stats import functio

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Reject incorrect range_bounds_histograms in stats import functio
Date: 2026-07-09 05:40:27
Message-ID: E1whhUo-000FvI-21@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Reject incorrect range_bounds_histograms in stats import functions

pg_restore_attribute_stats() and pg_restore_extended_stats()
(expressions) can handle a STATISTIC_KIND_BOUNDS_HISTOGRAM value, but
did not check its shape when importing, especially regarding:
- Empty ranges.
- Unsorted elements.

These properties are respected by ANALYZE in compute_range_stats(), when
computing a histogram for a [multi]range, and by the planner when
reading the data from the stats catalogs. The effects of importing data
with these properties depend on the compilation options:
- A assertion would be hit, if enabled.
- A non-sensical value that would make the load of the stats fail.
- A failure is hit if an empty range is loaded.

While the owner of the table or the one with MAINTAIN rights is
responsible for the stats data inserted, buggy data makes little sense
if their load is going to fail. This commit adds a validation step to
match what compute_range_stats() expects.

This issue would be unlikely hit in practice, so no backpatch is done.

Author: Ewan Young <kdbase(dot)hack(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAON2xHNM809WLR_g0ymKgU-tWxtbyH1Xvh4fqzRqy9YP2A59pg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/df293aed46e3133df3b5b337f095e3ebed69fd79

Modified Files
--------------
src/backend/statistics/attribute_stats.c | 3 +-
src/backend/statistics/extended_stats_funcs.c | 2 +-
src/backend/statistics/stat_utils.c | 77 +++++++++++++++++++++++++++
src/include/statistics/stat_utils.h | 2 +
src/test/regress/expected/stats_import.out | 56 +++++++++++++++++++
src/test/regress/sql/stats_import.sql | 34 ++++++++++++
6 files changed, 172 insertions(+), 2 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2026-07-09 08:00:58 pgsql: Fix outdated comment
Previous Message Nathan Bossart 2026-07-09 03:10:55 Re: pgsql: Refactor how some aux processes advertise their ProcNumber