Skip to content

Commit a7ee96d

Browse files
authored
fix: batch drop mempool transactions (#1920)
1 parent 183fdae commit a7ee96d

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/datastore/pg-write-store.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,22 +1933,24 @@ export class PgWriteStore extends PgStore {
19331933
}
19341934

19351935
async dropMempoolTxs({ status, txIds }: { status: DbTxStatus; txIds: string[] }): Promise<void> {
1936-
const updateResults = await this.sql<{ tx_id: string }[]>`
1937-
WITH pruned AS (
1938-
UPDATE mempool_txs
1939-
SET pruned = TRUE, status = ${status}
1940-
WHERE tx_id IN ${this.sql(txIds)} AND pruned = FALSE
1941-
RETURNING tx_id
1942-
),
1943-
count_update AS (
1944-
UPDATE chain_tip SET
1945-
mempool_tx_count = mempool_tx_count - (SELECT COUNT(*) FROM pruned),
1946-
mempool_updated_at = NOW()
1947-
)
1948-
SELECT tx_id FROM pruned
1949-
`;
1950-
for (const txId of updateResults.map(r => r.tx_id)) {
1951-
await this.notifier?.sendTx({ txId });
1936+
for (const batch of batchIterate(txIds, INSERT_BATCH_SIZE)) {
1937+
const updateResults = await this.sql<{ tx_id: string }[]>`
1938+
WITH pruned AS (
1939+
UPDATE mempool_txs
1940+
SET pruned = TRUE, status = ${status}
1941+
WHERE tx_id IN ${this.sql(batch)} AND pruned = FALSE
1942+
RETURNING tx_id
1943+
),
1944+
count_update AS (
1945+
UPDATE chain_tip SET
1946+
mempool_tx_count = mempool_tx_count - (SELECT COUNT(*) FROM pruned),
1947+
mempool_updated_at = NOW()
1948+
)
1949+
SELECT tx_id FROM pruned
1950+
`;
1951+
for (const txId of updateResults.map(r => r.tx_id)) {
1952+
await this.notifier?.sendTx({ txId });
1953+
}
19521954
}
19531955
}
19541956

0 commit comments

Comments
 (0)