pgsql: Fix pg_restore_extended_stats() with expressions

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix pg_restore_extended_stats() with expressions
Date: 2026-01-28 03:00:33
Message-ID: E1vkvnE-003544-2b@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix pg_restore_extended_stats() with expressions

This commit fixes an issue with the restore of ndistinct and
dependencies statistics, causing the operation to fail when any of these
kinds included expressions.

In extended statistics, expressions use strictly negative attribute
numbers, decremented from -1. For example, let's imagine an object
defined as follows:
CREATE STATISTICS stats_obj (dependencies) ON lower(name), upper(name)
FROM tab_obj;

This object would generate dependencies stats using -1 and -2 as
attribute numbers, like that:
[{"attributes": [-1], "dependency": -2, "degree": 1.000000},
{"attributes": [-2], "dependency": -1, "degree": 1.000000}]

However, pg_restore_extended_stats() forgot to account for the number of
expressions defined in an extended statistics object. This would cause
the validation step of ndistinct and dependencies data to fail,
preventing a restore of their stats even if the input is valid.

This issue has come up due to an incorrect split of the patch set. Some
tests are included to cover this behavior.

Author: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>
Co-authored-by: Michael Paquier <michael(at)paquier(dot)xyz>
Discussion: https://postgr.es/m/aXl4bMfSTQUxM_yy@paquier.xyz

Branch
------
master

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

Modified Files
--------------
src/backend/statistics/extended_stats_funcs.c | 39 ++++++++++++-
src/test/regress/expected/stats_import.out | 83 +++++++++++++++++++++++++++
src/test/regress/sql/stats_import.sql | 58 +++++++++++++++++++
3 files changed, 179 insertions(+), 1 deletion(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Amit Kapila 2026-01-28 04:33:51 Re: pgsql: Prevent invalidation of newly synced replication slots.
Previous Message Michael Paquier 2026-01-27 23:38:54 pgsql: Add output test for pg_dependencies statistics import