Sub topic 7
Handling Unmatched Rows with a Default Partition
This chapter assumes basic PostgreSQL table definitions, range partitioning, and EXPLAIN. A default partition stores rows that do not match any explicitly declared partition.
Why use a default partition?
A range-partitioned table can have gaps between its declared bounds. For example, partitions might cover January and February but not April. Without a default partition, PostgreSQL rejects an inserted row that matches no partition.
A default partition provides a safe destination for such rows. It is useful for unexpected dates, late-arriving data, and temporary data that needs review.
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);
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');
CREATE TABLE orders_other
PARTITION OF orders DEFAULT;
INSERT INTO orders (order_id, customer_id, order_date, amount)
VALUES (1, 42, DATE '2026-04-15', 19.99);
SELECT tableoid::regclass AS physical_partition,
order_id,
order_date
FROM orders
WHERE order_id = 1;The insert succeeds because April does not fit either monthly partition. The query reports orders_other as the physical table containing the row. Applications still query orders, which remains the logical parent table.
Common errors when adding partitions
The default partition represents all values not covered by the other partitions. When you create a new partition, PostgreSQL must verify that existing rows in the default partition do not belong in the new partition.
For example, adding an April partition fails if orders_other already contains an April row. Move or delete those rows first, or add a suitable CHECK constraint to the default table when its contents are known to exclude the new range.
A default partition also has operational costs:
- Queries that filter for a date outside declared bounds may scan the default partition.
- Adding a new partition can require a scan of the default partition.
- Retention jobs must inspect the default partition separately.
- Rows in the default partition may need correction before they can move into a new partition.
Exercise
What happens when an inserted row matches neither an explicit partition nor its partition bounds?
Show answer
PostgreSQL stores the row in the default partition if one exists. Otherwise, it rejects the insert.