From af89838e85a56099fc1c3912b76905b5beeb6f17 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Thu, 3 Oct 2024 21:43:21 +0200 Subject: [PATCH] fix: use insert ignore for filecache_extended The current approach is to insert a new row and, if a unique constraint violation occurs, update an existing one. PostgreSQL logs the unique constraint violation as error "duplicate key value violates unique constraint". Our Adapter.insertIgnoreConflict method provides a way to run an insert query without logging such errors by using the vendor-specific sql extensions like "on conflict do nothing" on Postgres. Signed-off-by: Daniel Kesselberg --- lib/private/Files/Cache/Cache.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index 9a6f7d41faa22..56069c88d1413 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -359,18 +359,12 @@ public function update($id, array $data) { } if (count($extensionValues)) { - try { - $query = $this->getQueryBuilder(); - $query->insert('filecache_extended'); - $query->hintShardKey('storage', $this->getNumericStorageId()); - - $query->setValue('fileid', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)); - foreach ($extensionValues as $column => $value) { - $query->setValue($column, $query->createNamedParameter($value)); - } + $insertCount = $this->connection->insertIgnoreConflict( + 'filecache_extended', + array_merge(['fileid' => $id], $extensionValues) + ); - $query->execute(); - } catch (UniqueConstraintViolationException $e) { + if ($insertCount === 0) { $query = $this->getQueryBuilder(); $query->update('filecache_extended') ->whereFileId($id) @@ -386,7 +380,7 @@ public function update($id, array $data) { $query->set($key, $query->createNamedParameter($value)); } - $query->execute(); + $query->executeStatement(); } }