@@ -120,34 +120,49 @@ func (t *ReorgCheckTask) Do(taskID harmonytask.TaskID, stillOwned func() bool) (
120120 }
121121
122122 var candidates []reorgCheckCandidate
123+ // reorgCheckCandidatesSQL selects sends to verify for canonical inclusion.
124+ // Split into UNION ALL branches so each side can use indexes (avoids a wide
125+ // LEFT JOIN + OR that hash-joins the full send/wait tables). Branch 2 uses
126+ // anti-joins (LEFT JOIN ... IS NULL) instead of NOT EXISTS.
123127 err = t .db .Select (ctx , & candidates , `
124128 SELECT LOWER(TRIM(BOTH FROM mse.signed_hash)) AS signed_tx_hash,
125- mse.send_reason,
126- mse.send_time,
127- mwe.confirmed_block_number,
128- COALESCE(mwe.tx_receipt->>'blockHash', '') AS stored_block_hash
129+ mse.send_reason,
130+ mse.send_time,
131+ mwe.confirmed_block_number,
132+ COALESCE(mwe.tx_receipt->>'blockHash', '') AS stored_block_hash
133+ FROM message_waits_eth mwe
134+ INNER JOIN message_sends_eth mse
135+ ON LOWER(TRIM(BOTH FROM mse.signed_hash)) = mwe.signed_tx_hash
136+ WHERE mwe.tx_status = 'confirmed'
137+ AND mwe.tx_success = TRUE
138+ AND mwe.confirmed_block_number IS NOT NULL
139+ AND ($4 - mwe.confirmed_block_number) >= $5
140+ AND mse.send_success = TRUE
141+ AND mse.send_time IS NOT NULL
142+ AND mse.send_time >= $1
143+ AND mse.send_time <= $2
144+ AND mse.send_reason = ANY($3)
145+
146+ UNION ALL
147+
148+ SELECT LOWER(TRIM(BOTH FROM mse.signed_hash)) AS signed_tx_hash,
149+ mse.send_reason,
150+ mse.send_time,
151+ NULL::bigint AS confirmed_block_number,
152+ '' AS stored_block_hash
129153 FROM message_sends_eth mse
130154 LEFT JOIN message_waits_eth mwe
131- ON LOWER(TRIM(BOTH FROM mse.signed_hash)) = LOWER(TRIM(BOTH FROM mwe.signed_tx_hash))
155+ ON mwe.signed_tx_hash = LOWER(TRIM(BOTH FROM mse.signed_hash))
156+ LEFT JOIN pdpv0_reorg_events re
157+ ON re.tx_hash = LOWER(TRIM(BOTH FROM mse.signed_hash))
132158 WHERE mse.send_success = TRUE
133- AND mse.send_time IS NOT NULL
134- AND mse.send_time >= $1
135- AND mse.send_time <= $2
136- AND mse.send_reason = ANY($3)
137- AND (
138- (mwe.tx_status = 'confirmed'
139- AND mwe.tx_success = TRUE
140- AND mwe.confirmed_block_number IS NOT NULL
141- AND ($4 - mwe.confirmed_block_number) >= $5)
142- OR (
143- mwe.signed_tx_hash IS NULL
144- AND NOT EXISTS (
145- SELECT 1 FROM pdpv0_reorg_events re
146- WHERE LOWER(re.tx_hash) = LOWER(TRIM(BOTH FROM mse.signed_hash))
147- )
148- )
149- )
150- ` , since , until , pdpv0SendReasons , headEpoch , int64 (policy .ChainFinality ))
159+ AND mse.send_time IS NOT NULL
160+ AND mse.send_time >= $1
161+ AND mse.send_time <= $2
162+ AND mse.send_reason = ANY($3)
163+ AND mwe.signed_tx_hash IS NULL
164+ AND re.tx_hash IS NULL
165+ ` , since , until , pdpv0SendReasons , headEpoch , int64 (policy .ChainFinality ))
151166 if err != nil {
152167 return false , xerrors .Errorf ("select candidates: %w" , err )
153168 }
@@ -279,15 +294,14 @@ func (t *ReorgCheckTask) reorgCheckTimeWindow(ctx context.Context, head *chainTy
279294 err = t .db .QueryRow (ctx , `
280295 SELECT COUNT(*)
281296 FROM message_sends_eth mse
297+ LEFT JOIN pdpv0_reorg_events re
298+ ON re.tx_hash = LOWER(TRIM(BOTH FROM mse.signed_hash))
282299 WHERE mse.send_success = TRUE
283300 AND mse.send_time IS NOT NULL
284301 AND mse.send_time >= $1
285302 AND mse.send_time < $2
286303 AND mse.send_reason = ANY($3)
287- AND NOT EXISTS (
288- SELECT 1 FROM pdpv0_reorg_events re
289- WHERE LOWER(re.tx_hash) = LOWER(TRIM(BOTH FROM mse.signed_hash))
290- )
304+ AND re.tx_hash IS NULL
291305 ` , lastEnd , since , pdpv0SendReasons ).Scan (& skipped )
292306 if err != nil {
293307 return time.Time {}, time.Time {}, 0 , xerrors .Errorf ("count skipped reorg check sends: %w" , err )
0 commit comments