Full answer
Sample schema and data:
sql
CREATE TABLE events (id bigint, happened date, kind text) PARTITION BY RANGE (happened);
CREATE TABLE events_2026_10 PARTITION OF events FOR VALUES FROM ('2026-10-01') TO ('2026-11-01');
CREATE TABLE events_2026_11 PARTITION OF events FOR VALUES FROM ('2026-11-01') TO ('2026-12-01');
CREATE TABLE events_2026_12 PARTITION OF events FOR VALUES FROM ('2026-12-01') TO ('2027-01-01');
INSERT INTO events SELECT g, date '2026-10-01' + (g % 92), 'view' FROM generate_series(1, 9200) g;
ANALYZE events;sql
SELECT tableoid::regclass::text AS partition, count(*) FROM events GROUP BY 1 ORDER BY 1;Result:
text
partition | count
----------------+-------
events_2026_10 | 3100
events_2026_11 | 3000
events_2026_12 | 3100
(3 rows)sql
EXPLAIN (COSTS OFF) SELECT count(*) FROM events WHERE happened >= '2026-11-10' AND happened < '2026-11-20';Output:
text
Aggregate
-> Seq Scan on events_2026_11 events
Filter: ((happened >= '2026-11-10'::date) AND (happened < '2026-11-20'::date))sql
INSERT INTO events VALUES (99999, '2027-02-01', 'view');PostgreSQL returns:
text
ERROR: no partition of relation "events" found for rowsql
CREATE TABLE events_default PARTITION OF events DEFAULT; INSERT INTO events VALUES (99999, '2027-02-01', 'view'); ALTER TABLE events DETACH PARTITION events_2026_10; SELECT count(*) AS remaining FROM events;Result on PostgreSQL 17:
text
remaining
-----------
6101
(1 row)Rows are routed to partitions automatically, about 3,000 per month. The plan for a mid-November filter scans only events_2026_11. A row dated February 2027 fails with 'no partition of relation "events" found for row' until a DEFAULT partition exists. Detaching the October partition removes its 3,100 rows from the parent instantly, and the detached table can be archived or dropped. Partitioning helps with very large tables and retention, but adds planning overhead and rules: unique constraints must include the partition key.
sql
CREATE TABLE events (id bigint, happened date, kind text) PARTITION BY RANGE (happened);
CREATE TABLE events_2026_10 PARTITION OF events FOR VALUES FROM ('2026-10-01') TO ('2026-11-01');
CREATE TABLE events_2026_11 PARTITION OF events FOR VALUES FROM ('2026-11-01') TO ('2026-12-01');
CREATE TABLE events_2026_12 PARTITION OF events FOR VALUES FROM ('2026-12-01') TO ('2027-01-01');
INSERT INTO events SELECT g, date '2026-10-01' + (g % 92), 'view' FROM generate_series(1, 9200) g;
ANALYZE events;
SELECT tableoid::regclass::text AS partition, count(*) FROM events GROUP BY 1 ORDER BY 1;
EXPLAIN (COSTS OFF) SELECT count(*) FROM events WHERE happened >= '2026-11-10' AND happened < '2026-11-20';
INSERT INTO events VALUES (99999, '2027-02-01', 'view');
CREATE TABLE events_default PARTITION OF events DEFAULT; INSERT INTO events VALUES (99999, '2027-02-01', 'view'); ALTER TABLE events DETACH PARTITION events_2026_10; SELECT count(*) AS remaining FROM events;Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- PARTITION BY RANGE splits by key ranges
- Pruning skips irrelevant partitions
- DEFAULT partitions catch other rows
- Detach or drop for fast retention
- Unique keys must include the partition key
Common mistakes
- Partitioning small tables
- Queries that do not filter on the partition key
Follow-ups
- How would you automate creating next month's partition?
- What does DETACH ... CONCURRENTLY change?
Tips
- Show the pruned plan