Quick revision
The short answer
Partition audit_log by month on created_at, so each month lives in its own table. Every month, export the oldest online partition to immutable archive storage such as Parquet files in object storage, verify the row count, record it in a manifest, then DETACH the partition (CONCURRENTLY on PostgreSQL 14 and later) and DROP it. Dropping a table is instant and leaves no bloat, unlike DELETE of 50 million rows, and the whole job runs on a schedule with alerts.
What to remember
In this answer, we saw that retention at 50 million rows a month is a partitioning problem, not a DELETE problem. Monthly partitions on created_at turn the monthly cleanup into export, verify, record, detach and drop, where dropping a partition is instant and leaves no bloat. The archive lives in immutable object storage for seven years with a manifest that proves what it holds, and a scheduled job creates future partitions, runs retention and raises alerts when anything is missing or does not match.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Partition audit_log by month on created_at, so each month lives in its own table. Every month, export the oldest online partition to immutable archive storage such as Parquet files in object storage, verify the row count, record it in a manifest, then DETACH the partition (CONCURRENTLY on PostgreSQL 14 and later) and DROP it. Dropping a table is instant and leaves no bloat, unlike DELETE of 50 million rows, and the whole job runs on a schedule with alerts.
Why this question is asked
An audit table is the kind of table nobody thinks about until it holds a billion rows and the nightly cleanup job takes the database down. At 50 million rows a month, keeping 13 months online means about 650 million rows, and the policy asks us to keep seven years somewhere cheaper. Interviewers ask this to see whether we know why DELETE is the wrong tool at this scale and how partitioning turns retention into a cheap metadata operation. In this answer, we will recap how PostgreSQL partitioning works, see why a big DELETE hurts, walk through the monthly archive and drop job step by step, and then cover immutable storage and the alerts that keep the job honest.
1A Quick Recap of Table Partitioning in PostgreSQL
Declarative partitioning lets us define one logical table, the partitioned table, whose rows are physically stored in smaller tables called partitions. With range partitioning on created_at, each partition holds one month, for example all rows from 1 October up to but not including 1 November. Queries and inserts go to audit_log as usual, and PostgreSQL routes each row to the right partition. When a query filters on created_at, the planner skips partitions that cannot match, which is called partition pruning.
For retention the important property is that a partition is a real table. We can detach it from the parent, after which it is an ordinary standalone table, and then drop it. Dropping a table removes its files in one step, no matter how many rows it holds.
Notice that the primary key includes created_at. PostgreSQL enforces unique constraints per partition, so it requires the partition key in any primary key or unique constraint on a partitioned table.
2Why DELETE Is the Wrong Tool Here
Imagine the unpartitioned version of this table and a nightly job that runs DELETE FROM audit_log WHERE created_at < now() - interval '13 months'. Each month that statement has to remove about 50 million rows. In PostgreSQL a delete does not free space immediately: it marks each row version as dead, writes the change to the write-ahead log, and leaves the space for VACUUM to reclaim later. So the job produces gigabytes of WAL that replicas and backups must also process, it keeps autovacuum busy for hours, and the indexes keep their dead entries until vacuum cleans them.
The reclaimed space is reused for new rows but is rarely returned to the operating system, so the table and its indexes stay large. That is what people mean by bloat. With partitions, the same retention step becomes DROP TABLE on one partition: no dead rows, almost no WAL, and the disk space is returned at once.
3The Monthly Archive Job, Step by Step
With 13 months online, on the first day of each month the job takes the partition that has just fallen out of the window. On 1 December 2026, that is November 2025. The order of the steps matters, because we must never drop data that has not been safely archived and checked.
- Pick the partition older than 13 monthsthe oldest online month
- Count rows and export to Parquetobject storage, files per month
- Do the archived rows match the count?YesRecord in manifest, continueNoStop and alert, keep the partition
- DETACH PARTITION CONCURRENTLYwriters keep running
- DROP TABLEinstant, no bloat
5 steps in order. Play them, or step through them one at a time.
Nothing is dropped until the archive is verified and recorded.
Notice that the export and the count happen before anything is removed. Parquet is a good archive format because it is columnar, compressed and readable by many engines, so an auditor can query it years later with Athena, Trino or DuckDB without restoring a database. We write it in files of a few hundred megabytes each, partitioned by month, and the manifest row ties the database month to the files.
A plain DETACH PARTITION takes a strong lock on the parent table, so inserts into audit_log would wait while it runs. The CONCURRENTLY form, available since PostgreSQL 14, uses a weaker lock and runs in two transactions, so writers keep working. It cannot run inside a transaction block, and it is not allowed when the parent has a default partition. If it is interrupted, ALTER TABLE ... DETACH PARTITION ... FINALIZE completes it. Check the documentation of your version for the exact rules.
4Keeping the Archive Immutable and Accountable
An audit log is evidence, so the archive must be protected from both accidents and tampering. Object storage services offer write-once settings, for example S3 Object Lock in compliance mode, where an object cannot be deleted or overwritten by anyone until its retention date passes. We set that retention to seven years from the month of the data, and add a lifecycle rule that deletes the objects after the seven years, because keeping data longer than policy allows is a compliance problem too.
The archive_manifest table is our record of what was archived: month, row count, file locations and checksums. With it we can answer an auditor's question such as 'show me that March 2024 is complete' without guessing.
5Automating the Job and Alerting on It
Retention only works if it runs every month without someone remembering it. The job also has a second duty: creating partitions ahead of time. If the partition for next month does not exist when the clock passes midnight on the first, inserts fail with an error saying no partition was found for the row. The extension pg_partman can create future partitions and run the retention step, or a small scheduled job in our own code can do both.
We alert on four things: fewer than two future partitions exist, an export failed or its counts did not match, more than 14 online partitions exist (meaning retention has stopped), and the newest archive month is older than expected.
6Follow-up questions
Why not keep a default partition to catch rows with unexpected dates?
A default partition prevents insert failures, but it has costs. Rows that land there have to be moved out before we can attach a partition covering their range, attaching a new partition must scan the default partition to check that no rows belong to the new range, and DETACH PARTITION CONCURRENTLY is not allowed while a default partition exists. Creating partitions ahead of time and alerting when they are missing is usually the better approach for a time-based audit log.
What if a legal hold says some records must be kept longer than seven years?
A legal hold overrides the normal retention. We record the hold in the manifest, extend the object lock retention for the affected files, and make the lifecycle deletion rule skip held objects. The hold is released only through the legal team, and the normal deletion resumes afterwards.
7Conclusion
In this answer, we saw that retention at 50 million rows a month is a partitioning problem, not a DELETE problem. Monthly partitions on created_at turn the monthly cleanup into export, verify, record, detach and drop, where dropping a partition is instant and leaves no bloat. The archive lives in immutable object storage for seven years with a manifest that proves what it holds, and a scheduled job creates future partitions, runs retention and raises alerts when anything is missing or does not match.
Open with
I would partition audit_log by month on created_at, so each month is its own table and retention becomes a metadata operation. At 50 million rows a month, a DELETE of old rows would generate huge amounts of WAL, keep vacuum busy and leave the table and indexes bloated, while dropping a partition is instant.
Then, if they want more
Every month a scheduled job takes the partition that is older than 13 months, exports it to object storage as Parquet, and checks that the number of rows written matches a count of the partition.
Only if they match does it write a manifest entry with the month, row count, file locations and checksums, then run ALTER TABLE DETACH PARTITION CONCURRENTLY, which on PostgreSQL 14 and later does not block inserts, and then DROP TABLE.
The archive bucket uses object lock in compliance mode with a seven year retention, plus a lifecycle rule that deletes objects after seven years unless a legal hold applies. The same job creates partitions two months ahead, because a missing partition makes inserts fail.
I would alert when future partitions are missing, when an export or count check fails, and when the number of online partitions grows beyond 14, which means retention has stopped.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 14
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An audit_log table in PostgreSQL grows by 50 million rows per month, and policy says...": https://scrivio-interview-library.netlify.app/databases/analytics-and-cdc/3b8fc13037/