diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2025-02-26 16:36:11 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2025-02-26 16:36:20 -0500 |
commit | 40e27d04b4f643cfb78af8db42a1f2e700ec9876 (patch) | |
tree | 80c8df6b40ca3eaec64fcebfff9eb4c3cd2fd824 /src/test | |
parent | f734c9fc3a91959c2473a1e33fd9b60116902175 (diff) | |
download | postgresql-40e27d04b4f643cfb78af8db42a1f2e700ec9876.tar.gz postgresql-40e27d04b4f643cfb78af8db42a1f2e700ec9876.zip |
Use attnum to identify index columns in pg_restore_attribute_stats().
Previously we used attname for both table and index columns, but
that is problematic for indexes because their attnames are assigned
by internal rules that don't guarantee to preserve the names across
dump and reload. (This is what's causing the remaining buildfarm
failures in cross-version-upgrade tests.) Fortunately we can use
attnum instead, since there's no such thing as adding or dropping
columns in an existing index. We met this same problem previously
with ALTER INDEX ... SET STATISTICS, and solved it the same way,
cf commit 5b6d13eec.
In pg_restore_attribute_stats() itself, we accept either attnum or
attname, but the policy used by pg_dump is to always use attname
for tables and attnum for indexes.
Author: Tom Lane <tgl@sss.pgh.pa.us>
Author: Corey Huinker <corey.huinker@gmail.com>
Discussion: https://postgr.es/m/1457469.1740419458@sss.pgh.pa.us
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/perl/PostgreSQL/Test/AdjustUpgrade.pm | 4 | ||||
-rw-r--r-- | src/test/regress/expected/stats_import.out | 40 | ||||
-rw-r--r-- | src/test/regress/sql/stats_import.sql | 30 |
3 files changed, 69 insertions, 5 deletions
diff --git a/src/test/perl/PostgreSQL/Test/AdjustUpgrade.pm b/src/test/perl/PostgreSQL/Test/AdjustUpgrade.pm index 0a707c69c5c..ec874852d12 100644 --- a/src/test/perl/PostgreSQL/Test/AdjustUpgrade.pm +++ b/src/test/perl/PostgreSQL/Test/AdjustUpgrade.pm @@ -345,7 +345,7 @@ sub adjust_old_dumpfile { $dump =~ s/ (^SELECT\s\*\sFROM\spg_catalog\.pg_restore_relation_stats\( - \s+'relation',\s'public\.hash_[a-z0-9]*_heap'::regclass, + [^;]*'relation',\s'public\.hash_[a-z0-9]*_heap'::regclass, [^;]*'relallvisible',)\s'\d+'::integer /$1 ''::integer/mgx; } @@ -692,7 +692,7 @@ sub adjust_new_dumpfile { $dump =~ s/ (^SELECT\s\*\sFROM\spg_catalog\.pg_restore_relation_stats\( - \s+'relation',\s'public\.hash_[a-z0-9]*_heap'::regclass, + [^;]*'relation',\s'public\.hash_[a-z0-9]*_heap'::regclass, [^;]*'relallvisible',)\s'\d+'::integer /$1 ''::integer/mgx; } diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out index 7e8b7f429c9..1f150f7b08d 100644 --- a/src/test/regress/expected/stats_import.out +++ b/src/test/regress/expected/stats_import.out @@ -278,6 +278,31 @@ SELECT pg_catalog.pg_restore_attribute_stats( 'attname', 'id'::name, 'inherited', false::boolean, 'version', 150000::integer, + 'null_frac', 0.2::real, + 'avg_width', 5::integer, + 'n_distinct', 0.6::real); + pg_restore_attribute_stats +---------------------------- + t +(1 row) + +SELECT * +FROM pg_stats +WHERE schemaname = 'stats_import' +AND tablename = 'test' +AND inherited = false +AND attname = 'id'; + schemaname | tablename | attname | inherited | null_frac | avg_width | n_distinct | most_common_vals | most_common_freqs | histogram_bounds | correlation | most_common_elems | most_common_elem_freqs | elem_count_histogram | range_length_histogram | range_empty_frac | range_bounds_histogram +--------------+-----------+---------+-----------+-----------+-----------+------------+------------------+-------------------+------------------+-------------+-------------------+------------------------+----------------------+------------------------+------------------+------------------------ + stats_import | test | id | f | 0.2 | 5 | 0.6 | | | | | | | | | | +(1 row) + +-- ok: restore by attnum +SELECT pg_catalog.pg_restore_attribute_stats( + 'relation', 'stats_import.test'::regclass, + 'attnum', 1::smallint, + 'inherited', false::boolean, + 'version', 150000::integer, 'null_frac', 0.4::real, 'avg_width', 5::integer, 'n_distinct', 0.6::real); @@ -1241,7 +1266,7 @@ SELECT pg_catalog.pg_restore_attribute_stats( 'avg_width', 2::integer, 'n_distinct', 0.3::real); ERROR: "relation" cannot be NULL --- error: attname null +-- error: missing attname SELECT pg_catalog.pg_restore_attribute_stats( 'relation', 'stats_import.test'::regclass, 'attname', NULL::name, @@ -1250,7 +1275,18 @@ SELECT pg_catalog.pg_restore_attribute_stats( 'null_frac', 0.1::real, 'avg_width', 2::integer, 'n_distinct', 0.3::real); -ERROR: "attname" cannot be NULL +ERROR: must specify either attname or attnum +-- error: both attname and attnum +SELECT pg_catalog.pg_restore_attribute_stats( + 'relation', 'stats_import.test'::regclass, + 'attname', 'id'::name, + 'attnum', 1::smallint, + 'inherited', false::boolean, + 'version', 150000::integer, + 'null_frac', 0.1::real, + 'avg_width', 2::integer, + 'n_distinct', 0.3::real); +ERROR: cannot specify both attname and attnum -- error: attname doesn't exist SELECT pg_catalog.pg_restore_attribute_stats( 'relation', 'stats_import.test'::regclass, diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql index 57422750b90..8c183bceb8a 100644 --- a/src/test/regress/sql/stats_import.sql +++ b/src/test/regress/sql/stats_import.sql @@ -184,6 +184,23 @@ SELECT pg_catalog.pg_restore_attribute_stats( 'attname', 'id'::name, 'inherited', false::boolean, 'version', 150000::integer, + 'null_frac', 0.2::real, + 'avg_width', 5::integer, + 'n_distinct', 0.6::real); + +SELECT * +FROM pg_stats +WHERE schemaname = 'stats_import' +AND tablename = 'test' +AND inherited = false +AND attname = 'id'; + +-- ok: restore by attnum +SELECT pg_catalog.pg_restore_attribute_stats( + 'relation', 'stats_import.test'::regclass, + 'attnum', 1::smallint, + 'inherited', false::boolean, + 'version', 150000::integer, 'null_frac', 0.4::real, 'avg_width', 5::integer, 'n_distinct', 0.6::real); @@ -902,7 +919,7 @@ SELECT pg_catalog.pg_restore_attribute_stats( 'avg_width', 2::integer, 'n_distinct', 0.3::real); --- error: attname null +-- error: missing attname SELECT pg_catalog.pg_restore_attribute_stats( 'relation', 'stats_import.test'::regclass, 'attname', NULL::name, @@ -912,6 +929,17 @@ SELECT pg_catalog.pg_restore_attribute_stats( 'avg_width', 2::integer, 'n_distinct', 0.3::real); +-- error: both attname and attnum +SELECT pg_catalog.pg_restore_attribute_stats( + 'relation', 'stats_import.test'::regclass, + 'attname', 'id'::name, + 'attnum', 1::smallint, + 'inherited', false::boolean, + 'version', 150000::integer, + 'null_frac', 0.1::real, + 'avg_width', 2::integer, + 'n_distinct', 0.3::real); + -- error: attname doesn't exist SELECT pg_catalog.pg_restore_attribute_stats( 'relation', 'stats_import.test'::regclass, |