[Feature]: Scale #23

Open
opened 2026-09-14 13:18:25 -05:00 by hannah-vernon · 0 comments
Owner

Problem / use case

The admin list pages (/events, /incidents, /decisions, /audit, /signatures) use keyset pagination with server-side sorting and filtering. Every page render issues three PostgreSQL queries: the page fetch (ORDER BY <col>, id LIMIT pageSize+1), a COUNT(*) for the total, and a preceding-row count for the "Page N of M" indicator. At current data volumes these are cheap, but the events and audit tables ingest continuously, so query cost grows linearly with table size:

  • Sorted page fetches without a matching index become top-N scans on every heading click. Today only some sort columns are indexed (events.occurred_at, audit_records.timestamp); others are not (decisions.created_at), and join-based sorts (events by source key, decisions by policy or classification) have no covering index.
  • The exact COUNT(*) and preceding count are O(table size) on every render, sorted or not. This is the dominant growth cost.

Proposed solution

In order of preference:

  1. Retention (primary control). Decide and implement retention periods for the high-volume tables (events, audit_records, raw_observations). D-0024 already contemplates partition-drop retention. Bounded tables keep every list query cheap and make the remaining items unnecessary for a long time. Retention periods are an open decision that needs Hannah's input.
  2. Composite (sort_col, id) indexes on the headings that real usage shows are clicked often. Each index taxes ingest writes, so add them based on observed slow queries rather than speculatively.
  3. Count strategy. Replace the exact counts when tables get large: use estimated totals (pg_class.reltuples) for the unfiltered case, cap the display (for example "10,000+"), or add short-TTL in-process count caching with IMemoryCache (no new infrastructure).

Alternatives considered

  • External key-value cache (Redis or similar): rejected for now. These tables ingest continuously, so cached pages are stale immediately or require invalidation plumbing. PostgreSQL already serves hot pages from shared_buffers, and a single-admin UI is not a caching workload. D-0024 deliberately keeps persistence PostgreSQL-only with no extra infrastructure at home scale.
  • In-memory sorting in the app: rejected. It would require loading entire tables into the admin process and would defeat keyset pagination.
  • OFFSET pagination: rejected earlier in the project; OFFSET re-reads skipped rows and degrades linearly with page depth.
### Problem / use case The admin list pages (/events, /incidents, /decisions, /audit, /signatures) use keyset pagination with server-side sorting and filtering. Every page render issues three PostgreSQL queries: the page fetch (`ORDER BY <col>, id LIMIT pageSize+1`), a `COUNT(*)` for the total, and a preceding-row count for the "Page N of M" indicator. At current data volumes these are cheap, but the events and audit tables ingest continuously, so query cost grows linearly with table size: - Sorted page fetches without a matching index become top-N scans on every heading click. Today only some sort columns are indexed (`events.occurred_at`, `audit_records.timestamp`); others are not (`decisions.created_at`), and join-based sorts (events by source key, decisions by policy or classification) have no covering index. - The exact `COUNT(*)` and preceding count are O(table size) on every render, sorted or not. This is the dominant growth cost. ### Proposed solution In order of preference: 1. **Retention (primary control).** Decide and implement retention periods for the high-volume tables (events, audit_records, raw_observations). D-0024 already contemplates partition-drop retention. Bounded tables keep every list query cheap and make the remaining items unnecessary for a long time. Retention periods are an open decision that needs Hannah's input. 2. **Composite `(sort_col, id)` indexes** on the headings that real usage shows are clicked often. Each index taxes ingest writes, so add them based on observed slow queries rather than speculatively. 3. **Count strategy.** Replace the exact counts when tables get large: use estimated totals (`pg_class.reltuples`) for the unfiltered case, cap the display (for example "10,000+"), or add short-TTL in-process count caching with `IMemoryCache` (no new infrastructure). ### Alternatives considered - **External key-value cache (Redis or similar):** rejected for now. These tables ingest continuously, so cached pages are stale immediately or require invalidation plumbing. PostgreSQL already serves hot pages from shared_buffers, and a single-admin UI is not a caching workload. D-0024 deliberately keeps persistence PostgreSQL-only with no extra infrastructure at home scale. - **In-memory sorting in the app:** rejected. It would require loading entire tables into the admin process and would defeat keyset pagination. - **OFFSET pagination:** rejected earlier in the project; OFFSET re-reads skipped rows and degrades linearly with page depth.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
hannah-vernon/viegard-sentinel#23
No description provided.