Database partitioning: break a large table into manageable pieces
Database partitioning stores one logically unified large table as multiple physical pieces. It can help queries skip pieces that cannot match their conditions, and it can make it faster to detach or remove old data as a unit. It is useful when a table is large and its access or retention rules are clear. Partitioning does not automatically make queries faster: if the partition key does not match query predicates, or if there are too many partitions, planning and operational costs can increase instead.
The SQL examples in this article target PostgreSQL 18. Partition syntax and constraints vary by database product and version, so do not apply them unchanged to another engine.
What changes when a table is partitioned?
An application can read and write through one parent table, while the actual rows live in child tables selected by partition bounds. For example, a range-partitioned event table can store each month’s rows in that month’s partition. The database routes an inserted row to the partition whose bounds match the partition key.
CREATE TABLE events (
occurred_at timestamptz NOT NULL,
event_id bigint NOT NULL,
payload jsonb NOT NULL
) PARTITION BY RANGE (occurred_at);
CREATE TABLE events_2026_11 PARTITION OF events
FOR VALUES FROM ('2026-11-01 00:00:00+00')
TO ('2026-12-01 00:00:00+00');
SELECT event_id, payload
FROM events
WHERE occurred_at >= '2026-11-10 00:00:00+00'
AND occurred_at < '2026-11-11 00:00:00+00';PostgreSQL range partitions include the lower bound and exclude the upper bound. The example’s November partition therefore covers timestamps from November 1 at 00:00 up to, but not including, December 1 at 00:00. Adjacent partitions should meet without overlapping. PostgreSQL can reject an insert if no partition exists for the row’s occurred_at value, so create future partitions in advance or deliberately design a default partition. The SQL is illustrative; it has not been executed or validated against a database instance. PostgreSQL 18: Table Partitioning
When does partition pruning help?
Partition pruning is an optimization that excludes partitions that cannot contain rows matching a query, during planning or execution. If the table has five years of monthly partitions but a query filters occurred_at to one day, pruning may let the database read only the relevant month. If the query has no condition on the partition key, it may need to inspect many partitions.
Pruning does not happen merely because an index exists. The database compares the partition bounds with the query predicates and determines which partitions can be skipped. An index on the partition key may help search within a selected partition, but index use and pruning are separate decisions. PostgreSQL can prune at planning time and, when values become known later (for example, parameters in a prepared query), during execution. Inspect the actual plan with a tool such as EXPLAIN. PostgreSQL 18: Table Partitioning
How should you choose a partition key and method?
The partition key determines both where rows go and which queries can skip partitions. Start with common WHERE predicates and the unit used for data retention or deletion. If retention is by date and queries often filter by time range, a date range may be a natural candidate. If queries often retrieve a particular customer’s data, list or hash partitioning may be worth evaluating, but consider whether the design remains manageable as the number and distribution of values change.
PostgreSQL 18 offers three common declarative methods:
| Method | How rows are assigned | May fit when | Watch for |
|---|---|---|---|
| Range | Non-overlapping intervals, such as date or numeric ranges | Time-ordered data, range filters, or retention and deletion by period | You must create future range partitions and manage boundaries correctly |
| List | Each partition lists the values it accepts | A small, known set of regions or categories | New values may require rule changes; many values can be cumbersome |
| Hash | A hash of the key is mapped by modulus and remainder | You want to distribute values of one key among partitions | It may not suit removing a specific key range or time period as one unit |
A useful key should match not only today’s query patterns but also expected data growth and retention changes. PostgreSQL’s documentation warns that too few partitions can leave each partition’s indexes large and data locality poor, while too many can increase planning time and memory use. There is no fixed rule that more partitions are always faster or that fewer are always better. Validate using representative queries and expected data volumes.
How is partitioning different from sharding or an index?
Partitioning is a way to manage a table as multiple physical pieces. With PostgreSQL declarative partitioning, those partitions may all remain within one database instance. Sharding distributes ownership of data across multiple database servers or nodes and therefore also involves routing and coordination between nodes. The concepts are different; whether a product supports combining them depends on that product.
An index is a search structure that helps locate matching rows within a table or partition. Partitioning can first reduce the physical areas to search; an index may then help locate rows inside a selected area. Neither may help if a query reads almost the whole table. Check the query plan to see which partitions are scanned and which access method is used.
How do you operate partitions and retire data?
For time-based data, create each upcoming partition before its period begins and establish a procedure to detach or remove partitions after their retention period. PostgreSQL can remove an old partition with DROP TABLE, but this may require a strong lock on the parent. DETACH PARTITION can instead separate the partition while keeping it accessible as an independent table; afterward, it can be backed up, aggregated, or deleted separately. In a live system, check the lock mode, concurrent writes, and recovery procedure against the documentation for your version and your workload.
If a new partition is not ready in time, inserts for values outside existing bounds can fail. Partition-creation jobs should account for date boundaries, time zones, month ends and leap years, as well as retries after deployment failures. If you use a default partition, monitor whether unexpected values are accumulating there and decide how to move them into the intended partitions. Reorganizing partitions or moving large amounts of data can itself require time and locks.
A decision sequence before adoption
Identify the large tables, queries that are actually slow, retention periods, and the amount of data removed at once.
Check whether representative query filters often include a candidate partition key. The best key for retention may differ from the best one for query performance; decide which goal matters more.
Choose among range, list, and hash based on data growth and value distribution. Estimate how partition count and the set of values may change over time.
Check for missing or overlapping boundaries, behavior when a partition is absent, and product-specific limits involving unique constraints and indexes.
Examine plans using realistic data and queries. Verify that irrelevant partitions are pruned and that the remaining partition count, planning time, and memory use are acceptable.
Automate partition creation, retention, removal, and backup; prepare alerts for failures and a recovery procedure. Even fast partition removal can involve locks or dependencies that need review.
Partitioning fits when a large table’s access range and lifecycle can be managed in smaller physical units. It is often useful when date or range predicates are clear and old data can be retired as one unit. If queries usually scan all the data, omit the partition key, or the team cannot maintain boundaries reliably, one table with suitable indexes may be simpler. The goal is not to maximize partition count; decide whether real queries and operations can skip or manage specific pieces effectively.
Scope and sources
[PostgreSQL 18: Table Partitioning](https://www.postgresql.org/docs/18/ddl-partitioning.html)
[MySQL 8.4 Reference Manual: Overview of Partitioning](https://dev.mysql.com/doc/refman/8.4/en/partitioning-overview.html)
[MySQL 8.4 Reference Manual: Partition Pruning](https://dev.mysql.com/doc/refman/8.4/en/partitioning-pruning.html)
[Microsoft Learn: Create partitioned tables and indexes in SQL Server](https://learn.microsoft.com/en-us/sql/relational-databases/partitions/create-partitioned-tables-and-indexes?view=sql-server-ver17)
Official documentation was checked on 2026-11-22. The detailed pruning, range-boundary, and maintenance discussion is scoped to PostgreSQL 18; cross-product comparisons stay within each product’s documented behavior. SQL is illustrative and was not executed; no performance measurements are claimed. The best partition count depends on data and workload, so no universal number is given.