Sub topic 14
Attaching and Detaching Partitions
This chapter assumes you know basic PostgreSQL table definitions, constraints, indexes, and query plans. You will use those concepts to move tables into and out of a partitioned table safely.
Why attach an existing table?
ATTACH PARTITION adds an existing table to a partitioned table. PostgreSQL keeps the existing rows and treats the table as one of the parent table’s partitions.
This is useful when you load or validate data in a separate table before making it visible through the partitioned table. It can also reduce the amount of work during a migration.
The table must have columns compatible with the partitioned parent. Every existing row must satisfy the new partition’s bounds.
CREATE TABLE orders_2025_12 (
order_id bigint NOT NULL,
customer_id bigint NOT NULL,
order_date date NOT NULL,
amount numeric(12, 2) NOT NULL
);
INSERT INTO orders_2025_12
(order_id, customer_id, order_date, amount)
VALUES
(10, 42, DATE '2025-12-15', 25.00);
ALTER TABLE orders_2025_12
ADD CONSTRAINT orders_2025_12_bounds
CHECK (
order_date >= DATE '2025-12-01'
AND order_date < DATE '2026-01-01'
);
ALTER TABLE orders
ATTACH PARTITION orders_2025_12
FOR VALUES FROM ('2025-12-01') TO ('2026-01-01');The check constraint describes the same range as the partition bound. PostgreSQL can use it to avoid scanning the table to prove that every row belongs in the range. If the constraint is absent or does not match, PostgreSQL checks the table during the attach operation.
Locks and the default partition
Attaching a partition changes the parent’s partition structure. Plan for locks and run the operation during a suitable maintenance window, especially for a busy table.
A default partition needs extra care. PostgreSQL must ensure that rows in the default partition do not belong to the partition being attached. Move or delete conflicting rows first. A matching check constraint on the default partition can help PostgreSQL verify this condition.
| Operation | What happens to the rows? |
|---|---|
| Attach | Existing rows remain in the table and become rows of the parent partition |
| Detach | Existing rows remain in the detached table |
Detaching a partition
DETACH PARTITION removes a child from the parent without deleting its data. The detached table becomes an ordinary table. You can archive it, export it, or drop it after checking retention and backup requirements.
ALTER TABLE orders
DETACH PARTITION orders_2025_12;
SELECT count(*)
FROM orders_2025_12;
DROP TABLE orders_2025_12;Detaching is often safer than deleting old rows one at a time. However, inspect foreign keys, dependent views, permissions, indexes, and backup policies before removing the detached table.
> callout note > Attaching and detaching changes table membership. It does not copy rows between tables.
Exercise
Why add a check constraint to an existing table before attaching it as a partition?
Show answer
The constraint proves that existing rows satisfy the partition bounds and can help PostgreSQL avoid a full validation scan.