Build the mental model
Partitioning splits one logical table into physical pieces. It supports query pruning and fast lifecycle operations such as detaching old data. On small tables it can add more planning and operational complexity than value, and you must manage partition keys, unique constraints, and future partitions.
Connect it to a real scenario
Range-partition audit events by month on `created_at`. Queries need useful partition-key conditions for pruning, which you should verify with `EXPLAIN`. Production runbooks need a default partition or automated future-partition creation.
Try the working example
CREATE TABLE app.audit_events (
event_id bigint GENERATED ALWAYS AS IDENTITY,
created_at timestamptz NOT NULL,
event_type text NOT NULL,
payload jsonb NOT NULL,
PRIMARY KEY (created_at, event_id)
) PARTITION BY RANGE (created_at);
CREATE TABLE app.audit_events_2026_08
PARTITION OF app.audit_events
FOR VALUES FROM ('2026-08-01') TO ('2026-09-01');
EXPLAIN SELECT * FROM app.audit_events
WHERE created_at >= '2026-08-10' AND created_at < '2026-08-11';You have an August partition and a plan that can demonstrate pruning.5-minute try-it
Create the September partition and test which partition receives boundary timestamps.
One important caution
Inserts can fail when time reaches a missing partition. Calendar-based automation and alerts are essential.
PostgreSQL — Table Partitioning — PostgreSQL Global Development Group