From 446306a274b981b4ea69b112f9947deb4736f27f Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Fri, 11 Feb 2022 13:54:47 +0530 Subject: [PATCH] MM-38419: Split UPSERT statement into 2 (#19536) The UPSERT statement was taking unexplained gap locks, due to which the import was somehow failing. I still don't know exactly how this is happening. But splitting the UPSERT into 2 different statements in the same transaction changes the locking semantics, and avoids the deadlock. Naturally, this proves that even our retrylayer is sometimes ineffective against some problems. ```release-note NONE ``` --- store/sqlstore/channel_store.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index c16b86c319..0c470fd721 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -540,14 +540,17 @@ func (s SqlChannelStore) upsertPublicChannelT(transaction *sqlxTxWrapper, channe PublicChannels(Id, DeleteAt, TeamId, DisplayName, Name, Header, Purpose) VALUES (:id, :deleteat, :teamid, :displayname, :name, :header, :purpose) - ON DUPLICATE KEY UPDATE - DeleteAt = :deleteat, + `, vals) + if err != nil && IsUniqueConstraintError(err, []string{"PRIMARY"}) { + _, err = transaction.NamedExec(`UPDATE PublicChannels + SET deleteAt = :deleteat, TeamId = :teamid, DisplayName = :displayname, Name = :name, Header = :header, - Purpose = :purpose; - `, vals) + Purpose = :purpose + WHERE Id=:id`, vals) + } } else { _, err = transaction.NamedExec(` INSERT INTO