From 0cc5c979c6b96af9440e3fafd23058f8b26ab943 Mon Sep 17 00:00:00 2001 From: Dom Dwyer Date: Wed, 16 Feb 2022 19:07:50 +0000 Subject: [PATCH] fix: assert on conflicting partition creation If two callers call create_or_get() for a partition, providing the same partition key & table ID, but different sequence numbers the catalog MUST NOT continue silently. As this is unexpected, this behaviour causes a panic. --- iox_catalog/src/postgres.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/iox_catalog/src/postgres.rs b/iox_catalog/src/postgres.rs index 0861fac26f..58c3d26a5f 100644 --- a/iox_catalog/src/postgres.rs +++ b/iox_catalog/src/postgres.rs @@ -578,7 +578,7 @@ impl PartitionRepo for PostgresTxn { sequencer_id: SequencerId, table_id: TableId, ) -> Result { - sqlx::query_as::<_, Partition>( + let v = sqlx::query_as::<_, Partition>( r#" INSERT INTO partition ( partition_key, sequencer_id, table_id ) @@ -599,7 +599,18 @@ impl PartitionRepo for PostgresTxn { } else { Error::SqlxError { source: e } } - }) + })?; + + // If the partition_key_unique constraint was hit because there was an + // existing record for (table_id, partition_key) ensure the partition + // key in the DB is mapped to the same sequencer_id the caller + // requested. + assert_eq!( + v.sequencer_id, sequencer_id, + "attempted to overwrite partition with different sequencer ID" + ); + + Ok(v) } async fn list_by_sequencer(&mut self, sequencer_id: SequencerId) -> Result> {