[MDEV-21786] mysqldump will forget sequence definition details on --no-data dump Created: 2020-02-20 Updated: 2021-04-19 Resolved: 2020-10-23 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Backup, Scripts & Clients, Sequences |
| Affects Version/s: | 10.4.12 |
| Fix Version/s: | 10.3.26, 10.4.16, 10.5.7 |
| Type: | Bug | Priority: | Critical |
| Reporter: | Hartmut Holzgraefe | Assignee: | Anel Husakovic |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Issue Links: |
|
||||||||||||||||||||
| Description |
|
How to reproduce:
Dump and restore it with regular mysqldump, and do SHOW CREATE SEQUENCE s:
So the sequence was restored perfectly. Now lets try the same with a --no-data dump:
Now the sequence was recreated completely differently, only its name was preserved, and the CACHE value matches by chance as the original create didn't specify that one, so that it was using the default. |
| Comments |
| Comment by Elena Stepanova [ 2020-02-20 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
It's obvious why it happens, mysqldump currently treats sequences as tables, running SHOW CREATE TABLE and dumping the data, so with no-data all values which are stored in the table are lost. I guess the fix would be to determine inside mysqldump that a table is actually a sequence and handle it accordingly. I'll leave it to serg to decide in which version, if any, it can be done. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Michael Widenius [ 2020-02-24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
We already check in mysqldump if a table is of type sequence or not, which means that fixing this would be really trivial. It's not clear what one should get in case of '--no-data' as the sequence information is kind of data. However, I assume that what a user would expect is to get a sequence that starts with it's minimal value. Ok to add a fix for this in 10.3. However, please add a test case that ensures the sequence starts from the minium value! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Hartmut Holzgraefe [ 2020-02-24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
"It's not clear what one should get in case of '--no-data' as the sequence information is kind of data." well, that's how it's implemented, but it is actually schema metadata. When I create a table with a certain auto_increment start value, that value is also preserved in a no-data dump. So I expect everything given in a CREATE statement to be preserved in a schema only (--no-data) dump. The actual counter state could be debatable, but the other values like MINVAL, MAXVAL, INCREMENT etc. should be preserved for sure. And even for the actual current counter value we also have sort of set precedence with the current counter value for AUTO_INCREMENT being preserved in schema only dumps so far. So for consistency sake, and by the principle of least surprise, AUTO_INCREMENT and SERIAL should behave the same in this respekt ... | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Daniel Black [ 2020-08-20 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
" please add a test case that ensures the sequence starts from the minium value!" Problem 1: the next minimum value isn't accessible, only the `next_not_cached_value`. While we could use NEXTVAL() to retreive this its disruptive for a mysqldump to trigger this. Hence two more MDEV are going to block this.
| ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Anel Husakovic [ 2020-09-01 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Anel Husakovic [ 2020-09-03 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Daniel Black [ 2020-09-10 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
key review points:
| ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Daniel Black [ 2020-09-11 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Other sequences mysqldump problems: Tables that depend on sequences
The sequences need to be created before the table it generates. portablilityusing create sequence in the output and also other std sql (well `setval` is postgres). the current value or higher of a sequence needs to be savedThis isn't exposed by SHOW CREATE SEQUENCE Take for instance a table
We can't have a restore that leave the seq_t_i at its initial value of 1 when it restores. We also need to account for that even with --single-transaction sequences can be retrieve during the dump. So we need to retreve a value at the end of the dump to ensure it won't conflict with any of the tables already dumped. The mapping between the sequence and its use in a table isn't always a table defination, user logic not embedded into the database can describe this. what is the next value? - use LASTVAL(sequence_name) , and then output {{SELECT SETVAL(sequence_name, {val}, 1)}} to indicate its used - https://mariadb.com/kb/en/previous-value-for-sequence_name/ . So that way, a) its accurate, b) dumping doesn't change the value by using nextval, c) the value returned isn't in the existing dataset already dumped. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Anel Husakovic [ 2020-09-28 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
danblack | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Anel Husakovic [ 2020-09-30 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
danblack based on above (not taking into account restoring the values from sequence until right algorithm is determined) and addressing the review here is the new patch. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Daniel Black [ 2020-10-08 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
restore valuesparsing cycle, increment, next value is unnecessary, if you have a value, `SETVAL(sequence_name, next_value, is_used)` is_used can be set a value of 0 on the restore to ensure the next value used is that value e.g. fiddle:
next value which?As you correctly pointed out lastval is unusable. Two options exist: Call nextval(sequence_name)pro: on restoration will contain an accurate number select next_not_cached_value from sequencenamepro: will not consume a sequence number of the live system At the moment I think this is the preferable option. A SQL output file commenting that there may be lost values. conclusionSo in conclusion: At the end of the dump, there will be for all sequences in the dump, output like for a single database dump:
If there was a single database, sequence_name will contain just the sequence name (for importing to a different database).
or alternately separated:
| ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Anel Husakovic [ 2020-10-23 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Pushed to 10.3 with commit 81870e499ff14af. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by JP Pozzi [ 2021-03-08 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hello, When I dump a table with a column linked to a sequence mysqldump (--no-create-table) only write "lock tables table_name write;". Is there a solution ? Regards JP P | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Anel Husakovic [ 2021-03-08 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hi jpp, yes there is MDEV-20070 related to that, it is still not fixed. |