Sub topic 11
Constraints, Primary Keys, and Uniqueness
Assume you know basic PostgreSQL table definitions, indexes, constraints, and query plans. This chapter focuses on how those features behave when one logical table is stored in several partitions.
Constraints on the partitioned table
A partitioned table has no rows of its own. Its partitions store the rows, but constraints declared on the parent describe the logical table. PostgreSQL applies those constraints to rows in every partition.
CREATE TABLE orders (
order_id bigint NOT NULL,
order_date date NOT NULL,
customer_id bigint NOT NULL,
amount numeric(12, 2) NOT NULL,
CONSTRAINT orders_amount_positive CHECK (amount > 0),
PRIMARY KEY (order_id, order_date)
) PARTITION BY RANGE (order_date);
CREATE TABLE orders_2026_01
PARTITION OF orders
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TABLE orders_2026_02
PARTITION OF orders
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');The CHECK constraint rejects non-positive amounts regardless of the partition. The partition bounds add another constraint: a January partition cannot contain a February row.
You can add a constraint to an individual partition when that partition needs a stricter rule. Such a constraint does not automatically apply to the parent or to sibling partitions.
Primary keys and unique constraints
A primary key identifies each row uniquely and also implies NOT NULL. PostgreSQL creates a unique index to enforce it.
On a partitioned table, a primary key or unique constraint must include every column in the partition key. This requirement lets PostgreSQL enforce uniqueness separately in each partition while still guaranteeing uniqueness for the logical table. For the example above, PRIMARY KEY (order_id, order_date) is valid because it includes order_date.
| Definition | Valid on this table? | Reason |
|---|---|---|
| `PRIMARY KEY (order_id, order_date)` | Yes | Includes the partition key |
| `UNIQUE (customer_id)` | No | Omits `order_date` |
| `UNIQUE (order_id, order_date)` | Yes | Includes the partition key |
The restriction matters when choosing an identifier. If order_id must be globally unique by itself, a partitioned table cannot enforce that with a normal parent-level unique constraint when it is partitioned by order_date. A sequence or UUID can generate practically unique values, but generation is different from a database uniqueness constraint. A separate unpartitioned registry table is one design for enforcing global uniqueness.
Indexes and foreign keys
An index created on the parent creates corresponding indexes on its partitions. When new partitions are added, PostgreSQL creates the matching index there as well.
CREATE INDEX orders_customer_date_idx
ON orders (customer_id, order_date);This index helps searches by customer and date, but it does not provide global uniqueness unless it is declared as a valid unique constraint and includes the partition key.
Foreign keys can reference partitioned tables, but test the exact PostgreSQL version and relationship design. Partitioning adds more indexes and objects to migrations, backups, and constraint validation.
Exercise
Why must a primary key on this table include order_date?
Show answer
PostgreSQL must include every partition-key column so it can enforce uniqueness across the logical table using partition-local indexes.