[MDEV-31164] default current_timestamp() not working when used INSERT ON DUPLICATE KEY in some cases Created: 2023-05-02  Updated: 2023-06-24  Resolved: 2023-06-24

Status: Closed
Project: MariaDB Server
Component/s: Data Manipulation - Insert
Affects Version/s: 10.3, 10.6
Fix Version/s: 11.1.1, 10.11.3, 11.0.2, 10.4.29, 10.5.20, 10.6.13, 10.8.8, 10.9.6, 10.10.4

Type: Bug Priority: Critical
Reporter: Lubos Takac Assignee: Sergei Golubchik
Resolution: Fixed Votes: 0
Labels: None
Environment:

10.6.5 - win10 Pro, 10.3.28 - linux 4.18.0-305.19.1.el8_4.x86_64



 Description   

If you have timestamp column with default current timestamp and you use INSERT with ON DUPLICATE KEY UPDATE, timestamp column is wrongly initialized to '0000-00-00 00:00:00' in case when some records in insert are inserted and some updated, when is no update timestamp is initialized correctly

drop table test;
 
create table test(
	pk integer primary key, 
    val varchar(20) not null,
    lt_timestamp timestamp not null default current_timestamp() on update current_timestamp());
    
insert into test(pk, val) values(1, 'val1');
 
select * from test;
 
+----+------+---------------------+
| pk | val  | lt_timestamp        |
+----+------+---------------------+
|  1 | val1 | 2023-05-02 07:47:19 |
+----+------+---------------------+
 
-- when both are to insert works correctly
insert into test(pk, val) 
	select 3, 'val3' from dual
	union
	select 4, 'val4' from dual
    on duplicate key update lt_timestamp=now();
    
+----+------+---------------------+
| pk | val  | lt_timestamp        |
+----+------+---------------------+
|  1 | val1 | 2023-05-02 07:47:19 |
|  3 | val3 | 2023-05-02 07:48:02 |
|  4 | val4 | 2023-05-02 07:48:02 |
+----+------+---------------------+
 
-- but when one is updated and one is inserted, the inserted ne is initialized to '0000-00-00 00:00:00'
insert into test(pk, val) 
	select 1, 'val1' from dual
	union
	select 2, 'val2' from dual
    on duplicate key update lt_timestamp=now();
 
+----+------+---------------------+
| pk | val  | lt_timestamp        |
+----+------+---------------------+
|  1 | val1 | 2023-05-02 07:51:56 |
|  2 | val2 | 0000-00-00 00:00:00 |
|  3 | val3 | 2023-05-02 07:51:18 |
|  4 | val4 | 2023-05-02 07:51:18 |
+----+------+---------------------+
-- see inserted record 2 with wrongly initialized timestamp
-- when change order and insert second as first datetime is initialized correctly


Generated at Thu Feb 08 10:21:46 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.