Sub topic 7
The default partition and rows with no home
This chapter assumes you can create range and list partitioned tables with PARTITION BY and PARTITION OF. It also assumes you can read basic EXPLAIN output. The examples use the measurements table from the range partitioning chapter.
What happens to a row with no partition
A partitioned table stores no rows itself. On every insert, PostgreSQL looks for the partition whose bounds contain the row's partition key. If no partition matches, the insert fails.
CREATE TABLE measurements (
device_id int NOT NULL,
recorded_at timestamptz NOT NULL,
reading numeric
) PARTITION BY RANGE (recorded_at);
CREATE TABLE measurements_2026_01 PARTITION OF measurements
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TABLE measurements_2026_02 PARTITION OF measurements
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');
INSERT INTO measurements (device_id, recorded_at, reading)
VALUES (1, '2030-06-01', 20.5);
-- ERROR: no partition of relation "measurements" found for row
-- DETAIL: Partition key of the failing row contains (recorded_at) = (2030-06-01 00:00:00+00).The same error appears when an UPDATE changes a row's key to a value that no partition covers. Failing loudly is often what you want. A missing partition usually means a scheduled job did not run, and the error tells you immediately. It also means one bad row can abort a whole batch load.
Creating a default partition
A default partition accepts every row that fits no other partition.
CREATE TABLE measurements_default PARTITION OF measurements DEFAULT;
INSERT INTO measurements (device_id, recorded_at, reading)
VALUES (1, '2030-06-01', 20.5);
SELECT tableoid::regclass, count(*) FROM measurements GROUP BY 1;The row is now stored in measurements_default. The tableoid::regclass expression shows which physical partition holds each row.
The rules for default partitions are:
- A partitioned table can have at most one default partition.
- The default partition has no bounds. Its implicit constraint is "not in any other partition", and PostgreSQL recomputes it each time a partition is added or removed.
- Range and list partitioned tables can have one. Hash partitioned tables cannot, because every hash value already maps to some remainder.
- The default partition is an ordinary table. You can index it, query it directly, and load data into it.
flowchart TDThe Mermaid source above is only a placeholder and must not be used. The diagram below is the one for this chapter.
NULL keys
Declare partition key columns NOT NULL, as the examples do, so the question never arises. If you do allow NULLs, the behavior depends on the method. A list partition can claim NULL explicitly with FOR VALUES IN (NULL). Range partitions have no bound that can hold NULL, so such a row goes to the default partition if one exists and is rejected otherwise.
Cost of adding partitions when a default exists
The default partition makes adding new partitions more expensive. Suppose rows for March 2026 have already landed in measurements_default because no March partition existed. Creating the March partition now would make those rows violate the default partition's constraint. PostgreSQL refuses:
CREATE TABLE measurements_2026_03 PARTITION OF measurements
FOR VALUES FROM ('2026-03-01') TO ('2026-04-01');
-- ERROR: updated partition constraint for default partition "measurements_default"
-- would be violated by some rowEven when the default partition holds no conflicting rows, PostgreSQL must prove that. It scans the whole default partition while holding a lock that blocks other access to it. The scan is skipped only when an existing CHECK constraint on the default partition already proves that no row falls in the new range. A default partition that grows large makes every future partition addition slower.
Moving stranded rows into a new partition
When rows have landed in the default partition, move them in one transaction. Create the new table, move the matching rows, then attach it.
BEGIN;
CREATE TABLE measurements_2026_03
(LIKE measurements INCLUDING DEFAULTS INCLUDING CONSTRAINTS);
WITH moved AS (
DELETE FROM measurements_default
WHERE recorded_at >= '2026-03-01' AND recorded_at < '2026-04-01'
RETURNING *
)
INSERT INTO measurements_2026_03 SELECT * FROM moved;
ALTER TABLE measurements ATTACH PARTITION measurements_2026_03
FOR VALUES FROM ('2026-03-01') TO ('2026-04-01');
COMMIT;The DELETE runs against the default partition directly, and RETURNING * feeds the deleted rows into the new table. By the time ATTACH PARTITION scans the default partition, no conflicting rows remain, so the attach succeeds. Because the transaction is atomic, concurrent readers never see the rows missing or duplicated.
If the default partition holds a lot of data, the row move is the slow step. Do it in a maintenance window or in batches, and expect the attach to hold its lock on the default partition for the length of the scan.
Pruning and the default partition
The default partition takes part in partition pruning like any other partition. Its constraint is the negation of all the others, so the planner can exclude it when a query's filter falls entirely inside existing partitions. A filter on February 2026 does not touch the default partition. A filter that reaches beyond the covered ranges, or an unbounded one such as recorded_at >= '2026-02-01', must include it in the plan. Run EXPLAIN on your common queries to see which case applies.
A default partition full of stray rows therefore costs you twice. Queries that cannot prune it scan it, and every partition change has to validate it.
Detecting rows in the default partition
Treat the default partition as a holding area and monitor it. The ONLY keyword is not needed on a partition that has no children, but it makes the intent clear.
SELECT count(*) AS stray_rows,
min(recorded_at) AS earliest,
max(recorded_at) AS latest
FROM measurements_default;Run this check from a scheduled job and alert when stray_rows is above zero. The earliest and latest values show which partitions you forgot to create.
Interaction with DETACH CONCURRENTLY
The retention chapter used DETACH PARTITION ... CONCURRENTLY. That form cannot be used on a table that has a default partition. PostgreSQL rejects it, because removing a partition changes the default partition's implicit constraint and the concurrent form cannot make that change safely. With a default partition you must use plain DETACH PARTITION, which takes a stronger lock on the parent table. This is a real cost for tables that must stay available during retention jobs.
ALTER TABLE measurements DETACH PARTITION measurements_2026_01;Deciding whether to have one
| Situation | Default partition? | Reason |
|---|---|---|
| Time-series table with automated partition creation | Usually no | A missing partition is a bug, and the insert error exposes it at once |
| Ingest pipeline where losing a row is worse than misfiling it | Yes, with monitoring | Rows are kept and can be moved later |
| List partitioning with an open-ended set of values such as new regions | Often yes | New values appear without warning |
| Hash partitioning | Not available | Every key already has a partition |
| Table that needs `DETACH ... CONCURRENTLY` | No | The concurrent form is not allowed with a default partition |
The two failure modes are different. Without a default partition, bad or early data causes visible errors and rejected inserts. With one, the same data is accepted silently and the cost shows up later, as slower queries and slower partition changes. Choose the first if your team will notice errors and the second if you cannot afford to reject writes. In either case, create future partitions ahead of time so the problem rarely arises.
Summary of rules
- Without a default partition, a row with no matching partition makes the
INSERTorUPDATEfail. - With one, such a row is stored there, and
tableoid::regclassshows where. - Only one default partition is allowed, and hash partitioning does not allow one.
- Adding a partition scans the default partition for conflicting rows, and fails if it finds any.
- Move conflicting rows out first, in one transaction with the attach.
DETACH ... CONCURRENTLYis unavailable while a default partition exists.
Exercise
A table sales is range partitioned by sold_on with partitions for 2026-01 and 2026-02 plus a default partition. Rows for March 2026 have been inserted and are in the default partition. You run CREATE TABLE sales_2026_03 PARTITION OF sales FOR VALUES FROM ('2026-03-01') TO ('2026-04-01'); and it fails. Why does it fail, and what steps fix it without losing or duplicating rows?
Show answer
The default partition's implicit constraint says "not in any other partition". Adding a March partition would make the March rows already in the default partition violate that constraint, so PostgreSQL raises "updated partition constraint for default partition would be violated by some row". To fix it, in one transaction: create a standalone table with LIKE sales, run WITH moved AS (DELETE FROM sales_default WHERE sold_on >= '2026-03-01' AND sold_on < '2026-04-01' RETURNING *) INSERT INTO sales_2026_03 SELECT * FROM moved, then ALTER TABLE sales ATTACH PARTITION sales_2026_03 FOR VALUES FROM ('2026-03-01') TO ('2026-04-01'), and commit. The default partition no longer holds March rows when the attach validates it.
Exercise
Why can a hash partitioned table not have a default partition, and why might a team running nightly DETACH PARTITION ... CONCURRENTLY retention jobs avoid a default partition on a range partitioned table?
Show answer
Hash partitioning with a given modulus assigns every possible key to exactly one remainder, so no key is ever left without a partition and a default partition would never receive a row. DETACH PARTITION ... CONCURRENTLY is not allowed when the table has a default partition, so a team that relies on it to avoid blocking queries during retention must leave the default partition out, or fall back to the plain DETACH PARTITION with its stronger lock.