How to Implement Table Partitioning in PostgreSQL
Partition large PostgreSQL tables by range: create partitions, enable pruning, manage old partitions, and know when partitioning helps.
On this page
Introduction
A table that grows without bound eventually makes every operation expensive. Vacuum takes longer, indexes grow, and deleting old data with a row-by-row DELETE is slow and bloats the table. Partitioning breaks one large table into smaller child tables by a key, so that queries touching a narrow key range touch a small table, and old data can be removed by dropping a partition instead of deleting rows.
This tutorial implements range partitioning in PostgreSQL for a time-series table. It covers choosing the key, creating partitions, relying on pruning, indexing per partition, and the operation that partitioning uniquely enables: reclaiming old data by dropping a partition. The examples use declarative partitioning, which PostgreSQL has supported natively since version ten.
Choose the partition key
The partition key should be the column that appears in the hot queries’ filters and in the natural lifecycle of the data. For time-series, that is usually a timestamp: queries ask for a recent window and data ages out by time. For a multi-tenant table, the tenant id lets a query that filters one tenant touch one partition. The read is off a single partition, and maintenance operations apply to one partition.
The key also drives the pruning the planner performs. A query with WHERE created_at >= now() - interval ‘7 days’ lets the planner skip partitions whose ranges lie entirely in the past. A query without a filter on the key cannot prune and scans all partitions, so partition only when the key is reliably present in the queries that matter. Measure whether the key is selective enough to justify the added operational complexity.
CREATE TABLE events (
id bigint GENERATED ALWAYS AS IDENTITY,
tenant_id bigint NOT NULL,
created_at timestamptz NOT NULL,
payload jsonb,
PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (created_at);
Create partitions for each range
With range partitioning you create a child table for each range, such as one per month. Inserts route to the child whose range contains the row’s key, automatically and without application changes. Name partitions consistently, for example events_2026_01, so maintenance scripts can find them. Create partitions ahead of time so an insert never fails because no partition covers the date, and add a default partition or an automated job that creates the next one.
A partition must cover a range with no gap for the key you insert. The common failure is forgetting to create next month’s partition and having inserts fail at the month boundary. Automate partition creation on a schedule and alert if the creation job fails, because the failure appears only when the calendar crosses into the unmade range.
CREATE TABLE events_2026_01 PARTITION OF events
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TABLE events_2026_02 PARTITION OF events
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');
Index per partition and rely on pruning
Indexes in declarative partitioning are created on the parent and PostgreSQL creates a matching index on each existing partition; a partitioned index keeps queries that span partitions fast and lets each partition have its own smaller index. Create indexes on the parent so new partitions inherit the definition, but remember that a partition created before an index was added will not have it unless you create it explicitly or attach it.
Confirm pruning with EXPLAIN. A pruned query shows only the relevant partitions under an Append or a scan node, and the plan lists fewer relations than there are partitions. If EXPLAIN shows every partition, the query is not filtering on the key in a way the planner can prune, which is the signal that either the query or the partition key needs to change. Keep statistics current on each partition so estimates remain accurate.
Append
-> Index Scan using events_2026_01_pkey on events_2026_01
Index Cond: ((created_at >= ...))
-> Index Scan using events_2026_02_pkey on events_2026_02
Index Cond: ((created_at >= ...))
-- only the two partitions whose ranges overlap the filter appear: pruning worked
Manage and retire old partitions
The operation partitioning makes cheap is dropping old data. To remove a month, detach the partition so it becomes a standalone table, then drop it or archive it. Detaching is fast because it only changes metadata; dropping the table removes its files without a row-by-row DELETE or the vacuum work that follows. This is the difference between a retention job that runs in seconds and one that holds locks for minutes.
Detach with CONCURRENTLY where supported to avoid a long lock on the parent. Keep the retention policy explicit and automated: compute the cutoff from the policy, detach partitions entirely older than it, archive them if compliance requires, then drop. Test the retention job against a copy, because a mistaken cutoff that drops the wrong partition loses real data irreversibly rather than marking a flag.
-- retire the oldest month without a row-by-row delete
ALTER TABLE events DETACH PARTITION events_2025_01 CONCURRENTLY;
-- archive or drop the now-standalone table
DROP TABLE events_2025_01;
Test pruning and maintenance
Test that a query filtered on the partition key prunes: run EXPLAIN and assert the plan does not reference partitions outside the range. Test that an insert lands in the expected partition by querying the child table directly. Test the retention job on a copy: detach the old partition and assert its rows are no longer visible through the parent, then dropped. These tests catch the two failures that matter, a query that scans everything and a retention job that removes the wrong data.
psql -d app -c "EXPLAIN SELECT count(*) FROM events WHERE created_at >= '2026-01-01' AND created_at < '2026-02-01';"
# assert the plan references only events_2026_01, not every partition
Common pitfalls
Partitioning by a key the hot queries do not filter on so nothing is pruned, forgetting to create the next partition so inserts fail at the boundary, expecting a partitioned table to replace an index, detaching without CONCURRENTLY and taking a long lock, and a retention cutoff that drops the wrong range are the recurring mistakes. Matching the key to the queries, automating partition creation, indexing per partition, and testing retention address them.
Conclusion
Partitioning splits a large table by a key so queries read a small child and old data is removed by dropping a partition. Choose a key the hot queries filter on, create partitions ahead of time, index on the parent, confirm pruning with EXPLAIN, and detach-and-drop to retire old data. Self-check by asserting a filtered query touches only in-range partitions and by running the retention job on a copy. When the key matches the workload, partitioning turns unbounded growth into routine maintenance.
Sources: PostgreSQL: Table partitioning, PostgreSQL: Declarative partitioning, PostgreSQL: Partition pruning.
FAQ
- Does partitioning speed up all queries?
- No. It speeds up queries whose filters match the partition key, because the planner prunes non-matching partitions. A query that does not filter on the partition key may scan every partition and be no faster, sometimes slower.
- How do I delete old data cheaply?
- Detach the oldest partition and drop it, which removes its files in one operation instead of deleting rows one by one. This is one of the strongest reasons to partition time-series data.
Report a problem with this articleOpens a pre-filled issue on GitHub.