From 20457aefeccee78f6f0cf3f5b3202be4641d9f59 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Abou Hatem de Liz Date: Wed, 10 Jun 2026 18:12:20 -0300 Subject: [PATCH] perf: add composite indexes for C1/C5, C3, and C2 stale sweep (COW-886) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - generator_c1c5_poll_idx(chainId, status, allCandidatesKnown, lastCheckBlock): covers C1 and C5 per-block SELECTs which filter on all four columns and ORDER BY lastCheckBlock. Replaces the old partial generator_check_block_active_idx (nextCheckBlock, status) which did not cover chainId or allCandidatesKnown. - discrete_order_c3_status_idx(chainId, status, promotedAt): covers C3 per-block SELECT WHERE chainId + status='open' ORDER BY promotedAt. The previous statusIdx(status) had no chainId and no promotedAt — PostgreSQL would need a separate sort step on every block. - candidate_discrete_order_stale_idx(chainId, validTo): covers C2 stale sweep SELECT WHERE chainId + validTo <= timestamp LIMIT 500. Co-Authored-By: Claude Sonnet 4.6 --- schema/tables.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/schema/tables.ts b/schema/tables.ts index ae58d1d..2b6fb07 100644 --- a/schema/tables.ts +++ b/schema/tables.ts @@ -86,8 +86,11 @@ export const conditionalOrderGenerator = onchainTable( chainOwnerIdx: index().on(table.chainId, table.owner), resolvedOwnerIdx: index().on(table.resolvedOwner), ownerAddressTypeIdx: index().on(table.ownerAddressType), - checkBlockActiveIdx: index("generator_check_block_active_idx") - .on(table.nextCheckBlock, table.status), + // C1 (OrderDiscoveryPoller) + C5 (CancellationWatcher): per-block SELECT with + // chainId + status + allCandidatesKnown equality filters, ORDER BY lastCheckBlock. + // Covers both handlers — C1 queries allCandidatesKnown=false, C5 queries true. + c1c5PollIdx: index("generator_c1c5_poll_idx") + .on(table.chainId, table.status, table.allCandidatesKnown, table.lastCheckBlock), }) ); @@ -111,7 +114,9 @@ export const discreteOrder = onchainTable( pk: primaryKey({ columns: [table.chainId, table.orderUid] }), generatorIdx: index("discrete_order_generator_idx") .on(table.chainId, table.conditionalOrderGeneratorId), - statusIdx: index("discrete_order_status_idx").on(table.status), + // C3 (OrderStatusTracker): per-block SELECT with chainId + status='open', ORDER BY promotedAt. + c3StatusIdx: index("discrete_order_c3_status_idx") + .on(table.chainId, table.status, table.promotedAt), }) ); @@ -132,6 +137,9 @@ export const candidateDiscreteOrder = onchainTable( pk: primaryKey({ columns: [table.chainId, table.orderUid] }), generatorIdx: index("candidate_discrete_order_generator_idx") .on(table.chainId, table.conditionalOrderGeneratorId), + // C2 stale sweep: SELECT WHERE chainId + validTo <= timestamp LIMIT 500. + staleIdx: index("candidate_discrete_order_stale_idx") + .on(table.chainId, table.validTo), }) );