Skip to content

Commit

Permalink
postgres_driver: add more error handling when creating partitions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ivansete-status committed Jul 8, 2024
1 parent bd1626f commit a49031b
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions waku/waku_archive/driver/postgres_driver/postgres_driver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a49031b

Please sign in to comment.