From f02b9a4b180ea1726960884c945d83f0ff7bcf7e Mon Sep 17 00:00:00 2001 From: MartinquaXD Date: Sat, 27 Jun 2026 16:29:05 +0000 Subject: [PATCH 1/2] Replace `auction_orders` table with index on `competition_auctions::order_uids` --- crates/autopilot/src/infra/persistence/mod.rs | 14 +++++-------- crates/database/src/auction.rs | 20 +------------------ crates/database/src/lib.rs | 1 - database/README.md | 1 + database/sql/V110__drop_auction_orders.sql | 6 ++++++ docs/COW_ORDER_DEBUG_SKILL.md | 9 ++++----- 6 files changed, 17 insertions(+), 34 deletions(-) create mode 100644 database/sql/V110__drop_auction_orders.sql diff --git a/crates/autopilot/src/infra/persistence/mod.rs b/crates/autopilot/src/infra/persistence/mod.rs index f82491537a..8f3be03b75 100644 --- a/crates/autopilot/src/infra/persistence/mod.rs +++ b/crates/autopilot/src/infra/persistence/mod.rs @@ -387,19 +387,17 @@ impl Persistence { let mut ex = self.postgres.pool.acquire().await?; - let order_uids: Vec<_> = auction - .orders - .iter() - .map(|order| ByteArray(order.uid.0)) - .collect(); - database::auction::save( &mut ex, database::auction::Auction { id: auction.id, block: i64::try_from(auction.block).context("block overflow")?, deadline: i64::try_from(deadline).context("deadline overflow")?, - order_uids: order_uids.clone(), + order_uids: auction + .orders + .iter() + .map(|order| ByteArray(order.uid.0)) + .collect(), price_tokens: auction .prices .keys() @@ -419,8 +417,6 @@ impl Persistence { ) .await?; - database::auction::save_auction_orders(&mut ex, auction.id, &order_uids).await?; - Ok(()) } diff --git a/crates/database/src/auction.rs b/crates/database/src/auction.rs index b682e76c15..3e35579430 100644 --- a/crates/database/src/auction.rs +++ b/crates/database/src/auction.rs @@ -110,30 +110,12 @@ pub async fn get_order_uids( Ok(record.map(|(order_uids,)| order_uids)) } -pub async fn save_auction_orders( - ex: &mut PgConnection, - auction_id: AuctionId, - order_uids: &[OrderUid], -) -> Result<(), sqlx::Error> { - const QUERY: &str = r#" -INSERT INTO auction_orders (auction_id, order_uid) -SELECT $1, unnest($2::bytea[]) -ON CONFLICT DO NOTHING - "#; - sqlx::query(QUERY) - .bind(auction_id) - .bind(order_uids) - .execute(ex) - .await?; - Ok(()) -} - pub async fn fetch_auction_ids_by_order_uid( ex: &mut PgConnection, order_uid: &OrderUid, ) -> Result, sqlx::Error> { const QUERY: &str = - "SELECT auction_id FROM auction_orders WHERE order_uid = $1 ORDER BY auction_id"; + "SELECT id FROM competition_auctions WHERE order_uids @> ARRAY[$1::bytea] ORDER BY id"; let rows: Vec<(AuctionId,)> = sqlx::query_as(QUERY).bind(order_uid).fetch_all(ex).await?; Ok(rows.into_iter().map(|(id,)| id).collect()) } diff --git a/crates/database/src/lib.rs b/crates/database/src/lib.rs index 16257f6849..35365afc51 100644 --- a/crates/database/src/lib.rs +++ b/crates/database/src/lib.rs @@ -78,7 +78,6 @@ pub const TABLES: &[&str] = &[ /// The names of potentially big volume tables we use in the db. pub const LARGE_TABLES: &[&str] = &[ - "auction_orders", "auction_prices", "competition_auctions", "fee_policies", diff --git a/database/README.md b/database/README.md index 4a4679f9e5..1201dfc9ca 100644 --- a/database/README.md +++ b/database/README.md @@ -66,6 +66,7 @@ Contains all auctions for which a valid solver competition exists. Indexes: - PRIMARY KEY: btree(`id`) - competition_auction_deadline: btree(`deadline`) +- competition_auctions_order_uids_gin: gin(`order_uids`) ### ethflow\_orders diff --git a/database/sql/V110__drop_auction_orders.sql b/database/sql/V110__drop_auction_orders.sql new file mode 100644 index 0000000000..16a7093b63 --- /dev/null +++ b/database/sql/V110__drop_auction_orders.sql @@ -0,0 +1,6 @@ +-- index to effeciently find auctions which contained a given order +CREATE INDEX CONCURRENTLY IF NOT EXISTS competition_auctions_order_uids_gin + ON competition_auctions USING GIN (order_uids); + +-- with the new index we don't need this redundant table anymore +DROP TABLE auction_orders; diff --git a/docs/COW_ORDER_DEBUG_SKILL.md b/docs/COW_ORDER_DEBUG_SKILL.md index 387b8d66ae..42eacfb21e 100644 --- a/docs/COW_ORDER_DEBUG_SKILL.md +++ b/docs/COW_ORDER_DEBUG_SKILL.md @@ -496,11 +496,10 @@ WHERE oq.order_uid = '\x$ORDER_UID_HEX'; ### Check auction inclusion history: ```sql -SELECT - auction_id, order_uid, included, filtered_reason -FROM auction_orders -WHERE order_uid = '\x$ORDER_UID_HEX' -ORDER BY auction_id DESC +SELECT id AS auction_id +FROM competition_auctions +WHERE order_uids @> ARRAY['\x$ORDER_UID_HEX'::bytea] +ORDER BY id DESC LIMIT 20; ``` From b8079d797316505cf508e957b06abaa88dcebe67 Mon Sep 17 00:00:00 2001 From: MartinquaXD Date: Sat, 27 Jun 2026 16:52:10 +0000 Subject: [PATCH 2/2] fix migration files --- database/sql/V110__drop_auction_orders.sql | 6 ------ database/sql/V111__gin_index_orders_in_auction.sql | 3 +++ database/sql/V112__drop_auction_orders.sql | 2 ++ 3 files changed, 5 insertions(+), 6 deletions(-) delete mode 100644 database/sql/V110__drop_auction_orders.sql create mode 100644 database/sql/V111__gin_index_orders_in_auction.sql create mode 100644 database/sql/V112__drop_auction_orders.sql diff --git a/database/sql/V110__drop_auction_orders.sql b/database/sql/V110__drop_auction_orders.sql deleted file mode 100644 index 16a7093b63..0000000000 --- a/database/sql/V110__drop_auction_orders.sql +++ /dev/null @@ -1,6 +0,0 @@ --- index to effeciently find auctions which contained a given order -CREATE INDEX CONCURRENTLY IF NOT EXISTS competition_auctions_order_uids_gin - ON competition_auctions USING GIN (order_uids); - --- with the new index we don't need this redundant table anymore -DROP TABLE auction_orders; diff --git a/database/sql/V111__gin_index_orders_in_auction.sql b/database/sql/V111__gin_index_orders_in_auction.sql new file mode 100644 index 0000000000..3dc43aeac5 --- /dev/null +++ b/database/sql/V111__gin_index_orders_in_auction.sql @@ -0,0 +1,3 @@ +-- index to efficiently find auctions which contained a given order +CREATE INDEX CONCURRENTLY IF NOT EXISTS competition_auctions_order_uids_gin + ON competition_auctions USING GIN (order_uids); diff --git a/database/sql/V112__drop_auction_orders.sql b/database/sql/V112__drop_auction_orders.sql new file mode 100644 index 0000000000..321ae302dc --- /dev/null +++ b/database/sql/V112__drop_auction_orders.sql @@ -0,0 +1,2 @@ +-- with the new index on `competition_auctions::order_uids` we don't need this redundant table anymore +DROP TABLE auction_orders;