feat(migrations): partition audit_logs and pii_access_audit_logs by month - #1875
Merged
Merged
Conversation
…onth Redesigns the audit schema so audit_logs (RANGE on created_at) and pii_access_audit_logs (RANGE on accessed_at) are monthly-partitioned, following the zero-downtime pattern established by 20260723_partition_logs: rename original -> *_legacy (attached as DEFAULT), pre-create monthly partitions, and route inserts on demand so a late timestamp never fails an audit write. Recreates the append-only immutability triggers on the partitioned parents, where PostgreSQL fires them for every partition. The down migration drains every monthly partition into the legacy table before dropping the parent, so rows written after the up migration survive a rollback, then restores the original PK, indexes and triggers. Closes sublime247#1858 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
|
@Gabbydunkk Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Partitions the two audit tables so they scale with the
transaction_logsapproach already in the codebase:audit_logs— RANGE-partitioned oncreated_atpii_access_audit_logs— RANGE-partitioned onaccessed_atMonthly partitions follow the same zero-downtime pattern established by
20260723_partition_logs: the existing table is renamed to*_legacyand re-attached as theDEFAULTpartition (no data copy), monthly partitions are pre-created (past, current, next 3 months), and aBEFORE INSERTroute trigger creates the partition for a row's timestamp on demand so an audit write can never fail for want of a partition. The append-only immutability triggers are recreated on the partitioned parents, where PostgreSQL ≥ 14 fires them for every current and future partition.Verification (Postgres 16)
Run against a real Postgres 16 container:
*_legacy+ monthly partitions; inserts route into the correct monthly partition; UPDATE/DELETE are blocked (Audit logs are immutable and cannot be modified or deleted).Changes
migrations/20260826_partition_audit_logs.sql— up migration (both tables, helpers, route + immutability triggers, indexes)migrations/20260826_partition_audit_logs.down.sql— rollback (drain partitions, restore original schema, data-safe)Closes #1858