59
59
p_attribute := lower (p_attribute);
60
60
PERFORM @
[email protected] _relation_checks(parent_relid, p_attribute);
61
61
62
+ IF p_count < 0 THEN
63
+ RAISE EXCEPTION ' Partitions count must not be less than zero' ;
64
+ END IF;
65
+
62
66
/* Try to determine partitions count if not set */
63
67
IF p_count IS NULL THEN
64
68
EXECUTE format(' SELECT count(*), max(%s) FROM %s' , p_attribute, parent_relid)
@@ -76,13 +80,19 @@ BEGIN
76
80
END LOOP;
77
81
END IF;
78
82
79
- /* Check boundaries */
80
- EXECUTE format(
' SELECT @[email protected] _boundaries(' ' %s' ' , ' ' %s' ' , ' ' %s' ' , ' ' %s' ' ::%s)' ,
81
- parent_relid,
82
- p_attribute,
83
- p_start_value,
84
- p_start_value + p_interval * p_count,
85
- pg_typeof(p_start_value));
83
+ /*
84
+ * In case when user doesn't want to automatically create partitions
85
+ * and specifies partition count as 0 then do not check boundaries
86
+ */
87
+ IF p_count != 0 THEN
88
+ /* Check boundaries */
89
+ EXECUTE format(
' SELECT @[email protected] _boundaries(' ' %s' ' , ' ' %s' ' , ' ' %s' ' , ' ' %s' ' ::%s)' ,
90
+ parent_relid,
91
+ p_attribute,
92
+ p_start_value,
93
+ p_start_value + p_interval * p_count,
94
+ pg_typeof(p_start_value));
95
+ END IF;
86
96
87
97
SELECT * INTO v_plain_schema, v_plain_relname
88
98
FROM @
[email protected] _plain_schema_and_relname(parent_relid);
@@ -147,8 +157,8 @@ BEGIN
147
157
p_attribute := lower (p_attribute);
148
158
PERFORM @
[email protected] _relation_checks(parent_relid, p_attribute);
149
159
150
- IF p_count <= 0 THEN
151
- RAISE EXCEPTION ' Partitions count must be greater than zero' ;
160
+ IF p_count < 0 THEN
161
+ RAISE EXCEPTION ' Partitions count must not be less than zero' ;
152
162
END IF;
153
163
154
164
/* Try to determine partitions count if not set */
@@ -172,11 +182,17 @@ BEGIN
172
182
END LOOP;
173
183
END IF;
174
184
175
- /* check boundaries */
176
- PERFORM @
[email protected] _boundaries(parent_relid,
177
- p_attribute,
178
- p_start_value,
179
- p_start_value + p_interval * p_count);
185
+ /*
186
+ * In case when user doesn't want to automatically create partitions
187
+ * and specifies partition count as 0 then do not check boundaries
188
+ */
189
+ IF p_count != 0 THEN
190
+ /* check boundaries */
191
+ PERFORM @
[email protected] _boundaries(parent_relid,
192
+ p_attribute,
193
+ p_start_value,
194
+ p_start_value + p_interval * p_count);
195
+ END IF;
180
196
181
197
SELECT * INTO v_plain_schema, v_plain_relname
182
198
FROM @
[email protected] _plain_schema_and_relname(parent_relid);
@@ -521,7 +537,8 @@ BEGIN
521
537
v_new_partition :
= @
[email protected] _single_range_partition(
522
538
@
[email protected] _schema_qualified_name(v_parent_relid),
523
539
p_value,
524
- p_range[2 ]);
540
+ p_range[2 ],
541
+ partition_name);
525
542
526
543
/* Copy data */
527
544
v_cond :
= @
[email protected] _range_condition(v_attname, p_value, p_range[
2 ]);
@@ -736,6 +753,10 @@ DECLARE
736
753
v_part_name TEXT ;
737
754
738
755
BEGIN
756
+ IF @
[email protected] _count(parent_relid)
= 0 THEN
757
+ RAISE EXCEPTION ' Cannot append to empty partitions set' ;
758
+ END IF;
759
+
739
760
p_range :
= @
[email protected] _range_by_idx(parent_relid,
- 1 ,
0 );
740
761
741
762
IF @
[email protected] _date_type(p_atttype::regtype) THEN
@@ -825,6 +846,10 @@ DECLARE
825
846
v_part_name TEXT ;
826
847
827
848
BEGIN
849
+ IF @
[email protected] _count(parent_relid)
= 0 THEN
850
+ RAISE EXCEPTION ' Cannot prepend to empty partitions set' ;
851
+ END IF;
852
+
828
853
p_range :
= @
[email protected] _range_by_idx(parent_relid,
0 ,
0 );
829
854
830
855
IF @
[email protected] _date_type(p_atttype::regtype) THEN
@@ -865,17 +890,17 @@ RETURNS TEXT AS
865
890
$$
866
891
DECLARE
867
892
v_part_name TEXT ;
868
-
869
893
BEGIN
870
- /* check range overlap */
871
- IF @
[email protected] _overlap(parent_relid, p_start_value, p_end_value) THEN
872
- RAISE EXCEPTION ' Specified range overlaps with existing partitions' ;
873
- END IF;
874
-
875
894
IF p_start_value >= p_end_value THEN
876
895
RAISE EXCEPTION ' Failed to create partition: p_start_value is greater than p_end_value' ;
877
896
END IF;
878
897
898
+ /* check range overlap */
899
+ IF @
[email protected] _count(parent_relid)
> 0
900
+ AND @
[email protected] _overlap(parent_relid, p_start_value, p_end_value) THEN
901
+ RAISE EXCEPTION ' Specified range overlaps with existing partitions' ;
902
+ END IF;
903
+
879
904
/* Create new partition */
880
905
v_part_name :
= @
[email protected] _single_range_partition(
881
906
parent_relid,
0 commit comments