Skip to content

Commit

Permalink
Update DeleteBlocksBefore query to use block_number index instead of …
Browse files Browse the repository at this point in the history
…LIMIT
  • Loading branch information
reductionista committed Sep 19, 2024
1 parent 7cd752f commit 069660e
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions core/chains/evm/logpoller/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,27 +294,33 @@ func (o *DSORM) SelectLatestLogByEventSigWithConfs(ctx context.Context, eventSig
// DeleteBlocksBefore delete blocks before and including end. When limit is set, it will delete at most limit blocks.
// Otherwise, it will delete all blocks at once.
func (o *DSORM) DeleteBlocksBefore(ctx context.Context, end int64, limit int64) (int64, error) {
if limit > 0 {
result, err := o.ds.ExecContext(ctx,
`DELETE FROM evm.log_poller_blocks
WHERE block_number IN (
SELECT block_number FROM evm.log_poller_blocks
WHERE block_number <= $1
AND evm_chain_id = $2
LIMIT $3
) AND evm_chain_id = $2`,
end, ubig.New(o.chainID), limit)
var result sql.Result
var err error

if limit == 0 {
result, err = o.ds.ExecContext(ctx, `DELETE FROM evm.log_poller_blocks
WHERE block_number <= $1 AND evm_chain_id = $2`, end, ubig.New(o.chainID))
if err != nil {
return 0, err
}
return result.RowsAffected()
}
result, err := o.ds.ExecContext(ctx, `DELETE FROM evm.log_poller_blocks
WHERE block_number <= $1 AND evm_chain_id = $2`, end, ubig.New(o.chainID))
if err != nil {
return 0, err

var deleted int64
// Remove up to limit blocks at a time, until we've reached the limit or removed everything eligible for deletion
for startOffset := limit; deleted < limit; startOffset += limit {
result, err = o.ds.ExecContext(ctx, `DELETE FROM evm.log_poller_blocks
WHERE block_number <= $1 AND block_number < MIN(block_number) + $2 AND evm_chain_id = $3`, end, startOffset, ubig.New(o.chainID))
if err != nil {
return deleted, err
}
if rows, err := result.RowsAffected(); err != nil {

Check failure on line 317 in core/chains/evm/logpoller/orm.go

View workflow job for this annotation

GitHub Actions / lint

shadow: declaration of "err" shadows declaration at line 298 (govet)
return deleted, err
} else {

Check failure on line 319 in core/chains/evm/logpoller/orm.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
deleted += rows
}
}
return result.RowsAffected()
return deleted, err
}

func (o *DSORM) DeleteLogsAndBlocksAfter(ctx context.Context, start int64) error {
Expand All @@ -328,8 +334,8 @@ func (o *DSORM) DeleteLogsAndBlocksAfter(ctx context.Context, start int64) error
// Latency without upper bound filter can be orders of magnitude higher for large number of logs.
_, err := o.ds.ExecContext(ctx, `DELETE FROM evm.log_poller_blocks
WHERE evm_chain_id = $1
AND block_number >= $2
AND block_number <= (SELECT MAX(block_number)
AND block_number >= $2
AND block_number <= (SELECT MAX(block_number)
FROM evm.log_poller_blocks
WHERE evm_chain_id = $1)`,
ubig.New(o.chainID), start)
Expand Down

0 comments on commit 069660e

Please sign in to comment.