File: partitions.sql

package info (click to toggle)
pgtap 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,792 kB
  • sloc: sql: 25,795; sh: 790; makefile: 287; perl: 175
file content (349 lines) | stat: -rw-r--r-- 9,241 bytes parent folder | download | duplicates (3)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
\unset ECHO
\i test/setup.sql

SELECT plan(102);
--SELECT * FROM no_plan();

-- This will be rolled back. :-)
SET client_min_messages = warning;

-- Create inherited tables (not partitions).
CREATE TABLE public.base(id INT PRIMARY KEY);
CREATE TABLE public.sub(id INT PRIMARY KEY) INHERITS (public.base);

-- Create a partitioned table with two partitions.
CREATE TABLE public.parted(id INT NOT NULL) PARTITION BY RANGE (id);
CREATE TABLE public.part1 PARTITION OF public.parted FOR VALUES FROM (1) TO (10);
CREATE TABLE public.part2 PARTITION OF public.parted FOR VALUES FROM (11) TO (20);

-- Create partitions outside of search path.
CREATE SCHEMA hide;
CREATE TABLE hide.hidden_parted(id INT NOT NULL) PARTITION BY RANGE (id);
CREATE TABLE hide.hidden_part1 PARTITION OF hide.hidden_parted FOR VALUES FROM (1) TO (10);
CREATE TABLE hide.hidden_part2 PARTITION OF hide.hidden_parted FOR VALUES FROM (11) TO (20);

-- Put a partition for the public table in the hidden schema.
CREATE TABLE hide.part3 PARTITION OF public.parted FOR VALUES FROM (21) TO (30);

-- Put a partition for the hidden table in the public schema.
CREATE TABLE public.not_hidden_part3 PARTITION OF hide.hidden_parted
   FOR VALUES FROM (21) TO (30);

RESET client_min_messages;

/****************************************************************************/
-- Test is_partition_of().
SELECT * FROM check_test(
    is_partition_of( 'public', 'part1', 'public', 'parted', 'whatevs' ),
    true,
    'is_partition_of( csch, ctab, psch, ptab, desc )',
    'whatevs',
    ''
);

SELECT * FROM check_test(
    is_partition_of( 'public', 'part1', 'public', 'parted' ),
    true,
    'is_partition_of( csch, ctab, psch, ptab )',
    'Table public.part1 should be a partition of public.parted',
    ''
);

SELECT * FROM check_test(
    is_partition_of( 'part1', 'parted', 'whatevs' ),
    true,
    'is_partition_of( ctab, ptab, desc )',
    'whatevs',
    ''
);

SELECT * FROM check_test(
    is_partition_of( 'part1', 'parted' ),
    true,
    'is_partition_of( ctab, ptab )',
    'Table part1 should be a partition of parted',
    ''
);

-- is_partition_of() should fail for inherited but not partitioned tables.
SELECT * FROM check_test(
    is_partition_of( 'public', 'sub', 'public', 'base', 'whatevs' ),
    false,
    'is_partition_of( csch, non-part ctab, psch, non-part ptab, desc )',
    'whatevs',
    ''
);

SELECT * FROM check_test(
    is_partition_of( 'sub', 'base', 'whatevs' ),
    false,
    'is_partition_of( non-part ctab, non-part ptab, desc )',
    'whatevs',
    ''
);

-- is_partition_of() should fail for parted table and non-part sub.
SELECT * FROM check_test(
    is_partition_of( 'public', 'sub', 'public', 'parted', 'whatevs' ),
    false,
    'is_partition_of( csch, non-part ctab, psch, ptab, desc )',
    'whatevs',
    ''
);

SELECT * FROM check_test(
    is_partition_of( 'sub', 'parted', 'whatevs' ),
    false,
    'is_partition_of( non-part ctab, ptab, desc )',
    'whatevs',
    ''
);

-- is_partition_of() should fail for partition sub but wrong base.
SELECT * FROM check_test(
    is_partition_of( 'public', 'part1', 'public', 'base', 'whatevs' ),
    false,
    'is_partition_of( csch, ctab, psch, non-part ptab, desc )',
    'whatevs',
    ''
);

SELECT * FROM check_test(
    is_partition_of( 'part1', 'base', 'whatevs' ),
    false,
    'is_partition_of( ctab, non-part ptab, desc )',
    'whatevs',
    ''
);

-- Should find tables outside search path for explicit schema.
SELECT * FROM check_test(
    is_partition_of( 'hide', 'hidden_part1', 'hide', 'hidden_parted', 'whatevs' ),
    true,
    'is_partition_of( priv csch, ctab, priv psch, ptab, desc )',
    'whatevs',
    ''
);

-- But not when the schema is not specified.
SELECT * FROM check_test(
    is_partition_of( 'hidden_part1', 'hidden_parted', 'whatevs' ),
    false,
    'is_partition_of( priv ctab, priv ptab, desc )',
    'whatevs',
    ''
);

-- Should find explicit hidden table for public partition.
SELECT * FROM check_test(
    is_partition_of( 'hide', 'part3', 'public', 'parted', 'whatevs' ),
    true,
    'is_partition_of( priv csch, ctab, psch, ptab, desc )',
    'whatevs',
    ''
);

-- But still not when schemas not specified.
SELECT * FROM check_test(
    is_partition_of( 'part3', 'hidden', 'whatevs' ),
    false,
    'is_partition_of( priv ctab, ptab, desc )',
    'whatevs',
    ''
);

-- Should find public partition for hidden base.
SELECT * FROM check_test(
    is_partition_of( 'public', 'not_hidden_part3', 'hide', 'hidden_parted', 'whatevs' ),
    true,
    'is_partition_of( csch, ctab, priv psch, ptab, desc )',
    'whatevs',
    ''
);

-- But not if no schemas are specified.
SELECT * FROM check_test(
    is_partition_of( 'not_hidden_part3', 'hidden_parted', 'whatevs' ),
    false,
    'is_partition_of( ctab, priv ptab, desc )',
    'whatevs',
    ''
);

-- And of course, it should not work for nonexistent partitions.
SELECT * FROM check_test(
    is_partition_of( 'public', 'nonesuch', 'public', 'nothing', 'whatevs' ),
    false,
    'is_partition_of( csch, non-ctab, psch, non-ptab, desc )',
    'whatevs',
    ''
);

SELECT * FROM check_test(
    is_partition_of( 'nonesuch', 'nothing', 'whatevs' ),
    false,
    'is_partition_of( non-ctab, non-ptab, desc )',
    'whatevs',
    ''
);

SELECT * FROM check_test(
    is_partition_of( 'public', 'part1', 'public', 'nothing', 'whatevs' ),
    false,
    'is_partition_of( csch, ctab, psch, non-ptab, desc )',
    'whatevs',
    ''
);

SELECT * FROM check_test(
    is_partition_of( 'nonesuch', 'part1', 'whatevs' ),
    false,
    'is_partition_of( ctab, non-ptab, desc )',
    'whatevs',
    ''
);

SELECT * FROM check_test(
    is_partition_of( 'public', 'nonesuch', 'public', 'parted', 'whatevs' ),
    false,
    'is_partition_of( csch, non-ctab, psch, ptab, desc )',
    'whatevs',
    ''
);

SELECT * FROM check_test(
    is_partition_of( 'nonesuch', 'parted', 'whatevs' ),
    false,
    'is_partition_of( non-ctab, ptab, desc )',
    'whatevs',
    ''
);

/****************************************************************************/
-- Test partitions_are().
SELECT * FROM check_test(
    partitions_are( 'public', 'parted', '{part1,part2,hide.part3}', 'hi' ),
    true,
    'partitions_are( sch, tab, parts, desc )',
    'hi',
    ''
);

SELECT * FROM check_test(
    partitions_are( 'public', 'parted', '{part1,part2,hide.part3}'::name[] ),
    true,
    'partitions_are( sch, tab, parts )',
    'Table public.parted should have the correct partitions',
    ''
);

SELECT * FROM check_test(
    partitions_are( 'parted', '{part1,part2,hide.part3}'::name[], 'hi' ),
    true,
    'partitions_are( tab, parts, desc )',
    'hi',
    ''
);

SELECT * FROM check_test(
    partitions_are( 'parted', '{part1,part2,hide.part3}' ),
    true,
    'partitions_are( tab, parts )',
    'Table parted should have the correct partitions',
    ''
);

-- Test diagnostics.
SELECT * FROM check_test(
    partitions_are( 'public', 'parted', '{part1,part2of2,hide.part3}', 'hi' ),
    false,
    'partitions_are( sch, tab, bad parts, desc )',
    'hi',
    '    Extra partitions:
        part2
    Missing partitions:
        part2of2'
);

SELECT * FROM check_test(
    partitions_are( 'parted', '{part1,part2of2,hide.part3}'::name[], 'hi' ),
    false,
    'partitions_are( tab, bad parts, desc )',
    'hi',
    '    Extra partitions:
        part2
    Missing partitions:
        part2of2'
);

-- Test with the hidden schema.
SELECT * FROM check_test(
    partitions_are(
        'hide', 'hidden_parted',
        '{hide.hidden_part1,hide.hidden_part2,not_hidden_part3}',
        'hi'
    ),
    true,
    'partitions_are( hidden sch, tab, parts, desc )',
    'hi',
    ''
);

-- Should fail for partitioned table outside search path.
SELECT * FROM check_test(
    partitions_are(
        'hidden_parted',
        '{hide.hidden_part1,hide.hidden_part2,not_hidden_part3}'::name[],
        'hi'
    ),
    false,
    'partitions_are( hidden tab, parts, desc )',
    'hi',
    '    Missing partitions:
        "hide.hidden_part1"
        "hide.hidden_part2"
        not_hidden_part3'
);

-- Should not work for unpartitioned but inherited table
SELECT * FROM check_test(
    partitions_are( 'public', 'base', '{sub}', 'hi' ),
    false,
    'partitions_are( sch, non-parted tab, inherited tab, desc )',
    'hi',
    '    Missing partitions:
        sub'
);

SELECT * FROM check_test(
    partitions_are( 'base', '{sub}'::name[], 'hi' ),
    false,
    'partitions_are( non-parted tab, inherited tab, desc )',
    'hi',
    '    Missing partitions:
        sub'
);

-- Should not work for non-existent table.
SELECT * FROM check_test(
    partitions_are( 'public', 'nonesuch', '{part1}', 'hi' ),
    false,
    'partitions_are( sch, non-existent tab, parts, desc )',
    'hi',
    '    Missing partitions:
        part1'
);

SELECT * FROM check_test(
    partitions_are( 'nonesuch', '{part1}'::name[], 'hi' ),
    false,
    'partitions_are( non-existent tab, parts, desc )',
    'hi',
    '    Missing partitions:
        part1'
);

/****************************************************************************/
-- Finish the tests and clean up.
SELECT * FROM finish();
ROLLBACK;