Skip to content

Commit 7890388

Browse files
anson1014grooverdan
authored andcommitted
MDEV-33044 Loading time zones does not work with alter_algorithm INPLACE
$MYSQL_TZINFO_TO_SQL works by truncating tables. Truncation is an operation that cannot be done in-place and therefore is fundamentally incompatible with alter_algorithm='INPLACE'. As a result, we override the default alter_algorithm setting in tztime.cc to alter_algorithm='COPY' so that timezones can be loaded regardless of the previously set alter_algorithm. All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc.
1 parent 81f75ca commit 7890388

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
set global alter_algorithm=INPLACE;
2+
RENAME TABLE mysql.time_zone TO mysql.time_zone_BACKUP;
3+
RENAME TABLE mysql.time_zone_name TO mysql.time_zone_name_BACKUP;
4+
RENAME TABLE mysql.time_zone_transition TO mysql.time_zone_transition_BACKUP;
5+
RENAME TABLE mysql.time_zone_transition_type TO mysql.time_zone_transition_type_BACKUP;
6+
CREATE TABLE mysql.time_zone LIKE mysql.time_zone_BACKUP;
7+
CREATE TABLE mysql.time_zone_name LIKE mysql.time_zone_name_BACKUP;
8+
CREATE TABLE mysql.time_zone_transition LIKE mysql.time_zone_transition_BACKUP;
9+
CREATE TABLE mysql.time_zone_transition_type LIKE mysql.time_zone_transition_type_BACKUP;
10+
DROP TABLE mysql.time_zone;
11+
DROP TABLE mysql.time_zone_name;
12+
DROP TABLE mysql.time_zone_transition;
13+
DROP TABLE mysql.time_zone_transition_type;
14+
RENAME TABLE mysql.time_zone_BACKUP TO mysql.time_zone;
15+
RENAME TABLE mysql.time_zone_name_BACKUP TO mysql.time_zone_name;
16+
RENAME TABLE mysql.time_zone_transition_BACKUP TO mysql.time_zone_transition;
17+
RENAME TABLE mysql.time_zone_transition_type_BACKUP TO mysql.time_zone_transition_type;
18+
set global alter_algorithm=DEFAULT;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# MDEV-33044 Loading time zones does not work with alter_algorithm INPLACE
2+
3+
set global alter_algorithm=INPLACE;
4+
5+
# Because loading timezones alters the mysql tables,
6+
# this test will leave mysql in a different state than when it started.
7+
# Furthermore, checksums on the various mysql.timezone_x tables will fail.
8+
9+
# Therefore we:
10+
# 1. Make "backups" of the existing tables by renaming them
11+
# 2. Make dummy clones of the tables we just backed up
12+
# 3. Load timezones with alterations made to the dummy clone tables
13+
# 4. Drop the newly made tables with changes made to them
14+
# 5. Restore the backed up tables so the checksums will pass
15+
16+
RENAME TABLE mysql.time_zone TO mysql.time_zone_BACKUP;
17+
RENAME TABLE mysql.time_zone_name TO mysql.time_zone_name_BACKUP;
18+
RENAME TABLE mysql.time_zone_transition TO mysql.time_zone_transition_BACKUP;
19+
RENAME TABLE mysql.time_zone_transition_type TO mysql.time_zone_transition_type_BACKUP;
20+
21+
CREATE TABLE mysql.time_zone LIKE mysql.time_zone_BACKUP;
22+
CREATE TABLE mysql.time_zone_name LIKE mysql.time_zone_name_BACKUP;
23+
CREATE TABLE mysql.time_zone_transition LIKE mysql.time_zone_transition_BACKUP;
24+
CREATE TABLE mysql.time_zone_transition_type LIKE mysql.time_zone_transition_type_BACKUP;
25+
26+
--exec $MYSQL_TZINFO_TO_SQL std_data/zoneinfo | $MYSQL mysql
27+
28+
DROP TABLE mysql.time_zone;
29+
DROP TABLE mysql.time_zone_name;
30+
DROP TABLE mysql.time_zone_transition;
31+
DROP TABLE mysql.time_zone_transition_type;
32+
33+
RENAME TABLE mysql.time_zone_BACKUP TO mysql.time_zone;
34+
RENAME TABLE mysql.time_zone_name_BACKUP TO mysql.time_zone_name;
35+
RENAME TABLE mysql.time_zone_transition_BACKUP TO mysql.time_zone_transition;
36+
RENAME TABLE mysql.time_zone_transition_type_BACKUP TO mysql.time_zone_transition_type;
37+
38+
set global alter_algorithm=DEFAULT;

mysql-test/main/mysql_tzinfo_to_sql_symlink.result

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ ALTER TABLE time_zone ENGINE=InnoDB;
2020
ALTER TABLE time_zone_name ENGINE=InnoDB;
2121
ALTER TABLE time_zone_transition ENGINE=InnoDB;
2222
ALTER TABLE time_zone_transition_type ENGINE=InnoDB;
23+
SET @old_alter_alg=@@SESSION.alter_algorithm;
24+
SET session alter_algorithm='COPY';
2325
TRUNCATE TABLE time_zone;
2426
TRUNCATE TABLE time_zone_name;
2527
TRUNCATE TABLE time_zone_transition;
2628
TRUNCATE TABLE time_zone_transition_type;
2729
START TRANSACTION;
2830
ELSE
31+
SET @old_alter_alg=@@SESSION.alter_algorithm;
32+
SET session alter_algorithm='COPY';
2933
TRUNCATE TABLE time_zone;
3034
TRUNCATE TABLE time_zone_name;
3135
TRUNCATE TABLE time_zone_transition;
@@ -62,6 +66,7 @@ ALTER TABLE time_zone_transition ENGINE=Aria, ORDER BY Time_zone_id, Transition_
6266
ALTER TABLE time_zone_transition_type ENGINE=Aria, ORDER BY Time_zone_id, Transition_type_id;
6367
END IF|
6468
\d ;
69+
SET session alter_algorithm=@old_alter_alg;
6570
SELECT COUNT(*) FROM time_zone;
6671
COUNT(*)
6772
0
@@ -85,12 +90,16 @@ ALTER TABLE time_zone ENGINE=InnoDB;
8590
ALTER TABLE time_zone_name ENGINE=InnoDB;
8691
ALTER TABLE time_zone_transition ENGINE=InnoDB;
8792
ALTER TABLE time_zone_transition_type ENGINE=InnoDB;
93+
SET @old_alter_alg=@@SESSION.alter_algorithm;
94+
SET session alter_algorithm='COPY';
8895
TRUNCATE TABLE time_zone;
8996
TRUNCATE TABLE time_zone_name;
9097
TRUNCATE TABLE time_zone_transition;
9198
TRUNCATE TABLE time_zone_transition_type;
9299
START TRANSACTION;
93100
ELSE
101+
SET @old_alter_alg=@@SESSION.alter_algorithm;
102+
SET session alter_algorithm='COPY';
94103
TRUNCATE TABLE time_zone;
95104
TRUNCATE TABLE time_zone_name;
96105
TRUNCATE TABLE time_zone_transition;
@@ -124,6 +133,7 @@ ALTER TABLE time_zone_transition ENGINE=Aria, ORDER BY Time_zone_id, Transition_
124133
ALTER TABLE time_zone_transition_type ENGINE=Aria, ORDER BY Time_zone_id, Transition_type_id;
125134
END IF|
126135
\d ;
136+
SET session alter_algorithm=@old_alter_alg;
127137
SELECT COUNT(*) FROM time_zone;
128138
COUNT(*)
129139
2
@@ -145,6 +155,8 @@ COUNT(*)
145155
set @prep1=if((select count(*) from information_schema.global_variables where variable_name='wsrep_on' and variable_value='ON'), 'SET SESSION WSREP_ON=OFF', 'do 0');
146156
SET SESSION SQL_LOG_BIN=0;
147157
execute immediate @prep1;
158+
SET @old_alter_alg=@@SESSION.alter_algorithm;
159+
SET session alter_algorithm='COPY';
148160
TRUNCATE TABLE time_zone;
149161
TRUNCATE TABLE time_zone_name;
150162
TRUNCATE TABLE time_zone_transition;
@@ -172,6 +184,7 @@ UNLOCK TABLES;
172184
COMMIT;
173185
ALTER TABLE time_zone_transition ORDER BY Time_zone_id, Transition_time;
174186
ALTER TABLE time_zone_transition_type ORDER BY Time_zone_id, Transition_type_id;
187+
SET session alter_algorithm=@old_alter_alg;
175188
SELECT COUNT(*) FROM time_zone;
176189
COUNT(*)
177190
2
@@ -432,12 +445,16 @@ ALTER TABLE time_zone ENGINE=InnoDB;
432445
ALTER TABLE time_zone_name ENGINE=InnoDB;
433446
ALTER TABLE time_zone_transition ENGINE=InnoDB;
434447
ALTER TABLE time_zone_transition_type ENGINE=InnoDB;
448+
SET @old_alter_alg=@@SESSION.alter_algorithm;
449+
SET session alter_algorithm='COPY';
435450
TRUNCATE TABLE time_zone;
436451
TRUNCATE TABLE time_zone_name;
437452
TRUNCATE TABLE time_zone_transition;
438453
TRUNCATE TABLE time_zone_transition_type;
439454
START TRANSACTION;
440455
ELSE
456+
SET @old_alter_alg=@@SESSION.alter_algorithm;
457+
SET session alter_algorithm='COPY';
441458
TRUNCATE TABLE time_zone;
442459
TRUNCATE TABLE time_zone_name;
443460
TRUNCATE TABLE time_zone_transition;
@@ -457,6 +474,7 @@ ALTER TABLE time_zone_transition ENGINE=Aria, ORDER BY Time_zone_id, Transition_
457474
ALTER TABLE time_zone_transition_type ENGINE=Aria, ORDER BY Time_zone_id, Transition_type_id;
458475
END IF|
459476
\d ;
477+
SET session alter_algorithm=@old_alter_alg;
460478
DROP TABLE time_zone;
461479
DROP TABLE time_zone_name;
462480
DROP TABLE time_zone_transition;

sql/tztime.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,6 +2718,8 @@ static const char *lock_tables=
27182718
" time_zone_transition WRITE,\n"
27192719
" time_zone_transition_type WRITE;\n";
27202720
static const char *trunc_tables_const=
2721+
"SET @old_alter_alg=@@SESSION.alter_algorithm;\n"
2722+
"SET session alter_algorithm='COPY';\n"
27212723
"TRUNCATE TABLE time_zone;\n"
27222724
"TRUNCATE TABLE time_zone_name;\n"
27232725
"TRUNCATE TABLE time_zone_transition;\n"
@@ -2833,6 +2835,9 @@ main(int argc, char **argv)
28332835
"END IF|\n"
28342836
"\\d ;\n");
28352837

2838+
if (argc == 1 && !opt_leap)
2839+
printf("SET session alter_algorithm=@old_alter_alg;\n");
2840+
28362841
free_allocated_data();
28372842
my_end(0);
28382843
return 0;

0 commit comments

Comments
 (0)