Skip to content

3.x.x: bound the pool hashrate series' ledger reads at both ends, so no plan walks the ledger - #754

Merged
RobertClarke merged 5 commits into
3.x.xfrom
RobertClarke/hashrate-series-boundary-ranges
Oct 8, 2026
Merged

RobertClarke merged 5 commits into
3.x.xfrom
RobertClarke/hashrate-series-boundary-ranges

Conversation

@RobertClarke

@RobertClarke RobertClarke commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

What's wrong

/public/v1/hashrate-series reads most buckets from the rollup tables. It re-reads two kinds of rows from the ledger:

  • the boundary: the current bucket and the leading partial bucket;
  • the tail: the shares past the rollup watermark.

Neither read gave the planner an index range it could use:

  • The boundary bounded accepted_at with an OR, (accepted_at >= current_bucket OR accepted_at < first_full_bucket), plus a NULL guard on the range start, (started_at IS NULL OR accepted_at >= started_at).
  • The tail bounded share_seq only from below, by the watermark. It had the same NULL guard on time, and an OR on the miner.

On a production-sized ledger (66M shares, rollup lag 0), the planner walked whole indexes:

  • rc.5, default pool request (range=1m, bucket=1h): 19–40 s. The boundary did an index-only scan of qbit_share_ledger_p0_accepted_seq_walk_idx with Index Cond: (share_seq <= $8), removing 66,090,385 rows by filter to return 2,390.
  • With only the boundary fixed: the generic plan's tail walked qbit_share_ledger_p0_accepted_recent_idx by accepted_at <= ended_at alone, filtering share_seq > $12. That's 66M entries for 0 rows, 17–27 s.
  • A custom plan with a miner walked the miner's whole history through accepted_miner_history_idx.

So pool hashrate-series requests ran past the public API's read deadline. PRISM answers those 503 read_timeout, and the proxy in front answered 504. Big miners' charts were slow too.

This is a 3.x regression. 2.x builds the same read with bounded predicates (lab/prism/share_ledger.py on 2.x.x, e.g. accepted_at >= (SELECT range_started_at FROM range_buckets)).

The fix

The changes are confined to dashboard_hashrate_rollups.sql. It's read-only SQL in the public read path: no migration, no schema change, no write path.

  • The boundary becomes a UNION ALL of two disjoint slices. Each is bounded by accepted_at at both ends, so each is a range scan on qbit_share_ledger_accepted_recent_idx, or on the miner index for a miner:

    • [GREATEST(current_bucket, started_at), ended_at];
    • [started_at, LEAST(first_full_bucket, current_bucket)), which is empty without a range start.

    GREATEST and LEAST skip a NULL range start or first full bucket.

  • The tail takes its shares from a subquery bounded by share_seq on both sides: share_seq > watermark AND share_seq <= max(share_seq), where the max is one backward probe per partition. The subquery is fenced by OFFSET 0, so the time and miner tests only filter its stream. It reads only the shares past the watermark: none while the rollups are current, the lag while they catch up.

Measured

The test copy is a 2.x ledger migrated to the native schema, with 6M shares plus one 600k-share miner. Every request runs in 1–17 ms under all of these conditions:

  • generic plan, custom plan, and auto after 7 executions;
  • random_page_cost 4 and 1.1;
  • the pool, a small miner and the big miner;
  • with and without an anchor.

Before the fix, at random_page_cost 1.1:

  • rc.5's default request: 1,957 ms generic and 346 ms custom;
  • the boundary-only version: 381 ms generic for the pool, and 161–176 ms custom for the big miner.

The fixed query's output matched rc.5's in 104 cases per watermark state, 312 in all:

  • every allowed range and bucket pair;
  • the pool, two miners and an unknown miner;
  • smoothing context 0 or 2 h;
  • with and without an anchor;
  • the watermark current, 200k shares behind, and with no progress row.

The only differences were in unanchored cases, where the range start slid during the slower old query. They fell only in the leading bucket, by one share.

By design, the tail's cost follows the rollup lag. With no progress row, which means the rollups have never run, it reads every share, as rc.5 did. The rollup lag metric is the thing to alarm on.

Not changed: the raw fallback, dashboard_hashrate_series.sql, used only when the rollup tables are absent, still has an unbounded range start. Both the 2.x and the 3.x schemas have the rollup tables.

Tests

  • chart_rollups_match_raw_for_every_range_bucket_and_watermark
    • What it checks: the rollup series must equal the raw-ledger series in 78 cases (every range and bucket pair, context, anchor and subject).
    • When: before the rollups have run (no progress row), at a current watermark, and with shares past it (one dated into an already rolled bucket).
    • Data: shares sit on both sides of every bucket, range and smoothing edge.
    • Sensitivity: disabling the leading-bucket slice makes it fail.
  • chart_rollups_read_only_the_partial_buckets_under_any_plan
    • Data: 20,000 rolled-up shares over 60 days, half of them one miner's.
    • Requests: the pool at 1m/1h and 1w/1h unanchored, 1w/5m anchored, a small miner and the big miner.
    • Plans: under force_custom_plan and force_generic_plan, at random_page_cost 4 and 1.1. Every plan runs twice: once with the rollups current, then again after 300 shares land past the watermark.
    • Checks on each plan:
      • it reads at most 1,000 ledger shares, plus the lag in the second run. Actual, filtered and recheck-dropped rows all count, over every loop;
      • every ledger index scan outside a Limit probe bounds one column from both sides;
      • no sequential scan reads ledger rows.
    • Why the shape check: a one-sided range can read nothing in a small fixture yet walk a whole index at production scale.
    • Sensitivity: against rc.5's query it fails (read 20000 of 20000 ledger shares). Against the boundary-only version it fails, flagging the tail's Index Cond: (share_seq > $12) in every mode and the big miner's history walk.

Run locally against PostgreSQL 16: api_2x (7), api_contract (6) and api_database (2) all pass. cargo fmt --check and clippy --all-targets -D warnings are clean.

🤖 Generated with Claude Code

…t at both ends, so it reads only their shares

The rollup series re-reads the current bucket and the leading partial
bucket from the ledger. Its filter wrote them as an OR, with a NULL
guard on the range start, and neither bounds an index range. With the
rollups current, the planner walks every share at or below the
watermark and filters on accepted_at. On a production-sized ledger
(66M shares) that is 19 s, past the public API's read deadline, so
every pool hashrate-series request answered 504. 2.x built the same
read with bounded predicates, so this is a 3.x regression.

The boundary is now two disjoint slices, each bounded at both ends:
the current bucket, and the leading partial bucket when the range has
a start. Each is a range scan on qbit_share_ledger_accepted_recent_idx.
On a 6M-share copy of a 2.x ledger migrated to the native schema, the
default request takes 8 ms instead of 1,957 ms (generic plan) and 4 ms
instead of 346 ms (custom plan). Its output was byte-identical to the
old query's in 312 cases: every allowed range and bucket, pool and
miner subjects, with and without smoothing context and an anchor,
with the watermark current, lagging and missing.

Two tests:
- the rollup series must equal the raw ledger series in each of those
  cases, before the rollups run, at a current watermark and with
  shares past it;
- under both plan modes, the pool chart may read at most 1,000 of
  20,000 ledger shares. The old filter reads all 20,000.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-08T10:43:27.160271Z 1be568d New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

RobertClarke and others added 2 commits October 8, 2026 05:53
…and check three request shapes in its plan test (review on #754)

Review on #754. The first boundary slice kept an IS NULL test on the
range start, the shape its own comment warned against. It still bounded
an index range on the current bucket, but read as if it didn't. The
second slice repeated bounds that the others already imply. Each slice
now has one lower and one upper accepted_at bound. GREATEST and LEAST
skip a NULL range start or first full bucket, and without a range
start the second slice matches no row. The set of shares is unchanged.
On the 6M-share copy, the output again matched rc.5's in the 312
cases, with the same plans: an accepted_recent_idx range per slice,
7.7 ms generic and 2.9 ms custom.

The plan test now checks three request shapes under both plan modes:
- the default pool request;
- a 5-minute one, whose smoothing context widens the range and binds
  the anchor;
- a miner's default request.
It reads row counts as floats, since PostgreSQL 18 prints per-loop
averages with decimals. It runs on a detached connection, so the
prepared statement and the plan mode can't return to the pool. The
identity test's share past the watermark now sits in a rolled bucket
six days back, so the 7-day ranges keep it on a slow runner.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ence, so no plan walks the ledger for it (real-data test on #754)

The tail re-reads the shares past the rollup watermark. Its only
index-usable bound was share_seq > watermark. Its accepted_at range
had the IS NULL test on the range start, and its miner test was an
OR. On a production-sized ledger, under the generic plan, the planner
walked p0's accepted_recent_idx by accepted_at <= ended_at alone, with
share_seq as a filter: 66M index entries and 5.9M heap fetches for no
row, 17 to 27 s. Under a custom plan with a miner it walked the miner's
whole history through accepted_miner_history_idx.

The tail's shares now come from a subquery that bounds share_seq on
both sides, by the watermark and by the ledger's max(share_seq), which
is one backward probe per partition. OFFSET 0 keeps the subquery's
plan to that range. The time and miner tests then filter the stream,
so the tail reads only the shares past the watermark. That's none
when the rollups are current, and the lag while they catch up.

On the 6M-share copy, with a 600k-share miner added, every request
now runs in 1 to 17 ms under each condition tried:
- generic, custom and auto plans (auto after 7 executions);
- random_page_cost 4 and 1.1;
- pool, small-miner and big-miner subjects, anchored or not.
Before, at random_page_cost 1.1, the generic pool request took 381 ms
and the big miner 161 to 176 ms. Output matched rc.5's in the 312
cases. The only differences were in unanchored cases, where the range
start slid while the slower old query ran, and only in its leading
bucket.

The 20,000-share plan test missed this. Its planner, at PostgreSQL's
default cost settings, happened to choose plans that read nothing. The
production planner, with an SSD's random_page_cost, chose the walk.
The test now covers:
- a miner holding half the shares;
- unanchored 7- and 30-day requests;
- both random_page_cost values.
It also checks plan shape as well as rows read: every ledger index
scan outside a Limit probe must bound one column from both sides, and
no sequential scan may read ledger rows. Against the previous commit
it reports the one-sided tail in every plan mode, and the miner
history walks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@RobertClarke RobertClarke changed the title 3.x.x: bound the pool hashrate series' boundary buckets by accepted_at at both ends, so it reads only their shares 3.x.x: bound the pool hashrate series' ledger reads at both ends, so no plan walks the ledger Oct 8, 2026
…atermark too, and count recheck rows (review on #754)

Review on #754. The plan test ran only with the rollups current, so
the tail it checked was always empty. It now runs every plan again
after 300 shares land past the watermark, half of them the big
miner's. Each request may then read the lag as well as the partial
buckets, and nothing more. The tail's cost follows the rollup lag,
and the test pins that.

Rows a lossy bitmap scan drops on its recheck now count as read, as
they do in the fanout lane's plan test. The comment on the test's
connection now says what ends it: the fixture's DROP DATABASE ...
WITH (FORCE). The query is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…must see executed

CI's "Prove every Rust PRISM gated test executed" failed. Two new
api_2x tests ran but are missing from test/prism-gated-tests.txt:
chart_rollups_match_raw_for_every_range_bucket_and_watermark and
chart_rollups_read_only_the_partial_buckets_under_any_plan. Both are
now listed, in order.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@RobertClarke
RobertClarke merged commit da02b1c into 3.x.x Oct 8, 2026
39 checks passed
RobertClarke added a commit that referenced this pull request Oct 8, 2026
Set VERSION and [workspace.package].version to 3.0.0-rc.6: rc.5 plus
#754 (the pool hashrate series bounds both of its ledger reads, the
boundary buckets and the tail past the rollup watermark, at both ends,
fixing a 3.x regression that timed it out on large ledgers), #750
(#748: a failed record of 2 is re-checked on a fresh connection when
its own one was lost, and each attempt is bounded on the client's
side), and #753, a test-only fix for #724. Both Cargo.lock files move
only their workspace-member entries.

doc/release-notes-3.0.0-rc.6.md covers:
- the changes, with #754's figures from its pull request (19 to 40 s
  and a 504 on a 66M-share ledger before, and 17 to 27 s with only the
  boundary fixed; 1 to 17 ms on a 6M-share copy after, under every plan
  mode and random_page_cost tried, with its output matching rc.5's; and
  on a production-sized ledger, 9 to 23 ms for the pool and 2 to 15 ms
  for miners in 20 of 20 runs, against rc.5's 15.8 to 25.2 s, with
  identical output),
  #750's lost connections, fresh read, client-side deadlines and two
  new stops, and #753's wait for current work;
- upgrading: rc.6 adds no migration, capability, setting or script, so
  an upgrade from rc.5 and a rollback to it replace the binary only, at
  fence 2 too, and a database that rc.5's migrate migrated is served by
  rc.6 unchanged, which a cutover that pinned rc.5 can rely on. An
  upgrade from rc.4 or earlier is as in rc.5's notes, which the section
  links: migrate for 026, fence 2 and the deferred backfill, #738's
  snapshot rule and fatal-state clear --timeout-seconds;
- the same evidence set as rc.5's, with the pair's serving evidence
  carried;
- the known issues: rc.5's, with #748 fixed and #724's drill race and
  new-tip shares fixed on the test side (#747, #753), plus #752 after
  the cutover, #738's fix now planned after the cutover, and #739 not in
  this candidate.

The development-line notes name 3.0.0-rc.6, and they and the 3.0.0
notes link all six candidates' notes.

docs/prism-rust-migration.md names rc.5 where it said "this release"
of serving at fence 2 and finishing the backfill. That is in three
places: that every build before rc.5 refuses fence 2, that the starts
of rc.5 and later accept it, and that only rc.5 and later finish a
pending backfill. In this candidate's tree "this release" would have
excluded rc.5, which does both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@RobertClarke
RobertClarke deleted the RobertClarke/hashrate-series-boundary-ranges branch November 3, 2026 20:46
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