Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
-- Tests with various custom types
CREATE EXTENSION test_custom_types;
-- Test comparison functions
SELECT '42'::int_custom = '42'::int_custom AS eq_test;
 eq_test 
---------
 t
(1 row)
 
SELECT '42'::int_custom <> '42'::int_custom AS nt_test;
 nt_test 
---------
 f
(1 row)
 
SELECT '42'::int_custom < '100'::int_custom AS lt_test;
 lt_test 
---------
 t
(1 row)
 
SELECT '100'::int_custom > '42'::int_custom AS gt_test;
 gt_test 
---------
 t
(1 row)
 
SELECT '42'::int_custom <= '100'::int_custom AS le_test;
 le_test 
---------
 t
(1 row)
 
SELECT '100'::int_custom >= '42'::int_custom AS ge_test;
 ge_test 
---------
 t
(1 row)
 
-- Create a table with the int_custom type
CREATE TABLE test_table (
    id int,
    data int_custom
);
INSERT INTO test_table VALUES (1, '42'), (2, '100'), (3, '200');
-- Verify data was inserted correctly
SELECT * FROM test_table ORDER BY id;
 id | data 
----+------
  1 | 42
  2 | 100
  3 | 200
(3 rows)
 
-- Dummy function used for expression evaluations.
-- Note that this function does not use a SQL-standard function body on
-- purpose, so as external statistics can be loaded from it.
CREATE OR REPLACE FUNCTION func_int_custom (p_value int_custom)
  RETURNS int_custom LANGUAGE plpgsql AS $$
  BEGIN
    RETURN p_value;
  END; $$;
-- Switch type to use typanalyze function that always returns false.
ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_false);
-- Extended statistics with an attribute that cannot be analyzed.
-- This includes all statistics kinds.
CREATE STATISTICS test_stats ON data, id FROM test_table;
-- Computation of the stats fails, no data generated.
ANALYZE test_table;
WARNING:  statistics object "public.test_stats" could not be computed for relation "public.test_table"
SELECT stxname, stxdexpr IS NULL as expr_stats_is_null
  FROM pg_statistic_ext s
  LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid
  WHERE stxname = 'test_stats';
  stxname   | expr_stats_is_null 
------------+--------------------
 test_stats | t
(1 row)
 
DROP STATISTICS test_stats;
-- Extended statistics with an expression that cannot be analyzed.
CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table;
-- Computation of the stats fails, no data generated.
ANALYZE test_table;
WARNING:  statistics object "public.test_stats" could not be computed for relation "public.test_table"
SELECT stxname, stxdexpr IS NULL as expr_stats_is_null
  FROM pg_statistic_ext s
  LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid
  WHERE stxname = 'test_stats';
  stxname   | expr_stats_is_null 
------------+--------------------
 test_stats | t
(1 row)
 
DROP STATISTICS test_stats;
-- There should be no data stored for the expression.
SELECT tablename,
  statistics_name,
  null_frac,
  avg_width
  FROM pg_stats_ext_exprs WHERE statistics_name = 'test_stats' \gx
(0 rows)
 
-- Switch type to use typanalyze function that generates invalid data.
ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_invalid);
-- Extended statistics with an attribute that generates invalid stats.
CREATE STATISTICS test_stats ON data, id FROM test_table;
-- Computation of the stats fails, no data generated.
ANALYZE test_table;
SELECT stxname, stxdexpr IS NULL as expr_stats_is_null
  FROM pg_statistic_ext s
  LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid
  WHERE stxname = 'test_stats';
  stxname   | expr_stats_is_null 
------------+--------------------
 test_stats | t
(1 row)
 
DROP STATISTICS test_stats;
-- Extended statistics with an expression that generates invalid data.
CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table;
-- Computation of the stats fails, some data generated.
ANALYZE test_table;
SELECT stxname, stxdexpr IS NULL as expr_stats_is_null
  FROM pg_statistic_ext s
  LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid
  WHERE stxname = 'test_stats';
  stxname   | expr_stats_is_null 
------------+--------------------
 test_stats | f
(1 row)
 
-- There should be some data stored for the expression, stored as NULL.
SELECT tablename,
  statistics_name,
  null_frac,
  avg_width,
  n_distinct,
  most_common_vals,
  most_common_freqs,
  histogram_bounds,
  correlation,
  most_common_elems,
  most_common_elem_freqs,
  elem_count_histogram,
  range_length_histogram,
  range_empty_frac,
  range_bounds_histogram
  FROM pg_stats_ext_exprs WHERE statistics_name = 'test_stats' \gx
-[ RECORD 1 ]----------+-----------
tablename              | test_table
statistics_name        | test_stats
null_frac              | 
avg_width              | 
n_distinct             | 
most_common_vals       | 
most_common_freqs      | 
histogram_bounds       | 
correlation            | 
most_common_elems      | 
most_common_elem_freqs | 
elem_count_histogram   | 
range_length_histogram | 
range_empty_frac       | 
range_bounds_histogram | 
 
-- Run a query able to load the extended stats, including the NULL data.
SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data));
 count 
-------
     1
     1
     1
(3 rows)
 
DROP STATISTICS test_stats;
-- Switch type to use typanalyze function that returns false with stats
-- target.
ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_false_target);
-- Expression rejected at evaluation.
CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table;
-- Computation of the stats fails, no data generated.
ANALYZE test_table;
WARNING:  statistics object "public.test_stats" could not be computed for relation "public.test_table"
SELECT stxname, d.stxoid IS NULL AS no_data
  FROM pg_statistic_ext s
  LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid
  WHERE stxname = 'test_stats';
  stxname   | no_data 
------------+---------
 test_stats | t
(1 row)
 
DROP STATISTICS test_stats;
-- Switch type to use typanalyze function that returns false on second call
-- with a stats target.
ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_false_second);
-- Expression rejected at build.
CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table;
-- Computation of the stats fails, no data generated.
ANALYZE test_table;
WARNING:  statistics object "public.test_stats" could not be computed for relation "public.test_table"
SELECT stxname, d.stxoid IS NULL AS no_data
  FROM pg_statistic_ext s
  LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid
  WHERE stxname = 'test_stats';
  stxname   | no_data 
------------+---------
 test_stats | t
(1 row)
 
DROP STATISTICS test_stats;
-- Cleanup
DROP FUNCTION func_int_custom;
DROP TABLE test_table;
DROP EXTENSION test_custom_types;