liveBook · Manning

Partitioning key

As explained in PostgreSQL Mistakes and How to Avoid Them

A partition key is a column or expression whose values determine how rows are assigned to table partitions.

A partitioning key is the column or expression used with a partitioning method and boundaries to assign rows to partitions. In PostgreSQL declarative partitioning, choose it around the access and maintenance work that must become smaller: a payments table keyed by timestamp can isolate monthly data for timestamp lookups and retention-driven removal. section 4.2

Use time when work follows time

A large payments table queried by tstamp required a sequential scan across 150,000,999 rejected rows and read 1,151,157 buffers. Defining the parent as PARTITION BY RANGE (tstamp) creates monthly partitions; the same lookup scans only the March 2022 partition. Old monthly data can then be removed by dropping its partition rather than deleting its rows. section 4.2

CREATE TABLE erp.payments_p (
    id bigint GENERATED ALWAYS AS IDENTITY,
    tstamp timestamp with time zone NOT NULL,
    amount numeric NOT NULL,
    invoice bigint NOT NULL
) PARTITION BY RANGE (tstamp);
CREATE TABLE

The parent-table definition makes tstamp the range partitioning key; monthly child boundaries supply the routing ranges. section 4.2

Do not mistake a multi-column key for multiple partitioning levels. RANGE (reading_time, branch_id) defines lexicographic bounds across the pair, so attempted branch ranges for the same month can overlap. For one monthly partition per branch group, partition first by reading_time, then partition each monthly partition by branch_id. section 4.3

CREATE TABLE erp.energy_usage_2024_01
PARTITION OF erp.energy_usage
FOR VALUES FROM ('2024-01-01') TO ('2024-02-01')
PARTITION BY RANGE (branch_id);
CREATE TABLE

CREATE TABLE erp.energy_usage_2024_01_01to10
PARTITION OF erp.energy_usage_2024_01
FOR VALUES FROM (1) TO (10);
CREATE TABLE

CREATE TABLE erp.energy_usage_2024_01_11to20
PARTITION OF erp.energy_usage_2024_01
FOR VALUES FROM (11) TO (20);
CREATE TABLE

This DDL shows the safer hierarchy: a time-keyed monthly partition that is itself partitioned by branch_id. section 4.3

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Explains range-key DDL, pruning evidence, composite-bound behavior, and sub-partitioning rather than exhaustive partitioning methods.
Examples
Demonstrates monthly payments partitions by tstamp and monthly energy-usage partitions further divided by branch_id.
What it assumes
Assumes familiarity with PostgreSQL tables, DDL, and query predicates while illustrating partition definitions directly.
What you can do after
Apply it via PostgreSQL DDL to select a range key and model time-first, branch-second partition hierarchies.
How it connects
Connects partition keys with declarative partitioning, partition pruning, range boundaries, and sub-partitioning.

Also explained in

Compare how 10 books explain partitioning key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.