Sub topic 2
Handling Unmatched Rows with a Default Partition
This chapter assumes you know basic SQL, primary keys, indexes, and how to create a partitioned table with PARTITION BY RANGE. It builds on the orders table from the earlier chapters.
Adding a default partition
A default partition is a catch-all partition that stores rows matching no other partition's bounds. Without one, an insert that matches no partition fails.
CREATE TABLE orders (
order_id bigint NOT NULL,
customer_id bigint NOT NULL,
order_date date NOT NULL,
amount numeric(12, 2) NOT NULL,
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_default PARTITION OF orders DEFAULT;
INSERT INTO orders VALUES (1, 42, DATE '2026-01-15', 19.99);
INSERT INTO orders VALUES (2, 77, DATE '2026-04-15', 5.00);
SELECT tableoid::regclass AS physical_partition, order_id
FROM orders;The second row has no matching range, so it lands in orders_default.
How PostgreSQL routes inserts and updates
For each inserted row, PostgreSQL checks the partition key against the bounds of each partition. If one matches, the row goes there. If none matches, the row goes to the default partition. If no default exists, the insert fails with an error.
flowchart TD
A[INSERT row] --> B{Matches a range partition?}
B -- Yes --> C[Insert into that partition]
B -- No --> D{Default partition exists?}
D -- Yes --> E[Insert into default partition]
D -- No --> F[Error: no partition found]An UPDATE that changes the partition key moves the row. PostgreSQL deletes it from the old partition and inserts it into the matching one.
How partition pruning reduces scanned data
Pruning means the planner skips partitions whose bounds cannot satisfy the WHERE clause. Pruning works only when the filter uses the partition key directly.
EXPLAIN (COSTS OFF)
SELECT * FROM orders
WHERE order_date >= DATE '2026-01-10'
AND order_date < DATE '2026-01-15';Check the plan output to confirm which partitions are scanned. A default partition can hold any unmatched value, so it is harder to exclude. Keep it small.
Indexes on partitioned tables
Creating an index on the parent table creates a matching index on each partition, including partitions you add later.
CREATE INDEX orders_customer_idx ON orders (customer_id);> Plain CREATE INDEX on a partitioned table does not support CONCURRENTLY. Plan for a lock during the build, or build the index on each partition separately.
Constraints, primary keys, and uniqueness
A primary key or unique constraint on a partitioned table must include every partition key column. In the example, the key is (order_id, order_date) because the table is partitioned by order_date. PostgreSQL can then enforce uniqueness within each partition, and it cannot check uniqueness across partitions unless the key includes the partition key.
Partition hierarchies and subpartitioning
A partition can itself be partitioned. This is subpartitioning. For example, orders_2026_01 could be partitioned by customer_id hash. Pruning applies at each level, so a filter on the top key skips whole branches of the hierarchy.
Exercise
Exercise
The orders table has a default partition and a range partition for January 2026. You insert a row dated 2026-01-20. Which partition stores it, and what happens if you later create a February partition while the default partition already holds February rows?
Show answer
The row goes to orders202601 because its date falls in the January range. Creating the February partition fails, because PostgreSQL must check the default partition for rows that belong in the new partition and finds some. Move or delete those rows first, then create the partition.