Skip to content

Commit e36c5ec

Browse files
committed
PARTITION BY SYSTEM_TIME INTERVAL ...
Lots of changes: * calculate the current history partition in ::external_lock(), not in ::write_row() or ::update_row() * remove dynamically collected per-partition row_end stats * no full table scan in open_table_from_share to calculate these stats, no manual MDL/thr_locks in open_table_from_share * no shared stats in TABLE_SHARE = no mutexes or condition waits when calculating current history partition * always compare timestamps, don't convert them to MYSQL_TIME (avoid DST ambiguity, and it's faster too) * correct interval handling, 1 month = 1 month, not 30 * 24 * 3600 seconds * save/restore first partition start time, and count intervals from there * only allow to drop first partitions if INTERVAL * when adding new history partitions, split the data in the last history parition, if it was overflowed * show partition boundaries in INFORMATION_SCHEMA.PARTITIONS
1 parent 7961bc4 commit e36c5ec

File tree

16 files changed

+462
-973
lines changed

16 files changed

+462
-973
lines changed

mysql-test/suite/versioning/r/partition.result

Lines changed: 122 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,6 @@ x A B
137137
execute select_pn;
138138
x C D
139139
1 1 1
140-
## pruning check
141-
explain partitions select * from tN;
142-
id select_type table partitions type possible_keys key key_len ref rows Extra
143-
N SIMPLE tN pN,pn system NULL NULL NULL NULL N
144140
set @str= concat('select row_start from t1 partition (pn) into @ts0');
145141
prepare stmt from @str;
146142
execute stmt;
@@ -225,42 +221,48 @@ t1 CREATE TABLE `t1` (
225221
PARTITION `pn` CURRENT ENGINE = DEFAULT_ENGINE)
226222
alter table t1 drop partition non_existent;
227223
ERROR HY000: Error in list of partitions to DROP
228-
insert into t1 values (1), (2), (3);
224+
insert into t1 values (1), (2), (3), (4), (5), (6);
229225
select * from t1 partition (pn);
230226
x
231227
1
232228
2
233229
3
234-
### warn about partition switching
230+
4
231+
5
232+
6
233+
delete from t1 where x < 4;
235234
delete from t1;
236-
Warnings:
237-
Note 4114 Versioned table `test`.`t1`: switching from partition `p0` to `p1`
238235
select * from t1 partition (p0);
239236
x
240237
1
241238
2
239+
3
242240
select * from t1 partition (p1);
243241
x
244-
3
245-
insert into t1 values (4), (5);
242+
4
243+
5
244+
6
245+
insert into t1 values (7), (8);
246+
Warnings:
247+
Warning 4112 Versioned table `test`.`t1`: partition `p1` is full, add more HISTORY partitions
246248
### warn about full partition
247249
delete from t1;
248250
Warnings:
249251
Warning 4112 Versioned table `test`.`t1`: partition `p1` is full, add more HISTORY partitions
250252
select * from t1 partition (p1) order by x;
251253
x
252-
3
253254
4
254255
5
256+
6
257+
7
258+
8
255259
### Assertion in ALTER on warning from partitioning LIMIT [#446]
256260
create or replace table t1 (x int) with system versioning;
257261
insert into t1 values (1), (2);
258262
delete from t1;
259263
alter table t1 partition by system_time limit 1 (
260264
partition p1 history,
261265
partition pn current);
262-
Warnings:
263-
Note 4112 Versioned table `test`.`t1`: partition `p1` is full, add more HISTORY partitions
264266
## rotation by INTERVAL
265267
create or replace table t1 (x int)
266268
with system versioning
@@ -269,6 +271,16 @@ partition p0 history,
269271
partition p1 history,
270272
partition pn current);
271273
ERROR HY000: Wrong parameters for partitioned `t1`: wrong value for 'INTERVAL'
274+
create or replace table t1 (x int)
275+
with system versioning
276+
partition by system_time interval 1 second starts 12345 (
277+
partition p0 history,
278+
partition p1 history,
279+
partition pn current);
280+
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'starts 12345 (
281+
partition p0 history,
282+
partition p1 history,
283+
partition pn current)' at line 3
272284
### ha_partition::update_row() check
273285
create or replace table t1 (x int)
274286
with system versioning
@@ -286,7 +298,7 @@ x
286298
delete from t1 where x < 3;
287299
delete from t1;
288300
Warnings:
289-
Note 4114Versioned table `test`.`t1`: switching from partition `p0` to `p1`
301+
Warning 4112Versioned table `test`.`t1`: partition `p1` is full, add more HISTORY partitions
290302
select * from t1 partition (p0) order by x;
291303
x
292304
1
@@ -306,7 +318,7 @@ insert into t1 values (1);
306318
update t1 set x= 2;
307319
update t1 set x= 3;
308320
Warnings:
309-
Note 4114Versioned table `test`.`t1`: switching from partition `p0` to `p1`
321+
Warning 4112Versioned table `test`.`t1`: partition `p1` is full, add more HISTORY partitions
310322
select * from t1 partition (p0);
311323
x
312324
1
@@ -332,24 +344,23 @@ select * from t1 partition (pnsp1);
332344
x
333345
2
334346
4
335-
### warn about partition switching and about full partition
347+
### warn about full partition
348+
delete from t1 where x < 3;
349+
delete from t1;
336350
delete from t1;
337-
Warnings:
338-
Note 4114 Versioned table `test`.`t1`: switching from partition `p0` to `p1`
339-
Warning 4112 Versioned table `test`.`t1`: partition `p1` is full, add more HISTORY partitions
340351
select * from t1 partition (p0sp0);
341352
x
342353
1
343354
3
355+
5
344356
select * from t1 partition (p0sp1);
345357
x
358+
2
359+
4
346360
select * from t1 partition (p1sp0);
347361
x
348-
5
349362
select * from t1 partition (p1sp1);
350363
x
351-
2
352-
4
353364
create or replace table t1 (a bigint)
354365
with system versioning
355366
partition by range (a)
@@ -419,14 +430,8 @@ alter table t1 partition by system_time limit 1 (
419430
partition p1 history,
420431
partition p2 history,
421432
partition pn current);
422-
Warnings:
423-
Note 4114 Versioned table `test`.`t1`: switching from partition `p1` to `p2`
424433
delete from t1 where x = 1;
425-
Warnings:
426-
Warning 4112 Versioned table `test`.`t1`: partition `p2` is full, add more HISTORY partitions
427434
delete from t1 where x = 2;
428-
Warnings:
429-
Warning 4112 Versioned table `test`.`t1`: partition `p2` is full, add more HISTORY partitions
430435
# MDEV-14923 Assertion upon INSERT into locked versioned partitioned table
431436
create or replace table t1 (x int) with system versioning
432437
partition by system_time (partition p1 history, partition pn current);
@@ -435,6 +440,95 @@ alter table t1 add partition (partition p1 history);
435440
ERROR HY000: Duplicate partition name p1
436441
insert into t1 values (1);
437442
unlock tables;
443+
create or replace table t1 (pk int) with system versioning
444+
partition by system_time interval 10 year (
445+
partition p1 history,
446+
partition p2 history,
447+
partition pn current
448+
);
449+
ERROR 22003: TIMESTAMP value is out of range in 'INTERVAL'
450+
create or replace table t1 (i int) with system versioning
451+
partition by system_time interval 1 hour (
452+
partition p0 history, partition pn current);
453+
set @ts=(select partition_description from information_schema.partitions
454+
where table_schema='test' and table_name='t1' and partition_name='p0');
455+
alter table t1 add column b int;
456+
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';
457+
partition_name partition_ordinal_position partition_method timediff(partition_description, @ts)
458+
p0 1 SYSTEM_TIME 00:00:00.000000
459+
pn 2 SYSTEM_TIME NULL
460+
Warnings:
461+
Warning 1292 Truncated incorrect time value: 'CURRENT'
462+
alter table t1 add partition (partition p1 history, partition p2 history);
463+
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';
464+
partition_name partition_ordinal_position partition_method timediff(partition_description, @ts)
465+
p0 1 SYSTEM_TIME 00:00:00.000000
466+
p1 2 SYSTEM_TIME 01:00:00.000000
467+
p2 3 SYSTEM_TIME 02:00:00.000000
468+
pn 4 SYSTEM_TIME NULL
469+
Warnings:
470+
Warning 1292 Truncated incorrect time value: 'CURRENT'
471+
alter table t1 drop partition p0;
472+
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';
473+
partition_name partition_ordinal_position partition_method timediff(partition_description, @ts)
474+
p1 1 SYSTEM_TIME 01:00:00.000000
475+
p2 2 SYSTEM_TIME 02:00:00.000000
476+
pn 3 SYSTEM_TIME NULL
477+
Warnings:
478+
Warning 1292 Truncated incorrect time value: 'CURRENT'
479+
alter table t1 drop partition p2;
480+
ERROR HY000: Can only drop oldest partitions when rotating by INTERVAL
481+
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';
482+
partition_name partition_ordinal_position partition_method timediff(partition_description, @ts)
483+
p1 1 SYSTEM_TIME 01:00:00.000000
484+
p2 2 SYSTEM_TIME 02:00:00.000000
485+
pn 3 SYSTEM_TIME NULL
486+
Warnings:
487+
Warning 1292 Truncated incorrect time value: 'CURRENT'
488+
create or replace table t1 (i int) with system versioning
489+
partition by system_time interval 1 second
490+
subpartition by key (i) subpartitions 2
491+
(partition p1 history, partition pn current);
492+
insert t1 values (1);
493+
delete from t1;
494+
insert t1 values (2);
495+
Warnings:
496+
Warning 4112 Versioned table `test`.`t1`: partition `p1` is full, add more HISTORY partitions
497+
delete from t1;
498+
Warnings:
499+
Warning 4112 Versioned table `test`.`t1`: partition `p1` is full, add more HISTORY partitions
500+
alter table t1 add partition (partition p0 history, partition p2 history);
501+
select subpartition_name,table_rows from information_schema.partitions where table_schema='test' and table_name='t1';
502+
subpartition_name table_rows
503+
p1sp0 1
504+
p1sp1 0
505+
p0sp0 0
506+
p0sp1 1
507+
p2sp0 0
508+
p2sp1 0
509+
pnsp0 0
510+
pnsp1 0
511+
## pruning check
512+
set @ts=(select partition_description from information_schema.partitions
513+
where table_schema='test' and table_name='t1' and partition_name='p0' limit 1);
514+
insert into t1 values (4),(5);
515+
select * from t1;
516+
i
517+
4
518+
5
519+
explain partitions select * from t1;
520+
id select_type table partitions type possible_keys key key_len ref rows Extra
521+
1 SIMPLE t1 pn_pnsp0,pn_pnsp1 ALL NULL NULL NULL NULL 2 Using where
522+
explain partitions select * from t1 for system_time as of from_unixtime(@ts);
523+
id select_type table partitions type possible_keys key key_len ref rows Extra
524+
1 SIMPLE t1 p1_p1sp0,p1_p1sp1,p0_p0sp0,p0_p0sp1,p2_p2sp0,p2_p2sp1,pn_pnsp0,pn_pnsp1 ALL NULL NULL NULL NULL # Using where
525+
set @ts=(select row_end from t1 for system_time all where i=1);
526+
select * from t1 for system_time all where row_end = @ts;
527+
i
528+
1
529+
explain partitions select * from t1 for system_time all where row_end = @ts;
530+
id select_type table partitions type possible_keys key key_len ref rows Extra
531+
1 SIMPLE t1 p1_p1sp0,p1_p1sp1 # NULL NULL NULL NULL # #
438532
# Test cleanup
439533
drop database test;
440534
create database test;

mysql-test/suite/versioning/r/truncate.result

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ partition pn current);
5656
insert into t values (1);
5757
update t set a= 2;
5858
update t set a= 3;
59-
Warnings:
60-
Note 4114 Versioned table `test`.`t`: switching from partition `p0` to `p1`
6159
delete history from t;
6260
select * from t for system_time all;
6361
a

mysql-test/suite/versioning/t/partition.test

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ prepare select_pn from @str;
139139
execute select_p0;
140140
execute select_pn;
141141

142-
--echo ## pruning check
143-
--replace_regex /\d/N/ /ALL/system/ /Using where//
144-
explain partitions select * from t1;
145-
146142
set @str= concat('select row_start from t1 partition (pn) into @ts0');
147143
prepare stmt from @str; execute stmt; drop prepare stmt;
148144

@@ -212,14 +208,14 @@ show create table t1;
212208
--error ER_DROP_PARTITION_NON_EXISTENT
213209
alter table t1 drop partition non_existent;
214210

215-
insert into t1 values (1), (2), (3);
211+
insert into t1 values (1), (2), (3), (4), (5), (6);
216212
select * from t1 partition (pn);
217-
--echo ### warn about partition switching
213+
delete from t1 where x < 4;
218214
delete from t1;
219215
select * from t1 partition (p0);
220216
select * from t1 partition (p1);
221217

222-
insert into t1 values (4), (5);
218+
insert into t1 values (7), (8);
223219
--echo ### warn about full partition
224220
delete from t1;
225221
select * from t1 partition (p1) order by x;
@@ -241,6 +237,14 @@ partition by system_time interval 0 second (
241237
partition p1 history,
242238
partition pn current);
243239

240+
--error ER_PARSE_ERROR
241+
create or replace table t1 (x int)
242+
with system versioning
243+
partition by system_time interval 1 second starts 12345 (
244+
partition p0 history,
245+
partition p1 history,
246+
partition pn current);
247+
244248
--echo ### ha_partition::update_row() check
245249
create or replace table t1 (x int)
246250
with system versioning
@@ -288,7 +292,9 @@ insert into t1 (x) values (1), (2), (3), (4), (5);
288292
select * from t1 partition (pnsp0);
289293
select * from t1 partition (pnsp1);
290294

291-
--echo ### warn about partition switching and about full partition
295+
--echo ### warn about full partition
296+
delete from t1 where x < 3;
297+
delete from t1;
292298
delete from t1;
293299
select * from t1 partition (p0sp0);
294300
select * from t1 partition (p0sp1);
@@ -384,6 +390,56 @@ alter table t1 add partition (partition p1 history);
384390
insert into t1 values (1);
385391
unlock tables;
386392

393+
--error ER_DATA_OUT_OF_RANGE
394+
create or replace table t1 (pk int) with system versioning
395+
partition by system_time interval 10 year (
396+
partition p1 history,
397+
partition p2 history,
398+
partition pn current
399+
);
400+
401+
# INTERVAL and ALTER TABLE
402+
create or replace table t1 (i int) with system versioning
403+
partition by system_time interval 1 hour (
404+
partition p0 history, partition pn current);
405+
406+
set @ts=(select partition_description from information_schema.partitions
407+
where table_schema='test' and table_name='t1' and partition_name='p0');
408+
409+
alter table t1 add column b int;
410+
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';
411+
alter table t1 add partition (partition p1 history, partition p2 history);
412+
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';
413+
alter table t1 drop partition p0;
414+
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';
415+
--error ER_VERS_DROP_PARTITION_INTERVAL
416+
alter table t1 drop partition p2;
417+
select partition_name,partition_ordinal_position,partition_method,timediff(partition_description, @ts) from information_schema.partitions where table_schema='test' and table_name='t1';
418+
419+
create or replace table t1 (i int) with system versioning
420+
partition by system_time interval 1 second
421+
subpartition by key (i) subpartitions 2
422+
(partition p1 history, partition pn current);
423+
insert t1 values (1); delete from t1;
424+
sleep 1;
425+
insert t1 values (2); delete from t1;
426+
alter table t1 add partition (partition p0 history, partition p2 history);
427+
select subpartition_name,table_rows from information_schema.partitions where table_schema='test' and table_name='t1';
428+
429+
--echo ## pruning check
430+
set @ts=(select partition_description from information_schema.partitions
431+
where table_schema='test' and table_name='t1' and partition_name='p0' limit 1);
432+
insert into t1 values (4),(5);
433+
--sorted_result
434+
select * from t1;
435+
explain partitions select * from t1;
436+
--replace_column 10 #
437+
explain partitions select * from t1 for system_time as of from_unixtime(@ts);
438+
set @ts=(select row_end from t1 for system_time all where i=1);
439+
select * from t1 for system_time all where row_end = @ts;
440+
--replace_column 5 # 10 # 11 #
441+
explain partitions select * from t1 for system_time all where row_end = @ts;
442+
387443
--echo # Test cleanup
388444
drop database test;
389445
create database test;

0 commit comments

Comments
 (0)