Contents
Module: Read API
Purpose: Query functions for balances, turnovers, and movements with hierarchical optimization via the totals layer.
Files
balance.c—<register>_balance()implementationturnover.c—<register>_turnover()implementationmovements.c—<register>_movements()implementation
Responsibilities
1. <register>_balance(dimensions, at_date) — Balance Query
- Current balance (no
at_date): O(1) direct read frombalance_cache. In high-write mode, adds pending deltas viaUNION ALLfrom the delta buffer. - Historical balance (
at_datespecified): Uses hierarchical aggregation — sums complete years fromtotals_year, complete months fromtotals_month, remaining days fromtotals_day, and any partial-period movements. Maximum rows scanned: ~20 years + ~11 months + ~31 days ≈ 62 rows, regardless of total data volume. - Partial dimensions: When not all dimensions are specified, aggregates across omitted dimensions.
- No dimensions: Returns the total balance across the entire register.
2. <register>_turnover(from_date, to_date, dimensions, group_by) — Turnover Query
- Computes net resource change within [from_date, to_date]
- Optimizes by reading from
totals_month/totals_yearfor complete periods, falling back tototals_dayor raw movements only for partial boundary periods group_byparameter returns SETOF JSONB, one object per distinct group value- Supports partial dimension filtering
3. <register>_movements(recorder, from_date, to_date, dimensions) — Movement Listing
- Direct filtered access to the movements table
- Uses partition pruning on
periodfor efficient range scans - Supports filtering by
recorder, date range, and/or dimension values - Returns SETOF JSONB
SQL Sources
- sql/06_read_api.sql — Internal generic functions (
_balance_internal,_turnover_internal) and per-register wrapper generation
Related Tests
- test/sql/09_balance_cache.sql — Balance reads, edge cases (11 assertions)
- test/sql/11_turnover_register.sql — Turnover-only registers, period queries
- test/sql/14_end_to_end_warehouse.sql — Full warehouse balance/turnover scenario
- test/sql/15_end_to_end_finance.sql — Full financial accounting scenario