Expand description
Raw table daily partitioning and retention automation (SCALE-09, #188).
The embedded analytics store’s raw table grows unbounded under
high sustained throughput between retention prunes. This module
rotates the raw table daily into partition tables (raw_YYYYMMDD)
within the same SQLite database, making retention enforcement O(1)
(DROP TABLE) instead of O(n) (DELETE rows + incremental vacuum).
§Design
- The
rawtable is always the CURRENT day’s partition. The writer inserts intorawunchanged. - At daily boundaries,
maybe_rotaterenamesrawtoraw_YYYYMMDD(yesterday’s date) and creates a new emptyrawtable with the full schema. - A
raw_allview UNION ALLs therawtable and allraw_YYYYMMDDpartition tables, so the rollup and query layers read across all partitions without per-query changes. drop_expired_partitionsdropsraw_YYYYMMDDtables older than the raw retention period (O(1) DROP TABLE).- The
raw_partitionsmeta table tracks partition names and their date boundaries for introspection.
§Why same-file partitions, not separate files
The issue proposed separate SQLite files. Same-file partition
tables achieve the same O(1) retention and bounded raw table size
while avoiding the complexity of ATTACH/DETACH, cross-file
transactions, and connection management. SQLite’s DROP TABLE is
O(1) (the pages are simply freed to the file’s free list), and
incremental vacuum reclaims them. The raw_all view provides
cross-partition querying without dynamic SQL.
Constants§
- SCHEMA_
V11 - The
raw_partitionsmeta table (schema v11, SCALE-09 #188): tracks the partition table name and the date it covers. One row per rotated partition. The current day’srawtable is NOT listed here (it is the active partition; it will be listed when it is rotated).
Functions§
- drop_
expired_ partitions - Drop partition tables older than the raw retention period. Returns the number of partitions dropped. Each drop is O(1) (SQLite frees the table’s pages to the file’s free list; incremental vacuum reclaims them).
- list_
partitions - List all active partition names (including the current
rawtable), ordered by name. Useful for introspection and diagnostics. - maybe_
rotate - Check if the current
rawtable has rows from a previous day. If so, rename it toraw_YYYYMMDD(the previous day’s date), create a freshrawtable, record the partition, and refresh theraw_allview. Returns the partition name if a rotation happened,Noneotherwise. - refresh_
raw_ all_ view - Recreate the
raw_allview as the UNION ALL of therawtable and allraw_YYYYMMDDpartition tables. Called after each rotation and after dropping expired partitions.