From 9ef381f5cf57bb9ead2626ec6ff2bc5d55387766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20C=C3=A1rdenas?= Date: Tue, 2 Apr 2024 11:03:50 -0600 Subject: [PATCH] fix: batch drop mempool transactions (#1920) --- src/datastore/pg-write-store.ts | 34 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/datastore/pg-write-store.ts b/src/datastore/pg-write-store.ts index ed4c463381..4438d4bc7a 100644 --- a/src/datastore/pg-write-store.ts +++ b/src/datastore/pg-write-store.ts @@ -1809,22 +1809,24 @@ export class PgWriteStore extends PgStore { } async dropMempoolTxs({ status, txIds }: { status: DbTxStatus; txIds: string[] }): Promise { - const updateResults = await this.sql<{ tx_id: string }[]>` - WITH pruned AS ( - UPDATE mempool_txs - SET pruned = TRUE, status = ${status} - WHERE tx_id IN ${this.sql(txIds)} AND pruned = FALSE - RETURNING tx_id - ), - count_update AS ( - UPDATE chain_tip SET - mempool_tx_count = mempool_tx_count - (SELECT COUNT(*) FROM pruned), - mempool_updated_at = NOW() - ) - SELECT tx_id FROM pruned - `; - for (const txId of updateResults.map(r => r.tx_id)) { - await this.notifier?.sendTx({ txId }); + for (const batch of batchIterate(txIds, INSERT_BATCH_SIZE)) { + const updateResults = await this.sql<{ tx_id: string }[]>` + WITH pruned AS ( + UPDATE mempool_txs + SET pruned = TRUE, status = ${status} + WHERE tx_id IN ${this.sql(batch)} AND pruned = FALSE + RETURNING tx_id + ), + count_update AS ( + UPDATE chain_tip SET + mempool_tx_count = mempool_tx_count - (SELECT COUNT(*) FROM pruned), + mempool_updated_at = NOW() + ) + SELECT tx_id FROM pruned + `; + for (const txId of updateResults.map(r => r.tx_id)) { + await this.notifier?.sendTx({ txId }); + } } }