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 <[email protected]>
Co-authored-by: Michael Paquier <[email protected]>
Discussion: https://postgr.es/m/[email protected]

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(-)

Reply via email to