- llm_request_events: LIST(is_rollup) with rollups in one partition and raw
events in daily RANGE(created_at) partitions. The cleanup worker drops
raw days after rolling them up instead of deleting rows in batches.
- telegram_outbox and telegram_outbox_attempts: daily RANGE partitions.
Operation ids can no longer be a unique key, so enqueue takes a
per-batch advisory lock, reuses identical existing parts and inserts
the missing ones in part order. Attempts lose the outbox foreign key.
- An hourly worker keeps 7 days of partitions ahead and drops outbox and
attempt days older than TELEGRAM_OUTBOX_RETENTION_DAYS (default 7),
keeping any day that still has rows in flight.
- Migration 206 keeps the last 7 days and in-flight rows of the outbox.
Summary
Retention of
llm_request_eventsdeleted raw rows in batches, and the Telegram outbox had no retention at all (about a million delivered rows since July). Both tables now use daily partitions, and retention drops whole days.llm_request_eventsPARTITION BY LIST (is_rollup):llm_request_events_rollupholds the long-lived rollups;llm_request_events_rawholds raw events,PARTITION BY RANGE (created_at)with one partition per UTC day.drop_daily_partitions_before). The batchedDELETEis gone.ON CONFLICT DO NOTHINGuses the unique rollup key on the rollup partition. The primary key onidbecomes a plain index; ids still come from the same sequence.telegram_outboxandtelegram_outbox_attemptsRANGEpartitions oncreated_at/claimed_at.UNIQUE (operation_id)orUNIQUE (batch_id, part_index), so enqueue changes:pg_advisory_xact_lockon the batch id.TelegramOutboxIdempotencyConflict.Partition maintenance
daily-partition-maintenancekeeps partitions for today plus 7 days for all three partitioned tables and drops outbox and attempt days older thanTELEGRAM_OUTBOX_RETENTION_DAYS(default 7;0keeps history). An outbox day that still haspending,leasedorretry_waitrows is kept.lock_timeout = 3sand retries on the next tick, so it never queues behind a long reader.Migrations
ensure_daily_partition(parent, day)anddrop_daily_partitions_before(parent, cutoff_day, keep_when).Deployment risk and verification plan
telegram_outboxclaims progress; check LLM events are written to today's raw partition; read thedaily_partition_maintenancereadiness entry; check table sizes.Verification
cargo fmt --all --check,cargo clippy --workspace --all-targets -- -D warnings,cargo test --workspace.live_storage tests and the outbox round trip (idempotency, ordering, fencing, recovery-scan index use) pass on the partitioned schema; appmaintenancelive tests pass.