Skip to content

Make sure we're using indexes during rollbacks 13.7.0.1#2084

Closed
kderme wants to merge 1 commit into
release/13.7.0.1from
kderme/fix-slow-rollbacks
Closed

Make sure we're using indexes during rollbacks 13.7.0.1#2084
kderme wants to merge 1 commit into
release/13.7.0.1from
kderme/fix-slow-rollbacks

Conversation

@kderme
Copy link
Copy Markdown
Contributor

@kderme kderme commented Mar 23, 2026

Fixes #2083 (comment)

The queryMinRefId query uses

SELECT id FROM <table> WHERE <field> >= $1 ORDER BY id ASC LIMIT 1.

The planner sometimes picks a bad plan:

Index Scan using tx_pkey on tx
  Filter: (block_id >= $1)

the filter is not Index Cond, so this ends up in a sequential scan. The index refers to the primary key and is only used for sorting. Instead we want to use

Sort (top-N heapsort)
  -> Index Scan using idx_tx_block_id
       Index Cond: (block_id >= $1)

With this change we use

SELECT MIN(id) FROM <table> WHERE <field> >= $1

which forces a good plan:

Aggregate
  -> Index Scan using idx_tx_block_id
       Index Cond: (block_id >= $1)

making sure we're using the Index Cond

@kderme kderme changed the title Make sure we're using indexes during rollbacks Make sure we're using indexes during rollbacks 13.7.0.1 Mar 23, 2026
@kderme kderme marked this pull request as ready for review March 23, 2026 16:41
@kderme kderme requested a review from a team as a code owner March 23, 2026 16:41
Fixes #2083

The `queryMinRefId` query uses
```sql
SELECT id FROM <table> WHERE <field> >= $1 ORDER BY id ASC LIMIT 1.
```

The planner sometimes picks a bad plan:

```sql
Index Scan using tx_pkey on tx
  Filter: (block_id >= $1)
```

the filter is not Index Cond, so this ends up in a sequential scan.
The index refers to the primary key and is only used for sorting.

Instead we use a simpler query without ORDER BY:

SELECT id FROM <table> WHERE <field> >= $1 LIMIT 10000
This forces the planner to use the field's index.
The results are fetched and the minimum is found in Haskell.
Near the tip this returns only a handful of rows.

If there are more than 10000 matching rows (large rollback),
we fall back to the original ORDER BY id ASC LIMIT 1 query.
@kderme kderme force-pushed the kderme/fix-slow-rollbacks branch from 759c2e8 to a07ed50 Compare March 23, 2026 21:30
@kderme
Copy link
Copy Markdown
Contributor Author

kderme commented Apr 24, 2026

Superseeded by #2080

@kderme kderme closed this Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant