From 14ff3c1e4255f8e1074f5dd790d433ec51e385c7 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Mon, 8 Jul 2024 14:57:52 +0200 Subject: [PATCH] postgres_driver: add more error handling when creating partitions Given that multiple nodes can be connected to the same database, it can happen that other node did something that my node was willing to do. In this commit, we overcome the possible "interleaved" partition creation. --- .../postgres_driver/postgres_driver.nim | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/waku/waku_archive/driver/postgres_driver/postgres_driver.nim b/waku/waku_archive/driver/postgres_driver/postgres_driver.nim index 5793139e2..e14920b83 100644 --- a/waku/waku_archive/driver/postgres_driver/postgres_driver.nim +++ b/waku/waku_archive/driver/postgres_driver/postgres_driver.nim @@ -954,6 +954,24 @@ proc performWriteQueryWithLock*( debug "skip performWriteQuery because the advisory lock is acquired by other" return ok() + if error.contains("already exists"): + ## expected to happen when trying to add a partition table constraint that already exists + ## e.g., constraint "constraint_name" for relation "messages_1720364735_1720364740" already exists + debug "skip already exists error", error = error + return ok() + + if error.contains("is already a partition"): + ## expected to happen when a node tries to add a partition that is already attached, + ## e.g., "messages_1720364735_1720364740" is already a partition + debug "skip is already a partition error", error = error + return ok() + + if error.contains("does not exist"): + ## expected to happen when trying to drop a constraint that has already been dropped by other + ## constraint "constraint_name" of relation "messages_1720364735_1720364740" does not exist + debug "skip does not exist error", error = error + return ok() + debug "protected query ended with error", error = $error return err("protected query ended with error:" & $error) @@ -984,10 +1002,6 @@ proc addPartition( " (LIKE messages INCLUDING DEFAULTS INCLUDING CONSTRAINTS);" (await self.performWriteQueryWithLock(createPartitionQuery)).isOkOr: - if error.contains("already exists"): - debug "skip create new partition as it already exists: ", skipped_error = $error - return ok() - return err(fmt"error adding partition [{partitionName}]: " & $error) ## Add constraint to the partition table so that EXCLUSIVE ACCESS is not performed when