Sub topic 3
Choosing a Partition Key
Assume you know basic SQL, PostgreSQL table definitions, indexes, constraints, and simple query plans. This chapter focuses on selecting the column or columns that determine where each row is stored.
A partition key is the expression PostgreSQL uses to assign a row to a partition. A good key matches the way data is queried and managed. A poor key creates too many partitions, uneven data sizes, or queries that scan every partition.
Start with workload and lifecycle
Choose a key by answering these questions:
- Which columns appear in common
WHEREclauses? - Do old rows need regular archiving or deletion?
- Can the key value be known when a row is inserted?
- Will the key distribute rows evenly?
- How many partitions will exist after one or five years?
- Must queries frequently find rows without knowing the key?
For time-series data, a timestamp or date is often a strong key. Queries commonly filter by time, and old time ranges can be detached or dropped efficiently. For tenant-isolated workloads, tenant_id may be useful when queries and maintenance operate per tenant. For a fixed set of categories, a status or region column may suit list partitioning.
Partitioning does not replace indexing. If queries search by customer_id but the table is partitioned by order_date, PostgreSQL can still use indexes inside the selected partitions. Queries without an order_date condition may need to examine every partition.
Match the partitioning method
The key and its value distribution should fit the partitioning method.
| Workload pattern | Suitable method | Typical key |
|---|---|---|
| Ordered values and retention windows | Range | `created_at` |
| Small, known set of groups | List | `region` |
| Even distribution without useful ranges | Hash | `account_id` |
Range partitioning works well when adjacent intervals have meaning. For example, monthly partitions can support date-based reporting and retention. Use a timestamp carefully: define whether bounds use a time zone and whether the upper bound is exclusive.
List partitioning is appropriate when values represent stable groups. It becomes harder to manage when new values appear frequently. Hash partitioning spreads rows across a fixed number of partitions, but it does not naturally support “drop everything older than January” operations.
CREATE TABLE orders (
order_id bigint NOT NULL,
customer_id bigint NOT NULL,
order_date date NOT NULL,
amount numeric(12, 2) NOT NULL
) PARTITION BY RANGE (order_date);The partition key can contain multiple columns, but composite keys increase design and operational complexity. Use them only when both columns are important for routing or pruning.
Check cardinality and skew
A key with very few values may create oversized partitions. A key with nearly unique values may create an impractical number of partitions if each value gets its own partition. Hash partitioning can reduce skew, but it also limits range-based maintenance benefits.
Validate the choice
Before creating production partitions, inspect value distributions and representative queries. Test both filtered queries, which should benefit from pruning, and unfiltered queries, which may touch every partition. Confirm that future values have a planned destination.
Exercise
A table stores five years of events. Most queries filter by event time, and old data is deleted monthly. Which partition key and method are a strong starting point?
Show answer
Use range partitioning on the event timestamp, with monthly partitions. Verify time zones, boundary behavior, partition sizes, and query plans before deployment.