Use it for very large time-series or log tables.
Table Partitioning
Table partitioning splits a large table into partitions by a key (often date) so queries can prune partitions and old data can be dropped quickly.
1. Partitions: \d+ <table> / information_schema.partitions / sys.partitions.
2. Query filters on the key → pruning in EXPLAIN.
3. Future partitions created ahead?
4. Drop/archive old partitions.
5. Indexes per partition.
row → partition by key → queries prune → old partitions dropped
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Check pruning in plans for both literal and parameterised queries. Automate creation of future partitions.
Step 2 Common failures Narrow the fault
Queries without the partition key scan every partition. Too many small partitions slow planning. A missing future partition makes inserts fail.
Queries must filter on the partition key to prune. Too many partitions add planning overhead. Automate creating future partitions.
\d+ <table>— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
The planner removes partitions whose range cannot match the WHERE clause. Retention becomes a metadata operation: detach or drop a partition instead of a large DELETE.
ReferenceImportant terms & boundaries3 sections · depth on demand
Partitioning example (PostgreSQL)
CREATE TABLE events (id bigint, created_at timestamptz NOT NULL, payload jsonb)
PARTITION BY RANGE (created_at);
CREATE TABLE events_2026_11 PARTITION OF events
FOR VALUES FROM ('2026-11-01') TO ('2026-12-01');
Documented example, not captured output. Benefits: partition pruning in queries that filter on the key, and fast retention by dropping old partitions instead of large DELETEs. Automate partition creation (pg_partman). Partitioning does not help queries that do not filter on the partition key.
In depth
When partitioning helps — and when it does not
| Helps | Does not help |
|---|---|
Dropping old data: detach/drop a partition instead of a huge DELETE | Speeding up queries that do not filter on the partition key |
| Queries that always filter on time (or tenant) can skip partitions | Small tables (under tens of GB) |
| Vacuum and index maintenance per partition | Replacing a missing index |
Worked monthly partitioning (PostgreSQL)
CREATE TABLE events (id bigint, created_at timestamptz NOT NULL, payload jsonb)
PARTITION BY RANGE (created_at);
CREATE TABLE events_2026_12 PARTITION OF events
FOR VALUES FROM ('2026-12-01') TO ('2027-01-01');
CREATE TABLE events_default PARTITION OF events DEFAULT; -- catches rows with no partition
CREATE INDEX ON events (created_at); -- created on every partition
Retention without a long lock:
ALTER TABLE events DETACH PARTITION events_2025_11 CONCURRENTLY;
DROP TABLE events_2025_11; -- or archive it first
Checking pruning
EXPLAIN SELECT count(*) FROM events WHERE created_at >= '2026-12-01' AND created_at < '2026-12-08';
-- only events_2026_12 appears in the plan
A filter on date(created_at) or a function of the key defeats pruning; filter on the column itself. With prepared statements, pruning can happen at execution time (Subplans Removed: 11).
Constraints
A primary key or unique index must include the partition key. Create future partitions ahead of time (a scheduled job, or pg_partman); otherwise inserts fail or land in the default partition, which then blocks creating the matching partition until its rows are moved. MySQL partitioning has similar rules: every unique key must contain the partitioning columns.
Common mistakes
- Partitioning small tables.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.