tags text[]
) WITH (autovacuum_enabled = false);
CREATE INDEX test_i ON stats_import.test(id);
+--
+-- relstats tests
+--
+--- error: relation is wrong type
+SELECT pg_catalog.pg_restore_relation_stats(
+ 'relation', 0::oid,
+ 'relpages', 17::integer);
+WARNING: argument "relation" has type "oid", expected type "regclass"
+ERROR: "relation" cannot be NULL
+-- error: relation not found
+SELECT pg_catalog.pg_restore_relation_stats(
+ 'relation', 0::oid::regclass,
+ 'relpages', 17::integer);
+ERROR: could not open relation with OID 0
+-- error: odd number of variadic arguments cannot be pairs
+SELECT pg_restore_relation_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'relallvisible');
+ERROR: variadic arguments must be name/value pairs
+HINT: Provide an even number of variadic arguments that can be divided into pairs.
+-- error: argument name is NULL
+SELECT pg_restore_relation_stats(
+ 'relation', 'stats_import.test'::regclass,
+ NULL, '17'::integer);
+ERROR: name at variadic position 3 is NULL
+-- error: argument name is not a text type
+SELECT pg_restore_relation_stats(
+ 'relation', '0'::oid::regclass,
+ 17, '17'::integer);
+ERROR: name at variadic position 3 has type "integer", expected type "text"
-- starting stats
SELECT relpages, reltuples, relallvisible, relallfrozen
FROM pg_class
-WHERE oid = 'stats_import.test'::regclass;
+WHERE oid = 'stats_import.test_i'::regclass;
relpages | reltuples | relallvisible | relallfrozen
----------+-----------+---------------+--------------
- 0 | -1 | 0 | 0
+ 1 | 0 | 0 | 0
(1 row)
-BEGIN;
-- regular indexes have special case locking rules
-SELECT
- pg_catalog.pg_restore_relation_stats(
+BEGIN;
+SELECT pg_catalog.pg_restore_relation_stats(
'relation', 'stats_import.test_i'::regclass,
'relpages', 18::integer);
pg_restore_relation_stats
(1 row)
COMMIT;
-SELECT
- pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.test_i'::regclass,
- 'relpages', 19::integer );
- pg_restore_relation_stats
----------------------------
- t
-(1 row)
-
--- clear
-SELECT
- pg_catalog.pg_clear_relation_stats(
- 'stats_import.test'::regclass);
- pg_clear_relation_stats
--------------------------
-
-(1 row)
-
-SELECT relpages, reltuples, relallvisible, relallfrozen
-FROM pg_class
-WHERE oid = 'stats_import.test'::regclass;
- relpages | reltuples | relallvisible | relallfrozen
-----------+-----------+---------------+--------------
- 0 | -1 | 0 | 0
-(1 row)
-
-- relpages may be -1 for partitioned tables
CREATE TABLE stats_import.part_parent ( i integer ) PARTITION BY RANGE(i);
CREATE TABLE stats_import.part_child_1
-1
(1 row)
--- although partitioned tables have no storage, setting relpages to a
--- positive value is still allowed
-SELECT
- pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.part_parent_i'::regclass,
- 'relpages', 2::integer);
- pg_restore_relation_stats
----------------------------
- t
-(1 row)
-
-SELECT
- pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.part_parent'::regclass,
- 'relpages', 2::integer);
- pg_restore_relation_stats
----------------------------
- t
-(1 row)
-
--
-- Partitioned indexes aren't analyzed but it is possible to set
-- stats. The locking rules are different from normal indexes due to
-- partitioned index are locked in ShareUpdateExclusive mode.
--
BEGIN;
-SELECT
- pg_catalog.pg_restore_relation_stats(
+SELECT pg_catalog.pg_restore_relation_stats(
'relation', 'stats_import.part_parent_i'::regclass,
'relpages', 2::integer);
pg_restore_relation_stats
(1 row)
COMMIT;
-SELECT
- pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.part_parent_i'::regclass,
- 'relpages', 2::integer);
- pg_restore_relation_stats
----------------------------
- t
-(1 row)
-
--- nothing stops us from setting it to -1
-SELECT
- pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.part_parent'::regclass,
- 'relpages', -1::integer);
- pg_restore_relation_stats
----------------------------
- t
+SELECT relpages
+FROM pg_class
+WHERE oid = 'stats_import.part_parent_i'::regclass;
+ relpages
+----------
+ 2
(1 row)
--- ok: set all stats
+-- ok: set all relstats, with version, no bounds checking
SELECT pg_restore_relation_stats(
'relation', 'stats_import.test'::regclass,
'version', 150000::integer,
- 'relpages', '17'::integer,
+ 'relpages', '-17'::integer,
'reltuples', 400::real,
'relallvisible', 4::integer,
'relallfrozen', 2::integer);
WHERE oid = 'stats_import.test'::regclass;
relpages | reltuples | relallvisible | relallfrozen
----------+-----------+---------------+--------------
- 17 | 400 | 4 | 2
+ -17 | 400 | 4 | 2
(1 row)
--- ok: just relpages
+-- ok: set just relpages, rest stay same
SELECT pg_restore_relation_stats(
'relation', 'stats_import.test'::regclass,
- 'version', 150000::integer,
'relpages', '16'::integer);
pg_restore_relation_stats
---------------------------
16 | 400 | 4 | 2
(1 row)
--- ok: just reltuples
+-- ok: set just reltuples, rest stay same
SELECT pg_restore_relation_stats(
'relation', 'stats_import.test'::regclass,
- 'version', 150000::integer,
'reltuples', '500'::real);
pg_restore_relation_stats
---------------------------
16 | 500 | 4 | 2
(1 row)
--- ok: just relallvisible
+-- ok: set just relallvisible, rest stay same
SELECT pg_restore_relation_stats(
'relation', 'stats_import.test'::regclass,
- 'version', 150000::integer,
'relallvisible', 5::integer);
pg_restore_relation_stats
---------------------------
16 | 500 | 5 | 3
(1 row)
--- warn: bad relpages type
+-- warn: bad relpages type, rest updated
SELECT pg_restore_relation_stats(
'relation', 'stats_import.test'::regclass,
- 'version', 150000::integer,
'relpages', 'nope'::text,
'reltuples', 400.0::real,
'relallvisible', 4::integer,
16 | 400 | 4 | 3
(1 row)
+-- unrecognized argument name, rest ok
+SELECT pg_restore_relation_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'relpages', '171'::integer,
+ 'nope', 10::integer);
+WARNING: unrecognized argument name: "nope"
+ pg_restore_relation_stats
+---------------------------
+ f
+(1 row)
+
+SELECT relpages, reltuples, relallvisible
+FROM pg_class
+WHERE oid = 'stats_import.test'::regclass;
+ relpages | reltuples | relallvisible
+----------+-----------+---------------
+ 171 | 400 | 4
+(1 row)
+
+-- ok: clear stats
+SELECT pg_catalog.pg_clear_relation_stats(
+ relation => 'stats_import.test'::regclass);
+ pg_clear_relation_stats
+-------------------------
+
+(1 row)
+
+SELECT relpages, reltuples, relallvisible
+FROM pg_class
+WHERE oid = 'stats_import.test'::regclass;
+ relpages | reltuples | relallvisible
+----------+-----------+---------------
+ 0 | -1 | 0
+(1 row)
+
-- invalid relkinds for statistics
CREATE SEQUENCE stats_import.testseq;
-CREATE VIEW stats_import.testview AS SELECT * FROM stats_import.test;
-SELECT
- pg_catalog.pg_clear_relation_stats(
+SELECT pg_catalog.pg_restore_relation_stats(
+ 'relation', 'stats_import.testseq'::regclass);
+ERROR: cannot modify statistics for relation "testseq"
+DETAIL: This operation is not supported for sequences.
+SELECT pg_catalog.pg_clear_relation_stats(
'stats_import.testseq'::regclass);
ERROR: cannot modify statistics for relation "testseq"
DETAIL: This operation is not supported for sequences.
-SELECT
- pg_catalog.pg_clear_relation_stats(
+CREATE VIEW stats_import.testview AS SELECT * FROM stats_import.test;
+SELECT pg_catalog.pg_restore_relation_stats(
+ 'relation', 'stats_import.testview'::regclass);
+ERROR: cannot modify statistics for relation "testview"
+DETAIL: This operation is not supported for views.
+SELECT pg_catalog.pg_clear_relation_stats(
'stats_import.testview'::regclass);
ERROR: cannot modify statistics for relation "testview"
DETAIL: This operation is not supported for views.
--- ok: no stakinds
+--
+-- attribute stats
+--
+-- error: object does not exist
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', '0'::oid::regclass,
+ 'attname', 'id'::name,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real);
+ERROR: could not open relation with OID 0
+-- error: relation null
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', NULL::oid::regclass,
+ 'attname', 'id'::name,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real);
+ERROR: "relation" cannot be NULL
+-- error: NULL attname
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'attname', NULL::name,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real);
+ERROR: must specify either attname or attnum
+-- error: attname doesn't exist
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'attname', 'nope'::name,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real,
+ 'avg_width', 2::integer,
+ 'n_distinct', 0.3::real);
+ERROR: column "nope" of relation "test" does not exist
+-- 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,
+ 'null_frac', 0.1::real);
+ERROR: cannot specify both attname and attnum
+-- error: neither attname nor attnum
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real);
+ERROR: must specify either attname or attnum
+-- error: attribute is system column
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'attname', 'xmin'::name,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real);
+ERROR: cannot modify statistics on system column "xmin"
+-- error: inherited null
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'attname', 'id'::name,
+ 'inherited', NULL::boolean,
+ 'null_frac', 0.1::real);
+ERROR: "inherited" cannot be NULL
+-- ok: just the fixed values, with version, no stakinds
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
stats_import | test | id | f | 0.2 | 5 | 0.6 | | | | | | | | | |
(1 row)
--- ok: restore by attnum
+--
+-- ok: restore by attnum, we normally reserve this for
+-- indexes, but there is no reason it shouldn't work
+-- for any stat-having relation.
+--
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);
+ 'null_frac', 0.4::real);
pg_restore_attribute_stats
----------------------------
t
stats_import | test | id | f | 0.4 | 5 | 0.6 | | | | | | | | | |
(1 row)
--- warn: unrecognized argument name
+-- warn: unrecognized argument name, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
'null_frac', 0.2::real,
- 'avg_width', NULL::integer,
'nope', 0.5::real);
WARNING: unrecognized argument name: "nope"
pg_restore_attribute_stats
stats_import | test | id | f | 0.2 | 5 | 0.6 | | | | | | | | | |
(1 row)
--- warn: mcv / mcf null mismatch part 1
+-- warn: mcv / mcf null mismatch part 1, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.6::real,
- 'avg_width', 7::integer,
- 'n_distinct', -0.7::real,
+ 'null_frac', 0.21::real,
'most_common_freqs', '{0.1,0.2,0.3}'::real[]
);
WARNING: "most_common_vals" must be specified when "most_common_freqs" is specified
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.6 | 7 | -0.7 | | | | | | | | | |
+ stats_import | test | id | f | 0.21 | 5 | 0.6 | | | | | | | | | |
(1 row)
--- warn: mcv / mcf null mismatch part 2
+-- warn: mcv / mcf null mismatch part 2, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.7::real,
- 'avg_width', 8::integer,
- 'n_distinct', -0.8::real,
+ 'null_frac', 0.21::real,
'most_common_vals', '{1,2,3}'::text
);
WARNING: "most_common_freqs" must be specified when "most_common_vals" is specified
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.7 | 8 | -0.8 | | | | | | | | | |
+ stats_import | test | id | f | 0.21 | 5 | 0.6 | | | | | | | | | |
(1 row)
--- warn: mcv / mcf type mismatch
+-- warn: mcf type mismatch, mcv-pair fails, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.8::real,
- 'avg_width', 9::integer,
- 'n_distinct', -0.9::real,
+ 'null_frac', 0.22::real,
'most_common_vals', '{2,1,3}'::text,
'most_common_freqs', '{0.2,0.1}'::double precision[]
);
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.8 | 9 | -0.9 | | | | | | | | | |
+ stats_import | test | id | f | 0.22 | 5 | 0.6 | | | | | | | | | |
(1 row)
--- warn: mcv cast failure
+-- warn: mcv cast failure, mcv-pair fails, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.9::real,
- 'avg_width', 10::integer,
- 'n_distinct', -0.4::real,
+ 'null_frac', 0.23::real,
'most_common_vals', '{2,four,3}'::text,
'most_common_freqs', '{0.3,0.25,0.05}'::real[]
);
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.9 | 10 | -0.4 | | | | | | | | | |
+ stats_import | test | id | f | 0.23 | 5 | 0.6 | | | | | | | | | |
(1 row)
-- ok: mcv+mcf
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 1::integer,
- 'n_distinct', -0.1::real,
'most_common_vals', '{2,1,3}'::text,
'most_common_freqs', '{0.3,0.25,0.05}'::real[]
);
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.1 | 1 | -0.1 | {2,1,3} | {0.3,0.25,0.05} | | | | | | | |
+ stats_import | test | id | f | 0.23 | 5 | 0.6 | {2,1,3} | {0.3,0.25,0.05} | | | | | | | |
(1 row)
--- warn: NULL in histogram array
+-- warn: NULL in histogram array, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.2::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.2::real,
+ 'null_frac', 0.24::real,
'histogram_bounds', '{1,NULL,3,4}'::text
);
WARNING: "histogram_bounds" array cannot contain NULL values
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 | 2 | -0.2 | {2,1,3} | {0.3,0.25,0.05} | | | | | | | |
+ stats_import | test | id | f | 0.24 | 5 | 0.6 | {2,1,3} | {0.3,0.25,0.05} | | | | | | | |
(1 row)
-- ok: histogram_bounds
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.3::real,
- 'avg_width', 3::integer,
- 'n_distinct', -0.3::real,
- 'histogram_bounds', '{1,2,3,4}'::text );
+ 'histogram_bounds', '{1,2,3,4}'::text
+ );
pg_restore_attribute_stats
----------------------------
t
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.3 | 3 | -0.3 | {2,1,3} | {0.3,0.25,0.05} | {1,2,3,4} | | | | | | |
+ stats_import | test | id | f | 0.24 | 5 | 0.6 | {2,1,3} | {0.3,0.25,0.05} | {1,2,3,4} | | | | | | |
(1 row)
--- warn: elem_count_histogram null element
+-- warn: elem_count_histogram null element, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'tags'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.4::real,
- 'avg_width', 5::integer,
- 'n_distinct', -0.4::real,
- 'elem_count_histogram', '{1,1,NULL,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}'::real[]
+ 'null_frac', 0.25::real,
+ 'elem_count_histogram', '{1,1,NULL,1,1,1,1,1}'::real[]
);
WARNING: "elem_count_histogram" array cannot contain NULL values
pg_restore_attribute_stats
AND attname = 'tags';
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 | tags | f | 0.4 | 5 | -0.4 | | | | | | | | | |
+ stats_import | test | tags | f | 0.25 | 0 | 0 | | | | | | | | | |
(1 row)
-- ok: elem_count_histogram
'relation', 'stats_import.test'::regclass,
'attname', 'tags'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.5::real,
- 'avg_width', 6::integer,
- 'n_distinct', -0.55::real,
+ 'null_frac', 0.26::real,
'elem_count_histogram', '{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}'::real[]
);
pg_restore_attribute_stats
AND attname = 'tags';
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 | tags | f | 0.5 | 6 | -0.55 | | | | | | | {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} | | |
+ stats_import | test | tags | f | 0.26 | 0 | 0 | | | | | | | {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} | | |
(1 row)
--- range stats on a scalar type
+-- warn: range stats on a scalar type, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.6::real,
- 'avg_width', 7::integer,
- 'n_distinct', -0.15::real,
+ 'null_frac', 0.27::real,
'range_empty_frac', 0.5::real,
'range_length_histogram', '{399,499,Infinity}'::text
);
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.6 | 7 | -0.15 | {2,1,3} | {0.3,0.25,0.05} | {1,2,3,4} | | | | | | |
+ stats_import | test | id | f | 0.27 | 5 | 0.6 | {2,1,3} | {0.3,0.25,0.05} | {1,2,3,4} | | | | | | |
(1 row)
--- warn: range_empty_frac range_length_hist null mismatch
+-- warn: range_empty_frac range_length_hist null mismatch, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'arange'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.7::real,
- 'avg_width', 8::integer,
- 'n_distinct', -0.25::real,
+ 'null_frac', 0.28::real,
'range_length_histogram', '{399,499,Infinity}'::text
);
WARNING: "range_empty_frac" must be specified when "range_length_histogram" is specified
AND attname = 'arange';
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 | arange | f | 0.7 | 8 | -0.25 | | | | | | | | | |
+ stats_import | test | arange | f | 0.28 | 0 | 0 | | | | | | | | | |
(1 row)
--- warn: range_empty_frac range_length_hist null mismatch part 2
+-- warn: range_empty_frac range_length_hist null mismatch part 2, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'arange'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.8::real,
- 'avg_width', 9::integer,
- 'n_distinct', -0.35::real,
+ 'null_frac', 0.29::real,
'range_empty_frac', 0.5::real
);
WARNING: "range_length_histogram" must be specified when "range_empty_frac" is specified
AND attname = 'arange';
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 | arange | f | 0.8 | 9 | -0.35 | | | | | | | | | |
+ stats_import | test | arange | f | 0.29 | 0 | 0 | | | | | | | | | |
(1 row)
-- ok: range_empty_frac + range_length_hist
'relation', 'stats_import.test'::regclass,
'attname', 'arange'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.9::real,
- 'avg_width', 1::integer,
- 'n_distinct', -0.19::real,
'range_empty_frac', 0.5::real,
'range_length_histogram', '{399,499,Infinity}'::text
);
AND attname = 'arange';
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 | arange | f | 0.9 | 1 | -0.19 | | | | | | | | {399,499,Infinity} | 0.5 |
+ stats_import | test | arange | f | 0.29 | 0 | 0 | | | | | | | | {399,499,Infinity} | 0.5 |
(1 row)
--- warn: range bounds histogram on scalar
+-- warn: range bounds histogram on scalar, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.29::real,
+ 'null_frac', 0.31::real,
'range_bounds_histogram', '{"[-1,1)","[0,4)","[1,4)","[1,100)"}'::text
);
WARNING: attribute "id" is not a range type
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.1 | 2 | -0.29 | {2,1,3} | {0.3,0.25,0.05} | {1,2,3,4} | | | | | | |
+ stats_import | test | id | f | 0.31 | 5 | 0.6 | {2,1,3} | {0.3,0.25,0.05} | {1,2,3,4} | | | | | | |
(1 row)
-- ok: range_bounds_histogram
'relation', 'stats_import.test'::regclass,
'attname', 'arange'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.2::real,
- 'avg_width', 3::integer,
- 'n_distinct', -0.39::real,
'range_bounds_histogram', '{"[-1,1)","[0,4)","[1,4)","[1,100)"}'::text
);
pg_restore_attribute_stats
AND attname = 'arange';
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 | arange | f | 0.2 | 3 | -0.39 | | | | | | | | {399,499,Infinity} | 0.5 | {"[-1,1)","[0,4)","[1,4)","[1,100)"}
+ stats_import | test | arange | f | 0.29 | 0 | 0 | | | | | | | | {399,499,Infinity} | 0.5 | {"[-1,1)","[0,4)","[1,4)","[1,100)"}
(1 row)
--- warn: cannot set most_common_elems for range type
+-- warn: cannot set most_common_elems for range type, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'arange'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
- 'most_common_vals', '{"[2,3)","[1,2)","[3,4)"}'::text,
- 'most_common_freqs', '{0.3,0.25,0.05}'::real[],
- 'histogram_bounds', '{"[1,2)","[2,3)","[3,4)","[4,5)"}'::text,
- 'correlation', 1.1::real,
+ 'null_frac', 0.32::real,
'most_common_elems', '{3,1}'::text,
- 'most_common_elem_freqs', '{0.3,0.2,0.2,0.3,0.0}'::real[],
- 'range_empty_frac', -0.5::real,
- 'range_length_histogram', '{399,499,Infinity}'::text,
- 'range_bounds_histogram', '{"[-1,1)","[0,4)","[1,4)","[1,100)"}'::text
+ 'most_common_elem_freqs', '{0.3,0.2,0.2,0.3,0.0}'::real[]
);
WARNING: unable to determine element type of attribute "arange"
DETAIL: Cannot set STATISTIC_KIND_MCELEM or STATISTIC_KIND_DECHIST.
AND tablename = 'test'
AND inherited = false
AND attname = 'arange';
- 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 | arange | f | 0.5 | 2 | -0.1 | {"[2,3)","[1,2)","[3,4)"} | {0.3,0.25,0.05} | {"[1,2)","[2,3)","[3,4)","[4,5)"} | 1.1 | | | | {399,499,Infinity} | -0.5 | {"[-1,1)","[0,4)","[1,4)","[1,100)"}
+ 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 | arange | f | 0.32 | 0 | 0 | | | | | | | | {399,499,Infinity} | 0.5 | {"[-1,1)","[0,4)","[1,4)","[1,100)"}
(1 row)
--- warn: scalars can't have mcelem
+-- warn: scalars can't have mcelem, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
+ 'null_frac', 0.33::real,
'most_common_elems', '{1,3}'::text,
'most_common_elem_freqs', '{0.3,0.2,0.2,0.3,0.0}'::real[]
);
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.5 | 2 | -0.1 | {2,1,3} | {0.3,0.25,0.05} | {1,2,3,4} | | | | | | |
+ stats_import | test | id | f | 0.33 | 5 | 0.6 | {2,1,3} | {0.3,0.25,0.05} | {1,2,3,4} | | | | | | |
(1 row)
--- warn: mcelem / mcelem mismatch
+-- warn: mcelem / mcelem mismatch, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'tags'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
+ 'null_frac', 0.34::real,
'most_common_elems', '{one,two}'::text
);
WARNING: "most_common_elem_freqs" must be specified when "most_common_elems" is specified
AND attname = 'tags';
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 | tags | f | 0.5 | 2 | -0.1 | | | | | | | {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} | | |
+ stats_import | test | tags | f | 0.34 | 0 | 0 | | | | | | | {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} | | |
(1 row)
--- warn: mcelem / mcelem null mismatch part 2
+-- warn: mcelem / mcelem null mismatch part 2, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'tags'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
+ 'null_frac', 0.35::real,
'most_common_elem_freqs', '{0.3,0.2,0.2,0.3}'::real[]
);
WARNING: "most_common_elems" must be specified when "most_common_elem_freqs" is specified
f
(1 row)
+SELECT *
+FROM pg_stats
+WHERE schemaname = 'stats_import'
+AND tablename = 'test'
+AND inherited = false
+AND attname = 'tags';
+ 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 | tags | f | 0.35 | 0 | 0 | | | | | | | {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} | | |
+(1 row)
+
-- ok: mcelem
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'tags'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
'most_common_elems', '{one,three}'::text,
'most_common_elem_freqs', '{0.3,0.2,0.2,0.3,0.0}'::real[]
);
AND attname = 'tags';
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 | tags | f | 0.5 | 2 | -0.1 | | | | | {one,three} | {0.3,0.2,0.2,0.3,0} | {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} | | |
+ stats_import | test | tags | f | 0.35 | 0 | 0 | | | | | {one,three} | {0.3,0.2,0.2,0.3,0} | {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} | | |
(1 row)
--- warn: scalars can't have elem_count_histogram
+-- warn: scalars can't have elem_count_histogram, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
- 'elem_count_histogram', '{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}'::real[]
+ 'null_frac', 0.36::real,
+ 'elem_count_histogram', '{1,1,1,1,1,1,1,1,1,1}'::real[]
);
WARNING: unable to determine element type of attribute "id"
DETAIL: Cannot set STATISTIC_KIND_MCELEM or STATISTIC_KIND_DECHIST.
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.5 | 2 | -0.1 | {2,1,3} | {0.3,0.25,0.05} | {1,2,3,4} | | | | | | |
-(1 row)
-
--- warn: too many stat kinds
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', 'stats_import.test'::regclass,
- 'attname', 'arange'::name,
- 'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
- 'most_common_vals', '{"[2,3)","[1,3)","[3,9)"}'::text,
- 'most_common_freqs', '{0.3,0.25,0.05}'::real[],
- 'histogram_bounds', '{"[1,2)","[2,3)","[3,4)","[4,)"}'::text,
- 'correlation', 1.1::real,
- 'most_common_elems', '{3,1}'::text,
- 'most_common_elem_freqs', '{0.3,0.2,0.2,0.3,0.0}'::real[],
- 'range_empty_frac', -0.5::real,
- 'range_length_histogram', '{399,499,Infinity}'::text,
- 'range_bounds_histogram', '{"[-1,1)","[0,4)","[1,4)","[1,100)"}'::text);
-WARNING: unable to determine element type of attribute "arange"
-DETAIL: Cannot set STATISTIC_KIND_MCELEM or STATISTIC_KIND_DECHIST.
- pg_restore_attribute_stats
-----------------------------
- f
-(1 row)
-
-SELECT *
-FROM pg_stats
-WHERE schemaname = 'stats_import'
-AND tablename = 'test'
-AND inherited = false
-AND attname = 'arange';
- 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 | arange | f | 0.5 | 2 | -0.1 | {"[2,3)","[1,3)","[3,9)"} | {0.3,0.25,0.05} | {"[1,2)","[2,3)","[3,4)","[4,)"} | 1.1 | | | | {399,499,Infinity} | -0.5 | {"[-1,1)","[0,4)","[1,4)","[1,100)"}
+ stats_import | test | id | f | 0.36 | 5 | 0.6 | {2,1,3} | {0.3,0.25,0.05} | {1,2,3,4} | | | | | | |
(1 row)
--
UNION ALL
SELECT 4, 'four', NULL, int4range(0,100), NULL;
CREATE INDEX is_odd ON stats_import.test(((comp).a % 2 = 1));
--- restoring stats on index
-SELECT * FROM pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.is_odd'::regclass,
- 'version', '180000'::integer,
- 'relpages', '11'::integer,
- 'reltuples', '10000'::real,
- 'relallvisible', '0'::integer,
- 'relallfrozen', '0'::integer
-);
- pg_restore_relation_stats
----------------------------
- t
-(1 row)
-
-- Generate statistics on table with data
ANALYZE stats_import.test;
CREATE TABLE stats_import.test_clone ( LIKE stats_import.test )
---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+----------+----------+----------+----------+----------+-------------+-------------+-------------+-------------+-------------+-----+-----+-----+-----+-----+-----------
(0 rows)
--- ok
+-- attribute stats exist before a clear, but not after
+SELECT COUNT(*)
+FROM pg_stats
+WHERE schemaname = 'stats_import'
+AND tablename = 'test'
+AND inherited = false
+AND attname = 'arange';
+ count
+-------
+ 1
+(1 row)
+
SELECT pg_catalog.pg_clear_attribute_stats(
relation => 'stats_import.test'::regclass,
attname => 'arange'::name,
(1 row)
---
--- Negative tests
---
---- error: relation is wrong type
-SELECT pg_catalog.pg_restore_relation_stats(
- 'relation', 0::oid,
- 'relpages', 17::integer,
- 'reltuples', 400.0::real,
- 'relallvisible', 4::integer,
- 'relallfrozen', 3::integer);
-WARNING: argument "relation" has type "oid", expected type "regclass"
-ERROR: "relation" cannot be NULL
---- error: relation not found
-SELECT pg_catalog.pg_restore_relation_stats(
- 'relation', 0::regclass,
- 'relpages', 17::integer,
- 'reltuples', 400.0::real,
- 'relallvisible', 4::integer,
- 'relallfrozen', 3::integer);
-ERROR: could not open relation with OID 0
--- warn and error: unrecognized argument name
-SELECT pg_restore_relation_stats(
- 'relation', '0'::oid::regclass,
- 'version', 150000::integer,
- 'relpages', '17'::integer,
- 'reltuples', 400::real,
- 'nope', 4::integer);
-WARNING: unrecognized argument name: "nope"
-ERROR: could not open relation with OID 0
--- error: argument name is NULL
-SELECT pg_restore_relation_stats(
- 'relation', '0'::oid::regclass,
- 'version', 150000::integer,
- NULL, '17'::integer,
- 'reltuples', 400::real,
- 'relallvisible', 4::integer,
- 'relallfrozen', 3::integer);
-ERROR: name at variadic position 5 is NULL
--- error: argument name is an integer
-SELECT pg_restore_relation_stats(
- 'relation', '0'::oid::regclass,
- 'version', 150000::integer,
- 17, '17'::integer,
- 'reltuples', 400::real,
- 'relallvisible', 4::integer,
- 'relallfrozen', 3::integer);
-ERROR: name at variadic position 5 has type "integer", expected type "text"
--- error: odd number of variadic arguments cannot be pairs
-SELECT pg_restore_relation_stats(
- 'relation', '0'::oid::regclass,
- 'version', 150000::integer,
- 'relpages', '17'::integer,
- 'reltuples', 400::real,
- 'relallfrozen', 3::integer,
- 'relallvisible');
-ERROR: variadic arguments must be name/value pairs
-HINT: Provide an even number of variadic arguments that can be divided into pairs.
--- error: object doesn't exist
-SELECT pg_restore_relation_stats(
- 'relation', '0'::oid::regclass,
- 'version', 150000::integer,
- 'relpages', '17'::integer,
- 'reltuples', 400::real,
- 'relallvisible', 4::integer,
- 'relallfrozen', 3::integer);
-ERROR: could not open relation with OID 0
--- error: object does not exist
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', '0'::oid::regclass,
- 'attname', 'id'::name,
- 'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', 0.3::real);
-ERROR: could not open relation with OID 0
--- error: relation null
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', NULL::oid,
- 'attname', 'id'::name,
- 'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', 0.3::real);
-ERROR: "relation" cannot be NULL
--- error: missing attname
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', 'stats_import.test'::regclass,
- 'attname', NULL::name,
- 'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', 0.3::real);
-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,
- 'attname', 'nope'::name,
- 'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', 0.3::real);
-ERROR: column "nope" of relation "test" does not exist
--- error: attribute is system column
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', 'stats_import.test'::regclass,
- 'attname', 'xmin'::name,
- 'inherited', false::boolean,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', 0.3::real);
-ERROR: cannot modify statistics on system column "xmin"
--- error: inherited null
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', 'stats_import.test'::regclass,
- 'attname', 'id'::name,
- 'inherited', NULL::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', 0.3::real);
-ERROR: "inherited" cannot be NULL
--- error: relation not found
-SELECT pg_catalog.pg_clear_relation_stats(
- relation => 'stats_import.nope'::regclass);
-ERROR: relation "stats_import.nope" does not exist
-LINE 2: relation => 'stats_import.nope'::regclass);
- ^
--- error: attribute is system column
-SELECT pg_catalog.pg_clear_attribute_stats(
- relation => 'stats_import.test'::regclass,
- attname => 'ctid'::name,
- inherited => false::boolean);
-ERROR: cannot clear statistics on system column "ctid"
--- error: attname doesn't exist
-SELECT pg_catalog.pg_clear_attribute_stats(
- relation => 'stats_import.test'::regclass,
- attname => 'nope'::name,
- inherited => false::boolean);
-ERROR: column "nope" of relation "test" does not exist
+SELECT COUNT(*)
+FROM pg_stats
+WHERE schemaname = 'stats_import'
+AND tablename = 'test'
+AND inherited = false
+AND attname = 'arange';
+ count
+-------
+ 0
+(1 row)
+
DROP SCHEMA stats_import CASCADE;
NOTICE: drop cascades to 6 other objects
DETAIL: drop cascades to type stats_import.complex_type
CREATE INDEX test_i ON stats_import.test(id);
+--
+-- relstats tests
+--
+
+--- error: relation is wrong type
+SELECT pg_catalog.pg_restore_relation_stats(
+ 'relation', 0::oid,
+ 'relpages', 17::integer);
+
+-- error: relation not found
+SELECT pg_catalog.pg_restore_relation_stats(
+ 'relation', 0::oid::regclass,
+ 'relpages', 17::integer);
+
+-- error: odd number of variadic arguments cannot be pairs
+SELECT pg_restore_relation_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'relallvisible');
+
+-- error: argument name is NULL
+SELECT pg_restore_relation_stats(
+ 'relation', 'stats_import.test'::regclass,
+ NULL, '17'::integer);
+
+-- error: argument name is not a text type
+SELECT pg_restore_relation_stats(
+ 'relation', '0'::oid::regclass,
+ 17, '17'::integer);
+
-- starting stats
SELECT relpages, reltuples, relallvisible, relallfrozen
FROM pg_class
-WHERE oid = 'stats_import.test'::regclass;
+WHERE oid = 'stats_import.test_i'::regclass;
-BEGIN;
-- regular indexes have special case locking rules
-SELECT
- pg_catalog.pg_restore_relation_stats(
+BEGIN;
+SELECT pg_catalog.pg_restore_relation_stats(
'relation', 'stats_import.test_i'::regclass,
'relpages', 18::integer);
COMMIT;
-SELECT
- pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.test_i'::regclass,
- 'relpages', 19::integer );
-
--- clear
-SELECT
- pg_catalog.pg_clear_relation_stats(
- 'stats_import.test'::regclass);
-
-SELECT relpages, reltuples, relallvisible, relallfrozen
-FROM pg_class
-WHERE oid = 'stats_import.test'::regclass;
-
-- relpages may be -1 for partitioned tables
CREATE TABLE stats_import.part_parent ( i integer ) PARTITION BY RANGE(i);
CREATE TABLE stats_import.part_child_1
FROM pg_class
WHERE oid = 'stats_import.part_parent'::regclass;
--- although partitioned tables have no storage, setting relpages to a
--- positive value is still allowed
-SELECT
- pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.part_parent_i'::regclass,
- 'relpages', 2::integer);
-
-SELECT
- pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.part_parent'::regclass,
- 'relpages', 2::integer);
-
--
-- Partitioned indexes aren't analyzed but it is possible to set
-- stats. The locking rules are different from normal indexes due to
--
BEGIN;
-SELECT
- pg_catalog.pg_restore_relation_stats(
+SELECT pg_catalog.pg_restore_relation_stats(
'relation', 'stats_import.part_parent_i'::regclass,
'relpages', 2::integer);
COMMIT;
-SELECT
- pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.part_parent_i'::regclass,
- 'relpages', 2::integer);
-
--- nothing stops us from setting it to -1
-SELECT
- pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.part_parent'::regclass,
- 'relpages', -1::integer);
+SELECT relpages
+FROM pg_class
+WHERE oid = 'stats_import.part_parent_i'::regclass;
--- ok: set all stats
+-- ok: set all relstats, with version, no bounds checking
SELECT pg_restore_relation_stats(
'relation', 'stats_import.test'::regclass,
'version', 150000::integer,
- 'relpages', '17'::integer,
+ 'relpages', '-17'::integer,
'reltuples', 400::real,
'relallvisible', 4::integer,
'relallfrozen', 2::integer);
FROM pg_class
WHERE oid = 'stats_import.test'::regclass;
--- ok: just relpages
+-- ok: set just relpages, rest stay same
SELECT pg_restore_relation_stats(
'relation', 'stats_import.test'::regclass,
- 'version', 150000::integer,
'relpages', '16'::integer);
SELECT relpages, reltuples, relallvisible, relallfrozen
FROM pg_class
WHERE oid = 'stats_import.test'::regclass;
--- ok: just reltuples
+-- ok: set just reltuples, rest stay same
SELECT pg_restore_relation_stats(
'relation', 'stats_import.test'::regclass,
- 'version', 150000::integer,
'reltuples', '500'::real);
SELECT relpages, reltuples, relallvisible, relallfrozen
FROM pg_class
WHERE oid = 'stats_import.test'::regclass;
--- ok: just relallvisible
+-- ok: set just relallvisible, rest stay same
SELECT pg_restore_relation_stats(
'relation', 'stats_import.test'::regclass,
- 'version', 150000::integer,
'relallvisible', 5::integer);
SELECT relpages, reltuples, relallvisible, relallfrozen
FROM pg_class
WHERE oid = 'stats_import.test'::regclass;
--- warn: bad relpages type
+-- warn: bad relpages type, rest updated
SELECT pg_restore_relation_stats(
'relation', 'stats_import.test'::regclass,
- 'version', 150000::integer,
'relpages', 'nope'::text,
'reltuples', 400.0::real,
'relallvisible', 4::integer,
FROM pg_class
WHERE oid = 'stats_import.test'::regclass;
+-- unrecognized argument name, rest ok
+SELECT pg_restore_relation_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'relpages', '171'::integer,
+ 'nope', 10::integer);
+
+SELECT relpages, reltuples, relallvisible
+FROM pg_class
+WHERE oid = 'stats_import.test'::regclass;
+
+-- ok: clear stats
+SELECT pg_catalog.pg_clear_relation_stats(
+ relation => 'stats_import.test'::regclass);
+
+SELECT relpages, reltuples, relallvisible
+FROM pg_class
+WHERE oid = 'stats_import.test'::regclass;
+
-- invalid relkinds for statistics
CREATE SEQUENCE stats_import.testseq;
-CREATE VIEW stats_import.testview AS SELECT * FROM stats_import.test;
-SELECT
- pg_catalog.pg_clear_relation_stats(
+
+SELECT pg_catalog.pg_restore_relation_stats(
+ 'relation', 'stats_import.testseq'::regclass);
+
+SELECT pg_catalog.pg_clear_relation_stats(
'stats_import.testseq'::regclass);
-SELECT
- pg_catalog.pg_clear_relation_stats(
+
+CREATE VIEW stats_import.testview AS SELECT * FROM stats_import.test;
+
+SELECT pg_catalog.pg_restore_relation_stats(
+ 'relation', 'stats_import.testview'::regclass);
+
+SELECT pg_catalog.pg_clear_relation_stats(
'stats_import.testview'::regclass);
--- ok: no stakinds
+--
+-- attribute stats
+--
+
+-- error: object does not exist
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', '0'::oid::regclass,
+ 'attname', 'id'::name,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real);
+
+-- error: relation null
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', NULL::oid::regclass,
+ 'attname', 'id'::name,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real);
+
+-- error: NULL attname
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'attname', NULL::name,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real);
+
+-- error: attname doesn't exist
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'attname', 'nope'::name,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real,
+ '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,
+ 'null_frac', 0.1::real);
+
+-- error: neither attname nor attnum
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real);
+
+-- error: attribute is system column
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'attname', 'xmin'::name,
+ 'inherited', false::boolean,
+ 'null_frac', 0.1::real);
+
+-- error: inherited null
+SELECT pg_catalog.pg_restore_attribute_stats(
+ 'relation', 'stats_import.test'::regclass,
+ 'attname', 'id'::name,
+ 'inherited', NULL::boolean,
+ 'null_frac', 0.1::real);
+
+-- ok: just the fixed values, with version, no stakinds
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
AND inherited = false
AND attname = 'id';
--- ok: restore by attnum
+--
+-- ok: restore by attnum, we normally reserve this for
+-- indexes, but there is no reason it shouldn't work
+-- for any stat-having relation.
+--
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);
+ 'null_frac', 0.4::real);
SELECT *
FROM pg_stats
AND inherited = false
AND attname = 'id';
--- warn: unrecognized argument name
+-- warn: unrecognized argument name, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
'null_frac', 0.2::real,
- 'avg_width', NULL::integer,
'nope', 0.5::real);
SELECT *
AND inherited = false
AND attname = 'id';
--- warn: mcv / mcf null mismatch part 1
+-- warn: mcv / mcf null mismatch part 1, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.6::real,
- 'avg_width', 7::integer,
- 'n_distinct', -0.7::real,
+ 'null_frac', 0.21::real,
'most_common_freqs', '{0.1,0.2,0.3}'::real[]
);
AND inherited = false
AND attname = 'id';
--- warn: mcv / mcf null mismatch part 2
+-- warn: mcv / mcf null mismatch part 2, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.7::real,
- 'avg_width', 8::integer,
- 'n_distinct', -0.8::real,
+ 'null_frac', 0.21::real,
'most_common_vals', '{1,2,3}'::text
);
AND inherited = false
AND attname = 'id';
--- warn: mcv / mcf type mismatch
+-- warn: mcf type mismatch, mcv-pair fails, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.8::real,
- 'avg_width', 9::integer,
- 'n_distinct', -0.9::real,
+ 'null_frac', 0.22::real,
'most_common_vals', '{2,1,3}'::text,
'most_common_freqs', '{0.2,0.1}'::double precision[]
);
AND inherited = false
AND attname = 'id';
--- warn: mcv cast failure
+-- warn: mcv cast failure, mcv-pair fails, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.9::real,
- 'avg_width', 10::integer,
- 'n_distinct', -0.4::real,
+ 'null_frac', 0.23::real,
'most_common_vals', '{2,four,3}'::text,
'most_common_freqs', '{0.3,0.25,0.05}'::real[]
);
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 1::integer,
- 'n_distinct', -0.1::real,
'most_common_vals', '{2,1,3}'::text,
'most_common_freqs', '{0.3,0.25,0.05}'::real[]
);
AND inherited = false
AND attname = 'id';
--- warn: NULL in histogram array
+-- warn: NULL in histogram array, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.2::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.2::real,
+ 'null_frac', 0.24::real,
'histogram_bounds', '{1,NULL,3,4}'::text
);
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.3::real,
- 'avg_width', 3::integer,
- 'n_distinct', -0.3::real,
- 'histogram_bounds', '{1,2,3,4}'::text );
+ 'histogram_bounds', '{1,2,3,4}'::text
+ );
SELECT *
FROM pg_stats
AND inherited = false
AND attname = 'id';
--- warn: elem_count_histogram null element
+-- warn: elem_count_histogram null element, rest get set
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'tags'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.4::real,
- 'avg_width', 5::integer,
- 'n_distinct', -0.4::real,
- 'elem_count_histogram', '{1,1,NULL,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}'::real[]
+ 'null_frac', 0.25::real,
+ 'elem_count_histogram', '{1,1,NULL,1,1,1,1,1}'::real[]
);
SELECT *
'relation', 'stats_import.test'::regclass,
'attname', 'tags'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.5::real,
- 'avg_width', 6::integer,
- 'n_distinct', -0.55::real,
+ 'null_frac', 0.26::real,
'elem_count_histogram', '{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}'::real[]
);
AND inherited = false
AND attname = 'tags';
--- range stats on a scalar type
+-- warn: range stats on a scalar type, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.6::real,
- 'avg_width', 7::integer,
- 'n_distinct', -0.15::real,
+ 'null_frac', 0.27::real,
'range_empty_frac', 0.5::real,
'range_length_histogram', '{399,499,Infinity}'::text
);
AND inherited = false
AND attname = 'id';
--- warn: range_empty_frac range_length_hist null mismatch
+-- warn: range_empty_frac range_length_hist null mismatch, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'arange'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.7::real,
- 'avg_width', 8::integer,
- 'n_distinct', -0.25::real,
+ 'null_frac', 0.28::real,
'range_length_histogram', '{399,499,Infinity}'::text
);
AND inherited = false
AND attname = 'arange';
--- warn: range_empty_frac range_length_hist null mismatch part 2
+-- warn: range_empty_frac range_length_hist null mismatch part 2, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'arange'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.8::real,
- 'avg_width', 9::integer,
- 'n_distinct', -0.35::real,
+ 'null_frac', 0.29::real,
'range_empty_frac', 0.5::real
);
'relation', 'stats_import.test'::regclass,
'attname', 'arange'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.9::real,
- 'avg_width', 1::integer,
- 'n_distinct', -0.19::real,
'range_empty_frac', 0.5::real,
'range_length_histogram', '{399,499,Infinity}'::text
);
AND inherited = false
AND attname = 'arange';
--- warn: range bounds histogram on scalar
+-- warn: range bounds histogram on scalar, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.29::real,
+ 'null_frac', 0.31::real,
'range_bounds_histogram', '{"[-1,1)","[0,4)","[1,4)","[1,100)"}'::text
);
'relation', 'stats_import.test'::regclass,
'attname', 'arange'::name,
'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.2::real,
- 'avg_width', 3::integer,
- 'n_distinct', -0.39::real,
'range_bounds_histogram', '{"[-1,1)","[0,4)","[1,4)","[1,100)"}'::text
);
AND inherited = false
AND attname = 'arange';
--- warn: cannot set most_common_elems for range type
+-- warn: cannot set most_common_elems for range type, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'arange'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
- 'most_common_vals', '{"[2,3)","[1,2)","[3,4)"}'::text,
- 'most_common_freqs', '{0.3,0.25,0.05}'::real[],
- 'histogram_bounds', '{"[1,2)","[2,3)","[3,4)","[4,5)"}'::text,
- 'correlation', 1.1::real,
+ 'null_frac', 0.32::real,
'most_common_elems', '{3,1}'::text,
- 'most_common_elem_freqs', '{0.3,0.2,0.2,0.3,0.0}'::real[],
- 'range_empty_frac', -0.5::real,
- 'range_length_histogram', '{399,499,Infinity}'::text,
- 'range_bounds_histogram', '{"[-1,1)","[0,4)","[1,4)","[1,100)"}'::text
+ 'most_common_elem_freqs', '{0.3,0.2,0.2,0.3,0.0}'::real[]
);
SELECT *
AND inherited = false
AND attname = 'arange';
--- warn: scalars can't have mcelem
+-- warn: scalars can't have mcelem, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
+ 'null_frac', 0.33::real,
'most_common_elems', '{1,3}'::text,
'most_common_elem_freqs', '{0.3,0.2,0.2,0.3,0.0}'::real[]
);
AND inherited = false
AND attname = 'id';
--- warn: mcelem / mcelem mismatch
+-- warn: mcelem / mcelem mismatch, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'tags'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
+ 'null_frac', 0.34::real,
'most_common_elems', '{one,two}'::text
);
AND inherited = false
AND attname = 'tags';
--- warn: mcelem / mcelem null mismatch part 2
+-- warn: mcelem / mcelem null mismatch part 2, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'tags'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
+ 'null_frac', 0.35::real,
'most_common_elem_freqs', '{0.3,0.2,0.2,0.3}'::real[]
);
+SELECT *
+FROM pg_stats
+WHERE schemaname = 'stats_import'
+AND tablename = 'test'
+AND inherited = false
+AND attname = 'tags';
+
-- ok: mcelem
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'tags'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
'most_common_elems', '{one,three}'::text,
'most_common_elem_freqs', '{0.3,0.2,0.2,0.3,0.0}'::real[]
);
AND inherited = false
AND attname = 'tags';
--- warn: scalars can't have elem_count_histogram
+-- warn: scalars can't have elem_count_histogram, rest ok
SELECT pg_catalog.pg_restore_attribute_stats(
'relation', 'stats_import.test'::regclass,
'attname', 'id'::name,
'inherited', false::boolean,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
- 'elem_count_histogram', '{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}'::real[]
+ 'null_frac', 0.36::real,
+ 'elem_count_histogram', '{1,1,1,1,1,1,1,1,1,1}'::real[]
);
SELECT *
AND inherited = false
AND attname = 'id';
--- warn: too many stat kinds
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', 'stats_import.test'::regclass,
- 'attname', 'arange'::name,
- 'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.5::real,
- 'avg_width', 2::integer,
- 'n_distinct', -0.1::real,
- 'most_common_vals', '{"[2,3)","[1,3)","[3,9)"}'::text,
- 'most_common_freqs', '{0.3,0.25,0.05}'::real[],
- 'histogram_bounds', '{"[1,2)","[2,3)","[3,4)","[4,)"}'::text,
- 'correlation', 1.1::real,
- 'most_common_elems', '{3,1}'::text,
- 'most_common_elem_freqs', '{0.3,0.2,0.2,0.3,0.0}'::real[],
- 'range_empty_frac', -0.5::real,
- 'range_length_histogram', '{399,499,Infinity}'::text,
- 'range_bounds_histogram', '{"[-1,1)","[0,4)","[1,4)","[1,100)"}'::text);
-
-SELECT *
-FROM pg_stats
-WHERE schemaname = 'stats_import'
-AND tablename = 'test'
-AND inherited = false
-AND attname = 'arange';
-
--
-- Test the ability to exactly copy data from one table to an identical table,
-- correctly reconstructing the stakind order as well as the staopN and
CREATE INDEX is_odd ON stats_import.test(((comp).a % 2 = 1));
--- restoring stats on index
-SELECT * FROM pg_catalog.pg_restore_relation_stats(
- 'relation', 'stats_import.is_odd'::regclass,
- 'version', '180000'::integer,
- 'relpages', '11'::integer,
- 'reltuples', '10000'::real,
- 'relallvisible', '0'::integer,
- 'relallfrozen', '0'::integer
-);
-
-- Generate statistics on table with data
ANALYZE stats_import.test;
JOIN pg_attribute a ON a.attrelid = s.starelid AND a.attnum = s.staattnum
WHERE s.starelid = 'stats_import.is_odd'::regclass;
--- ok
-SELECT pg_catalog.pg_clear_attribute_stats(
- relation => 'stats_import.test'::regclass,
- attname => 'arange'::name,
- inherited => false::boolean);
-
---
--- Negative tests
---
-
---- error: relation is wrong type
-SELECT pg_catalog.pg_restore_relation_stats(
- 'relation', 0::oid,
- 'relpages', 17::integer,
- 'reltuples', 400.0::real,
- 'relallvisible', 4::integer,
- 'relallfrozen', 3::integer);
-
---- error: relation not found
-SELECT pg_catalog.pg_restore_relation_stats(
- 'relation', 0::regclass,
- 'relpages', 17::integer,
- 'reltuples', 400.0::real,
- 'relallvisible', 4::integer,
- 'relallfrozen', 3::integer);
-
--- warn and error: unrecognized argument name
-SELECT pg_restore_relation_stats(
- 'relation', '0'::oid::regclass,
- 'version', 150000::integer,
- 'relpages', '17'::integer,
- 'reltuples', 400::real,
- 'nope', 4::integer);
-
--- error: argument name is NULL
-SELECT pg_restore_relation_stats(
- 'relation', '0'::oid::regclass,
- 'version', 150000::integer,
- NULL, '17'::integer,
- 'reltuples', 400::real,
- 'relallvisible', 4::integer,
- 'relallfrozen', 3::integer);
-
--- error: argument name is an integer
-SELECT pg_restore_relation_stats(
- 'relation', '0'::oid::regclass,
- 'version', 150000::integer,
- 17, '17'::integer,
- 'reltuples', 400::real,
- 'relallvisible', 4::integer,
- 'relallfrozen', 3::integer);
-
--- error: odd number of variadic arguments cannot be pairs
-SELECT pg_restore_relation_stats(
- 'relation', '0'::oid::regclass,
- 'version', 150000::integer,
- 'relpages', '17'::integer,
- 'reltuples', 400::real,
- 'relallfrozen', 3::integer,
- 'relallvisible');
-
--- error: object doesn't exist
-SELECT pg_restore_relation_stats(
- 'relation', '0'::oid::regclass,
- 'version', 150000::integer,
- 'relpages', '17'::integer,
- 'reltuples', 400::real,
- 'relallvisible', 4::integer,
- 'relallfrozen', 3::integer);
-
--- error: object does not exist
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', '0'::oid::regclass,
- 'attname', 'id'::name,
- 'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', 0.3::real);
-
--- error: relation null
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', NULL::oid,
- 'attname', 'id'::name,
- 'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', 0.3::real);
-
--- error: missing attname
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', 'stats_import.test'::regclass,
- 'attname', NULL::name,
- 'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- '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,
- 'attname', 'nope'::name,
- 'inherited', false::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', 0.3::real);
-
--- error: attribute is system column
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', 'stats_import.test'::regclass,
- 'attname', 'xmin'::name,
- 'inherited', false::boolean,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', 0.3::real);
-
--- error: inherited null
-SELECT pg_catalog.pg_restore_attribute_stats(
- 'relation', 'stats_import.test'::regclass,
- 'attname', 'id'::name,
- 'inherited', NULL::boolean,
- 'version', 150000::integer,
- 'null_frac', 0.1::real,
- 'avg_width', 2::integer,
- 'n_distinct', 0.3::real);
-
--- error: relation not found
-SELECT pg_catalog.pg_clear_relation_stats(
- relation => 'stats_import.nope'::regclass);
+-- attribute stats exist before a clear, but not after
+SELECT COUNT(*)
+FROM pg_stats
+WHERE schemaname = 'stats_import'
+AND tablename = 'test'
+AND inherited = false
+AND attname = 'arange';
--- error: attribute is system column
SELECT pg_catalog.pg_clear_attribute_stats(
relation => 'stats_import.test'::regclass,
- attname => 'ctid'::name,
+ attname => 'arange'::name,
inherited => false::boolean);
--- error: attname doesn't exist
-SELECT pg_catalog.pg_clear_attribute_stats(
- relation => 'stats_import.test'::regclass,
- attname => 'nope'::name,
- inherited => false::boolean);
+SELECT COUNT(*)
+FROM pg_stats
+WHERE schemaname = 'stats_import'
+AND tablename = 'test'
+AND inherited = false
+AND attname = 'arange';
DROP SCHEMA stats_import CASCADE;