Skip to content

Commit 9d2838a

Browse files
authored
friendlier sql (#1262)
1 parent c5d67f6 commit 9d2838a

3 files changed

Lines changed: 65 additions & 27 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DROP INDEX IF EXISTS idx_message_waits_eth_reorg_confirmed;
2+
DROP INDEX IF EXISTS idx_message_sends_eth_reorg_check;
3+
DROP INDEX IF EXISTS idx_message_sends_eth_signed_hash_norm;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- Indexes for PDPv0 reorg check candidate selection (message_sends_eth + message_waits_eth).
2+
3+
-- Drive branch 1 from confirmed waits past finality depth.
4+
CREATE INDEX IF NOT EXISTS idx_message_waits_eth_reorg_confirmed
5+
ON message_waits_eth (confirmed_block_number, signed_tx_hash)
6+
WHERE tx_status = 'confirmed'
7+
AND tx_success = TRUE
8+
AND confirmed_block_number IS NOT NULL;
9+
10+
-- Time-window scan of successful sends for reorg check.
11+
CREATE INDEX IF NOT EXISTS idx_message_sends_eth_reorg_check
12+
ON message_sends_eth (send_time, send_reason)
13+
WHERE send_success = TRUE
14+
AND send_time IS NOT NULL
15+
AND signed_hash IS NOT NULL;
16+
17+
-- Join send rows to wait rows by normalized tx hash.
18+
CREATE INDEX IF NOT EXISTS idx_message_sends_eth_signed_hash_norm
19+
ON message_sends_eth (LOWER(TRIM(BOTH FROM signed_hash)))
20+
WHERE send_success = TRUE
21+
AND signed_hash IS NOT NULL;

tasks/pdpv0/task_reorg_check.go

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)