You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(views): load fee history for quiet wallets in milliseconds - #1498
Fee history for a wallet with few fee rows reads the whole transaction_events table and can run past the RPC timeout. list_transaction_fees in received and both mode, and get_last_transactions with a wallet, OR the caller test with the fee recipient test. No index answers that filter, so Postgres reads transaction_events_block_idx from the newest row and tests every row until it has a page. A wallet with fewer rows than the page asks for makes it read every row.
Migration 065 redefines both actions and adds one index.
transaction_events_fee_recipient_idx on (fee_recipient, block_height, tx_id), the same shape as transaction_events_caller_idx.
Each wallet test is its own branch. A branch reads the keys of the wallet's events from one index, newest first, and stops at limit + offset. paid reads the caller index, received the fee recipient index, and both the UNION of the two. Only the page's events are then read by tx_id.
Parameters, columns, row order, paging and distribution rows stay as they were. Where limit + offset passes the INT maximum, the branches read every key instead, so a huge offset still returns an empty page as before.
The filters no longer look for the wallet in transaction_event_distributions. Its only writer, record_transaction_event (027), writes at most one row per event, to the event's fee_recipient, so that test never matched an extra event. The ledger test that writes a split row by hand now expects that row's second recipient to have no fee history.
The change is SQL only: two actions and one index, applied by the migration. No new binary.
Measurements
I copied the mainnet fee events into a local Postgres 17 with the node's schema: 3,980,764 events, plus 1,613,278 distribution rows written the way record_transaction_event writes them, 2.5 GB with indexes. I ran the SQL that kwil generates for the old and new actions, with custom plans as kwil gets them, and took the median of three runs after one EXPLAIN (ANALYZE, BUFFERS).
list_transaction_fees, 20 rows, first page / offset 10,000:
wallet
mode
old
new
no fee rows
received
700 / 780 ms
0.6 / 0.5 ms
no fee rows
both
805 / 769 ms
0.5 / 0.4 ms
3 fee rows
paid
0.3 / 0.3 ms
0.5 / 0.4 ms
3 fee rows
received
762 / 765 ms
0.4 / 0.4 ms
3 fee rows
both
792 / 798 ms
0.6 / 0.4 ms
busiest caller
paid
107 / 123 ms
0.7 / 10 ms
busiest caller
received
708 / 722 ms
0.6 / 0.6 ms
busiest caller
both
134 / 143 ms
0.9 / 10 ms
leader
paid
0.5 / 2.5 ms
0.9 / 2.1 ms
leader
received
0.7 / 5.9 ms
0.8 / 8.0 ms
leader
both
0.6 / 6.7 ms
0.9 / 13 ms
get_last_transactions, 6 / 100 rows:
wallet
old
new
none given
0.4 / 1.0 ms
0.5 / 1.3 ms
no fee rows
730 / 817 ms
0.4 / 0.5 ms
3 fee rows
766 / 731 ms
0.7 / 0.6 ms
busiest caller
114 / 122 ms
0.5 / 1.5 ms
leader
0.4 / 0.9 ms
0.6 / 1.6 ms
The busiest caller holds 1.73M events, the newest of them 830k blocks below the tip; the leader received 1.61M.
Every old plan above that takes more than 100 ms walks transaction_events_block_idx backward with the wallet as a filter, reading about 189,700 buffers for a full walk. No new plan reads that index for a wallet: each branch is an index only scan of the caller or fee recipient index, with no heap fetches.
The leader's deep pages are a few ms slower, since a branch reads limit + offset keys before the page is cut. At offset 100,000 in both mode the leader takes 85 ms (old 55 ms) and the busiest caller 103 ms (old 221 ms).
The index only scans rely on the visibility map. With 25% or fewer of the table's pages marked all visible, Postgres planned the busiest caller's branch on the block index again; from 30% up it kept the caller index.
Old and new returned the same rows in the same order in every case above.
CREATE INDEX on this data took 3.4 s with two parallel workers and 4.7 to 5.4 s with none. The index is 506 MB.
Checks
go test -p 1 -timeout 60m -count=1 -tags=kwiltest ./tests/streams -run 'TestTransactionEventsLedger$|TestTransactionIDTracking$|TestTrimTransactionEvents$|TestTrimTransactionEventsE2E$|TestTransactionFeeHistoryReads$' passes.
TestTransactionFeeHistoryReads installs the 027 and 010 definitions under other names and compares every row with the new actions over 7 wallets, 5 modes and 13 pages (three with offsets near the INT maximum), and 9 wallets and 7 limits for get_last_transactions. It also checks the results against the dataset and that pages of 4 read back the full list. With the branch limit set to limit instead of limit + offset, it fails.
go test -p 1 -count=1 -tags=kwiltest on extensions/tn_utils, tn_digest, tn_settlement and tn_attestation, which seed every migration, passes.
golangci-lint v2.10.1 on ./tests/streams/ reports 0 issues.
Deploy
Apply internal/migrations/065-transaction-fee-history-reads.sql on testnet, then mainnet. The index is built while the migration runs.
Added transaction fee history views for wallet activity, with options to see fees paid, received, or both, and controls for paging through results.
Added a latest-transactions view that can show recent activity overall or transactions associated with a wallet, including activity where the wallet received fees.
No actionable comments were generated in the recent review.
Walkthrough
Migration 065 adds wallet-specific fee-history queries and a fee-recipient index. The queries support transaction ordering and pagination. Tests compare the updated actions with prior definitions and check recipient-related results.
Adds an index on fee recipient, block height, and transaction ID. Defines list_transaction_fees with paid, received, and combined modes, input validation, and pagination.
Adds integration coverage for ordering, pagination, and fee-recipient cases. Updates ledger expectations so a bonus recipient has no fee-history or transaction-history rows.
The migration rewrites the wallet fee-history and latest-transaction reads to use indexes while keeping results and paging unchanged. No concrete defect was established, so it looks safe to merge. Validate the performance claims on testnet before mainnet, as the author planned.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "feat(views): load fee history for quiet wallets in milliseconds" matches the main change. The PR replaces the list_transaction_fees and get_last_transactions views with indexed reads and ad…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title "feat(views): load fee history for quiet wallets in milliseconds" matches the main change. The PR replaces the list_transaction_fees and get_last_transactions views with indexed reads and adds a fee-recipient index for wallet-filtered queries. The objectives say the goal is millisecond-scale fee history for quiet wallets. The title names the performance goal and the affected area. The title uses "feat" while the PR objectives use "perf" as the prefix, which is a minor mismatch and does not change the meaning. The title also does not mention the removal of distribution-recipient matching, but the title does not need to cover every detail.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Fix failing CI checks
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.
MicBun
changed the title
perf(views): load fee history for quiet wallets in milliseconds
feat(views): load fee history for quiet wallets in milliseconds
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fee history for a wallet with few fee rows reads the whole
transaction_eventstable and can run past the RPC timeout.list_transaction_feesinreceivedandbothmode, andget_last_transactionswith a wallet, OR the caller test with the fee recipient test. No index answers that filter, so Postgres readstransaction_events_block_idxfrom the newest row and tests every row until it has a page. A wallet with fewer rows than the page asks for makes it read every row.Migration 065 redefines both actions and adds one index.
transaction_events_fee_recipient_idxon(fee_recipient, block_height, tx_id), the same shape astransaction_events_caller_idx.limit + offset.paidreads the caller index,receivedthe fee recipient index, andboththeUNIONof the two. Only the page's events are then read bytx_id.limit + offsetpasses the INT maximum, the branches read every key instead, so a huge offset still returns an empty page as before.transaction_event_distributions. Its only writer,record_transaction_event(027), writes at most one row per event, to the event'sfee_recipient, so that test never matched an extra event. The ledger test that writes a split row by hand now expects that row's second recipient to have no fee history.The change is SQL only: two actions and one index, applied by the migration. No new binary.
Measurements
I copied the mainnet fee events into a local Postgres 17 with the node's schema: 3,980,764 events, plus 1,613,278 distribution rows written the way
record_transaction_eventwrites them, 2.5 GB with indexes. I ran the SQL that kwil generates for the old and new actions, with custom plans as kwil gets them, and took the median of three runs after oneEXPLAIN (ANALYZE, BUFFERS).list_transaction_fees, 20 rows, first page / offset 10,000:get_last_transactions, 6 / 100 rows:transaction_events_block_idxbackward with the wallet as a filter, reading about 189,700 buffers for a full walk. No new plan reads that index for a wallet: each branch is an index only scan of the caller or fee recipient index, with no heap fetches.limit + offsetkeys before the page is cut. At offset 100,000 inbothmode the leader takes 85 ms (old 55 ms) and the busiest caller 103 ms (old 221 ms).CREATE INDEXon this data took 3.4 s with two parallel workers and 4.7 to 5.4 s with none. The index is 506 MB.Checks
go test -p 1 -timeout 60m -count=1 -tags=kwiltest ./tests/streams -run 'TestTransactionEventsLedger$|TestTransactionIDTracking$|TestTrimTransactionEvents$|TestTrimTransactionEventsE2E$|TestTransactionFeeHistoryReads$'passes.TestTransactionFeeHistoryReadsinstalls the 027 and 010 definitions under other names and compares every row with the new actions over 7 wallets, 5 modes and 13 pages (three with offsets near the INT maximum), and 9 wallets and 7 limits forget_last_transactions. It also checks the results against the dataset and that pages of 4 read back the full list. With the branch limit set tolimitinstead oflimit + offset, it fails.go test -p 1 -count=1 -tags=kwiltestonextensions/tn_utils,tn_digest,tn_settlementandtn_attestation, which seed every migration, passes../tests/streams/reports 0 issues.Deploy
Apply
internal/migrations/065-transaction-fee-history-reads.sqlon testnet, then mainnet. The index is built while the migration runs.resolves: https://github.com/trufnetwork/indexer/issues/241
Summary by CodeRabbit