Task: Order the page by an indexed key

Table of Contents

This page documents a task in the Let the blotter read a page, not the book — the remaining work story. It captures the goal, current status, acceptance, and any notes or results.

1. Goal

The page key is built by casting the sort column to text (coalesce(trade_date::text, )) and compared as text against the previous key. A text expression over a cast column cannot use an index on the column, so the page still sorts every row its filter matches. The order is an index in name only.

This task makes the page ordered by a key an index can serve. The key is the sort column in its own type and the trade id. The page reads its keys from the current booking rows alone, ordered by trade date and trade id, and reads the other columns only for the keys the page named. A current-only index on (tenant, book, trade date, trade id) serves the book a desk reads; a second on (tenant, trade date, trade id) serves the whole book.

The measurement is the acceptance: the first page of a book of a hundred thousand trades, read through the service, in the low tens of milliseconds, with EXPLAIN showing an index scan and no sort of the book.

2. Status

Field Value
State BACKLOG
Parent story Let the blotter read a page, not the book — the remaining work
Now Not yet started.
Waiting on List a package as the trade it is: the view is one shape before its order is indexed.
Next Change the key comparison first, then add the index the plan shows it needs.
Last touched 2026-11-03

3. Acceptance

  • A keyset page compares its sort column in the column own type, for every sortable field.
  • The default order reads the page keys from the current bookings through an index, and sorts only the page.
  • The first page of a hundred-thousand-trade book answers in the low tens of milliseconds through the service, and EXPLAIN (ANALYZE, BUFFERS) shows the index.
  • A page after a key returns the rows an offset page returns, and the rail counts do not change.

4. Plan

(Implementation strategy. Written when work starts; key decisions are distilled into the parent story's * Decisions at close, but the plan itself stays — it is the historical record of what we did.)

5. Measurements

Every number this task claims about a tree, with the command that produced it and the commit it was measured at. A number without both is not a measurement, because a clean-up record describes a tree and trunk moves under a branch: a consumer count, a gate's component count and a suite's size all change with no code changing, and the reader of a bare number cannot tell which tree it came from. Where the raw output matters, put it in an evidence/ file beside the task and name that file in the Command column.

What Value Command Commit
The page key as the store builds it coalesce(trade_date::text, '') and id::text, compared as text projects/ores.trading/core/src/repository/trade_summary_repository.cpp, read_blotter_page 23f46a8a0a
Indexes on the booking table 6, and none of them states trade_date select indexname, indexdef from pg_indexes where tablename = 'ores_trading_trade_bookings_tbl' 23f46a8a0a
Indexes on the state table 6, one of which is trade_states_status_idx (tenant_id, status_id) select indexname from pg_indexes where tablename = 'ores_trading_trade_states_tbl' 23f46a8a0a
The page's cost at ten thousand trades 543 ms, with 10,030 rows read and top-N sorted; 123,970 shared buffers explain (analyze, buffers) select * from ores_trading_trade_summaries_vw where tenant_id = '87d6f2c1-...' order by coalesce(trade_date::text, ''), id::text limit 100 23f46a8a0a
Where the time goes 10,000 index searches on the identifiers and 10,000 memoized laterals, then the sort; the booking and state tables are scanned whole the same EXPLAIN output 23f46a8a0a
An index the order could use none: the sort key is (COALESCE((b.trade_date)::text, ''::text)), ((t.id)::text) the same EXPLAIN output, Sort Key 23f46a8a0a

6. Notes

  • The key is text on both sides. read_blotter_page builds coalesce(<field>::text, '') and id::text, compares the pair with < or >, and states the same expressions in the ORDER BY. Every sortable field therefore sorts as text, and no index on the underlying column can serve it. A typed key means the order's value is a date, a uuid or a text value as its column states it, and the page key keeps the wire format it has.
  • Even a typed order still sorts, because the page reads the view and the view joins a lateral to each trade row. The planner cannot push a sort below a lateral. So the page must read its keys from the current booking rows alone — select trade_id, trade_date ... order by trade_date desc, trade_id desc limit n — and then read the other columns for the keys the page named. The rails keep their own grouped read; they never need the order.
  • The index that serves the desk is the book's: (tenant_id, book_id, trade_date desc, trade_id desc) where valid_to = ores_utility_infinity_timestamp_fn(). The second, on (tenant_id, trade_date desc, trade_id desc), serves a page with no book filter. Both are additive; no existing index changes.
  • The composite's date is its own booking's, so a package page needs no special case in the index.

7. Test Scenarios

Manual QA scenarios (scaffolded via compass add test_scenario) that verify this task. Link new ones here as they're created; the scenario doc itself links back via its "Verifies task" field.

Scenario State Notes
     

8. PRs

PR Title
   

9. Review

Comment summary File Decision Notes
       

10. Result

Emacs 29.3 (Org mode 9.6.15)