HerokuRimas SilkaitisTutorial
Handling Very Large Tables in Postgres Using Partitioning
Summary
Very large Postgres tables containing application events or log-like data can degrade query performance, slow index updates, and make vacuum disproportionately long. The article explains table partitioning through inheritance, using a master table, child partitions, CHECK constraints, and a trigger that routes inserts according to created_at; for an events workload with 10 million daily inserts and short-lived data, it uses daily partitions. It then introduces pg_partman, whose create_parent command configures time-based partitioning and whose run_maintenance function creates new partitions and removes old ones according to retention settings. Partitioning can improve performance and simplify deletion of expired data, but it should follow indexing and usage analysis, especially when queries cover a small time range and older data has little value.
Context
Tables that grow to hundreds of gigabytes or terabytes can experience degraded query performance, slower index updates, and lengthy vacuum operations. The described events data is queried for only a few days, receives 10 million inserts per day, and has little value after a short retention period.
Approach / What changed
The setup uses Postgres table inheritance, daily child partitions keyed by created_at, CHECK constraints, and an insert trigger for manual routing. It then replaces that operationally fragile process with pg_partman, using create_parent for time-based partitioning and run_maintenance on a regular schedule to create and remove partitions according to retention settings.
Takeaways
Postgres queries against a parent table include its descendant tables by default, while individual child partitions can also be queried directly.
Manual partitioning requires maintaining child-table CHECK constraints, insert-routing triggers, future partitions, and procedures for removing old data.
pg_partman can configure daily time-based partitions with create_parent and automate partition creation and retention cleanup through run_maintenance.
Source and freshness
- Source
- heroku.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.