forked from postgrespro/pg_pathman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range.sql
889 lines (750 loc) · 23.3 KB
/
range.sql
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/* ------------------------------------------------------------------------
*
* range.sql
* RANGE partitioning functions
*
* Copyright (c) 2015-2020, Postgres Professional
*
* ------------------------------------------------------------------------
*/
/*
* Check RANGE partition boundaries.
*/
CREATE FUNCTION @[email protected]_boundaries(
parent_relid REGCLASS,
expression TEXT,
start_value ANYELEMENT,
end_value ANYELEMENT)
RETURNS VOID AS $$
DECLARE
min_value start_value%TYPE;
max_value start_value%TYPE;
rows_count BIGINT;
BEGIN
/* Get min and max values */
EXECUTE pg_catalog.format('SELECT count(*), min(%1$s), max(%1$s)
FROM %2$s WHERE NOT %1$s IS NULL',
expression, parent_relid::TEXT)
INTO rows_count, min_value, max_value;
/* Check if column has NULL values */
IF rows_count > 0 AND (min_value IS NULL OR max_value IS NULL) THEN
RAISE EXCEPTION 'expression "%" returns NULL values', expression;
END IF;
/* Check lower boundary */
IF start_value > min_value THEN
RAISE EXCEPTION 'start value is greater than min value of "%"', expression;
END IF;
/* Check upper boundary */
IF end_value <= max_value THEN
RAISE EXCEPTION 'not enough partitions to fit all values of "%"', expression;
END IF;
END
$$ LANGUAGE plpgsql;
/*
* Creates RANGE partitions for specified relation based on datetime attribute
*/
CREATE FUNCTION @[email protected]_range_partitions(
parent_relid REGCLASS,
expression TEXT,
start_value ANYELEMENT,
p_interval INTERVAL,
p_count INTEGER DEFAULT NULL,
partition_data BOOLEAN DEFAULT TRUE)
RETURNS INTEGER AS $$
DECLARE
rows_count BIGINT;
max_value start_value%TYPE;
cur_value start_value%TYPE := start_value;
end_value start_value%TYPE;
part_count INTEGER := 0;
i INTEGER;
BEGIN
PERFORM @[email protected]_for_partitioning(parent_relid,
expression,
partition_data);
IF p_count < 0 THEN
RAISE EXCEPTION '"p_count" must not be less than 0';
END IF;
/* Try to determine partitions count if not set */
IF p_count IS NULL THEN
EXECUTE pg_catalog.format('SELECT count(*), max(%s) FROM %s', expression, parent_relid)
INTO rows_count, max_value;
IF rows_count = 0 THEN
RAISE EXCEPTION 'cannot determine partitions count for empty table';
END IF;
p_count := 0;
WHILE cur_value <= max_value
LOOP
cur_value := cur_value + p_interval;
p_count := p_count + 1;
END LOOP;
END IF;
/*
* In case when user doesn't want to automatically create partitions
* and specifies partition count as 0 then do not check boundaries
*/
IF p_count != 0 THEN
/* Compute right bound of partitioning through additions */
end_value := start_value;
FOR i IN 1..p_count
LOOP
end_value := end_value + p_interval;
END LOOP;
/* Check boundaries */
PERFORM @[email protected]_boundaries(parent_relid,
expression,
start_value,
end_value);
END IF;
/* Create sequence for child partitions names */
PERFORM @[email protected]_naming_sequence(parent_relid);
/* Insert new entry to pathman config */
PERFORM @[email protected]_to_pathman_config(parent_relid, expression,
p_interval::TEXT);
IF p_count != 0 THEN
part_count := @[email protected]_range_partitions_internal(
parent_relid,
@[email protected]_range_bounds(start_value,
p_interval,
p_count),
NULL,
NULL);
END IF;
/* Relocate data if asked to */
IF partition_data = true THEN
PERFORM @[email protected]_enable_parent(parent_relid, false);
PERFORM @[email protected]_data(parent_relid);
ELSE
PERFORM @[email protected]_enable_parent(parent_relid, true);
END IF;
RETURN part_count;
END
$$ LANGUAGE plpgsql;
/*
* Creates RANGE partitions for specified relation based on numerical expression
*/
CREATE FUNCTION @[email protected]_range_partitions(
parent_relid REGCLASS,
expression TEXT,
start_value ANYELEMENT,
p_interval ANYELEMENT,
p_count INTEGER DEFAULT NULL,
partition_data BOOLEAN DEFAULT TRUE)
RETURNS INTEGER AS $$
DECLARE
rows_count BIGINT;
max_value start_value%TYPE;
cur_value start_value%TYPE := start_value;
end_value start_value%TYPE;
part_count INTEGER := 0;
i INTEGER;
BEGIN
PERFORM @[email protected]_for_partitioning(parent_relid,
expression,
partition_data);
IF p_count < 0 THEN
RAISE EXCEPTION 'partitions count must not be less than zero';
END IF;
/* Try to determine partitions count if not set */
IF p_count IS NULL THEN
EXECUTE pg_catalog.format('SELECT count(*), max(%s) FROM %s', expression, parent_relid)
INTO rows_count, max_value;
IF rows_count = 0 THEN
RAISE EXCEPTION 'cannot determine partitions count for empty table';
END IF;
IF max_value IS NULL THEN
RAISE EXCEPTION 'expression "%" can return NULL values', expression;
END IF;
p_count := 0;
WHILE cur_value <= max_value
LOOP
cur_value := cur_value + p_interval;
p_count := p_count + 1;
END LOOP;
END IF;
/*
* In case when user doesn't want to automatically create partitions
* and specifies partition count as 0 then do not check boundaries
*/
IF p_count != 0 THEN
/* Compute right bound of partitioning through additions */
end_value := start_value;
FOR i IN 1..p_count
LOOP
end_value := end_value + p_interval;
END LOOP;
/* Check boundaries */
PERFORM @[email protected]_boundaries(parent_relid,
expression,
start_value,
end_value);
END IF;
/* Create sequence for child partitions names */
PERFORM @[email protected]_naming_sequence(parent_relid);
/* Insert new entry to pathman config */
PERFORM @[email protected]_to_pathman_config(parent_relid, expression,
p_interval::TEXT);
IF p_count != 0 THEN
part_count := @[email protected]_range_partitions_internal(
parent_relid,
@[email protected]_range_bounds(start_value,
p_interval,
p_count),
NULL,
NULL);
END IF;
/* Relocate data if asked to */
IF partition_data = true THEN
PERFORM @[email protected]_enable_parent(parent_relid, false);
PERFORM @[email protected]_data(parent_relid);
ELSE
PERFORM @[email protected]_enable_parent(parent_relid, true);
END IF;
RETURN p_count;
END
$$ LANGUAGE plpgsql;
/*
* Creates RANGE partitions for specified relation based on bounds array
*/
CREATE FUNCTION @[email protected]_range_partitions(
parent_relid REGCLASS,
expression TEXT,
bounds ANYARRAY,
partition_names TEXT[] DEFAULT NULL,
tablespaces TEXT[] DEFAULT NULL,
partition_data BOOLEAN DEFAULT TRUE)
RETURNS INTEGER AS $$
DECLARE
part_count INTEGER := 0;
BEGIN
IF array_ndims(bounds) > 1 THEN
RAISE EXCEPTION 'Bounds array must be a one dimensional array';
END IF;
IF array_length(bounds, 1) < 2 THEN
RAISE EXCEPTION 'Bounds array must have at least two values';
END IF;
PERFORM @[email protected]_for_partitioning(parent_relid,
expression,
partition_data);
/* Check boundaries */
PERFORM @[email protected]_boundaries(parent_relid,
expression,
bounds[1],
bounds[array_length(bounds, 1)]);
/* Create sequence for child partitions names */
PERFORM @[email protected]_naming_sequence(parent_relid);
/* Insert new entry to pathman config */
PERFORM @[email protected]_to_pathman_config(parent_relid, expression, NULL);
/* Create partitions */
part_count := @[email protected]_range_partitions_internal(parent_relid,
bounds,
partition_names,
tablespaces);
/* Relocate data if asked to */
IF partition_data = true THEN
PERFORM @[email protected]_enable_parent(parent_relid, false);
PERFORM @[email protected]_data(parent_relid);
ELSE
PERFORM @[email protected]_enable_parent(parent_relid, true);
END IF;
RETURN part_count;
END
$$
LANGUAGE plpgsql;
/*
* Append new partition.
*/
CREATE FUNCTION @[email protected]_range_partition(
parent_relid REGCLASS,
partition_name TEXT DEFAULT NULL,
tablespace TEXT DEFAULT NULL)
RETURNS TEXT AS $$
DECLARE
part_expr_type REGTYPE;
part_name TEXT;
part_interval TEXT;
BEGIN
PERFORM @[email protected]_relname(parent_relid);
/* Acquire lock on parent's scheme */
PERFORM @[email protected]_part_modification(parent_relid);
part_expr_type := @[email protected]_partition_key_type(parent_relid);
IF NOT @[email protected]_date_type(part_expr_type) AND
NOT @[email protected]_operator_supported(part_expr_type, '+') THEN
RAISE EXCEPTION 'type % does not support ''+'' operator', part_expr_type::REGTYPE;
END IF;
SELECT range_interval
FROM @[email protected]_config
WHERE partrel = parent_relid
INTO part_interval;
EXECUTE
pg_catalog.format('SELECT @[email protected]_partition_internal($1, $2, $3, ARRAY[]::%s[], $4, $5)',
@[email protected]_base_type(part_expr_type)::TEXT)
USING
parent_relid,
part_expr_type,
part_interval,
partition_name,
tablespace
INTO
part_name;
RETURN part_name;
END
$$ LANGUAGE plpgsql;
/*
* Spawn logic for append_partition(). We have to
* separate this in order to pass the 'p_range'.
*
* NOTE: we don't take a xact_handling lock here.
*/
CREATE FUNCTION @[email protected]_partition_internal(
parent_relid REGCLASS,
p_atttype REGTYPE,
p_interval TEXT,
p_range ANYARRAY DEFAULT NULL,
partition_name TEXT DEFAULT NULL,
tablespace TEXT DEFAULT NULL)
RETURNS TEXT AS $$
DECLARE
part_expr_type REGTYPE;
part_name TEXT;
v_args_format TEXT;
BEGIN
IF @[email protected]_number_of_partitions(parent_relid) = 0 THEN
RAISE EXCEPTION 'cannot append to empty partitions set';
END IF;
part_expr_type := @[email protected]_base_type(p_atttype);
/* We have to pass fake NULL casted to column's type */
EXECUTE pg_catalog.format('SELECT @[email protected]_part_range($1, -1, NULL::%s)',
part_expr_type::TEXT)
USING parent_relid
INTO p_range;
IF p_range[2] IS NULL THEN
RAISE EXCEPTION 'Cannot append partition because last partition''s range is half open';
END IF;
IF @[email protected]_date_type(p_atttype) THEN
v_args_format := pg_catalog.format('$1, $2, ($2 + $3::interval)::%s, $4, $5', part_expr_type::TEXT);
ELSE
v_args_format := pg_catalog.format('$1, $2, $2 + $3::%s, $4, $5', part_expr_type::TEXT);
END IF;
EXECUTE
pg_catalog.format('SELECT @[email protected]_single_range_partition(%s)', v_args_format)
USING
parent_relid,
p_range[2],
p_interval,
partition_name,
tablespace
INTO
part_name;
RETURN part_name;
END
$$ LANGUAGE plpgsql;
/*
* Prepend new partition.
*/
CREATE FUNCTION @[email protected]_range_partition(
parent_relid REGCLASS,
partition_name TEXT DEFAULT NULL,
tablespace TEXT DEFAULT NULL)
RETURNS TEXT AS $$
DECLARE
part_expr_type REGTYPE;
part_name TEXT;
part_interval TEXT;
BEGIN
PERFORM @[email protected]_relname(parent_relid);
/* Acquire lock on parent's scheme */
PERFORM @[email protected]_part_modification(parent_relid);
part_expr_type := @[email protected]_partition_key_type(parent_relid);
IF NOT @[email protected]_date_type(part_expr_type) AND
NOT @[email protected]_operator_supported(part_expr_type, '-') THEN
RAISE EXCEPTION 'type % does not support ''-'' operator', part_expr_type::REGTYPE;
END IF;
SELECT range_interval
FROM @[email protected]_config
WHERE partrel = parent_relid
INTO part_interval;
EXECUTE
pg_catalog.format('SELECT @[email protected]_partition_internal($1, $2, $3, ARRAY[]::%s[], $4, $5)',
@[email protected]_base_type(part_expr_type)::TEXT)
USING
parent_relid,
part_expr_type,
part_interval,
partition_name,
tablespace
INTO
part_name;
RETURN part_name;
END
$$ LANGUAGE plpgsql;
/*
* Spawn logic for prepend_partition(). We have to
* separate this in order to pass the 'p_range'.
*
* NOTE: we don't take a xact_handling lock here.
*/
CREATE FUNCTION @[email protected]_partition_internal(
parent_relid REGCLASS,
p_atttype REGTYPE,
p_interval TEXT,
p_range ANYARRAY DEFAULT NULL,
partition_name TEXT DEFAULT NULL,
tablespace TEXT DEFAULT NULL)
RETURNS TEXT AS $$
DECLARE
part_expr_type REGTYPE;
part_name TEXT;
v_args_format TEXT;
BEGIN
IF @[email protected]_number_of_partitions(parent_relid) = 0 THEN
RAISE EXCEPTION 'cannot prepend to empty partitions set';
END IF;
part_expr_type := @[email protected]_base_type(p_atttype);
/* We have to pass fake NULL casted to column's type */
EXECUTE pg_catalog.format('SELECT @[email protected]_part_range($1, 0, NULL::%s)',
part_expr_type::TEXT)
USING parent_relid
INTO p_range;
IF p_range[1] IS NULL THEN
RAISE EXCEPTION 'Cannot prepend partition because first partition''s range is half open';
END IF;
IF @[email protected]_date_type(p_atttype) THEN
v_args_format := pg_catalog.format('$1, ($2 - $3::interval)::%s, $2, $4, $5', part_expr_type::TEXT);
ELSE
v_args_format := pg_catalog.format('$1, $2 - $3::%s, $2, $4, $5', part_expr_type::TEXT);
END IF;
EXECUTE
pg_catalog.format('SELECT @[email protected]_single_range_partition(%s)', v_args_format)
USING
parent_relid,
p_range[1],
p_interval,
partition_name,
tablespace
INTO
part_name;
RETURN part_name;
END
$$ LANGUAGE plpgsql;
/*
* Add new partition
*/
CREATE FUNCTION @[email protected]_range_partition(
parent_relid REGCLASS,
start_value ANYELEMENT,
end_value ANYELEMENT,
partition_name TEXT DEFAULT NULL,
tablespace TEXT DEFAULT NULL)
RETURNS TEXT AS $$
DECLARE
part_name TEXT;
BEGIN
PERFORM @[email protected]_relname(parent_relid);
/* Acquire lock on parent's scheme */
PERFORM @[email protected]_part_modification(parent_relid);
IF start_value >= end_value THEN
RAISE EXCEPTION 'failed to create partition: start_value is greater than end_value';
END IF;
/* Check range overlap */
IF @[email protected]_number_of_partitions(parent_relid) > 0 THEN
PERFORM @[email protected]_range_available(parent_relid,
start_value,
end_value);
END IF;
/* Create new partition */
part_name := @[email protected]_single_range_partition(parent_relid,
start_value,
end_value,
partition_name,
tablespace);
RETURN part_name;
END
$$ LANGUAGE plpgsql;
/*
* Drop range partition
*/
CREATE FUNCTION @[email protected]_range_partition(
partition_relid REGCLASS,
delete_data BOOLEAN DEFAULT TRUE)
RETURNS TEXT AS $$
DECLARE
parent_relid REGCLASS;
part_name TEXT;
part_type INTEGER;
v_relkind CHAR;
v_rows BIGINT;
BEGIN
parent_relid := @[email protected]_parent_of_partition(partition_relid);
PERFORM @[email protected]_relname(parent_relid);
PERFORM @[email protected]_relname(partition_relid);
part_name := partition_relid::TEXT; /* save the name to be returned */
part_type := @[email protected]_partition_type(parent_relid);
/* Check if this is a RANGE partition */
IF part_type != 2 THEN
RAISE EXCEPTION '"%" is not a RANGE partition', partition_relid::TEXT;
END IF;
/* Acquire lock on parent's scheme */
PERFORM @[email protected]_part_modification(parent_relid);
IF NOT delete_data THEN
EXECUTE pg_catalog.format('INSERT INTO %s SELECT * FROM %s',
parent_relid::TEXT,
partition_relid::TEXT);
GET DIAGNOSTICS v_rows = ROW_COUNT;
/* Show number of copied rows */
RAISE NOTICE '% rows copied from %', v_rows, partition_relid::TEXT;
END IF;
SELECT relkind FROM pg_catalog.pg_class
WHERE oid = partition_relid
INTO v_relkind;
/*
* Determine the kind of child relation. It can be either regular
* table (r) or foreign table (f). Depending on relkind we use
* DROP TABLE or DROP FOREIGN TABLE.
*/
IF v_relkind = 'f' THEN
EXECUTE pg_catalog.format('DROP FOREIGN TABLE %s', partition_relid::TEXT);
ELSE
EXECUTE pg_catalog.format('DROP TABLE %s', partition_relid::TEXT);
END IF;
RETURN part_name;
END
$$ LANGUAGE plpgsql
SET pg_pathman.enable_partitionfilter = off; /* ensures that PartitionFilter is OFF */
/*
* Attach range partition
*/
CREATE FUNCTION @[email protected]_range_partition(
parent_relid REGCLASS,
partition_relid REGCLASS,
start_value ANYELEMENT,
end_value ANYELEMENT)
RETURNS TEXT AS $$
DECLARE
part_expr TEXT;
part_type INTEGER;
rel_persistence CHAR;
v_init_callback REGPROCEDURE;
BEGIN
PERFORM @[email protected]_relname(parent_relid);
PERFORM @[email protected]_relname(partition_relid);
/* Acquire lock on parent's scheme */
PERFORM @[email protected]_part_modification(parent_relid);
/* Ignore temporary tables */
SELECT relpersistence FROM pg_catalog.pg_class
WHERE oid = partition_relid INTO rel_persistence;
IF rel_persistence = 't'::CHAR THEN
RAISE EXCEPTION 'temporary table "%" cannot be used as a partition',
partition_relid::TEXT;
END IF;
/* Check range overlap */
PERFORM @[email protected]_range_available(parent_relid, start_value, end_value);
BEGIN
PERFORM @[email protected]_tuple_convertible(parent_relid, partition_relid);
EXCEPTION WHEN OTHERS THEN
RAISE EXCEPTION 'partition must have a compatible tuple format';
END;
part_expr := @[email protected]_partition_key(parent_relid);
part_type := @[email protected]_partition_type(parent_relid);
IF part_expr IS NULL THEN
RAISE EXCEPTION 'table "%" is not partitioned', parent_relid::TEXT;
END IF;
/* Check if this is a RANGE partition */
IF part_type != 2 THEN
RAISE EXCEPTION '"%" is not a RANGE partition', partition_relid::TEXT;
END IF;
/* Set inheritance */
EXECUTE pg_catalog.format('ALTER TABLE %s INHERIT %s', partition_relid, parent_relid);
/* Set check constraint */
EXECUTE pg_catalog.format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)',
partition_relid::TEXT,
@[email protected]_check_constraint_name(partition_relid),
@[email protected]_range_condition(partition_relid,
part_expr,
start_value,
end_value));
/* Fetch init_callback from 'params' table */
WITH stub_callback(stub) as (values (0))
SELECT init_callback
FROM stub_callback
LEFT JOIN @[email protected]_config_params AS params
ON params.partrel = parent_relid
INTO v_init_callback;
/* Invoke an initialization callback */
PERFORM @[email protected]_on_partition_created_callback(parent_relid,
partition_relid,
v_init_callback,
start_value,
end_value);
RETURN partition_relid;
END
$$ LANGUAGE plpgsql;
/*
* Detach range partition
*/
CREATE FUNCTION @[email protected]_range_partition(
partition_relid REGCLASS)
RETURNS TEXT AS $$
DECLARE
parent_relid REGCLASS;
part_type INTEGER;
BEGIN
parent_relid := @[email protected]_parent_of_partition(partition_relid);
PERFORM @[email protected]_relname(parent_relid);
PERFORM @[email protected]_relname(partition_relid);
/* Acquire lock on partition's scheme */
PERFORM @[email protected]_part_modification(partition_relid);
/* Acquire lock on parent */
PERFORM @[email protected]_data_modification(parent_relid);
part_type := @[email protected]_partition_type(parent_relid);
/* Check if this is a RANGE partition */
IF part_type != 2 THEN
RAISE EXCEPTION '"%" is not a RANGE partition', partition_relid::TEXT;
END IF;
/* Remove inheritance */
EXECUTE pg_catalog.format('ALTER TABLE %s NO INHERIT %s',
partition_relid::TEXT,
parent_relid::TEXT);
/* Remove check constraint */
EXECUTE pg_catalog.format('ALTER TABLE %s DROP CONSTRAINT %s',
partition_relid::TEXT,
@[email protected]_check_constraint_name(partition_relid));
RETURN partition_relid;
END
$$ LANGUAGE plpgsql;
/*
* Create a naming sequence for partitioned table.
*/
CREATE FUNCTION @[email protected]_naming_sequence(
parent_relid REGCLASS)
RETURNS TEXT AS $$
DECLARE
seq_name TEXT;
BEGIN
seq_name := @[email protected]_sequence_name(parent_relid);
EXECUTE pg_catalog.format('DROP SEQUENCE IF EXISTS %s', seq_name);
EXECUTE pg_catalog.format('CREATE SEQUENCE %s START 1', seq_name);
RETURN seq_name;
END
$$ LANGUAGE plpgsql
SET client_min_messages = WARNING; /* mute NOTICE message */
/*
* Drop a naming sequence for partitioned table.
*/
CREATE FUNCTION @[email protected]_naming_sequence(
parent_relid REGCLASS)
RETURNS VOID AS $$
DECLARE
seq_name TEXT;
BEGIN
seq_name := @[email protected]_sequence_name(parent_relid);
EXECUTE pg_catalog.format('DROP SEQUENCE IF EXISTS %s', seq_name);
END
$$ LANGUAGE plpgsql
SET client_min_messages = WARNING; /* mute NOTICE message */
/*
* Split RANGE partition in two using a pivot.
*/
CREATE FUNCTION @[email protected]_range_partition(
partition_relid REGCLASS,
split_value ANYELEMENT,
partition_name TEXT DEFAULT NULL,
tablespace TEXT DEFAULT NULL)
RETURNS REGCLASS AS 'pg_pathman', 'split_range_partition'
LANGUAGE C;
/*
* Merge RANGE partitions.
*/
CREATE FUNCTION @[email protected]_range_partitions(
variadic partitions REGCLASS[])
RETURNS REGCLASS AS 'pg_pathman', 'merge_range_partitions'
LANGUAGE C STRICT;
/*
* Drops partition and expands the next partition so that it cover dropped one
*
* This function was written in order to support Oracle-like ALTER TABLE ...
* DROP PARTITION. In Oracle partitions only have upper bound and when
* partition is dropped the next one automatically covers freed range
*/
CREATE FUNCTION @[email protected]_range_partition_expand_next(
partition_relid REGCLASS)
RETURNS VOID AS 'pg_pathman', 'drop_range_partition_expand_next'
LANGUAGE C STRICT;
CREATE FUNCTION @[email protected]_range_partitions_internal(
parent_relid REGCLASS,
bounds ANYARRAY,
partition_names TEXT[],
tablespaces TEXT[])
RETURNS REGCLASS AS 'pg_pathman', 'create_range_partitions_internal'
LANGUAGE C;
/*
* Creates new RANGE partition. Returns partition name.
* NOTE: This function SHOULD NOT take xact_handling lock (BGWs in 9.5).
*/
CREATE FUNCTION @[email protected]_single_range_partition(
parent_relid REGCLASS,
start_value ANYELEMENT,
end_value ANYELEMENT,
partition_name TEXT DEFAULT NULL,
tablespace TEXT DEFAULT NULL)
RETURNS REGCLASS AS 'pg_pathman', 'create_single_range_partition_pl'
LANGUAGE C;
/*
* Construct CHECK constraint condition for a range partition.
*/
CREATE FUNCTION @[email protected]_range_condition(
partition_relid REGCLASS,
expression TEXT,
start_value ANYELEMENT,
end_value ANYELEMENT)
RETURNS TEXT AS 'pg_pathman', 'build_range_condition'
LANGUAGE C;
/*
* Generate a name for naming sequence.
*/
CREATE FUNCTION @[email protected]_sequence_name(
parent_relid REGCLASS)
RETURNS TEXT AS 'pg_pathman', 'build_sequence_name'
LANGUAGE C STRICT;
/*
* Returns N-th range (as an array of two elements).
*/
CREATE FUNCTION @[email protected]_part_range(
parent_relid REGCLASS,
partition_idx INTEGER,
dummy ANYELEMENT)
RETURNS ANYARRAY AS 'pg_pathman', 'get_part_range_by_idx'
LANGUAGE C;
/*
* Returns min and max values for specified RANGE partition.
*/
CREATE FUNCTION @[email protected]_part_range(
partition_relid REGCLASS,
dummy ANYELEMENT)
RETURNS ANYARRAY AS 'pg_pathman', 'get_part_range_by_oid'
LANGUAGE C;
/*
* Checks if range overlaps with existing partitions.
* Returns TRUE if overlaps and FALSE otherwise.
*/
CREATE FUNCTION @[email protected]_range_available(
parent_relid REGCLASS,
range_min ANYELEMENT,
range_max ANYELEMENT)
RETURNS VOID AS 'pg_pathman', 'check_range_available_pl'
LANGUAGE C;
/*
* Generate range bounds starting with 'p_start' using 'p_interval'.
*/
CREATE FUNCTION @[email protected]_range_bounds(
p_start ANYELEMENT,
p_interval INTERVAL,
p_count INTEGER)
RETURNS ANYARRAY AS 'pg_pathman', 'generate_range_bounds_pl'
LANGUAGE C STRICT;
CREATE FUNCTION @[email protected]_range_bounds(
p_start ANYELEMENT,
p_interval ANYELEMENT,
p_count INTEGER)
RETURNS ANYARRAY AS 'pg_pathman', 'generate_range_bounds_pl'
LANGUAGE C STRICT;