-- Smoke regression for DBMS_STATS.
|
SELECT dbms_stats_get_version();
|
dbms_stats_get_version
|
------------------------
|
0.5.1
|
(1 row)
|
|
-- == gather_table_stats tests ==
|
-- Test basic lifecycle: create table, insert data, gather stats, verify
|
CREATE TABLE dbms_stats_test(id INT, val TEXT);
|
-- Initially no statistics: reltuples should be 0 or negative (unknown)
|
SELECT reltuples, relpages
|
FROM pg_class
|
WHERE relname = 'dbms_stats_test';
|
reltuples | relpages
|
-----------+----------
|
-1 | 0
|
(1 row)
|
|
INSERT INTO dbms_stats_test VALUES (1, 'a'), (2, 'b'), (3, 'c');
|
-- Call procedure with only ownname and tabname, all other args default to NULL
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- After gather, reltuples should reflect the row count
|
SELECT reltuples, relpages
|
FROM pg_class
|
WHERE relname = 'dbms_stats_test';
|
reltuples | relpages
|
-----------+----------
|
3 | 1
|
(1 row)
|
|
-- pg_statistic should have entries for the table's columns
|
SELECT count(*) > 0 AS has_column_stats
|
FROM pg_statistic
|
WHERE starelid = 'dbms_stats_test'::regclass;
|
has_column_stats
|
------------------
|
t
|
(1 row)
|
|
-- Test with explicit schema (ownname)
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
DROP TABLE dbms_stats_test;
|
-- == gather_index_stats tests ==
|
-- Test basic lifecycle: create table + index, insert data, gather index stats, verify
|
CREATE TABLE dbms_stats_idx_test(id INT, val TEXT);
|
CREATE INDEX dbms_stats_idx_test_idx ON dbms_stats_idx_test(id);
|
-- Initially no index statistics: reltuples should be 0 or negative (unknown)
|
SELECT reltuples, relpages
|
FROM pg_class
|
WHERE relname = 'dbms_stats_idx_test_idx';
|
reltuples | relpages
|
-----------+----------
|
0 | 1
|
(1 row)
|
|
INSERT INTO dbms_stats_idx_test VALUES (1, 'a'), (2, 'b'), (3, 'c');
|
-- Call gather_index_stats with ownname and indname
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_idx_test_idx');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
dbms_stats_gather_index_stats
|
-------------------------------
|
|
(1 row)
|
|
-- After gather, index reltuples and relpages should reflect the data
|
SELECT reltuples, relpages
|
FROM pg_class
|
WHERE relname = 'dbms_stats_idx_test_idx';
|
reltuples | relpages
|
-----------+----------
|
3 | 2
|
(1 row)
|
|
DROP TABLE dbms_stats_idx_test;
|
-- == privilege check tests ==
|
-- Ensure that an unprivileged role cannot gather stats (permission denied)
|
CREATE TABLE dbms_stats_priv_test(id INT);
|
INSERT INTO dbms_stats_priv_test VALUES (1), (2);
|
CREATE INDEX dbms_stats_priv_test_idx ON dbms_stats_priv_test(id);
|
CREATE ROLE dbms_stats_priv_test_user;
|
GRANT USAGE ON SCHEMA public TO dbms_stats_priv_test_user; -- not enough to analyze
|
SET ROLE dbms_stats_priv_test_user;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_priv_test');
|
ERROR: TABLE "public"."dbms_stats_priv_test" does not exist or insufficient privileges
|
RESET ROLE;
|
SET ROLE dbms_stats_priv_test_user;
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_priv_test_idx');
|
ERROR: INDEX "public"."dbms_stats_priv_test_idx" does not exist or insufficient privileges
|
RESET ROLE;
|
DROP TABLE dbms_stats_priv_test;
|
REVOKE ALL ON SCHEMA public FROM dbms_stats_priv_test_user;
|
DROP ROLE dbms_stats_priv_test_user;
|
-- == gather_schema_stats tests ==
|
-- Test basic lifecycle: two tables + a materialized view in public, gather stats, verify
|
CREATE TABLE dbms_stats_schema_test_1(id INT, val TEXT);
|
CREATE INDEX dbms_stats_schema_test_1_idx ON dbms_stats_schema_test_1(id);
|
CREATE TABLE dbms_stats_schema_test_2(id INT, val TEXT);
|
INSERT INTO dbms_stats_schema_test_1 VALUES (1, 'a'), (2, 'b'), (3, 'c');
|
INSERT INTO dbms_stats_schema_test_2 VALUES (1, 'x'), (2, 'y'), (3, 'z'), (4, 'w');
|
CREATE MATERIALIZED VIEW dbms_stats_schema_test_mv AS SELECT id, val FROM dbms_stats_schema_test_1;
|
-- Initially no statistics: reltuples should be -1 (unknown) for the tables
|
SELECT relname, reltuples
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_schema_test_1', 'dbms_stats_schema_test_2', 'dbms_stats_schema_test_mv')
|
ORDER BY relname;
|
relname | reltuples
|
---------------------------+-----------
|
dbms_stats_schema_test_1 | -1
|
dbms_stats_schema_test_2 | -1
|
dbms_stats_schema_test_mv | -1
|
(3 rows)
|
|
-- Call with explicit schema
|
SELECT dbms_stats_gather_schema_stats('public');
|
dbms_stats_gather_schema_stats
|
--------------------------------
|
|
(1 row)
|
|
-- After gather, reltuples should reflect the row counts for tables, index, and matview
|
SELECT relname, reltuples
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_schema_test_1', 'dbms_stats_schema_test_2',
|
'dbms_stats_schema_test_mv', 'dbms_stats_schema_test_1_idx')
|
ORDER BY relname;
|
relname | reltuples
|
------------------------------+-----------
|
dbms_stats_schema_test_1 | 3
|
dbms_stats_schema_test_1_idx | 3
|
dbms_stats_schema_test_2 | 4
|
dbms_stats_schema_test_mv | 3
|
(4 rows)
|
|
-- Call with NULL ownname: should target the current session schema (public)
|
SELECT dbms_stats_gather_schema_stats(NULL);
|
dbms_stats_gather_schema_stats
|
--------------------------------
|
|
(1 row)
|
|
-- Empty schema: completes without error and returns NULL
|
CREATE SCHEMA dbms_stats_empty_schema;
|
SELECT dbms_stats_gather_schema_stats('dbms_stats_empty_schema');
|
dbms_stats_gather_schema_stats
|
--------------------------------
|
|
(1 row)
|
|
DROP SCHEMA dbms_stats_empty_schema;
|
DROP MATERIALIZED VIEW dbms_stats_schema_test_mv;
|
DROP TABLE dbms_stats_schema_test_1;
|
DROP TABLE dbms_stats_schema_test_2;
|
-- Schema privilege check: a role without USAGE on a schema receives an error
|
CREATE SCHEMA dbms_stats_secured_schema;
|
CREATE ROLE dbms_stats_schema_priv_user;
|
GRANT USAGE ON SCHEMA public TO dbms_stats_schema_priv_user;
|
SET ROLE dbms_stats_schema_priv_user;
|
SELECT dbms_stats_gather_schema_stats('dbms_stats_secured_schema');
|
ERROR: Schema "dbms_stats_secured_schema" does not exist or insufficient privileges
|
RESET ROLE;
|
DROP SCHEMA dbms_stats_secured_schema;
|
REVOKE ALL ON SCHEMA public FROM dbms_stats_schema_priv_user;
|
DROP ROLE dbms_stats_schema_priv_user;
|
-- == get_table_stats tests ==
|
-- Test reading table-level statistics via the three Oracle overloads
|
CREATE TABLE dbms_stats_get_test(id INT, val TEXT);
|
INSERT INTO dbms_stats_get_test VALUES (1, 'a'), (2, 'b'), (3, 'c');
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_get_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Overload 1: basic metrics (numrows, numblks, avgrlen)
|
SELECT numrows, numblks, avgrlen
|
FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_get_test');
|
numrows | numblks | avgrlen
|
---------+---------+---------
|
3 | 1 | 2731
|
(1 row)
|
|
-- Overload 2: In-Memory metrics are always NULL
|
SELECT numrows, numblks, avgrlen,
|
im_imcu_count,
|
im_block_count,
|
scanrate
|
FROM dbms_stats_get_table_stats_ovr2('public', 'dbms_stats_get_test');
|
numrows | numblks | avgrlen | im_imcu_count | im_block_count | scanrate
|
---------+---------+---------+---------------+----------------+----------
|
3 | 1 | 2731 | | |
|
(1 row)
|
|
-- Overload 3: cache metrics are always NULL
|
SELECT numrows, numblks, avgrlen,
|
cachedblk,
|
cachehit
|
FROM dbms_stats_get_table_stats_ovr3('public', 'dbms_stats_get_test');
|
NOTICE: User-defined statistics are not currently supported in this version
|
numrows | numblks | avgrlen | cachedblk | cachehit
|
---------+---------+---------+-----------+----------
|
3 | 1 | 2731 | |
|
(1 row)
|
|
-- Table without gathered statistics: numrows and avgrlen are 0
|
CREATE TABLE dbms_stats_get_empty(id INT);
|
SELECT numrows, numblks, avgrlen
|
FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_get_empty');
|
numrows | numblks | avgrlen
|
---------+---------+---------
|
0 | 0 | 0
|
(1 row)
|
|
-- Missing table raises an error
|
SELECT * FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_no_such_table');
|
ERROR: TABLE "public"."dbms_stats_no_such_table" does not exist or insufficient privileges
|
DROP TABLE dbms_stats_get_empty;
|
DROP TABLE dbms_stats_get_test;
|
-- Privilege check: a role without privileges on the table receives an error
|
CREATE TABLE dbms_stats_get_priv(id INT);
|
INSERT INTO dbms_stats_get_priv VALUES (1);
|
CREATE ROLE dbms_stats_get_priv_user;
|
GRANT USAGE ON SCHEMA public TO dbms_stats_get_priv_user;
|
SET ROLE dbms_stats_get_priv_user;
|
SELECT * FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_get_priv');
|
ERROR: TABLE "public"."dbms_stats_get_priv" does not exist or insufficient privileges
|
RESET ROLE;
|
DROP TABLE dbms_stats_get_priv;
|
REVOKE ALL ON SCHEMA public FROM dbms_stats_get_priv_user;
|
DROP ROLE dbms_stats_get_priv_user;
|
-- == get_column_stats tests ==
|
-- Test reading column-level statistics via the two Oracle overloads
|
CREATE TABLE dbms_stats_col_test(id INT, cat INT, nullable TEXT);
|
INSERT INTO dbms_stats_col_test
|
SELECT g, (g % 5) + 1, CASE WHEN g % 2 = 0 THEN NULL ELSE 'x' END
|
FROM generate_series(1, 100) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_col_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Overload 1: scalar metrics for a low-cardinality column (cat: 5 distinct, no nulls)
|
SELECT distcnt, density, nullcnt, avgclen
|
FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'cat');
|
distcnt | density | nullcnt | avgclen
|
---------+---------+---------+---------
|
5 | 0.2 | 0 | 4
|
(1 row)
|
|
-- Overload 1: nullable column (1 distinct non-null value, half the rows NULL)
|
SELECT distcnt, nullcnt
|
FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'nullable');
|
distcnt | nullcnt
|
---------+---------
|
1 | 50
|
(1 row)
|
|
-- Overload 1: unique column (negative stadistinct decoded into a row count)
|
SELECT distcnt AS distcnt_is_rowcount
|
FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'id');
|
distcnt_is_rowcount
|
---------------------
|
100
|
(1 row)
|
|
-- Overload 1: srec reflects the histogram for a high-cardinality column
|
SELECT srec
|
FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'id');
|
srec
|
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
{"min": "1", "max": "100", "histogram": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100"]}
|
(1 row)
|
|
-- Overload 1: no histogram (MCV covers all values) -> srec is NULL
|
SELECT srec
|
FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'cat');
|
srec
|
------
|
|
(1 row)
|
|
-- Overload 2: user-defined statistics triple is always NULL
|
SELECT ext_stats,
|
stattypown,
|
stattypname
|
FROM dbms_stats_get_column_stats_ovr2('public', 'dbms_stats_col_test', 'cat');
|
ext_stats | stattypown | stattypname
|
-----------+------------+-------------
|
| |
|
(1 row)
|
|
-- Never-analyzed column raises an ORA-20000-style error
|
CREATE TABLE dbms_stats_col_empty(id INT);
|
SELECT * FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_empty', 'id');
|
ERROR: No statistics have been stored for requested object
|
-- Missing column raises an error
|
SELECT * FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'no_such_col');
|
ERROR: Unable to get values for column "no_such_col"
|
DROP TABLE dbms_stats_col_empty;
|
-- Privilege check: a role without privileges on the table receives an error
|
CREATE ROLE dbms_stats_col_priv_user;
|
GRANT USAGE ON SCHEMA public TO dbms_stats_col_priv_user;
|
SET ROLE dbms_stats_col_priv_user;
|
SELECT * FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'cat');
|
ERROR: TABLE "public"."dbms_stats_col_test" does not exist or insufficient privileges
|
RESET ROLE;
|
DROP TABLE dbms_stats_col_test;
|
REVOKE ALL ON SCHEMA public FROM dbms_stats_col_priv_user;
|
DROP ROLE dbms_stats_col_priv_user;
|
-- == partition statistics tests ==
|
-- Gather and read statistics for one partition without touching its siblings
|
CREATE TABLE dbms_stats_part_test(id INT, val TEXT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_part_test_p1 PARTITION OF dbms_stats_part_test
|
FOR VALUES FROM (1) TO (101);
|
CREATE TABLE dbms_stats_part_test_p2 PARTITION OF dbms_stats_part_test
|
FOR VALUES FROM (101) TO (201);
|
INSERT INTO dbms_stats_part_test
|
SELECT g, 'x' FROM generate_series(1, 100) g;
|
INSERT INTO dbms_stats_part_test
|
SELECT g, 'y' FROM generate_series(101, 200) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_part_test',
|
'dbms_stats_part_test_p1');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Only the requested partition gained statistics
|
SELECT relname, reltuples
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_part_test',
|
'dbms_stats_part_test_p1',
|
'dbms_stats_part_test_p2')
|
ORDER BY relname;
|
relname | reltuples
|
-------------------------+-----------
|
dbms_stats_part_test | -1
|
dbms_stats_part_test_p1 | 100
|
dbms_stats_part_test_p2 | -1
|
(3 rows)
|
|
-- Overload 1 reads the requested partition, not the global table
|
SELECT (numrows)::int AS numrows,
|
numblks,
|
avgrlen,
|
(numrows)::int = (SELECT reltuples::int FROM pg_class
|
WHERE relname = 'dbms_stats_part_test_p1')
|
AS numrows_matches,
|
numblks = (SELECT relpages FROM pg_class
|
WHERE relname = 'dbms_stats_part_test_p1')
|
AS numblks_matches,
|
avgrlen = trunc(avgrlen) AS avgrlen_is_integer
|
FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_part_test',
|
'dbms_stats_part_test_p1');
|
numrows | numblks | avgrlen | numrows_matches | numblks_matches | avgrlen_is_integer
|
---------+---------+---------+-----------------+-----------------+--------------------
|
100 | 1 | 82 | t | t | t
|
(1 row)
|
|
-- Overloads 2 and 3 use the same partition target
|
SELECT (numrows)::int AS numrows,
|
avgrlen,
|
im_imcu_count,
|
im_block_count,
|
scanrate
|
FROM dbms_stats_get_table_stats_ovr2('public', 'dbms_stats_part_test',
|
'dbms_stats_part_test_p1');
|
numrows | avgrlen | im_imcu_count | im_block_count | scanrate
|
---------+---------+---------------+----------------+----------
|
100 | 82 | | |
|
(1 row)
|
|
SELECT (numrows)::int AS numrows,
|
avgrlen,
|
cachedblk,
|
cachehit
|
FROM dbms_stats_get_table_stats_ovr3('public', 'dbms_stats_part_test',
|
'dbms_stats_part_test_p1');
|
NOTICE: User-defined statistics are not currently supported in this version
|
numrows | avgrlen | cachedblk | cachehit
|
---------+---------+-----------+----------
|
100 | 82 | |
|
(1 row)
|
|
-- Column statistics are read from the requested partition
|
SELECT distcnt,
|
nullcnt,
|
avgclen,
|
distcnt = (SELECT count(*) FROM dbms_stats_part_test_p1)
|
AS distcnt_matches
|
FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_part_test',
|
'id', 'dbms_stats_part_test_p1');
|
distcnt | nullcnt | avgclen | distcnt_matches
|
---------+---------+---------+-----------------
|
100 | 0 | 4 | t
|
(1 row)
|
|
-- Global gathering still covers the whole table and differs from one partition
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_part_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT (numrows)::int AS partition_numrows,
|
(SELECT reltuples::int FROM pg_class
|
WHERE relname = 'dbms_stats_part_test') AS global_numrows
|
FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_part_test',
|
'dbms_stats_part_test_p1');
|
partition_numrows | global_numrows
|
-------------------+----------------
|
100 | 200
|
(1 row)
|
|
-- == subpartition statistics tests ==
|
-- Nested partition names resolve to the matching subpartition
|
CREATE TABLE dbms_stats_subpart_test(id INT, val INT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_subpart_test_p1 PARTITION OF dbms_stats_subpart_test
|
FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
|
CREATE TABLE dbms_stats_subpart_test_p1_s1 PARTITION OF dbms_stats_subpart_test_p1
|
FOR VALUES FROM (1) TO (51);
|
CREATE TABLE dbms_stats_subpart_test_p1_s2 PARTITION OF dbms_stats_subpart_test_p1
|
FOR VALUES FROM (51) TO (101);
|
INSERT INTO dbms_stats_subpart_test
|
SELECT g, g FROM generate_series(1, 50) g;
|
INSERT INTO dbms_stats_subpart_test
|
SELECT g, g FROM generate_series(51, 100) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_subpart_test',
|
'dbms_stats_subpart_test_p1_s1');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_subpart_test_p1_s1',
|
'dbms_stats_subpart_test_p1_s2')
|
ORDER BY relname;
|
relname | reltuples
|
-------------------------------+-----------
|
dbms_stats_subpart_test_p1_s1 | 50
|
dbms_stats_subpart_test_p1_s2 | -1
|
(2 rows)
|
|
SELECT (numrows)::int AS subpartition_numrows, avgrlen
|
FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_subpart_test',
|
'dbms_stats_subpart_test_p1_s1');
|
subpartition_numrows | avgrlen
|
----------------------+---------
|
50 | 164
|
(1 row)
|
|
SELECT distcnt
|
FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_subpart_test',
|
'id',
|
'dbms_stats_subpart_test_p1_s1');
|
distcnt
|
---------
|
50
|
(1 row)
|
|
-- Gathering an intermediate partition follows ANALYZE recursion
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_subpart_test',
|
'dbms_stats_subpart_test_p1');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_subpart_test_p1_s1',
|
'dbms_stats_subpart_test_p1_s2')
|
ORDER BY relname;
|
relname | reltuples
|
-------------------------------+-----------
|
dbms_stats_subpart_test_p1_s1 | 50
|
dbms_stats_subpart_test_p1_s2 | 50
|
(2 rows)
|
|
-- == partition error handling ==
|
CREATE TABLE dbms_stats_part_plain(id INT);
|
INSERT INTO dbms_stats_part_plain VALUES (1);
|
-- Missing partition
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_part_test',
|
'dbms_stats_no_such_partition');
|
ERROR: dbms_stats_no_such_partition invalid partition name
|
SELECT * FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_part_test',
|
'dbms_stats_no_such_partition');
|
ERROR: dbms_stats_no_such_partition invalid partition name
|
-- Partition of another table
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_part_test',
|
'dbms_stats_subpart_test_p1');
|
ERROR: dbms_stats_subpart_test_p1 invalid partition name
|
SELECT * FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_part_test',
|
'dbms_stats_subpart_test_p1');
|
ERROR: dbms_stats_subpart_test_p1 invalid partition name
|
SELECT * FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_part_test',
|
'id',
|
'dbms_stats_subpart_test_p1');
|
ERROR: dbms_stats_subpart_test_p1 invalid partition name
|
-- Partition name on a non-partitioned table
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_part_plain',
|
'dbms_stats_part_test_p1');
|
ERROR: dbms_stats_part_test_p1 invalid partition name
|
SELECT * FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_part_plain',
|
'dbms_stats_part_test_p1');
|
ERROR: dbms_stats_part_test_p1 invalid partition name
|
DROP TABLE dbms_stats_part_plain;
|
DROP TABLE dbms_stats_subpart_test;
|
DROP TABLE dbms_stats_part_test;
|
-- == granularity tests: ALL, AUTO, DEFAULT ==
|
-- AUTO, ALL, and DEFAULT all mean "native whole-table ANALYZE": PostgreSQL
|
-- recursively analyzes the parent, every partition, and every subpartition.
|
CREATE TABLE dbms_stats_gran_all(id INT, val TEXT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_gran_all_p1 PARTITION OF dbms_stats_gran_all
|
FOR VALUES FROM (1) TO (101);
|
CREATE TABLE dbms_stats_gran_all_p2 PARTITION OF dbms_stats_gran_all
|
FOR VALUES FROM (101) TO (201);
|
INSERT INTO dbms_stats_gran_all SELECT g, 'x' FROM generate_series(1, 100) g;
|
INSERT INTO dbms_stats_gran_all SELECT g, 'y' FROM generate_series(101, 200) g;
|
-- Initial layout: 100 rows in p1 and 100 in p2. ALL analyzes the parent
|
-- recursively, so the root becomes 200 and both partitions become 100.
|
-- Every analyzed relation has one data page (relpages = 1). The partitioned
|
-- parent has relpages = -1 because it has no storage; ANALYZE only maintains
|
-- its aggregated reltuples.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_all',
|
granularity => 'ALL');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_all',
|
'dbms_stats_gran_all_p1',
|
'dbms_stats_gran_all_p2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
------------------------+-----------+----------
|
dbms_stats_gran_all | 200 | -1
|
dbms_stats_gran_all_p1 | 100 | 1
|
dbms_stats_gran_all_p2 | 100 | 1
|
(3 rows)
|
|
-- Add 50 rows to p1 and 25 to p2. AUTO must behave like ALL and refresh the
|
-- whole hierarchy: root becomes 275, p1 150, p2 125.
|
INSERT INTO dbms_stats_gran_all SELECT g, 'z' FROM generate_series(1, 50) g;
|
INSERT INTO dbms_stats_gran_all SELECT g, 'w' FROM generate_series(101, 125) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_all',
|
granularity => 'AUTO');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_all',
|
'dbms_stats_gran_all_p1',
|
'dbms_stats_gran_all_p2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
------------------------+-----------+----------
|
dbms_stats_gran_all | 275 | -1
|
dbms_stats_gran_all_p1 | 150 | 1
|
dbms_stats_gran_all_p2 | 125 | 1
|
(3 rows)
|
|
-- Add 10 rows to p1 and 5 to p2. DEFAULT is an alias for AUTO/ALL:
|
-- root becomes 290, p1 160, p2 130.
|
INSERT INTO dbms_stats_gran_all SELECT g, 'v' FROM generate_series(1, 10) g;
|
INSERT INTO dbms_stats_gran_all SELECT g, 'u' FROM generate_series(101, 105) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_all',
|
granularity => 'DEFAULT');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_all',
|
'dbms_stats_gran_all_p1',
|
'dbms_stats_gran_all_p2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
------------------------+-----------+----------
|
dbms_stats_gran_all | 290 | -1
|
dbms_stats_gran_all_p1 | 160 | 1
|
dbms_stats_gran_all_p2 | 130 | 1
|
(3 rows)
|
|
-- == granularity tests: PARTITION ==
|
-- PARTITION without partname selects only the direct partitions. The parent's
|
-- global statistics must stay untouched, so the root remains -1.
|
CREATE TABLE dbms_stats_gran_part(id INT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_gran_part_p1 PARTITION OF dbms_stats_gran_part
|
FOR VALUES FROM (1) TO (101);
|
CREATE TABLE dbms_stats_gran_part_p2 PARTITION OF dbms_stats_gran_part
|
FOR VALUES FROM (101) TO (201);
|
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(1, 100) g;
|
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(101, 200) g;
|
-- Initial layout: p1=100 rows, p2=100 rows. The gather analyzes p1 and p2
|
-- only: their reltuples become 100, while the root stays -1.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_part',
|
granularity => 'PARTITION');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_part',
|
'dbms_stats_gran_part_p1',
|
'dbms_stats_gran_part_p2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
-------------------------+-----------+----------
|
dbms_stats_gran_part | -1 | 0
|
dbms_stats_gran_part_p1 | 100 | 1
|
dbms_stats_gran_part_p2 | 100 | 1
|
(3 rows)
|
|
-- Add 50 rows to p1 and 20 to p2. With partname, PARTITION targets only the
|
-- named partition: p1 becomes 150, p2 keeps its previous 100, root stays -1.
|
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(1, 50) g;
|
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(101, 120) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_part',
|
granularity => 'PARTITION',
|
partname => 'dbms_stats_gran_part_p1');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_part',
|
'dbms_stats_gran_part_p1',
|
'dbms_stats_gran_part_p2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
-------------------------+-----------+----------
|
dbms_stats_gran_part | -1 | 0
|
dbms_stats_gran_part_p1 | 150 | 1
|
dbms_stats_gran_part_p2 | 100 | 1
|
(3 rows)
|
|
-- Add 10 rows to both partitions. Lowercase 'partition' is accepted and,
|
-- without partname, refreshes both direct partitions: p1=160, p2=130, root=-1.
|
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(1, 10) g;
|
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(101, 110) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_part',
|
granularity => 'partition');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_part',
|
'dbms_stats_gran_part_p1',
|
'dbms_stats_gran_part_p2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
-------------------------+-----------+----------
|
dbms_stats_gran_part | -1 | 0
|
dbms_stats_gran_part_p1 | 160 | 1
|
dbms_stats_gran_part_p2 | 130 | 1
|
(3 rows)
|
|
-- == granularity tests: SUBPARTITION without subpartitions ==
|
-- This table has two direct partitions but no second-level subpartitions.
|
-- SUBPARTITION therefore resolves to an empty target list: the procedure
|
-- succeeds without running ANALYZE, so every reltuples stays -1 and relpages 0.
|
CREATE TABLE dbms_stats_gran_nosub(id INT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_gran_nosub_p1 PARTITION OF dbms_stats_gran_nosub
|
FOR VALUES FROM (1) TO (101);
|
CREATE TABLE dbms_stats_gran_nosub_p2 PARTITION OF dbms_stats_gran_nosub
|
FOR VALUES FROM (101) TO (201);
|
INSERT INTO dbms_stats_gran_nosub SELECT g FROM generate_series(1, 200) g;
|
-- The call is intentionally a no-op: no target exists at the subpartition level.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_nosub',
|
granularity => 'SUBPARTITION');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_nosub',
|
'dbms_stats_gran_nosub_p1',
|
'dbms_stats_gran_nosub_p2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
--------------------------+-----------+----------
|
dbms_stats_gran_nosub | -1 | 0
|
dbms_stats_gran_nosub_p1 | -1 | 0
|
dbms_stats_gran_nosub_p2 | -1 | 0
|
(3 rows)
|
|
-- == granularity tests: SUBPARTITION ==
|
-- Composite table: p1/p2 each contain two leaf subpartitions. The inserts put
|
-- 25 rows in every leaf, so p1/p2 hold 50 rows each and the root holds 100.
|
-- SUBPARTITION without partname analyzes only the second-level leaves:
|
-- root, p1, and p2 stay -1; all four leaves become 25.
|
CREATE TABLE dbms_stats_gran_sub(id INT, val INT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_gran_sub_p1 PARTITION OF dbms_stats_gran_sub
|
FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
|
CREATE TABLE dbms_stats_gran_sub_p1_s1 PARTITION OF dbms_stats_gran_sub_p1
|
FOR VALUES FROM (1) TO (51);
|
CREATE TABLE dbms_stats_gran_sub_p1_s2 PARTITION OF dbms_stats_gran_sub_p1
|
FOR VALUES FROM (51) TO (101);
|
CREATE TABLE dbms_stats_gran_sub_p2 PARTITION OF dbms_stats_gran_sub
|
FOR VALUES FROM (101) TO (201) PARTITION BY RANGE (val);
|
CREATE TABLE dbms_stats_gran_sub_p2_s1 PARTITION OF dbms_stats_gran_sub_p2
|
FOR VALUES FROM (1) TO (51);
|
CREATE TABLE dbms_stats_gran_sub_p2_s2 PARTITION OF dbms_stats_gran_sub_p2
|
FOR VALUES FROM (51) TO (101);
|
INSERT INTO dbms_stats_gran_sub SELECT g, g FROM generate_series(1, 25) g;
|
INSERT INTO dbms_stats_gran_sub SELECT g, g + 50 FROM generate_series(26, 50) g;
|
INSERT INTO dbms_stats_gran_sub SELECT g, g - 100 FROM generate_series(101, 125) g;
|
INSERT INTO dbms_stats_gran_sub SELECT g, g - 50 FROM generate_series(126, 150) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_sub',
|
granularity => 'SUBPARTITION');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_sub',
|
'dbms_stats_gran_sub_p1',
|
'dbms_stats_gran_sub_p2',
|
'dbms_stats_gran_sub_p1_s1',
|
'dbms_stats_gran_sub_p1_s2',
|
'dbms_stats_gran_sub_p2_s1',
|
'dbms_stats_gran_sub_p2_s2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
---------------------------+-----------+----------
|
dbms_stats_gran_sub | -1 | 0
|
dbms_stats_gran_sub_p1 | -1 | 0
|
dbms_stats_gran_sub_p1_s1 | 25 | 1
|
dbms_stats_gran_sub_p1_s2 | 25 | 1
|
dbms_stats_gran_sub_p2 | -1 | 0
|
dbms_stats_gran_sub_p2_s1 | 25 | 1
|
dbms_stats_gran_sub_p2_s2 | 25 | 1
|
(7 rows)
|
|
-- Add 5 rows to p1's first leaf and 5 to p2's first leaf. SUBPARTITION with
|
-- partname p1 targets only subpartitions of p1: p1_s1 becomes 30, p1_s2 stays
|
-- 25, and both p2 leaves stay 25. p1 and the root stay unanalyzed.
|
INSERT INTO dbms_stats_gran_sub SELECT g, g FROM generate_series(1, 5) g;
|
INSERT INTO dbms_stats_gran_sub SELECT g, g - 100 FROM generate_series(101, 105) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_sub',
|
granularity => 'SUBPARTITION',
|
partname => 'dbms_stats_gran_sub_p1');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_sub',
|
'dbms_stats_gran_sub_p1',
|
'dbms_stats_gran_sub_p2',
|
'dbms_stats_gran_sub_p1_s1',
|
'dbms_stats_gran_sub_p1_s2',
|
'dbms_stats_gran_sub_p2_s1',
|
'dbms_stats_gran_sub_p2_s2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
---------------------------+-----------+----------
|
dbms_stats_gran_sub | -1 | 0
|
dbms_stats_gran_sub_p1 | -1 | 0
|
dbms_stats_gran_sub_p1_s1 | 30 | 1
|
dbms_stats_gran_sub_p1_s2 | 25 | 1
|
dbms_stats_gran_sub_p2 | -1 | 0
|
dbms_stats_gran_sub_p2_s1 | 25 | 1
|
dbms_stats_gran_sub_p2_s2 | 25 | 1
|
(7 rows)
|
|
-- == granularity tests: GLOBAL AND PARTITION ==
|
-- Without partname this level analyzes the whole table, so global and
|
-- partition statistics are refreshed together.
|
CREATE TABLE dbms_stats_gran_gp(id INT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_gran_gp_p1 PARTITION OF dbms_stats_gran_gp
|
FOR VALUES FROM (1) TO (101);
|
CREATE TABLE dbms_stats_gran_gp_p2 PARTITION OF dbms_stats_gran_gp
|
FOR VALUES FROM (101) TO (201);
|
INSERT INTO dbms_stats_gran_gp SELECT g FROM generate_series(1, 100) g;
|
INSERT INTO dbms_stats_gran_gp SELECT g FROM generate_series(101, 200) g;
|
-- Initial layout: 100 rows per partition. Result: root=200, p1=100, p2=100.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_gp',
|
granularity => 'GLOBAL AND PARTITION');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_gp',
|
'dbms_stats_gran_gp_p1',
|
'dbms_stats_gran_gp_p2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
-----------------------+-----------+----------
|
dbms_stats_gran_gp | 200 | -1
|
dbms_stats_gran_gp_p1 | 100 | 1
|
dbms_stats_gran_gp_p2 | 100 | 1
|
(3 rows)
|
|
-- Add 30 rows to p1 and 40 to p2. With partname the behavior is PARTITION:
|
-- only p1 is analyzed, so p1=130; p2 keeps 100 and the root keeps 200.
|
INSERT INTO dbms_stats_gran_gp SELECT g FROM generate_series(1, 30) g;
|
INSERT INTO dbms_stats_gran_gp SELECT g FROM generate_series(101, 140) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_gp',
|
granularity => 'GLOBAL AND PARTITION',
|
partname => 'dbms_stats_gran_gp_p1');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_gp',
|
'dbms_stats_gran_gp_p1',
|
'dbms_stats_gran_gp_p2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
-----------------------+-----------+----------
|
dbms_stats_gran_gp | 200 | -1
|
dbms_stats_gran_gp_p1 | 130 | 1
|
dbms_stats_gran_gp_p2 | 100 | 1
|
(3 rows)
|
|
-- == granularity tests: non-partitioned fallback ==
|
-- Oracle ignores granularity for non-partitioned tables, so every supported
|
-- value degrades to a normal whole-table ANALYZE.
|
CREATE TABLE dbms_stats_gran_plain(id INT);
|
INSERT INTO dbms_stats_gran_plain SELECT g FROM generate_series(1, 10) g;
|
-- PARTITION on a plain table must analyze the table: reltuples=10.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_plain',
|
granularity => 'PARTITION');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT reltuples, relpages FROM pg_class WHERE relname = 'dbms_stats_gran_plain';
|
reltuples | relpages
|
-----------+----------
|
10 | 1
|
(1 row)
|
|
-- Add 5 rows; SUBPARTITION must also analyze the whole table: reltuples=15.
|
INSERT INTO dbms_stats_gran_plain SELECT g FROM generate_series(1, 5) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_plain',
|
granularity => 'SUBPARTITION');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT reltuples, relpages FROM pg_class WHERE relname = 'dbms_stats_gran_plain';
|
reltuples | relpages
|
-----------+----------
|
15 | 1
|
(1 row)
|
|
-- == granularity error handling ==
|
-- Every call in this section must fail before ANALYZE, so the table and its
|
-- partitions stay unanalyzed. The final query proves reltuples are still -1.
|
CREATE TABLE dbms_stats_gran_err(id INT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_gran_err_p1 PARTITION OF dbms_stats_gran_err
|
FOR VALUES FROM (1) TO (101);
|
CREATE TABLE dbms_stats_gran_err_p2 PARTITION OF dbms_stats_gran_err
|
FOR VALUES FROM (101) TO (201);
|
INSERT INTO dbms_stats_gran_err SELECT g FROM generate_series(1, 200) g;
|
-- GLOBAL and APPROX_GLOBAL require core support; an unknown keyword is
|
-- invalid; and a nonexistent partname is rejected by partition resolution.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_err',
|
granularity => 'GLOBAL');
|
ERROR: granularity "GLOBAL" is not supported in this version
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_err',
|
granularity => 'APPROX_GLOBAL');
|
ERROR: granularity "APPROX_GLOBAL" is not supported in this version
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_err',
|
granularity => 'NONSENSE');
|
ERROR: Illegal granularity "NONSENSE": must be AUTO | ALL | GLOBAL | PARTITION | SUBPARTITION | GLOBAL AND PARTITION | APPROX_GLOBAL AND PARTITION
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_err',
|
granularity => 'PARTITION',
|
partname => 'dbms_stats_no_such_partition');
|
ERROR: dbms_stats_no_such_partition invalid partition name
|
-- The role may analyze the parent (MAINTAIN on the parent) but not its
|
-- partitions. PARTITION selects the partitions, so the preflight privilege
|
-- check fails before any target is analyzed.
|
CREATE ROLE dbms_stats_gran_priv_user;
|
GRANT USAGE ON SCHEMA public TO dbms_stats_gran_priv_user;
|
GRANT MAINTAIN ON dbms_stats_gran_err TO dbms_stats_gran_priv_user;
|
SET ROLE dbms_stats_gran_priv_user;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_err',
|
granularity => 'PARTITION');
|
ERROR: TABLE "public"."dbms_stats_gran_err_p1" does not exist or insufficient privileges
|
RESET ROLE;
|
REVOKE ALL ON dbms_stats_gran_err FROM dbms_stats_gran_priv_user;
|
REVOKE ALL ON SCHEMA public FROM dbms_stats_gran_priv_user;
|
DROP ROLE dbms_stats_gran_priv_user;
|
-- Confirm none of the failed calls collected statistics.
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_err',
|
'dbms_stats_gran_err_p1',
|
'dbms_stats_gran_err_p2')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
------------------------+-----------+----------
|
dbms_stats_gran_err | -1 | 0
|
dbms_stats_gran_err_p1 | -1 | 0
|
dbms_stats_gran_err_p2 | -1 | 0
|
(3 rows)
|
|
-- == schema granularity tests ==
|
-- A dedicated schema isolates schema-level gathering from public. plain holds
|
-- 10 rows. comp is composite: p1/p2 each hold 50 rows in two leaves of 25.
|
CREATE SCHEMA dbms_stats_gran_schema;
|
CREATE TABLE dbms_stats_gran_schema.plain(id INT);
|
INSERT INTO dbms_stats_gran_schema.plain SELECT g FROM generate_series(1, 10) g;
|
CREATE TABLE dbms_stats_gran_schema.comp(id INT, val INT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_gran_schema.comp_p1 PARTITION OF dbms_stats_gran_schema.comp
|
FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
|
CREATE TABLE dbms_stats_gran_schema.comp_p1_s1 PARTITION OF dbms_stats_gran_schema.comp_p1
|
FOR VALUES FROM (1) TO (51);
|
CREATE TABLE dbms_stats_gran_schema.comp_p1_s2 PARTITION OF dbms_stats_gran_schema.comp_p1
|
FOR VALUES FROM (51) TO (101);
|
CREATE TABLE dbms_stats_gran_schema.comp_p2 PARTITION OF dbms_stats_gran_schema.comp
|
FOR VALUES FROM (101) TO (201) PARTITION BY RANGE (val);
|
CREATE TABLE dbms_stats_gran_schema.comp_p2_s1 PARTITION OF dbms_stats_gran_schema.comp_p2
|
FOR VALUES FROM (1) TO (51);
|
CREATE TABLE dbms_stats_gran_schema.comp_p2_s2 PARTITION OF dbms_stats_gran_schema.comp_p2
|
FOR VALUES FROM (51) TO (101);
|
INSERT INTO dbms_stats_gran_schema.comp
|
SELECT g, g FROM generate_series(1, 25) g;
|
INSERT INTO dbms_stats_gran_schema.comp
|
SELECT g, g + 50 FROM generate_series(26, 50) g;
|
INSERT INTO dbms_stats_gran_schema.comp
|
SELECT g, g - 100 FROM generate_series(101, 125) g;
|
INSERT INTO dbms_stats_gran_schema.comp
|
SELECT g, g - 50 FROM generate_series(126, 150) g;
|
-- ALL keeps the full relation list, so comp and all descendants are analyzed:
|
-- comp=100, p1/p2=50, every leaf=25, plain=10.
|
SELECT dbms_stats_gather_schema_stats('dbms_stats_gran_schema',
|
granularity => 'ALL');
|
dbms_stats_gather_schema_stats
|
--------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class c
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
WHERE n.nspname = 'dbms_stats_gran_schema'
|
ORDER BY relname;
|
relname | reltuples | relpages
|
------------+-----------+----------
|
comp | 100 | -1
|
comp_p1 | 50 | -1
|
comp_p1_s1 | 25 | 1
|
comp_p1_s2 | 25 | 1
|
comp_p2 | 50 | -1
|
comp_p2_s1 | 25 | 1
|
comp_p2_s2 | 25 | 1
|
plain | 10 | 1
|
(8 rows)
|
|
-- Add 10 rows to comp_p1_s1, 10 to comp_p2_s1, and 10 to plain. PARTITION drops
|
-- the root, analyzes depth-1 partitions and standalone tables: p1/p2=60,
|
-- plain=20, root comp keeps 100. Native recursion into p1/p2 also refreshes
|
-- their leaves, so p1_s1/p2_s1 become 35; this is documented PostgreSQL
|
-- behavior, not a separate target chosen by the procedure.
|
INSERT INTO dbms_stats_gran_schema.comp
|
SELECT g, g FROM generate_series(1, 10) g;
|
INSERT INTO dbms_stats_gran_schema.comp
|
SELECT g, g - 100 FROM generate_series(101, 110) g;
|
INSERT INTO dbms_stats_gran_schema.plain
|
SELECT g FROM generate_series(1, 10) g;
|
SELECT dbms_stats_gather_schema_stats('dbms_stats_gran_schema',
|
granularity => 'PARTITION');
|
dbms_stats_gather_schema_stats
|
--------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class c
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
WHERE n.nspname = 'dbms_stats_gran_schema'
|
ORDER BY relname;
|
relname | reltuples | relpages
|
------------+-----------+----------
|
comp | 100 | -1
|
comp_p1 | 60 | -1
|
comp_p1_s1 | 35 | 1
|
comp_p1_s2 | 25 | 1
|
comp_p2 | 60 | -1
|
comp_p2_s1 | 35 | 1
|
comp_p2_s2 | 25 | 1
|
plain | 20 | 1
|
(8 rows)
|
|
-- Add 5 rows to comp_p1_s2, 5 to comp_p2_s2, and 5 to plain. SUBPARTITION
|
-- analyzes depth-2 leaves and standalone tables: p1_s2/p2_s2=30, plain=25,
|
-- while comp, p1, and p2 keep their previous values.
|
INSERT INTO dbms_stats_gran_schema.comp
|
SELECT g, g + 50 FROM generate_series(26, 30) g;
|
INSERT INTO dbms_stats_gran_schema.comp
|
SELECT g, g - 50 FROM generate_series(126, 130) g;
|
INSERT INTO dbms_stats_gran_schema.plain
|
SELECT g FROM generate_series(1, 5) g;
|
SELECT dbms_stats_gather_schema_stats('dbms_stats_gran_schema',
|
granularity => 'SUBPARTITION');
|
dbms_stats_gather_schema_stats
|
--------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class c
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
WHERE n.nspname = 'dbms_stats_gran_schema'
|
ORDER BY relname;
|
relname | reltuples | relpages
|
------------+-----------+----------
|
comp | 100 | -1
|
comp_p1 | 60 | -1
|
comp_p1_s1 | 35 | 1
|
comp_p1_s2 | 30 | 1
|
comp_p2 | 60 | -1
|
comp_p2_s1 | 35 | 1
|
comp_p2_s2 | 30 | 1
|
plain | 25 | 1
|
(8 rows)
|
|
-- Unsupported granularity must fail before any relation is analyzed; the
|
-- snapshot below must be identical to the previous one.
|
SELECT dbms_stats_gather_schema_stats('dbms_stats_gran_schema',
|
granularity => 'GLOBAL');
|
ERROR: granularity "GLOBAL" is not supported in this version
|
SELECT relname, reltuples, relpages
|
FROM pg_class c
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
WHERE n.nspname = 'dbms_stats_gran_schema'
|
ORDER BY relname;
|
relname | reltuples | relpages
|
------------+-----------+----------
|
comp | 100 | -1
|
comp_p1 | 60 | -1
|
comp_p1_s1 | 35 | 1
|
comp_p1_s2 | 30 | 1
|
comp_p2 | 60 | -1
|
comp_p2_s1 | 35 | 1
|
comp_p2_s2 | 30 | 1
|
plain | 25 | 1
|
(8 rows)
|
|
-- == index granularity and partname tests ==
|
-- Index statistics are gathered by analyzing tables, so all indexes on a
|
-- selected table are refreshed. A partitioned index root reports reltuples=0;
|
-- unanalyzed child indexes also report 0 (not -1).
|
CREATE TABLE dbms_stats_gran_idx(id INT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_gran_idx_p1 PARTITION OF dbms_stats_gran_idx
|
FOR VALUES FROM (1) TO (101);
|
CREATE TABLE dbms_stats_gran_idx_p2 PARTITION OF dbms_stats_gran_idx
|
FOR VALUES FROM (101) TO (201);
|
CREATE INDEX dbms_stats_gran_idx_idx ON dbms_stats_gran_idx(id);
|
INSERT INTO dbms_stats_gran_idx SELECT g FROM generate_series(1, 100) g;
|
INSERT INTO dbms_stats_gran_idx SELECT g FROM generate_series(101, 200) g;
|
-- Nothing has been analyzed yet: tables and partitions are -1, index root 0.
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_idx',
|
'dbms_stats_gran_idx_p1',
|
'dbms_stats_gran_idx_p2',
|
'dbms_stats_gran_idx_idx')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
-------------------------+-----------+----------
|
dbms_stats_gran_idx | -1 | 0
|
dbms_stats_gran_idx_idx | 0 | 0
|
dbms_stats_gran_idx_p1 | -1 | 0
|
dbms_stats_gran_idx_p2 | -1 | 0
|
(4 rows)
|
|
-- partname selects only p1's table. p1 becomes 100 and its child index 100.
|
-- Sibling p2 stays -1, its child index stays 0, and the index root stays 0.
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
|
partname => 'dbms_stats_gran_idx_p1');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
dbms_stats_gather_index_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_idx',
|
'dbms_stats_gran_idx_p1',
|
'dbms_stats_gran_idx_p2',
|
'dbms_stats_gran_idx_idx')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
-------------------------+-----------+----------
|
dbms_stats_gran_idx | -1 | 0
|
dbms_stats_gran_idx_idx | 0 | 0
|
dbms_stats_gran_idx_p1 | 100 | 1
|
dbms_stats_gran_idx_p2 | -1 | 0
|
(4 rows)
|
|
SELECT c.relname, c.reltuples::int AS reltuples
|
FROM pg_class c
|
JOIN pg_inherits i ON i.inhrelid = c.oid
|
WHERE i.inhparent = 'dbms_stats_gran_idx_idx'::regclass
|
ORDER BY c.relname;
|
relname | reltuples
|
-------------------------------+-----------
|
dbms_stats_gran_idx_p1_id_idx | 100
|
dbms_stats_gran_idx_p2_id_idx | 0
|
(2 rows)
|
|
-- Add 50 rows to each partition. Lowercase 'partition' analyzes both direct
|
-- partitions: p1/p2=150 and both child indexes=150. The table root and the
|
-- partitioned index root keep their previous statistics.
|
INSERT INTO dbms_stats_gran_idx SELECT g FROM generate_series(1, 50) g;
|
INSERT INTO dbms_stats_gran_idx SELECT g FROM generate_series(101, 150) g;
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
|
granularity => 'partition');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
dbms_stats_gather_index_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_idx',
|
'dbms_stats_gran_idx_p1',
|
'dbms_stats_gran_idx_p2',
|
'dbms_stats_gran_idx_idx')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
-------------------------+-----------+----------
|
dbms_stats_gran_idx | -1 | 0
|
dbms_stats_gran_idx_idx | 0 | 0
|
dbms_stats_gran_idx_p1 | 150 | 1
|
dbms_stats_gran_idx_p2 | 150 | 1
|
(4 rows)
|
|
SELECT c.relname, c.reltuples::int AS reltuples
|
FROM pg_class c
|
JOIN pg_inherits i ON i.inhrelid = c.oid
|
WHERE i.inhparent = 'dbms_stats_gran_idx_idx'::regclass
|
ORDER BY c.relname;
|
relname | reltuples
|
-------------------------------+-----------
|
dbms_stats_gran_idx_p1_id_idx | 150
|
dbms_stats_gran_idx_p2_id_idx | 150
|
(2 rows)
|
|
-- There are no second-level subpartitions, so SUBPARTITION selects an empty
|
-- target set: the call succeeds without ANALYZE and every statistic below is
|
-- unchanged from the previous result.
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
|
granularity => 'SUBPARTITION');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
dbms_stats_gather_index_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT relname, reltuples, relpages
|
FROM pg_class
|
WHERE relname IN ('dbms_stats_gran_idx',
|
'dbms_stats_gran_idx_p1',
|
'dbms_stats_gran_idx_p2',
|
'dbms_stats_gran_idx_idx')
|
ORDER BY relname;
|
relname | reltuples | relpages
|
-------------------------+-----------+----------
|
dbms_stats_gran_idx | -1 | 0
|
dbms_stats_gran_idx_idx | 0 | 0
|
dbms_stats_gran_idx_p1 | 150 | 1
|
dbms_stats_gran_idx_p2 | 150 | 1
|
(4 rows)
|
|
-- Composite table with a partitioned index. Each leaf holds 25 rows.
|
CREATE TABLE dbms_stats_gran_idx_sub(id INT, val INT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_gran_idx_sub_p1 PARTITION OF dbms_stats_gran_idx_sub
|
FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
|
CREATE TABLE dbms_stats_gran_idx_sub_p1_s1 PARTITION OF dbms_stats_gran_idx_sub_p1
|
FOR VALUES FROM (1) TO (51);
|
CREATE TABLE dbms_stats_gran_idx_sub_p1_s2 PARTITION OF dbms_stats_gran_idx_sub_p1
|
FOR VALUES FROM (51) TO (101);
|
CREATE TABLE dbms_stats_gran_idx_sub_p2 PARTITION OF dbms_stats_gran_idx_sub
|
FOR VALUES FROM (101) TO (201) PARTITION BY RANGE (val);
|
CREATE TABLE dbms_stats_gran_idx_sub_p2_s1 PARTITION OF dbms_stats_gran_idx_sub_p2
|
FOR VALUES FROM (1) TO (51);
|
CREATE TABLE dbms_stats_gran_idx_sub_p2_s2 PARTITION OF dbms_stats_gran_idx_sub_p2
|
FOR VALUES FROM (51) TO (101);
|
CREATE INDEX dbms_stats_gran_idx_sub_idx ON dbms_stats_gran_idx_sub(id);
|
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g FROM generate_series(1, 25) g;
|
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g + 50 FROM generate_series(26, 50) g;
|
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g - 100 FROM generate_series(101, 125) g;
|
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g - 50 FROM generate_series(126, 150) g;
|
-- SUBPARTITION targets only the leaves. Leaf tables and their child indexes
|
-- become 25; the root, p1, p2, and their indexes stay at 0/-1.
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_sub_idx',
|
granularity => 'SUBPARTITION');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
dbms_stats_gather_index_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT c.relname, c.reltuples::int AS reltuples
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_gran_idx_sub',
|
'dbms_stats_gran_idx_sub_p1',
|
'dbms_stats_gran_idx_sub_p2',
|
'dbms_stats_gran_idx_sub_p1_s1',
|
'dbms_stats_gran_idx_sub_p1_s2',
|
'dbms_stats_gran_idx_sub_p2_s1',
|
'dbms_stats_gran_idx_sub_p2_s2')
|
ORDER BY c.relname;
|
relname | reltuples
|
-------------------------------+-----------
|
dbms_stats_gran_idx_sub | -1
|
dbms_stats_gran_idx_sub_p1 | -1
|
dbms_stats_gran_idx_sub_p1_s1 | 25
|
dbms_stats_gran_idx_sub_p1_s2 | 25
|
dbms_stats_gran_idx_sub_p2 | -1
|
dbms_stats_gran_idx_sub_p2_s1 | 25
|
dbms_stats_gran_idx_sub_p2_s2 | 25
|
(7 rows)
|
|
SELECT t.relname AS table_name, c.reltuples::int AS index_tuples
|
FROM pg_class t
|
JOIN pg_index ix ON ix.indrelid = t.oid
|
JOIN pg_class c ON c.oid = ix.indexrelid
|
WHERE t.relname LIKE 'dbms_stats_gran_idx_sub%'
|
ORDER BY t.relname;
|
table_name | index_tuples
|
-------------------------------+--------------
|
dbms_stats_gran_idx_sub | 0
|
dbms_stats_gran_idx_sub_p1 | 0
|
dbms_stats_gran_idx_sub_p1_s1 | 25
|
dbms_stats_gran_idx_sub_p1_s2 | 25
|
dbms_stats_gran_idx_sub_p2 | 0
|
dbms_stats_gran_idx_sub_p2_s1 | 25
|
dbms_stats_gran_idx_sub_p2_s2 | 25
|
(7 rows)
|
|
-- Add 5 rows to sub_p1_s1 and 5 to sub_p2_s1. SUBPARTITION with partname p1
|
-- targets only p1's leaves: p1_s1 and its index become 30; p1_s2, p2 leaves,
|
-- and their indexes stay 25; parent/index-root statistics stay unchanged.
|
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g FROM generate_series(1, 5) g;
|
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g - 100 FROM generate_series(101, 105) g;
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_sub_idx',
|
granularity => 'SUBPARTITION',
|
partname => 'dbms_stats_gran_idx_sub_p1');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
dbms_stats_gather_index_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT c.relname, c.reltuples::int AS reltuples
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_gran_idx_sub',
|
'dbms_stats_gran_idx_sub_p1',
|
'dbms_stats_gran_idx_sub_p2',
|
'dbms_stats_gran_idx_sub_p1_s1',
|
'dbms_stats_gran_idx_sub_p1_s2',
|
'dbms_stats_gran_idx_sub_p2_s1',
|
'dbms_stats_gran_idx_sub_p2_s2')
|
ORDER BY c.relname;
|
relname | reltuples
|
-------------------------------+-----------
|
dbms_stats_gran_idx_sub | -1
|
dbms_stats_gran_idx_sub_p1 | -1
|
dbms_stats_gran_idx_sub_p1_s1 | 30
|
dbms_stats_gran_idx_sub_p1_s2 | 25
|
dbms_stats_gran_idx_sub_p2 | -1
|
dbms_stats_gran_idx_sub_p2_s1 | 25
|
dbms_stats_gran_idx_sub_p2_s2 | 25
|
(7 rows)
|
|
SELECT t.relname AS table_name, c.reltuples::int AS index_tuples
|
FROM pg_class t
|
JOIN pg_index ix ON ix.indrelid = t.oid
|
JOIN pg_class c ON c.oid = ix.indexrelid
|
WHERE t.relname LIKE 'dbms_stats_gran_idx_sub%'
|
ORDER BY t.relname;
|
table_name | index_tuples
|
-------------------------------+--------------
|
dbms_stats_gran_idx_sub | 0
|
dbms_stats_gran_idx_sub_p1 | 0
|
dbms_stats_gran_idx_sub_p1_s1 | 30
|
dbms_stats_gran_idx_sub_p1_s2 | 25
|
dbms_stats_gran_idx_sub_p2 | 0
|
dbms_stats_gran_idx_sub_p2_s1 | 25
|
dbms_stats_gran_idx_sub_p2_s2 | 25
|
(7 rows)
|
|
-- == index granularity error handling ==
|
-- GLOBAL requires core support; missing and foreign partnames are rejected by
|
-- partition resolution. Both must fail before any ANALYZE.
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
|
granularity => 'GLOBAL');
|
ERROR: granularity "GLOBAL" is not supported in this version
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
|
partname => 'dbms_stats_no_such_partition');
|
ERROR: dbms_stats_no_such_partition invalid partition name
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
|
partname => 'dbms_stats_gran_idx_sub_p1');
|
ERROR: dbms_stats_gran_idx_sub_p1 invalid partition name
|
-- A non-partitioned index has no index partitions, so partname is invalid.
|
CREATE TABLE dbms_stats_gran_idx_plain(id INT);
|
CREATE INDEX dbms_stats_gran_idx_plain_idx ON dbms_stats_gran_idx_plain(id);
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_plain_idx',
|
partname => 'dbms_stats_gran_idx_plain');
|
ERROR: INDEX "public"."dbms_stats_gran_idx_plain_idx" is not partitioned
|
-- ON ONLY creates a partitioned index without child indexes. p1 has no child
|
-- index for this index, so the request is rejected before ANALYZE.
|
CREATE TABLE dbms_stats_gran_idx_only(id INT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_gran_idx_only_p1 PARTITION OF dbms_stats_gran_idx_only
|
FOR VALUES FROM (1) TO (101);
|
CREATE INDEX dbms_stats_gran_idx_only_idx ON ONLY dbms_stats_gran_idx_only(id);
|
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_only_idx',
|
partname => 'dbms_stats_gran_idx_only_p1');
|
ERROR: partition "dbms_stats_gran_idx_only_p1" does not belong to INDEX "public"."dbms_stats_gran_idx_only_idx"
|
DROP TABLE dbms_stats_gran_idx_only;
|
DROP TABLE dbms_stats_gran_idx_plain;
|
DROP TABLE dbms_stats_gran_idx_sub;
|
DROP TABLE dbms_stats_gran_idx;
|
DROP SCHEMA dbms_stats_gran_schema CASCADE;
|
NOTICE: drop cascades to 2 other objects
|
DETAIL: drop cascades to table dbms_stats_gran_schema.plain
|
drop cascades to table dbms_stats_gran_schema.comp
|
DROP TABLE dbms_stats_gran_err;
|
DROP TABLE dbms_stats_gran_plain;
|
DROP TABLE dbms_stats_gran_gp;
|
DROP TABLE dbms_stats_gran_sub;
|
DROP TABLE dbms_stats_gran_nosub;
|
DROP TABLE dbms_stats_gran_part;
|
DROP TABLE dbms_stats_gran_all;
|
-- == delete_table_stats tests ==
|
-- PostgreSQL splits Oracle's statistics across two catalogs: table estimates
|
-- (reltuples, relpages) live in pg_class, while column statistics live in
|
-- pg_statistic. The queries below show both, so each cascade flag can be
|
-- verified independently.
|
--
|
-- Data layout: p1 holds ids 1..100, p2 holds ids 101..200, and the partitioned
|
-- index has one child index per partition. A whole-table gather therefore
|
-- gives root reltuples=200, each partition reltuples=100, and each child index
|
-- reltuples=100. Partitioned parents have no storage, so the root table gets
|
-- relpages=-1 and the partitioned index root stays reltuples=0, relpages=0.
|
--
|
-- Deleting table statistics resets reltuples to -1 and relpages to 0 (or -1 for
|
-- a partitioned parent), which is PostgreSQL's "not analyzed" state. Deleting
|
-- column statistics removes pg_statistic rows; deleting index statistics resets
|
-- the index pg_class entry. Every case re-gathers first, so each deletion starts
|
-- from the known values described above.
|
CREATE TABLE dbms_stats_del_test(id INT, val TEXT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_del_test_p1 PARTITION OF dbms_stats_del_test
|
FOR VALUES FROM (1) TO (101);
|
CREATE TABLE dbms_stats_del_test_p2 PARTITION OF dbms_stats_del_test
|
FOR VALUES FROM (101) TO (201);
|
CREATE INDEX dbms_stats_del_test_idx ON dbms_stats_del_test(id);
|
INSERT INTO dbms_stats_del_test
|
SELECT g, 'x' FROM generate_series(1, 100) g;
|
INSERT INTO dbms_stats_del_test
|
SELECT g, 'y' FROM generate_series(101, 200) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Baseline after gather: root=200, p1=100, p2=100. Every relation has two
|
-- pg_statistic rows because both id and val were analyzed.
|
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
|
(SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
|
AS column_stats
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY c.relname;
|
relname | reltuples | relpages | column_stats
|
------------------------+-----------+----------+--------------
|
dbms_stats_del_test | 200 | -1 | 2
|
dbms_stats_del_test_p1 | 100 | 1 | 2
|
dbms_stats_del_test_p2 | 100 | 1 | 2
|
(3 rows)
|
|
-- Baseline index estimates: the partitioned index root is 0/0, while each
|
-- child index reports the 100 rows and 2 index pages of its partition.
|
SELECT t.relname AS table_name, c.relname AS index_name,
|
c.reltuples::int AS reltuples, c.relpages
|
FROM pg_class t
|
JOIN pg_index ix ON ix.indrelid = t.oid
|
JOIN pg_class c ON c.oid = ix.indexrelid
|
WHERE t.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY t.relname, c.relname;
|
table_name | index_name | reltuples | relpages
|
------------------------+-------------------------------+-----------+----------
|
dbms_stats_del_test | dbms_stats_del_test_idx | 0 | 0
|
dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx | 100 | 2
|
dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx | 100 | 2
|
(3 rows)
|
|
-- Case 1: global delete with cascade_parts=false, cascade_columns=false, and
|
-- cascade_indexes=false. Only the root table estimates change: reltuples=-1,
|
-- relpages=-1. Partitions, column statistics, and all indexes stay unchanged.
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
|
cascade_parts => false,
|
cascade_columns => false,
|
cascade_indexes => false,
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
dbms_stats_delete_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- get_table_stats observes the reset global estimates: numrows=0 because
|
-- reltuples is negative, and numblks=-1 is the raw relpages value stored for a
|
-- partitioned parent. avgrlen is 0 because there is no positive row estimate.
|
SELECT numrows, numblks, avgrlen
|
FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_del_test');
|
numrows | numblks | avgrlen
|
---------+---------+---------
|
0 | -1 | 0
|
(1 row)
|
|
-- Only the root row changed. p1/p2 still have reltuples=100 and both column
|
-- statistics, because cascade_parts and cascade_columns are false.
|
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
|
(SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
|
AS column_stats
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY c.relname;
|
relname | reltuples | relpages | column_stats
|
------------------------+-----------+----------+--------------
|
dbms_stats_del_test | -1 | -1 | 2
|
dbms_stats_del_test_p1 | 100 | 1 | 2
|
dbms_stats_del_test_p2 | 100 | 1 | 2
|
(3 rows)
|
|
-- Indexes were not cascaded, so the root index and both child indexes keep
|
-- their pre-delete estimates (root 0/0, children 100/2).
|
SELECT t.relname AS table_name, c.relname AS index_name,
|
c.reltuples::int AS reltuples, c.relpages
|
FROM pg_class t
|
JOIN pg_index ix ON ix.indrelid = t.oid
|
JOIN pg_class c ON c.oid = ix.indexrelid
|
WHERE t.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY t.relname, c.relname;
|
table_name | index_name | reltuples | relpages
|
------------------------+-------------------------------+-----------+----------
|
dbms_stats_del_test | dbms_stats_del_test_idx | 0 | 0
|
dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx | 100 | 2
|
dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx | 100 | 2
|
(3 rows)
|
|
-- Case 2: partname=dbms_stats_del_test_p1 with cascade_parts=true. Oracle
|
-- applies cascade_parts only when partname is NULL, so exactly p1 is targeted.
|
-- p1 table estimates become -1/0; root (200), p2 (100), all column stats, and
|
-- all index estimates remain unchanged.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
|
partname => 'dbms_stats_del_test_p1',
|
cascade_parts => true,
|
cascade_columns => false,
|
cascade_indexes => false,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
dbms_stats_delete_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Only p1 changes: root=200, p1=-1/0, p2=100/1. All column_stats stay 2
|
-- because cascade_columns=false.
|
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
|
(SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
|
AS column_stats
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY c.relname;
|
relname | reltuples | relpages | column_stats
|
------------------------+-----------+----------+--------------
|
dbms_stats_del_test | 200 | -1 | 2
|
dbms_stats_del_test_p1 | -1 | 0 | 2
|
dbms_stats_del_test_p2 | 100 | 1 | 2
|
(3 rows)
|
|
-- cascade_indexes=false keeps the root index at 0/0 and both child indexes at
|
-- 100/2, including p1's child index.
|
SELECT t.relname AS table_name, c.relname AS index_name,
|
c.reltuples::int AS reltuples, c.relpages
|
FROM pg_class t
|
JOIN pg_index ix ON ix.indrelid = t.oid
|
JOIN pg_class c ON c.oid = ix.indexrelid
|
WHERE t.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY t.relname, c.relname;
|
table_name | index_name | reltuples | relpages
|
------------------------+-------------------------------+-----------+----------
|
dbms_stats_del_test | dbms_stats_del_test_idx | 0 | 0
|
dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx | 100 | 2
|
dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx | 100 | 2
|
(3 rows)
|
|
-- Case 3: partname=p1 with cascade_columns=true and cascade_indexes=true.
|
-- The single p1 target loses table estimates (-1/0), all column statistics
|
-- (column_stats=0), and its child index estimates (-1/0). Everything outside
|
-- p1 remains unchanged.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
|
partname => 'dbms_stats_del_test_p1',
|
cascade_columns => true,
|
cascade_indexes => true,
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
dbms_stats_delete_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Expected: root=200 with 2 column stats, p1=-1/0 with 0 column stats,
|
-- p2=100/1 with 2 column stats.
|
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
|
(SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
|
AS column_stats
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY c.relname;
|
relname | reltuples | relpages | column_stats
|
------------------------+-----------+----------+--------------
|
dbms_stats_del_test | 200 | -1 | 2
|
dbms_stats_del_test_p1 | -1 | 0 | 0
|
dbms_stats_del_test_p2 | 100 | 1 | 2
|
(3 rows)
|
|
-- p1's child index is now -1/0; the root index (0/0) and p2 child (100/2)
|
-- were not selected.
|
SELECT t.relname AS table_name, c.relname AS index_name,
|
c.reltuples::int AS reltuples, c.relpages
|
FROM pg_class t
|
JOIN pg_index ix ON ix.indrelid = t.oid
|
JOIN pg_class c ON c.oid = ix.indexrelid
|
WHERE t.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY t.relname, c.relname;
|
table_name | index_name | reltuples | relpages
|
------------------------+-------------------------------+-----------+----------
|
dbms_stats_del_test | dbms_stats_del_test_idx | 0 | 0
|
dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx | -1 | 0
|
dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx | 100 | 2
|
(3 rows)
|
|
-- Case 4: cascade_parts=true, cascade_columns=false, cascade_indexes=false.
|
-- All table relations are reset (root reltuples=-1 with relpages=-1, partitions
|
-- reltuples=-1 with relpages=0), but column statistics stay 2 and indexes keep
|
-- their 100/2 estimates.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
|
cascade_parts => true,
|
cascade_columns => false,
|
cascade_indexes => false,
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
dbms_stats_delete_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Expected: every table reltuples is -1; column_stats is still 2 everywhere.
|
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
|
(SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
|
AS column_stats
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY c.relname;
|
relname | reltuples | relpages | column_stats
|
------------------------+-----------+----------+--------------
|
dbms_stats_del_test | -1 | -1 | 2
|
dbms_stats_del_test_p1 | -1 | 0 | 2
|
dbms_stats_del_test_p2 | -1 | 0 | 2
|
(3 rows)
|
|
-- cascade_indexes=false preserves the root index (0/0) and both children
|
-- (100/2).
|
SELECT t.relname AS table_name, c.relname AS index_name,
|
c.reltuples::int AS reltuples, c.relpages
|
FROM pg_class t
|
JOIN pg_index ix ON ix.indrelid = t.oid
|
JOIN pg_class c ON c.oid = ix.indexrelid
|
WHERE t.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY t.relname, c.relname;
|
table_name | index_name | reltuples | relpages
|
------------------------+-------------------------------+-----------+----------
|
dbms_stats_del_test | dbms_stats_del_test_idx | 0 | 0
|
dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx | 100 | 2
|
dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx | 100 | 2
|
(3 rows)
|
|
-- Case 5: full cascade. Every table relation becomes reltuples=-1 (root
|
-- relpages=-1, partitions 0), all column statistics are removed (0), and all
|
-- index estimates are reset to -1/0.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
|
cascade_parts => true,
|
cascade_columns => true,
|
cascade_indexes => true,
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
dbms_stats_delete_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Expected: all three table relations are -1 with column_stats=0.
|
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
|
(SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
|
AS column_stats
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY c.relname;
|
relname | reltuples | relpages | column_stats
|
------------------------+-----------+----------+--------------
|
dbms_stats_del_test | -1 | -1 | 0
|
dbms_stats_del_test_p1 | -1 | 0 | 0
|
dbms_stats_del_test_p2 | -1 | 0 | 0
|
(3 rows)
|
|
-- The partitioned index root and both child indexes are now -1/0.
|
SELECT t.relname AS table_name, c.relname AS index_name,
|
c.reltuples::int AS reltuples, c.relpages
|
FROM pg_class t
|
JOIN pg_index ix ON ix.indrelid = t.oid
|
JOIN pg_class c ON c.oid = ix.indexrelid
|
WHERE t.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY t.relname, c.relname;
|
table_name | index_name | reltuples | relpages
|
------------------------+-------------------------------+-----------+----------
|
dbms_stats_del_test | dbms_stats_del_test_idx | -1 | 0
|
dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx | -1 | 0
|
dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx | -1 | 0
|
(3 rows)
|
|
-- Cases 6a/6b: stat_category is a comma-separated Oracle category list.
|
-- SYNOPSES has no PostgreSQL source, so it only emits a NOTICE. When it is the
|
-- only category, OBJECT_STATS is absent and the call is a no-op. When it is
|
-- combined with OBJECT_STATS, the NOTICE is still emitted and the deletion runs.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
|
cascade_parts => false,
|
cascade_columns => false,
|
cascade_indexes => false,
|
stat_category => 'SYNOPSES',
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL);
|
NOTICE: stat_category value "SYNOPSES" is not currently supported
|
dbms_stats_delete_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Case 6a expected: root stays 200 and both partitions stay 100 because no
|
-- OBJECT_STATS category was requested.
|
SELECT c.relname, c.reltuples::int AS reltuples
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY c.relname;
|
relname | reltuples
|
------------------------+-----------
|
dbms_stats_del_test | 200
|
dbms_stats_del_test_p1 | 100
|
dbms_stats_del_test_p2 | 100
|
(3 rows)
|
|
-- Case 6b: OBJECT_STATS is present, so the global table delete runs after the
|
-- ignored SYNOPSES notice. Expected: root=-1; p1/p2 remain 100.
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
|
cascade_parts => false,
|
cascade_columns => false,
|
cascade_indexes => false,
|
stat_category => 'OBJECT_STATS, SYNOPSES',
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL);
|
NOTICE: stat_category value "SYNOPSES" is not currently supported
|
dbms_stats_delete_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT c.relname, c.reltuples::int AS reltuples
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_test',
|
'dbms_stats_del_test_p1',
|
'dbms_stats_del_test_p2')
|
ORDER BY c.relname;
|
relname | reltuples
|
------------------------+-----------
|
dbms_stats_del_test | -1
|
dbms_stats_del_test_p1 | 100
|
dbms_stats_del_test_p2 | 100
|
(3 rows)
|
|
-- == delete_table_stats subpartition tests ==
|
-- Composite layout: root -> p1/p2, and each pN -> s1/s2. Every leaf holds 25
|
-- rows, so a whole-table gather gives root=100, p1=50, p2=50, and every leaf=25.
|
-- p1/p2 are partitioned parents, so their relpages is -1; leaves use relpages=1.
|
-- These cases check that a named subpartition is selected alone and that
|
-- cascade_parts walks the full hierarchy only when no partname is supplied.
|
CREATE TABLE dbms_stats_del_sub(id INT, val INT) PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_del_sub_p1 PARTITION OF dbms_stats_del_sub
|
FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
|
CREATE TABLE dbms_stats_del_sub_p1_s1 PARTITION OF dbms_stats_del_sub_p1
|
FOR VALUES FROM (1) TO (51);
|
CREATE TABLE dbms_stats_del_sub_p1_s2 PARTITION OF dbms_stats_del_sub_p1
|
FOR VALUES FROM (51) TO (101);
|
CREATE TABLE dbms_stats_del_sub_p2 PARTITION OF dbms_stats_del_sub
|
FOR VALUES FROM (101) TO (201) PARTITION BY RANGE (val);
|
CREATE TABLE dbms_stats_del_sub_p2_s1 PARTITION OF dbms_stats_del_sub_p2
|
FOR VALUES FROM (1) TO (51);
|
CREATE TABLE dbms_stats_del_sub_p2_s2 PARTITION OF dbms_stats_del_sub_p2
|
FOR VALUES FROM (51) TO (101);
|
INSERT INTO dbms_stats_del_sub SELECT g, g FROM generate_series(1, 25) g;
|
INSERT INTO dbms_stats_del_sub SELECT g, g + 50 FROM generate_series(26, 50) g;
|
INSERT INTO dbms_stats_del_sub SELECT g, g - 100 FROM generate_series(101, 125) g;
|
INSERT INTO dbms_stats_del_sub SELECT g, g - 50 FROM generate_series(126, 150) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_sub');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Case 1: delete subpartition p1_s1. partname selects exactly one relation, so
|
-- cascade_parts=true is ignored. Only p1_s1's table estimates become -1/0. Its
|
-- column statistics stay because cascade_columns=false, and p1_s2, all p2
|
-- leaves, p1/p2, and the root keep their gathered values.
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_sub',
|
partname => 'dbms_stats_del_sub_p1_s1',
|
cascade_parts => true,
|
cascade_columns => false,
|
cascade_indexes => false,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
dbms_stats_delete_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Expected: p1_s1=-1/0; every other relation keeps 25/50/100 or -1 for
|
-- partitioned parents. All column_stats remain 2.
|
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
|
(SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
|
AS column_stats
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_sub',
|
'dbms_stats_del_sub_p1',
|
'dbms_stats_del_sub_p2',
|
'dbms_stats_del_sub_p1_s1',
|
'dbms_stats_del_sub_p1_s2',
|
'dbms_stats_del_sub_p2_s1',
|
'dbms_stats_del_sub_p2_s2')
|
ORDER BY c.relname;
|
relname | reltuples | relpages | column_stats
|
--------------------------+-----------+----------+--------------
|
dbms_stats_del_sub | 100 | -1 | 2
|
dbms_stats_del_sub_p1 | 50 | -1 | 2
|
dbms_stats_del_sub_p1_s1 | -1 | 0 | 2
|
dbms_stats_del_sub_p1_s2 | 25 | 1 | 2
|
dbms_stats_del_sub_p2 | 50 | -1 | 2
|
dbms_stats_del_sub_p2_s1 | 25 | 1 | 2
|
dbms_stats_del_sub_p2_s2 | 25 | 1 | 2
|
(7 rows)
|
|
-- Case 2: cascade_parts=false at the root. Only the global root entry is reset
|
-- (reltuples=-1); both partitions and all four leaves keep their estimates.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_sub');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_sub',
|
cascade_parts => false,
|
cascade_columns => false,
|
cascade_indexes => false,
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
dbms_stats_delete_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Expected: root=-1; p1/p2=50; all four leaves=25.
|
SELECT c.relname, c.reltuples::int AS reltuples
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_sub',
|
'dbms_stats_del_sub_p1',
|
'dbms_stats_del_sub_p2',
|
'dbms_stats_del_sub_p1_s1',
|
'dbms_stats_del_sub_p1_s2',
|
'dbms_stats_del_sub_p2_s1',
|
'dbms_stats_del_sub_p2_s2')
|
ORDER BY c.relname;
|
relname | reltuples
|
--------------------------+-----------
|
dbms_stats_del_sub | -1
|
dbms_stats_del_sub_p1 | 50
|
dbms_stats_del_sub_p1_s1 | 25
|
dbms_stats_del_sub_p1_s2 | 25
|
dbms_stats_del_sub_p2 | 50
|
dbms_stats_del_sub_p2_s1 | 25
|
dbms_stats_del_sub_p2_s2 | 25
|
(7 rows)
|
|
-- Case 3: cascade_parts=true at the root expands through p1/p2 into every
|
-- subpartition. Table estimates are reset for root, partitions, and leaves.
|
-- cascade_columns/cascade_indexes stay false, so only table estimates change.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_sub');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_sub',
|
cascade_parts => true,
|
cascade_columns => false,
|
cascade_indexes => false,
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
dbms_stats_delete_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Expected: every relation reports reltuples=-1; column_stats is unchanged.
|
SELECT c.relname, c.reltuples::int AS reltuples
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_sub',
|
'dbms_stats_del_sub_p1',
|
'dbms_stats_del_sub_p2',
|
'dbms_stats_del_sub_p1_s1',
|
'dbms_stats_del_sub_p1_s2',
|
'dbms_stats_del_sub_p2_s1',
|
'dbms_stats_del_sub_p2_s2')
|
ORDER BY c.relname;
|
relname | reltuples
|
--------------------------+-----------
|
dbms_stats_del_sub | -1
|
dbms_stats_del_sub_p1 | -1
|
dbms_stats_del_sub_p1_s1 | -1
|
dbms_stats_del_sub_p1_s2 | -1
|
dbms_stats_del_sub_p2 | -1
|
dbms_stats_del_sub_p2_s1 | -1
|
dbms_stats_del_sub_p2_s2 | -1
|
(7 rows)
|
|
-- == delete_column_stats tests ==
|
-- Same composite layout as above, with columns id, val, and note. The gather
|
-- creates one pg_statistic row per column per relation, so the baseline below
|
-- shows id_stats=1 and val_stats=1 everywhere. reltuples is included after
|
-- deletion to prove that removing a column does not reset table estimates.
|
CREATE TABLE dbms_stats_del_col(id INT, val INT, note TEXT)
|
PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_del_col_p1 PARTITION OF dbms_stats_del_col
|
FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
|
CREATE TABLE dbms_stats_del_col_p1_s1 PARTITION OF dbms_stats_del_col_p1
|
FOR VALUES FROM (1) TO (51);
|
CREATE TABLE dbms_stats_del_col_p1_s2 PARTITION OF dbms_stats_del_col_p1
|
FOR VALUES FROM (51) TO (101);
|
CREATE TABLE dbms_stats_del_col_p2 PARTITION OF dbms_stats_del_col
|
FOR VALUES FROM (101) TO (201) PARTITION BY RANGE (val);
|
CREATE TABLE dbms_stats_del_col_p2_s1 PARTITION OF dbms_stats_del_col_p2
|
FOR VALUES FROM (1) TO (51);
|
CREATE TABLE dbms_stats_del_col_p2_s2 PARTITION OF dbms_stats_del_col_p2
|
FOR VALUES FROM (51) TO (101);
|
INSERT INTO dbms_stats_del_col
|
SELECT g, g, 'n' FROM generate_series(1, 25) g;
|
INSERT INTO dbms_stats_del_col
|
SELECT g, g + 50, 'n' FROM generate_series(26, 50) g;
|
INSERT INTO dbms_stats_del_col
|
SELECT g, g - 100, 'n' FROM generate_series(101, 125) g;
|
INSERT INTO dbms_stats_del_col
|
SELECT g, g - 50, 'n' FROM generate_series(126, 150) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Baseline: every relation has one id row and one val row.
|
SELECT c.relname,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'id') AS id_stats,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
|
FROM pg_class c
|
WHERE c.relname LIKE 'dbms_stats_del_col%'
|
ORDER BY c.relname;
|
relname | id_stats | val_stats
|
--------------------------+----------+-----------
|
dbms_stats_del_col | 1 | 1
|
dbms_stats_del_col_p1 | 1 | 1
|
dbms_stats_del_col_p1_s1 | 1 | 1
|
dbms_stats_del_col_p1_s2 | 1 | 1
|
dbms_stats_del_col_p2 | 1 | 1
|
dbms_stats_del_col_p2_s1 | 1 | 1
|
dbms_stats_del_col_p2_s2 | 1 | 1
|
(7 rows)
|
|
-- Case 1: global delete of val with cascade_parts=false. Only the root val
|
-- statistics are removed. Root reltuples stays 100, id stats stay 1, and every
|
-- partition/subpartition val row stays 1.
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
|
cascade_parts => false,
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
col_stat_type => NULL);
|
dbms_stats_delete_column_stats
|
--------------------------------
|
|
(1 row)
|
|
-- Expected: root val_stats=0; all other val_stats stay 1; id_stats stay 1;
|
-- reltuples stays 100/50/25 because column deletion does not touch pg_class.
|
SELECT c.relname, c.reltuples::int AS reltuples,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'id') AS id_stats,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
|
FROM pg_class c
|
WHERE c.relname LIKE 'dbms_stats_del_col%'
|
ORDER BY c.relname;
|
relname | reltuples | id_stats | val_stats
|
--------------------------+-----------+----------+-----------
|
dbms_stats_del_col | 100 | 1 | 0
|
dbms_stats_del_col_p1 | 50 | 1 | 1
|
dbms_stats_del_col_p1_s1 | 25 | 1 | 1
|
dbms_stats_del_col_p1_s2 | 25 | 1 | 1
|
dbms_stats_del_col_p2 | 50 | 1 | 1
|
dbms_stats_del_col_p2_s1 | 25 | 1 | 1
|
dbms_stats_del_col_p2_s2 | 25 | 1 | 1
|
(7 rows)
|
|
-- get_column_stats must now fail for the deleted root val statistics. This is
|
-- the observable effect of the missing pg_statistic row.
|
SELECT * FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_del_col',
|
'val');
|
ERROR: No statistics have been stored for requested object
|
-- Case 2: partname=p1 with cascade_parts=true. Because partname is supplied,
|
-- cascade_parts does not descend into p1_s1/p1_s2: only p1's own val stats are
|
-- removed. Root, both p1 subpartitions, and every p2 relation keep val_stats=1;
|
-- id stats and reltuples are untouched.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
|
partname => 'dbms_stats_del_col_p1',
|
cascade_parts => true,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
col_stat_type => NULL);
|
dbms_stats_delete_column_stats
|
--------------------------------
|
|
(1 row)
|
|
-- Expected: p1 val_stats=0 only; all other val_stats and id_stats stay 1.
|
SELECT c.relname,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'id') AS id_stats,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
|
FROM pg_class c
|
WHERE c.relname LIKE 'dbms_stats_del_col%'
|
ORDER BY c.relname;
|
relname | id_stats | val_stats
|
--------------------------+----------+-----------
|
dbms_stats_del_col | 1 | 1
|
dbms_stats_del_col_p1 | 1 | 0
|
dbms_stats_del_col_p1_s1 | 1 | 1
|
dbms_stats_del_col_p1_s2 | 1 | 1
|
dbms_stats_del_col_p2 | 1 | 1
|
dbms_stats_del_col_p2_s1 | 1 | 1
|
dbms_stats_del_col_p2_s2 | 1 | 1
|
(7 rows)
|
|
-- Case 3: partname=p1_s1 and col_stat_type='all' (case-insensitive). Only the
|
-- p1_s1 val row is removed; every other val/id row remains 1.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
|
partname => 'dbms_stats_del_col_p1_s1',
|
col_stat_type => 'all',
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL);
|
dbms_stats_delete_column_stats
|
--------------------------------
|
|
(1 row)
|
|
-- Expected: p1_s1 val_stats=0 only.
|
SELECT c.relname,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'id') AS id_stats,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
|
FROM pg_class c
|
WHERE c.relname LIKE 'dbms_stats_del_col%'
|
ORDER BY c.relname;
|
relname | id_stats | val_stats
|
--------------------------+----------+-----------
|
dbms_stats_del_col | 1 | 1
|
dbms_stats_del_col_p1 | 1 | 1
|
dbms_stats_del_col_p1_s1 | 1 | 0
|
dbms_stats_del_col_p1_s2 | 1 | 1
|
dbms_stats_del_col_p2 | 1 | 1
|
dbms_stats_del_col_p2_s1 | 1 | 1
|
dbms_stats_del_col_p2_s2 | 1 | 1
|
(7 rows)
|
|
-- Case 4: cascade_parts=true without partname. The root, both partitions, and
|
-- all four subpartitions lose val stats. id stats stay 1 and reltuples is
|
-- unchanged, proving only the requested column was removed.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
|
cascade_parts => true,
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
col_stat_type => NULL);
|
dbms_stats_delete_column_stats
|
--------------------------------
|
|
(1 row)
|
|
-- Expected: val_stats=0 everywhere; id_stats=1 everywhere; reltuples stays
|
-- 100 at the root and 50/25 at partitions/subpartitions.
|
SELECT c.relname, c.reltuples::int AS reltuples,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'id') AS id_stats,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
|
FROM pg_class c
|
WHERE c.relname LIKE 'dbms_stats_del_col%'
|
ORDER BY c.relname;
|
relname | reltuples | id_stats | val_stats
|
--------------------------+-----------+----------+-----------
|
dbms_stats_del_col | 100 | 1 | 0
|
dbms_stats_del_col_p1 | 50 | 1 | 0
|
dbms_stats_del_col_p1_s1 | 25 | 1 | 0
|
dbms_stats_del_col_p1_s2 | 25 | 1 | 0
|
dbms_stats_del_col_p2 | 50 | 1 | 0
|
dbms_stats_del_col_p2_s1 | 25 | 1 | 0
|
dbms_stats_del_col_p2_s2 | 25 | 1 | 0
|
(7 rows)
|
|
-- == NULL ownname tests ==
|
-- NULL ownname must resolve through the current search_path, matching the
|
-- DBMS_STATS get procedures. Pin search_path to public so the first schema is
|
-- deterministic, then verify both delete procedures against that schema.
|
SET search_path = public;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- delete_table_stats with ownname=NULL resolves dbms_stats_del_test in public.
|
-- Only the root is targeted (cascade_parts=false), so the table estimate should
|
-- become -1 while p1/p2 and all column/index statistics remain untouched.
|
SELECT dbms_stats_delete_table_stats(NULL, 'dbms_stats_del_test',
|
cascade_parts => false,
|
cascade_columns => false,
|
cascade_indexes => false,
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
dbms_stats_delete_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Expected: the resolved public.dbms_stats_del_test has reltuples=-1.
|
SELECT relname, reltuples::int AS reltuples
|
FROM pg_class
|
WHERE relname = 'dbms_stats_del_test';
|
relname | reltuples
|
---------------------+-----------
|
dbms_stats_del_test | -1
|
(1 row)
|
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- delete_column_stats with ownname=NULL resolves dbms_stats_del_col in public
|
-- and removes only the root val row (cascade_parts=false).
|
SELECT dbms_stats_delete_column_stats(NULL, 'dbms_stats_del_col', 'val',
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => false,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
col_stat_type => NULL);
|
dbms_stats_delete_column_stats
|
--------------------------------
|
|
(1 row)
|
|
-- Expected: the resolved public.dbms_stats_del_col has val_stats=0.
|
SELECT c.relname,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
|
FROM pg_class c
|
WHERE c.relname = 'dbms_stats_del_col';
|
relname | val_stats
|
--------------------+-----------
|
dbms_stats_del_col | 0
|
(1 row)
|
|
RESET search_path;
|
-- == delete error handling ==
|
-- Snapshot objects with known statistics so failed calls can be checked for
|
-- accidental mutation.
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
CREATE TABLE dbms_stats_del_plain(id INT, val INT);
|
INSERT INTO dbms_stats_del_plain SELECT g, g FROM generate_series(1, 10) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_plain');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Missing table: delete_table_stats fails during relation resolution with
|
-- TABLE ... does not exist or insufficient privileges.
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_no_such',
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
cascade_columns => NULL,
|
cascade_indexes => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
ERROR: TABLE "public"."dbms_stats_del_no_such" does not exist or insufficient privileges
|
-- Missing table for column deletion: the table lookup fails before the column
|
-- is examined and raises the same TABLE error.
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_no_such',
|
'id',
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
col_stat_type => NULL);
|
ERROR: TABLE "public"."dbms_stats_del_no_such" does not exist or insufficient privileges
|
-- Missing column: the table exists, but get_attnum fails and reports
|
-- "<column> invalid column name".
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_plain',
|
'no_such_col', cascade_parts => false,
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
col_stat_type => NULL);
|
ERROR: no_such_col invalid column name
|
-- Missing, foreign, and plain-table partition names. In all three cases
|
-- resolve_partition_oid rejects the name instead of falling back to the global
|
-- table level.
|
-- Missing partition: no descendant with this name exists.
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
|
partname => 'dbms_stats_del_no_such_part',
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
cascade_columns => NULL,
|
cascade_indexes => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
ERROR: dbms_stats_del_no_such_part invalid partition name
|
-- Foreign partition: dbms_stats_del_col_p1_s1 belongs to another table.
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
|
partname => 'dbms_stats_del_col_p1_s1',
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
cascade_columns => NULL,
|
cascade_indexes => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
ERROR: dbms_stats_del_col_p1_s1 invalid partition name
|
-- partname on a non-partitioned table is invalid.
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_plain',
|
partname => 'dbms_stats_del_test_p1',
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
cascade_columns => NULL,
|
cascade_indexes => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
ERROR: dbms_stats_del_test_p1 invalid partition name
|
-- The same three partition errors apply to column deletion: missing name,
|
-- foreign partition, and partname on a plain table.
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
|
partname => 'dbms_stats_del_no_such_part',
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
col_stat_type => NULL);
|
ERROR: dbms_stats_del_no_such_part invalid partition name
|
-- Foreign partition for column deletion.
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
|
partname => 'dbms_stats_del_test_p1',
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
col_stat_type => NULL);
|
ERROR: dbms_stats_del_test_p1 invalid partition name
|
-- partname on a non-partitioned table for column deletion.
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_plain', 'val',
|
partname => 'dbms_stats_del_test_p1',
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
col_stat_type => NULL);
|
ERROR: dbms_stats_del_test_p1 invalid partition name
|
-- Invalid stat_category and col_stat_type. stat_category accepts only Oracle
|
-- category names; col_stat_type accepts ALL but rejects HISTOGRAM as unsupported.
|
-- Unknown stat_category token: invalid parameter error.
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_plain',
|
stat_category => 'NONSENSE',
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
cascade_columns => NULL,
|
cascade_indexes => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL);
|
ERROR: Invalid or inconsistent input values
|
-- HISTOGRAM is rejected because PostgreSQL has no supported API for removing
|
-- only histogram components while preserving base column statistics.
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_plain', 'val',
|
col_stat_type => 'HISTOGRAM',
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL);
|
ERROR: col_stat_type "HISTOGRAM" is not supported in this version
|
-- Unknown col_stat_type token: invalid parameter error.
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_plain', 'val',
|
col_stat_type => 'NONSENSE',
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL);
|
ERROR: Illegal col_stat_type NONSENSE: must be ALL | HISTOGRAM
|
-- Every invalid call above must abort before mutation. The plain table still
|
-- has its 10-row/1-page estimates, and the snapshot gathers of del_test and
|
-- del_col still show root reltuples 200/100 with one val statistic.
|
SELECT reltuples::int AS reltuples, relpages
|
FROM pg_class WHERE relname = 'dbms_stats_del_plain';
|
reltuples | relpages
|
-----------+----------
|
10 | 1
|
(1 row)
|
|
SELECT c.relname, c.reltuples::int AS reltuples,
|
(SELECT count(*) FROM pg_statistic s
|
JOIN pg_attribute a
|
ON a.attrelid = s.starelid AND a.attnum = s.staattnum
|
WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
|
FROM pg_class c
|
WHERE c.relname IN ('dbms_stats_del_test', 'dbms_stats_del_col')
|
ORDER BY c.relname;
|
relname | reltuples | val_stats
|
---------------------+-----------+-----------
|
dbms_stats_del_col | 100 | 1
|
dbms_stats_del_test | 200 | 1
|
(2 rows)
|
|
-- Insufficient privileges for both procedures. The new role gets schema USAGE
|
-- but neither table ownership nor MAINTAIN, so both calls must fail without
|
-- changing the gathered statistics.
|
CREATE TABLE dbms_stats_del_priv(id INT, val INT);
|
INSERT INTO dbms_stats_del_priv SELECT g, g FROM generate_series(1, 10) g;
|
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_priv');
|
NOTICE: estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
|
NOTICE: cascade=false is not supported — ANALYZE always includes index statistics
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
CREATE ROLE dbms_stats_del_priv_user;
|
GRANT USAGE ON SCHEMA public TO dbms_stats_del_priv_user;
|
SET ROLE dbms_stats_del_priv_user;
|
-- Table deletion requires ownership or MAINTAIN; schema USAGE is not enough.
|
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_priv',
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
cascade_columns => NULL,
|
cascade_indexes => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
stat_category => NULL);
|
ERROR: TABLE "public"."dbms_stats_del_priv" does not exist or insufficient privileges
|
-- Column deletion uses the same table-privilege check and must also fail.
|
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_priv', 'val',
|
partname => NULL,
|
stattab => NULL,
|
statid => NULL,
|
cascade_parts => NULL,
|
statown => NULL,
|
no_invalidate => NULL,
|
force => NULL,
|
col_stat_type => NULL);
|
ERROR: TABLE "public"."dbms_stats_del_priv" does not exist or insufficient privileges
|
RESET ROLE;
|
REVOKE ALL ON SCHEMA public FROM dbms_stats_del_priv_user;
|
DROP ROLE dbms_stats_del_priv_user;
|
DROP TABLE dbms_stats_del_priv;
|
DROP TABLE dbms_stats_del_col;
|
DROP TABLE dbms_stats_del_sub;
|
DROP TABLE dbms_stats_del_test;
|
DROP TABLE dbms_stats_del_plain;
|
-- == create_stat_table tests ==
|
-- CREATE_STAT_TABLE creates Oracle's generic statistics table, refuses
|
-- duplicates and missing schemas, honors tablespace, and requires CREATE.
|
CREATE SCHEMA dbms_stats_stattab_schema;
|
CREATE ROLE dbms_stats_stattab_limited;
|
GRANT USAGE ON SCHEMA dbms_stats_stattab_schema TO dbms_stats_stattab_limited;
|
-- Two-argument call creates an empty table in the named schema.
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_1');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT n.nspname, c.relname, c.relkind, c.relpersistence
|
FROM pg_class c
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
WHERE n.nspname = 'dbms_stats_stattab_schema'
|
AND c.relname = 'dbms_stats_stattab_1';
|
nspname | relname | relkind | relpersistence
|
---------------------------+----------------------+---------+----------------
|
dbms_stats_stattab_schema | dbms_stats_stattab_1 | r | p
|
(1 row)
|
|
SELECT count(*) AS row_count
|
FROM dbms_stats_stattab_schema.dbms_stats_stattab_1;
|
row_count
|
-----------
|
0
|
(1 row)
|
|
-- Exact generic layout: 27 columns in Oracle order with expected types.
|
SELECT attnum, attname, format_type(atttypid, atttypmod) AS type,
|
attnotnull
|
FROM pg_attribute
|
WHERE attrelid = 'dbms_stats_stattab_schema.dbms_stats_stattab_1'::regclass
|
AND attnum > 0 AND NOT attisdropped
|
ORDER BY attnum;
|
attnum | attname | type | attnotnull
|
--------+---------+--------------------------------+------------
|
1 | statid | varchar2(30) | f
|
2 | type | character(1) | f
|
3 | version | number | f
|
4 | flags | number | f
|
5 | c1 | varchar2(63) | f
|
6 | c2 | varchar2(63) | f
|
7 | c3 | varchar2(63) | f
|
8 | c4 | varchar2(63) | f
|
9 | c5 | varchar2(63) | f
|
10 | n1 | number | f
|
11 | n2 | number | f
|
12 | n3 | number | f
|
13 | n4 | number | f
|
14 | n5 | number | f
|
15 | n6 | number | f
|
16 | n7 | number | f
|
17 | n8 | number | f
|
18 | n9 | number | f
|
19 | n10 | number | f
|
20 | n11 | number | f
|
21 | n12 | number | f
|
22 | n13 | number | f
|
23 | d1 | timestamp(0) without time zone | f
|
24 | r1 | raw(1000) | f
|
25 | r2 | raw(1000) | f
|
26 | r3 | raw(1000) | f
|
27 | ch1 | varchar2(1000) | f
|
(27 rows)
|
|
SELECT count(*) AS column_count,
|
count(*) FILTER (WHERE attnotnull) AS not_null_columns
|
FROM pg_attribute
|
WHERE attrelid = 'dbms_stats_stattab_schema.dbms_stats_stattab_1'::regclass
|
AND attnum > 0 AND NOT attisdropped;
|
column_count | not_null_columns
|
--------------+------------------
|
27 | 0
|
(1 row)
|
|
SELECT count(*) AS index_count
|
FROM pg_index
|
WHERE indrelid = 'dbms_stats_stattab_schema.dbms_stats_stattab_1'::regclass;
|
index_count
|
-------------
|
0
|
(1 row)
|
|
SELECT count(*) AS constraint_count
|
FROM pg_constraint
|
WHERE conrelid = 'dbms_stats_stattab_schema.dbms_stats_stattab_1'::regclass;
|
constraint_count
|
------------------
|
0
|
(1 row)
|
|
SELECT count(*) AS information_schema_columns,
|
count(*) FILTER (WHERE is_nullable = 'YES') AS nullable_columns
|
FROM information_schema.columns
|
WHERE table_schema = 'dbms_stats_stattab_schema'
|
AND table_name = 'dbms_stats_stattab_1';
|
information_schema_columns | nullable_columns
|
----------------------------+------------------
|
27 | 27
|
(1 row)
|
|
-- NULL ownname resolves to the first schema in the search path (public).
|
SELECT dbms_stats_create_stat_table(NULL, 'dbms_stats_stattab_public');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT n.nspname, c.relname
|
FROM pg_class c
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
WHERE c.relname = 'dbms_stats_stattab_public';
|
nspname | relname
|
---------+---------------------------
|
public | dbms_stats_stattab_public
|
(1 row)
|
|
-- Quoted mixed-case name is preserved.
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'MixedCase Stat Table');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT relname
|
FROM pg_class
|
WHERE relnamespace = 'dbms_stats_stattab_schema'::regnamespace
|
AND relname = 'MixedCase Stat Table';
|
relname
|
----------------------
|
MixedCase Stat Table
|
(1 row)
|
|
-- Three- and four-argument forms use the declared defaults.
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_three_args', NULL);
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_four_args', NULL, false);
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
-- Tablespace: an existing tablespace is applied, NULL uses the default.
|
SET allow_in_place_tablespaces = true;
|
CREATE TABLESPACE dbms_stats_stattab_space LOCATION '';
|
RESET allow_in_place_tablespaces;
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_ts',
|
'dbms_stats_stattab_space');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT c.reltablespace = (SELECT oid FROM pg_tablespace
|
WHERE spcname = 'dbms_stats_stattab_space')
|
AS in_stattab_tablespace
|
FROM pg_class c
|
WHERE c.oid = 'dbms_stats_stattab_schema.dbms_stats_stattab_ts'::regclass;
|
in_stattab_tablespace
|
-----------------------
|
t
|
(1 row)
|
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_default_ts', NULL);
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT reltablespace
|
FROM pg_class
|
WHERE oid = 'dbms_stats_stattab_schema.dbms_stats_stattab_default_ts'::regclass;
|
reltablespace
|
---------------
|
0
|
(1 row)
|
|
-- Missing tablespace fails before creating the table.
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_missing_ts',
|
'dbms_stats_no_such_tablespace');
|
ERROR: Tablespace dbms_stats_no_such_tablespace does not exist
|
-- global_temporary => TRUE emits a NOTICE and creates a regular table.
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_gtt', NULL, true);
|
NOTICE: global_temporary is not supported in this version
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT relkind, relpersistence
|
FROM pg_class
|
WHERE oid = 'dbms_stats_stattab_schema.dbms_stats_stattab_gtt'::regclass;
|
relkind | relpersistence
|
---------+----------------
|
r | p
|
(1 row)
|
|
-- Duplicate names: tables, indexes, and views all block creation.
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_1');
|
ERROR: Unable to create table dbms_stats_stattab_1: already exists
|
CREATE TABLE dbms_stats_stattab_schema.dbms_stats_stattab_idx_target(id int);
|
CREATE INDEX dbms_stats_stattab_idx_name
|
ON dbms_stats_stattab_schema.dbms_stats_stattab_idx_target(id);
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_idx_name');
|
ERROR: Unable to create table dbms_stats_stattab_idx_name: already exists
|
CREATE VIEW dbms_stats_stattab_schema.dbms_stats_stattab_view AS SELECT 1 AS x;
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_view');
|
ERROR: Unable to create table dbms_stats_stattab_view: already exists
|
SELECT relkind
|
FROM pg_class
|
WHERE oid = 'dbms_stats_stattab_schema.dbms_stats_stattab_idx_name'::regclass;
|
relkind
|
---------
|
i
|
(1 row)
|
|
SELECT relkind
|
FROM pg_class
|
WHERE oid = 'dbms_stats_stattab_schema.dbms_stats_stattab_view'::regclass;
|
relkind
|
---------
|
v
|
(1 row)
|
|
-- Missing schema, empty name, overlong name.
|
SELECT dbms_stats_create_stat_table('dbms_stats_no_such_schema',
|
'dbms_stats_stattab_x');
|
ERROR: Schema "dbms_stats_no_such_schema" does not exist
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema', '');
|
ERROR: Statistics table must be specified
|
SELECT dbms_stats_create_stat_table(
|
'dbms_stats_stattab_schema',
|
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
|
ERROR: Statistics table name is too long
|
-- USAGE without CREATE is insufficient.
|
SET ROLE dbms_stats_stattab_limited;
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_denied');
|
ERROR: no privileges on schema dbms_stats_stattab_schema
|
RESET ROLE;
|
-- Creation participates in the caller's transaction.
|
BEGIN;
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_rollback');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT to_regclass('dbms_stats_stattab_schema.dbms_stats_stattab_rollback')
|
IS NOT NULL AS exists_before_rollback;
|
exists_before_rollback
|
------------------------
|
t
|
(1 row)
|
|
ROLLBACK;
|
SELECT to_regclass('dbms_stats_stattab_schema.dbms_stats_stattab_rollback')
|
IS NULL AS gone_after_rollback;
|
gone_after_rollback
|
---------------------
|
t
|
(1 row)
|
|
BEGIN;
|
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
|
'dbms_stats_stattab_commit');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
COMMIT;
|
SELECT to_regclass('dbms_stats_stattab_schema.dbms_stats_stattab_commit')
|
IS NOT NULL AS exists_after_commit;
|
exists_after_commit
|
---------------------
|
t
|
(1 row)
|
|
-- Cleanup.
|
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_1;
|
DROP TABLE dbms_stats_stattab_schema."MixedCase Stat Table";
|
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_three_args;
|
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_four_args;
|
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_ts;
|
DROP TABLESPACE dbms_stats_stattab_space;
|
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_default_ts;
|
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_gtt;
|
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_idx_target;
|
DROP VIEW dbms_stats_stattab_schema.dbms_stats_stattab_view;
|
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_commit;
|
DROP TABLE dbms_stats_stattab_public;
|
REVOKE ALL ON SCHEMA dbms_stats_stattab_schema FROM dbms_stats_stattab_limited;
|
DROP SCHEMA dbms_stats_stattab_schema;
|
DROP ROLE dbms_stats_stattab_limited;
|
-- == drop_stat_table tests ==
|
-- DROP_STAT_TABLE removes validated user statistics tables only, honors
|
-- ownership, and participates in the caller's transaction.
|
CREATE SCHEMA dbms_stats_drop_schema;
|
CREATE SCHEMA dbms_stats_drop_other_schema;
|
CREATE ROLE dbms_stats_drop_limited;
|
GRANT USAGE ON SCHEMA dbms_stats_drop_schema TO dbms_stats_drop_limited;
|
-- Named and NULL owners, with representative rows.
|
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_named');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
INSERT INTO dbms_stats_drop_schema.dbms_stats_drop_named(statid, type)
|
VALUES ('dropme', 'T');
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_named');
|
dbms_stats_drop_stat_table
|
----------------------------
|
|
(1 row)
|
|
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_named') IS NULL
|
AS dropped_named;
|
dropped_named
|
---------------
|
t
|
(1 row)
|
|
SET search_path = dbms_stats_drop_schema, public;
|
SELECT dbms_stats_create_stat_table(NULL, 'dbms_stats_drop_null_owner');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
INSERT INTO dbms_stats_drop_null_owner(statid, type) VALUES ('dropme', 'T');
|
SELECT dbms_stats_drop_stat_table(NULL, 'dbms_stats_drop_null_owner');
|
dbms_stats_drop_stat_table
|
----------------------------
|
|
(1 row)
|
|
SELECT to_regclass('dbms_stats_drop_null_owner') IS NULL
|
AS dropped_null_owner;
|
dropped_null_owner
|
--------------------
|
t
|
(1 row)
|
|
RESET search_path;
|
-- Only the requested table is dropped from the requested schema.
|
SELECT dbms_stats_create_stat_table('dbms_stats_drop_other_schema',
|
'dbms_stats_drop_other');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_target');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_target');
|
dbms_stats_drop_stat_table
|
----------------------------
|
|
(1 row)
|
|
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_target') IS NULL
|
AS target_gone,
|
to_regclass('dbms_stats_drop_other_schema.dbms_stats_drop_other')
|
IS NOT NULL AS other_schema_kept;
|
target_gone | other_schema_kept
|
-------------+-------------------
|
t | t
|
(1 row)
|
|
DROP TABLE dbms_stats_drop_other_schema.dbms_stats_drop_other;
|
-- Mixed-case quoted identifiers are preserved; the unquoted spelling misses.
|
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
|
'MixedCase Stat Table');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'mixedcase stat table');
|
ERROR: Table "dbms_stats_drop_schema"."mixedcase stat table" does not exist or insufficient privileges
|
SELECT to_regclass('dbms_stats_drop_schema."MixedCase Stat Table"')
|
IS NOT NULL AS mixed_case_kept;
|
mixed_case_kept
|
-----------------
|
t
|
(1 row)
|
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'MixedCase Stat Table');
|
dbms_stats_drop_stat_table
|
----------------------------
|
|
(1 row)
|
|
SELECT to_regclass('dbms_stats_drop_schema."MixedCase Stat Table"')
|
IS NULL AS dropped_mixed_case;
|
dropped_mixed_case
|
--------------------
|
t
|
(1 row)
|
|
-- Missing schema, missing table, non-table relations, and plain tables fail.
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_no_such_schema',
|
'dbms_stats_drop_any');
|
ERROR: Schema "dbms_stats_drop_no_such_schema" does not exist
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_missing');
|
ERROR: Table "dbms_stats_drop_schema"."dbms_stats_drop_missing" does not exist or insufficient privileges
|
CREATE VIEW dbms_stats_drop_schema.dbms_stats_drop_view AS SELECT 1 AS x;
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_view');
|
ERROR: Bad user statistics table "dbms_stats_drop_view"
|
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_view') IS NOT NULL
|
AS view_kept;
|
view_kept
|
-----------
|
t
|
(1 row)
|
|
CREATE TABLE dbms_stats_drop_schema.dbms_stats_drop_plain(id int);
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_plain');
|
ERROR: Bad user statistics table "dbms_stats_drop_plain"
|
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_plain') IS NOT NULL
|
AS plain_table_kept;
|
plain_table_kept
|
------------------
|
t
|
(1 row)
|
|
CREATE INDEX dbms_stats_drop_idx
|
ON dbms_stats_drop_schema.dbms_stats_drop_plain(id);
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_idx');
|
ERROR: Bad user statistics table "dbms_stats_drop_idx"
|
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_idx') IS NOT NULL
|
AS index_kept;
|
index_kept
|
------------
|
t
|
(1 row)
|
|
-- Empty and overlong names are rejected before catalog lookup.
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema', '');
|
ERROR: Statistics table must be specified
|
SELECT dbms_stats_drop_stat_table(
|
'dbms_stats_drop_schema',
|
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
|
ERROR: Statistics table name is too long
|
-- A caller that can see but does not own the table cannot drop it.
|
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_priv');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
GRANT SELECT ON dbms_stats_drop_schema.dbms_stats_drop_priv
|
TO dbms_stats_drop_limited;
|
SET ROLE dbms_stats_drop_limited;
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_priv');
|
ERROR: must be owner of table dbms_stats_drop_priv
|
CONTEXT: SQL statement "DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_priv"
|
RESET ROLE;
|
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_priv') IS NOT NULL
|
AS privilege_table_kept;
|
privilege_table_kept
|
----------------------
|
t
|
(1 row)
|
|
-- Rollback restores the dropped table and its rows.
|
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_rollback');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
INSERT INTO dbms_stats_drop_schema.dbms_stats_drop_rollback(statid)
|
VALUES ('rollback');
|
BEGIN;
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_rollback');
|
dbms_stats_drop_stat_table
|
----------------------------
|
|
(1 row)
|
|
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_rollback') IS NULL
|
AS gone_inside_transaction;
|
gone_inside_transaction
|
-------------------------
|
t
|
(1 row)
|
|
ROLLBACK;
|
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_rollback') IS NOT NULL
|
AS restored_after_rollback,
|
(SELECT count(*) FROM dbms_stats_drop_schema.dbms_stats_drop_rollback)
|
AS restored_rows;
|
restored_after_rollback | restored_rows
|
-------------------------+---------------
|
t | 1
|
(1 row)
|
|
-- Commit makes the drop durable.
|
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_commit');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
BEGIN;
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_commit');
|
dbms_stats_drop_stat_table
|
----------------------------
|
|
(1 row)
|
|
COMMIT;
|
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_commit') IS NULL
|
AS gone_after_commit;
|
gone_after_commit
|
-------------------
|
t
|
(1 row)
|
|
-- Dependent objects block the non-cascading drop.
|
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_dep');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
CREATE VIEW dbms_stats_drop_schema.dbms_stats_drop_dep_view AS
|
SELECT count(*) AS n FROM dbms_stats_drop_schema.dbms_stats_drop_dep;
|
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
|
'dbms_stats_drop_dep');
|
ERROR: cannot drop table dbms_stats_drop_schema.dbms_stats_drop_dep because other objects depend on it
|
DETAIL: view dbms_stats_drop_schema.dbms_stats_drop_dep_view depends on table dbms_stats_drop_schema.dbms_stats_drop_dep
|
HINT: Use DROP ... CASCADE to drop the dependent objects too.
|
CONTEXT: SQL statement "DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_dep"
|
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_dep') IS NOT NULL
|
AS dependent_table_kept,
|
to_regclass('dbms_stats_drop_schema.dbms_stats_drop_dep_view') IS NOT NULL
|
AS dependent_view_kept;
|
dependent_table_kept | dependent_view_kept
|
----------------------+---------------------
|
t | t
|
(1 row)
|
|
-- Cleanup.
|
DROP VIEW dbms_stats_drop_schema.dbms_stats_drop_view;
|
DROP VIEW dbms_stats_drop_schema.dbms_stats_drop_dep_view;
|
DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_plain;
|
DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_priv;
|
DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_rollback;
|
DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_dep;
|
REVOKE ALL ON SCHEMA dbms_stats_drop_schema FROM dbms_stats_drop_limited;
|
DROP SCHEMA dbms_stats_drop_schema;
|
DROP SCHEMA dbms_stats_drop_other_schema;
|
DROP ROLE dbms_stats_drop_limited;
|
-- == export_table_stats tests ==
|
CREATE SCHEMA dbms_stats_export_schema;
|
CREATE ROLE dbms_stats_export_limited;
|
GRANT USAGE ON SCHEMA dbms_stats_export_schema TO dbms_stats_export_limited;
|
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_test(
|
id int, val text, unused int);
|
INSERT INTO dbms_stats_export_schema.dbms_stats_export_test
|
SELECT g, 'value_' || (g % 10), NULL FROM generate_series(1, 1000) g;
|
CREATE INDEX dbms_stats_export_test_idx
|
ON dbms_stats_export_schema.dbms_stats_export_test(id);
|
SELECT dbms_stats_gather_table_stats('dbms_stats_export_schema',
|
'dbms_stats_export_test',
|
estimate_percent => NULL,
|
cascade => true);
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_test
|
ADD COLUMN late int;
|
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
|
'dbms_stats_export_stat');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
-- Successful non-partitioned export with cascade.
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => 'set1',
|
cascade => true,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
-- Export holds a writer-serializing lock on the statistics table.
|
BEGIN;
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => 'lockcheck',
|
cascade => false,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT mode
|
FROM pg_locks
|
WHERE relation = 'dbms_stats_export_schema.dbms_stats_export_stat'::regclass
|
AND pid = pg_backend_pid()
|
ORDER BY mode;
|
mode
|
-----------------------
|
RowExclusiveLock
|
ShareRowExclusiveLock
|
(2 rows)
|
|
ROLLBACK;
|
SELECT type, c1, c2, c3, c4, c5, version, flags, n1, n2, n3
|
FROM dbms_stats_export_schema.dbms_stats_export_stat
|
WHERE statid = 'set1'
|
ORDER BY type, c2, c4 NULLS LAST;
|
type | c1 | c2 | c3 | c4 | c5 | version | flags | n1 | n2 | n3
|
------+--------------------------+----------------------------+----+--------+------------------------+---------+-------+------+-------+------
|
C | dbms_stats_export_schema | dbms_stats_export_test | | id | | 1 | 0 | 1000 | 0.001 | 0
|
C | dbms_stats_export_schema | dbms_stats_export_test | | unused | | 1 | 0 | 0 | 0 | 1000
|
C | dbms_stats_export_schema | dbms_stats_export_test | | val | | 1 | 0 | 10 | 0.1 | 0
|
I | dbms_stats_export_schema | dbms_stats_export_test_idx | | | dbms_stats_export_test | 1 | 0 | 1000 | 5 |
|
T | dbms_stats_export_schema | dbms_stats_export_test | | | | 1 | 0 | 1000 | 6 | 49
|
(5 rows)
|
|
SELECT c4,
|
ch1, r1, r2, n5 AS n5_is_buckets
|
FROM dbms_stats_export_schema.dbms_stats_export_stat
|
WHERE statid = 'set1' AND type = 'C'
|
ORDER BY c4;
|
c4 | ch1 | r1 | r2 | n5_is_buckets
|
--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+------------+---------------
|
id | {"min": "1", "max": "1000", "histogram": ["1", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100", "110", "120", "130", "140", "150", "160", "170", "180", "190", "200", "210", "220", "230", "240", "250", "260", "270", "280", "290", "300", "310", "320", "330", "340", "350", "360", "370", "380", "390", "400", "410", "420", "430", "440", "450", "460", "470", "480", "490", "500", "510", "520", "530", "540", "550", "560", "570", "580", "590", "600", "610", "620", "630", "640", "650", "660", "670", "680", "690", "700", "710", "720", "730", "740", "750", "760", "770", "780", "790", "800", "810", "820", "830", "840", "850", "860", "870", "880", "890", "900", "910", "920", "930", "940", "950", "960", "970", "980", "990", "1000"]} | \x31 | \x31303030 | 101
|
unused | | | | 0
|
val | | | | 0
|
(3 rows)
|
|
-- NULL ownname uses the first schema in the search path.
|
SET search_path = dbms_stats_export_schema, public;
|
SELECT dbms_stats_export_table_stats(
|
NULL,
|
'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => 'nullown',
|
cascade => false,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
RESET search_path;
|
SELECT c1, c2
|
FROM dbms_stats_export_schema.dbms_stats_export_stat
|
WHERE statid = 'nullown';
|
c1 | c2
|
--------------------------+------------------------
|
dbms_stats_export_schema | dbms_stats_export_test
|
(1 row)
|
|
-- statown selects a statistics table from another schema.
|
CREATE SCHEMA dbms_stats_export_stat_schema;
|
SELECT dbms_stats_create_stat_table('dbms_stats_export_stat_schema',
|
'dbms_stats_export_other_stat');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_other_stat',
|
statown => 'dbms_stats_export_stat_schema',
|
statid => 'other',
|
cascade => false,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT c1, c2
|
FROM dbms_stats_export_stat_schema.dbms_stats_export_other_stat
|
WHERE statid = 'other';
|
c1 | c2
|
--------------------------+------------------------
|
dbms_stats_export_schema | dbms_stats_export_test
|
(1 row)
|
|
-- Cascade false exports only table rows.
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => 'tableonly',
|
cascade => false,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT type, count(*)
|
FROM dbms_stats_export_schema.dbms_stats_export_stat
|
WHERE statid = 'tableonly'
|
GROUP BY type
|
ORDER BY type;
|
type | count
|
------+-------
|
T | 1
|
(1 row)
|
|
-- A table with no collected statistics still yields a table row.
|
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_empty(id int, val text);
|
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
|
'dbms_stats_export_empty_stat');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_empty',
|
stattab => 'dbms_stats_export_empty_stat',
|
statid => 'empty',
|
cascade => true,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT type, n1, n2, n3
|
FROM dbms_stats_export_schema.dbms_stats_export_empty_stat
|
WHERE statid = 'empty'
|
ORDER BY type;
|
type | n1 | n2 | n3
|
------+----+----+----
|
T | 0 | 0 | 0
|
(1 row)
|
|
-- Partitioned and subpartitioned export.
|
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_part(id int, val int)
|
PARTITION BY RANGE (id);
|
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_part_p1
|
PARTITION OF dbms_stats_export_schema.dbms_stats_export_part
|
FOR VALUES FROM (0) TO (100) PARTITION BY HASH (val);
|
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_part_p1_s0
|
PARTITION OF dbms_stats_export_schema.dbms_stats_export_part_p1
|
FOR VALUES WITH (MODULUS 2, REMAINDER 0);
|
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_part_p1_s1
|
PARTITION OF dbms_stats_export_schema.dbms_stats_export_part_p1
|
FOR VALUES WITH (MODULUS 2, REMAINDER 1);
|
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_part_p2
|
PARTITION OF dbms_stats_export_schema.dbms_stats_export_part
|
FOR VALUES FROM (100) TO (200);
|
INSERT INTO dbms_stats_export_schema.dbms_stats_export_part
|
SELECT g % 200, g FROM generate_series(1, 1000) g;
|
SELECT dbms_stats_gather_table_stats('dbms_stats_export_schema',
|
'dbms_stats_export_part',
|
estimate_percent => NULL,
|
cascade => true);
|
dbms_stats_gather_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
|
'dbms_stats_export_part_stat');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_part',
|
stattab => 'dbms_stats_export_part_stat',
|
statid => 'partall',
|
cascade => false,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT type, c1, c2, c3, c4, c5, version, flags, n1, n2, n3
|
FROM dbms_stats_export_schema.dbms_stats_export_part_stat
|
WHERE statid = 'partall'
|
ORDER BY c3 NULLS FIRST;
|
type | c1 | c2 | c3 | c4 | c5 | version | flags | n1 | n2 | n3
|
------+--------------------------+------------------------+------------------------------+----+----+---------+-------+------+----+----
|
T | dbms_stats_export_schema | dbms_stats_export_part | | | | 1 | 0 | 1000 | -1 | 0
|
T | dbms_stats_export_schema | dbms_stats_export_part | dbms_stats_export_part_p1 | | | 1 | 0 | 500 | -1 | 0
|
T | dbms_stats_export_schema | dbms_stats_export_part | dbms_stats_export_part_p1_s0 | | | 1 | 0 | 259 | 2 | 63
|
T | dbms_stats_export_schema | dbms_stats_export_part | dbms_stats_export_part_p1_s1 | | | 1 | 0 | 241 | 2 | 68
|
T | dbms_stats_export_schema | dbms_stats_export_part | dbms_stats_export_part_p2 | | | 1 | 0 | 500 | 3 | 49
|
(5 rows)
|
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_part',
|
partname => 'dbms_stats_export_part_p1',
|
stattab => 'dbms_stats_export_part_stat',
|
statid => 'partone',
|
cascade => false,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT type, c1, c2, c3, c4, c5, version, flags, n1, n2, n3
|
FROM dbms_stats_export_schema.dbms_stats_export_part_stat
|
WHERE statid = 'partone'
|
ORDER BY c3 NULLS LAST;
|
type | c1 | c2 | c3 | c4 | c5 | version | flags | n1 | n2 | n3
|
------+--------------------------+------------------------+---------------------------+----+----+---------+-------+-----+----+----
|
T | dbms_stats_export_schema | dbms_stats_export_part | dbms_stats_export_part_p1 | | | 1 | 0 | 500 | -1 | 0
|
(1 row)
|
|
-- Re-export replaces the same statid; cascade=false preserves C/I rows.
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => 'set1',
|
cascade => false,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT type, count(*)
|
FROM dbms_stats_export_schema.dbms_stats_export_stat
|
WHERE statid = 'set1'
|
GROUP BY type
|
ORDER BY type;
|
type | count
|
------+-------
|
C | 3
|
I | 1
|
T | 1
|
(3 rows)
|
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => 'set1',
|
cascade => true,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT count(*) AS set1_rows
|
FROM dbms_stats_export_schema.dbms_stats_export_stat
|
WHERE statid = 'set1';
|
set1_rows
|
-----------
|
5
|
(1 row)
|
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => 'set2',
|
cascade => true,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT statid, count(*)
|
FROM dbms_stats_export_schema.dbms_stats_export_stat
|
WHERE statid IN ('set1', 'set2')
|
GROUP BY statid
|
ORDER BY statid;
|
statid | count
|
--------+-------
|
set1 | 5
|
set2 | 5
|
(2 rows)
|
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => NULL,
|
cascade => false,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => NULL,
|
cascade => false,
|
stat_category => 'OBJECT_STATS');
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT count(*) AS null_statid_rows
|
FROM dbms_stats_export_schema.dbms_stats_export_stat
|
WHERE statid IS NULL;
|
null_statid_rows
|
------------------
|
1
|
(1 row)
|
|
-- A failed replacement rolls back to the previous export.
|
CREATE FUNCTION dbms_stats_export_schema.dbms_stats_export_fail()
|
RETURNS trigger
|
LANGUAGE plpgsql
|
AS $$
|
BEGIN
|
IF NEW.statid = 'set1' THEN
|
RAISE EXCEPTION 'forced export failure';
|
END IF;
|
RETURN NEW;
|
END$$;
|
CREATE TRIGGER dbms_stats_export_fail
|
BEFORE INSERT ON dbms_stats_export_schema.dbms_stats_export_stat
|
FOR EACH ROW EXECUTE FUNCTION
|
dbms_stats_export_schema.dbms_stats_export_fail();
|
BEGIN;
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => 'set1',
|
cascade => true,
|
stat_category => 'OBJECT_STATS');
|
ERROR: forced export failure
|
CONTEXT: PL/pgSQL function dbms_stats_export_schema.dbms_stats_export_fail() line 4 at RAISE
|
SQL statement "INSERT INTO dbms_stats_export_schema.dbms_stats_export_stat (statid, "type", version, flags, c1, c2, c3, c4, c5, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, d1, r1, r2, r3, ch1) VALUES ($1, $2, 1, 0, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25)"
|
ROLLBACK;
|
SELECT count(*) AS set1_rows_after_failed_replace
|
FROM dbms_stats_export_schema.dbms_stats_export_stat
|
WHERE statid = 'set1';
|
set1_rows_after_failed_replace
|
--------------------------------
|
5
|
(1 row)
|
|
DROP TRIGGER dbms_stats_export_fail
|
ON dbms_stats_export_schema.dbms_stats_export_stat;
|
DROP FUNCTION dbms_stats_export_schema.dbms_stats_export_fail();
|
-- Category handling.
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => 'catset',
|
cascade => false,
|
stat_category => 'OBJECT_STATS, SYNOPSES');
|
NOTICE: stat_category value "SYNOPSES" is not currently supported
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT statid, count(*)
|
FROM dbms_stats_export_schema.dbms_stats_export_stat
|
WHERE statid = 'catset'
|
GROUP BY statid;
|
statid | count
|
--------+-------
|
catset | 1
|
(1 row)
|
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => 'synonly',
|
cascade => false,
|
stat_category => 'SYNOPSES');
|
NOTICE: stat_category value "SYNOPSES" is not currently supported
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT count(*) AS synonly_rows
|
FROM dbms_stats_export_schema.dbms_stats_export_stat
|
WHERE statid = 'synonly';
|
synonly_rows
|
--------------
|
0
|
(1 row)
|
|
-- Error paths.
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => NULL);
|
ERROR: Statistics table must be specified
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => '');
|
ERROR: Statistics table must be specified
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_no_such_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat');
|
ERROR: TABLE "dbms_stats_no_such_schema"."dbms_stats_export_test" does not exist or insufficient privileges
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_no_such_table',
|
stattab => 'dbms_stats_export_stat');
|
ERROR: TABLE "dbms_stats_export_schema"."dbms_stats_no_such_table" does not exist or insufficient privileges
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_part',
|
partname => 'dbms_stats_no_such_part',
|
stattab => 'dbms_stats_export_part_stat');
|
ERROR: dbms_stats_no_such_part invalid partition name
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_missing_stat');
|
ERROR: Table "dbms_stats_export_schema"."dbms_stats_export_missing_stat" does not exist or insufficient privileges
|
CREATE VIEW dbms_stats_export_schema.dbms_stats_export_view AS SELECT 1 AS x;
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_view');
|
ERROR: Bad user statistics table "dbms_stats_export_view"
|
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
|
'dbms_stats_export_narrow');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_narrow
|
ALTER COLUMN c1 TYPE varchar2(30);
|
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_narrow
|
ALTER COLUMN c2 TYPE varchar2(30);
|
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_narrow
|
ALTER COLUMN c3 TYPE varchar2(30);
|
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_narrow
|
ALTER COLUMN c4 TYPE varchar2(30);
|
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_narrow
|
ALTER COLUMN c5 TYPE varchar2(30);
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_narrow');
|
ERROR: Bad user statistics table "dbms_stats_export_narrow"
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
stat_category => 'BOGUS');
|
ERROR: Invalid or inconsistent input values
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat',
|
statid => repeat('a', 31));
|
ERROR: numeric or value error: character string buffer too small
|
-- Insufficient source privilege fails before touching the statistics table.
|
SET ROLE dbms_stats_export_limited;
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_test',
|
stattab => 'dbms_stats_export_stat');
|
ERROR: TABLE "dbms_stats_export_schema"."dbms_stats_export_test" does not exist or insufficient privileges
|
RESET ROLE;
|
-- A writer without statistics-table privileges is rejected.
|
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_limited_tbl(
|
id int, val int);
|
INSERT INTO dbms_stats_export_schema.dbms_stats_export_limited_tbl
|
SELECT g, g FROM generate_series(1, 10) g;
|
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_limited_tbl
|
OWNER TO dbms_stats_export_limited;
|
SET ROLE dbms_stats_export_limited;
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_limited_tbl',
|
stattab => 'dbms_stats_export_stat');
|
ERROR: TABLE "dbms_stats_export_schema"."dbms_stats_export_stat" does not exist or insufficient privileges
|
RESET ROLE;
|
-- Full-length PostgreSQL identifiers are stored untruncated.
|
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
|
'dbms_stats_export_long_stat');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
DO $$
|
DECLARE
|
t name := repeat('t', 63);
|
c name := repeat('c', 63);
|
i name := repeat('i', 63);
|
BEGIN
|
EXECUTE format('CREATE TABLE dbms_stats_export_schema.%I (%I int)', t, c);
|
EXECUTE format('INSERT INTO dbms_stats_export_schema.%I '
|
'SELECT generate_series(1, 100)', t);
|
EXECUTE format('CREATE INDEX %I ON dbms_stats_export_schema.%I (%I)',
|
i, t, c);
|
EXECUTE format('ANALYZE dbms_stats_export_schema.%I', t);
|
PERFORM dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => t::varchar2,
|
stattab => 'dbms_stats_export_long_stat',
|
statid => 'long',
|
cascade => true,
|
stat_category => 'OBJECT_STATS');
|
END$$;
|
SELECT type, length(c1), length(c2), length(c4), length(c5)
|
FROM dbms_stats_export_schema.dbms_stats_export_long_stat
|
WHERE statid = 'long'
|
ORDER BY type;
|
type | length | length | length | length
|
------+--------+--------+--------+--------
|
C | 24 | 63 | 63 |
|
I | 24 | 63 | | 63
|
T | 24 | 63 | |
|
(3 rows)
|
|
-- Oversized histogram payload becomes NULL with a NOTICE.
|
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_hist(
|
id int, big text);
|
INSERT INTO dbms_stats_export_schema.dbms_stats_export_hist
|
SELECT g, repeat(md5(g::text), 4) FROM generate_series(1, 1000) g;
|
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_hist
|
ALTER COLUMN big SET STATISTICS 100;
|
ANALYZE dbms_stats_export_schema.dbms_stats_export_hist;
|
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
|
'dbms_stats_export_hist_stat');
|
dbms_stats_create_stat_table
|
------------------------------
|
|
(1 row)
|
|
SELECT dbms_stats_export_table_stats(
|
ownname => 'dbms_stats_export_schema',
|
tabname => 'dbms_stats_export_hist',
|
stattab => 'dbms_stats_export_hist_stat',
|
statid => 'hist',
|
cascade => true,
|
stat_category => 'OBJECT_STATS');
|
NOTICE: column "big" histogram payload exceeds varchar2(1000); stored as NULL
|
dbms_stats_export_table_stats
|
-------------------------------
|
|
(1 row)
|
|
SELECT type, c4, n5, ch1 IS NULL AS ch1_null, r1 IS NULL AS r1_null,
|
r2 IS NULL AS r2_null
|
FROM dbms_stats_export_schema.dbms_stats_export_hist_stat
|
WHERE statid = 'hist' AND type = 'C' AND c4 = 'big';
|
type | c4 | n5 | ch1_null | r1_null | r2_null
|
------+-----+-----+----------+---------+---------
|
C | big | 101 | t | f | f
|
(1 row)
|
|
-- Cleanup.
|
DROP VIEW dbms_stats_export_schema.dbms_stats_export_view;
|
DROP TABLE dbms_stats_export_schema.dbms_stats_export_test;
|
DROP TABLE dbms_stats_export_schema.dbms_stats_export_empty;
|
DROP TABLE dbms_stats_export_schema.dbms_stats_export_part;
|
DROP TABLE dbms_stats_export_schema.dbms_stats_export_hist;
|
DROP TABLE dbms_stats_export_schema.dbms_stats_export_stat;
|
DROP TABLE dbms_stats_export_schema.dbms_stats_export_empty_stat;
|
DROP TABLE dbms_stats_export_schema.dbms_stats_export_part_stat;
|
DROP TABLE dbms_stats_export_schema.dbms_stats_export_narrow;
|
DROP TABLE dbms_stats_export_schema.dbms_stats_export_long_stat;
|
DROP TABLE dbms_stats_export_schema.dbms_stats_export_hist_stat;
|
DROP TABLE dbms_stats_export_stat_schema.dbms_stats_export_other_stat;
|
DROP SCHEMA dbms_stats_export_stat_schema;
|
DO $$
|
DECLARE
|
t name := repeat('t', 63);
|
BEGIN
|
EXECUTE format('DROP TABLE IF EXISTS dbms_stats_export_schema.%I', t);
|
END$$;
|
DROP OWNED BY dbms_stats_export_limited;
|
REVOKE ALL ON SCHEMA dbms_stats_export_schema FROM dbms_stats_export_limited;
|
DROP SCHEMA dbms_stats_export_schema;
|
DROP ROLE dbms_stats_export_limited;
|