-
Notifications
You must be signed in to change notification settings - Fork 767
Expand file tree
/
Copy pathpropagate_statistics.sql
More file actions
134 lines (109 loc) · 3.53 KB
/
propagate_statistics.sql
File metadata and controls
134 lines (109 loc) · 3.53 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
CREATE SCHEMA "statistics'Test";
SET search_path TO "statistics'Test";
SET citus.next_shard_id TO 980000;
SET client_min_messages TO WARNING;
SET citus.shard_count TO 32;
SET citus.shard_replication_factor TO 1;
-- test create statistics propagation
CREATE TABLE test_stats (
a int,
b int
);
SELECT create_distributed_table('test_stats', 'a');
CREATE STATISTICS pg_temp.s1 (dependencies) ON a, b FROM test_stats;
CREATE STATISTICS s1 (dependencies) ON a, b FROM test_stats;
-- test for distributing an already existing statistics
CREATE TABLE "test'stats2" (
a int,
b int
);
CREATE STATISTICS s2 (dependencies) ON a, b FROM "test'stats2";
SELECT create_distributed_table('test''stats2', 'a');
-- test when stats is on a different schema
CREATE SCHEMA sc1;
CREATE TABLE tbl (a int, "B" text);
SELECT create_distributed_table ('tbl', 'a');
CREATE STATISTICS sc1.st1 ON a, "B" FROM tbl;
-- test distributing table with already created stats on a new schema
CREATE TABLE test_stats3 (
a int,
b int
);
CREATE SCHEMA sc2;
CREATE STATISTICS sc2."neW'Stat" ON a,b FROM test_stats3;
SELECT create_distributed_table ('test_stats3','a');
-- test creating custom stats with expressions and distributing it.
CREATE TABLE sc2.test_stats_expr (
a int,
b int,
c float8
);
CREATE STATISTICS s_expr ON (a + b / 2) FROM sc2.test_stats_expr;
-- succeeds since we replicate it into the shards.
SELECT create_distributed_table('sc2.test_stats_expr', 'a');
-- add expression stats on the distributed table should work.
CREATE STATISTICS s_expr_post ON (a - (b * 2)), round(c) FROM sc2.test_stats_expr;
-- test dropping statistics
CREATE TABLE test_stats4 (
a int,
b int
);
SELECT create_reference_table ('test_stats4');
CREATE STATISTICS s3 ON a,b FROM test_stats3;
CREATE STATISTICS sc2.s4 ON a,b FROM test_stats3;
CREATE STATISTICS s5 ON a,b FROM test_stats4;
-- s6 doesn't exist
DROP STATISTICS IF EXISTS s3, sc2.s4, s6;
DROP STATISTICS s5,s6;
DROP STATISTICS IF EXISTS s5,s5,s6,s6;
-- test renaming statistics
CREATE STATISTICS s6 ON a,b FROM test_stats4;
DROP STATISTICS s7;
ALTER STATISTICS s6 RENAME TO s7;
ALTER STATISTICS sc1.st1 RENAME TO st1_new;
-- test altering stats schema
CREATE SCHEMA test_alter_schema;
ALTER STATISTICS s7 SET SCHEMA test_alter_schema;
-- test alter owner
ALTER STATISTICS sc2."neW'Stat" OWNER TO pg_monitor;
-- test alter owner before distribution
CREATE TABLE ownertest(a int, b int);
CREATE STATISTICS sc1.s9 ON a,b FROM ownertest;
ALTER STATISTICS sc1.s9 OWNER TO pg_signal_backend;
SELECT create_distributed_table('ownertest','a');
-- test invalid column expressions
CREATE TABLE test (x int, y int);
SELECT create_distributed_table('test','x');
CREATE STATISTICS stats_xy ON x,y FROM test;
\c - - - :worker_1_port
SELECT stxname
FROM pg_statistic_ext
WHERE stxnamespace IN (
SELECT oid
FROM pg_namespace
WHERE nspname IN ('public', 'statistics''Test', 'sc1', 'sc2')
)
AND stxname SIMILAR TO '%\_\d+'
ORDER BY stxname ASC;
SELECT count(DISTINCT stxnamespace)
FROM pg_statistic_ext
WHERE stxnamespace IN (
SELECT oid
FROM pg_namespace
WHERE nspname IN ('public', 'statistics''Test', 'sc1', 'sc2')
)
AND stxname SIMILAR TO '%\_\d+';
SELECT COUNT(DISTINCT stxowner)
FROM pg_statistic_ext
WHERE stxnamespace IN (
SELECT oid
FROM pg_namespace
WHERE nspname IN ('public', 'statistics''Test', 'sc1', 'sc2')
)
AND stxname SIMILAR TO '%\_\d+';
\c - - - :master_port
SET client_min_messages TO WARNING;
DROP SCHEMA "statistics'Test" CASCADE;
DROP SCHEMA test_alter_schema CASCADE;
DROP SCHEMA sc1 CASCADE;
DROP SCHEMA sc2 CASCADE;