[MDEV-31365] Not following of the definition of the TIMESTAMP in the table. Created: 2023-05-30 Updated: 2023-06-05 Resolved: 2023-06-05 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Data Manipulation - Update |
| Affects Version/s: | 10.6.13 |
| Fix Version/s: | N/A |
| Type: | Bug | Priority: | Critical |
| Reporter: | Linkang, Zhang | Assignee: | Sergei Golubchik |
| Resolution: | Not a Bug | Votes: | 0 |
| Labels: | None | ||
| Description |
|
When we use ON DUPLICATE UPDATE syntax to insert data, CURRENT_TIMESTAMP in primary key, we get wrong answer. How to repeat: create one file named 'test_update.test' in directory mysql-test/main The content of 'test_update.test' is:
now, we can use this command in directory mysql-test:
now, the wrong answer is:
|
| Comments |
| Comment by Elena Stepanova [ 2023-06-04 ] | |||||||||||||||||||
|
It looks confusing at first, as it often happens with TIMESTAMP, but if we take it step by step, it doesn't appear to do anything it shouldn't do. Let's say, just for specifics in the example, that's current time is 2023-06-05 00:00:00. Your first INSERT inserts the row 2023-05-29 17:34:55,1 into the empty table:
The second INSERT attempts to insert the row 2023-05-29 17:34:55.01, 2; because it hits the duplicate key in ts, it performs an update instead, updating data to 2, but since now it is UPDATE, it also updates ts to CURRENT_TIMESTAMP, just as the table definition specifies. So, now we have
The third INSERT inserts the row 2023-05-29 17:34:55.01, 3. Since we no longer have this timestamp in the table, it inserts successfully, and we have two rows now:
Finally, the fourth INSERT attempts to insert the row 2023-05-29 17:34:55.01, 4. Like the 2nd one, it hits the duplicate key (conflicting with the row (2023-05-29 17:34:55,3)) and attempts to convert it into update, updating data column to 4, and – again – ts column to CURRENT_TIMESTAMP, which is still 2023-06-05 00:00:00 because the test is fast. However unlike the 2nd INSERT, this UPDATE hits a duplicate key too, because we already have 2023-06-05 00:00:00 in the table, so you get the error. The only TIMESTAMP specifics here is that the ts column is updated – or attempted to get updated – automatically, according to ON UPDATE CURRENT_TIMESTAMP clause. Otherwise, you would observe the same behavior with a regular numeric primary key, if you tried to update it to a value which already exists, something like
will also hit a duplicate key error on the 4th insert. Does it explain the behavior you are getting, or do you still think that it's incorrect? | |||||||||||||||||||
| Comment by Linkang, Zhang [ 2023-06-05 ] | |||||||||||||||||||
|
I think the MariaDB syntax "on duplicate key update" should know the case,and do something to avoid this. | |||||||||||||||||||
| Comment by Elena Stepanova [ 2023-06-05 ] | |||||||||||||||||||
|
I'll leave it to serg as an architect to decide. In my opinion it currently works exactly according to the table definition, and any change, while possible, would be a deviation from it; neither do I expect that MySQL will actually change the behavior, despite the verification team confirmation. | |||||||||||||||||||
| Comment by Sergei Golubchik [ 2023-06-05 ] | |||||||||||||||||||
|
Yes, elenst is right, this is expected behavior. Her comment explains why MariaDB behaves that way. |