Skip to content

Technical ·

One Stray Row Can Stop Every New Partition

What a default partition costs in PostgreSQL: one misdated row that blocks a month from being created, and a scan under an exclusive lock every time a month is.

In January, a payment arrives dated the seventeenth of May. The table is partitioned by month, and May does not exist yet, so the row goes to the default partition. Nothing fails, and nothing is logged.

Months later the job that creates partitions reaches May, and PostgreSQL refuses. The job creates every table’s months in one call, so it creates none of them. When the months that already exist run out, every new row of every table goes to a default partition too.

I met this on a service I work on, before it cost anything, and rebuilt it here on a synthetic schema. The rule underneath is short: a row in the default partition is a claim PostgreSQL must check before it creates any partition, and it checks while holding the table exclusively. The default partition looks like a safety net, and it comes with those two costs.

This article is for people running range-partitioned tables with a scheduled job that creates the next partitions. Everything below ran on PostgreSQL 18.6.

The setup

Two tables are partitioned by month, each with a default partition, and one function creates the coming months for both:

CREATE TABLE payments (
    id          bigint GENERATED ALWAYS AS IDENTITY,
    occurred_on date NOT NULL,
    note        text NOT NULL
) PARTITION BY RANGE (occurred_on);
CREATE TABLE payments_default PARTITION OF payments DEFAULT;

-- audit_log is the same shape, partitioned by written_on.

CREATE FUNCTION mint_together(first_month date, months int) RETURNS void
LANGUAGE plpgsql AS $$
DECLARE
    tbl text;
BEGIN
    FOREACH tbl IN ARRAY ARRAY['payments', 'audit_log'] LOOP
        FOR i IN 0..months - 1 LOOP
            PERFORM create_month(tbl, (first_month + make_interval(months => i))::date);
        END LOOP;
    END LOOP;
END;
$$;

create_month runs one CREATE TABLE … PARTITION OF … FOR VALUES FROM … TO …. This is a common shape for a partition job: one scheduled call that creates everything due.

The refusal

Insert one row dated in a month that has no partition, then run the job for six months:

row dated 2026-05-17 is stored in: payments_default

SQLSTATE 23514: updated partition constraint for default partition
"payments_default" would be violated by some row
payments  partitions: ['default']
audit_log partitions: ['default']

The error is a check violation, and it is PostgreSQL being correct. Creating May changes what the default partition is allowed to hold: everything except the listed months, now including May. A May row already in the default would make that false, and rows there would become invisible to queries that prune to the May partition. So the month is refused.

The damage comes from the scope of the failure. January to April of payments were created and then rolled back, and audit_log never got a turn, although nothing is wrong with its data. One row in one table stopped twelve partitions.

It also gets worse with time. With no months to go to, new rows land in the default partitions:

payments_default holds 7 rows
audit_log_default holds 6 rows

Each of those rows is now a claim on its own month. By the same rule, removing the original May row would no longer be enough to let the job run.

Give each month its own subtransaction

The first fix is to stop one refusal from failing the rest. In PL/pgSQL, a BEGIN … EXCEPTION block is a subtransaction, so a failed month rolls back alone:

BEGIN
    PERFORM create_month(tbl, month_start);
EXCEPTION WHEN check_violation THEN
    RAISE WARNING '% % not created: %', tbl, to_char(month_start, 'YYYY-MM'), SQLERRM;
END;

Only the refusal is caught. Any other error still fails the whole job, as it should. The same stray row now costs one month of one table:

WARNING: payments 2026-05 not created: updated partition constraint for
default partition "payments_default" would be violated by some row
payments  partitions: ['2026_01', '2026_02', '2026_03', '2026_04', '2026_06', 'default']
audit_log partitions: ['2026_01', ..., '2026_05', '2026_06', 'default']

This contains the failure without repairing it. May still has no partition, so every May payment will go to the default, and the job now reports success. The warning is the only signal, which means something has to be listening for it.

The repair is to move the rows out and create the month in one transaction:

BEGIN;
CREATE TEMP TABLE moved ON COMMIT DROP AS
    SELECT occurred_on, note FROM payments LIMIT 0;
WITH gone AS (
    DELETE FROM payments_default
    WHERE occurred_on >= '2026-05-01' AND occurred_on < '2026-06-01'
    RETURNING occurred_on, note
)
INSERT INTO moved SELECT * FROM gone;
SELECT create_month('payments', '2026-05-01');
INSERT INTO payments (occurred_on, note) SELECT occurred_on, note FROM moved;
COMMIT;

In the experiment the row ends up in payments_2026_05. On a real table, check what references those rows first: deleting them from the default fires their foreign-key actions, and here they also get new identity values.

The check is a scan, and it holds the table

To know whether to refuse, PostgreSQL has to look. The documentation says so directly: when a partition is added, the default partition is scanned for rows that belong in the new one. So every month you create pays for every row the default holds, including rows that have nothing to do with that month.

That matters most when the default holds history. A table that starts creating months from the day it was deployed sends everything dated earlier to the default, and that can be most of the data. I filled the default with rows dated years before the first month and timed the creation of one new month, five runs each:

Rows in the defaultSizeCreate one month, median
00 bytes0.5 ms
10,000912 kB0.9 ms
100,0009 MB4.2 ms
1,000,00089 MB35.9 ms
2,000,000178 MB74.4 ms

The cost is linear, about 37 ms per million of these narrow rows with a warm cache.

The time alone would be tolerable. The locks are the problem. CREATE TABLE … PARTITION OF holds these until its transaction ends:

payments          AccessExclusiveLock
payments_default  AccessExclusiveLock

An access exclusive lock conflicts with everything, plain reads included. While the scan runs, nothing else touches the table. I ran three ordinary statements in a loop on other connections and recorded the slowest run of each while a month was created five times:

Slowest statementIdleDuring PARTITION OFDuring ATTACH
Insert into the current month0.7 ms81.7 ms0.7 ms
Read of the current month0.8 ms81.7 ms2.8 ms
Read across all months35.2 ms108.1 ms107.6 ms

An insert that normally takes under a millisecond waited for the whole scan.

Two ways to pay less

Attach instead of create. The third column is the alternative the partitioning chapter describes: create the month as an ordinary table, then ALTER TABLE … ATTACH PARTITION. The scan still happens and the default partition is still locked exclusively, but the parent takes a weaker lock:

payments          ShareUpdateExclusiveLock
payments_default  AccessExclusiveLock

Inserts into the current month and reads that prune to it never touch the default, so they no longer wait. A read across all months does touch it, and waits as before. Attaching narrows who waits, and the wait itself is as long as before.

Tell PostgreSQL what the default can hold. The scan is skipped when a constraint on the default partition already proves no row belongs in the new month. With two million rows in the default:

ALTER TABLE payments_default
    ADD CONSTRAINT history_only CHECK (occurred_on < date '2026-01-01');
create, median ms: 0.6
insert dated 2026-05-17: SQLSTATE 23514: new row for relation
"payments_default" violates check constraint "history_only"

Creating a month drops from 74 ms to 0.6 ms. The second line is the price, and arguably the point: the default now holds history only, and a row dated past the last month is rejected at insert instead of being kept quietly. The stray row from the opening could never have landed. Whether that is acceptable depends on whether your writers can handle a rejected row. If they cannot, the default has to stay a catch-all and you pay for the scan.

The third option needs no SQL at all: keep the default small. Create partitions for the history you expect before loading it, so the default holds only what is truly unexpected.

Where this ends

The complete experiment and its recorded output are one Python file against any PostgreSQL you point it at:

docker run --rm -d -p 5432:5432 -e POSTGRES_PASSWORD=pg postgres:18
pip install "psycopg[binary]"
DATABASE_URL=postgresql://postgres:pg@localhost:5432/postgres python3 experiment.py

The recorded run used PostgreSQL 18.6 with default settings on an 8-core arm64 Linux VM, Python 3.14.10 and psycopg 3.3.8. What it does not show:

  • The timings are one machine’s. The rows are about 90 bytes and the cache was warm. Wider rows or a cold cache make the scan slower, and the 37 ms per million is a shape to expect, not a number to plan with.
  • The wait can be longer than the scan. A statement that needs an exclusive lock queues behind any transaction already using the table, and everything arriving later queues behind it. That is PostgreSQL’s documented lock behaviour and is not measured here. A job that creates partitions should set a lock_timeout and retry.
  • A warning nobody reads protects nothing. The subtransaction version turns a failed job into a successful one with a warning. Without an alert on that warning, the refused month fills the default silently, which is the original problem on a smaller scale. A job that never runs raises no warning at all.
  • Tools were not tested. This is plain SQL and PL/pgSQL. Extensions that manage partitions have their own handling of the default partition, which I did not run.

Before trusting a default partition as a safety net, count what is in it and ask which month each row is waiting for. Every row there is either history you chose to keep or a month you will not be able to create.