Sub topic 15
Migrating an Existing Table to Declarative Partitioning
Assume you know basic PostgreSQL table definitions, constraints, indexes, and EXPLAIN. Migration means moving data from one ordinary table into a new partitioned table while preserving application behavior and avoiding unnecessary downtime.
Choose a migration strategy
A partitioned table is a new table definition. PostgreSQL does not convert an ordinary table into a partitioned table with a simple ALTER TABLE. You usually choose between these approaches:
- Short maintenance window: create the new table, copy the data, briefly stop writes, and rename the tables.
- Attach existing data: create a separately populated table, add a suitable
CHECKconstraint, then attach it as a partition. - Incremental migration: copy data in batches while application writes continue, then use a change-capture process or brief write pause to reconcile the final changes.
The first approach is simplest. The third reduces downtime but requires more application or replication machinery.
Create and populate the replacement
Suppose the original table is orders. Create a partitioned replacement with the same logical columns, then create partitions that cover the data you intend to load.
CREATE TABLE orders_new (
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_new_2025_12
PARTITION OF orders_new
FOR VALUES FROM ('2025-12-01') TO ('2026-01-01');
CREATE TABLE orders_new_2026_01
PARTITION OF orders_new
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
INSERT INTO orders_new (order_id, customer_id, order_date, amount)
SELECT order_id, customer_id, order_date, amount
FROM orders;Before copying, ensure every source row matches a partition. Otherwise, the insert fails when PostgreSQL cannot route a row. A temporary DEFAULT partition can catch unexpected values, but inspect it before treating the migration as complete.
Recreate indexes, foreign keys, triggers, permissions, and application-facing dependencies deliberately. An index on the old table does not automatically move to the replacement.
Reduce locking when attaching data
For a large table, create and validate a standalone partition first:
CREATE TABLE orders_2025_11 (
order_id bigint NOT NULL,
customer_id bigint NOT NULL,
order_date date NOT NULL,
amount numeric(12, 2) NOT NULL
);
ALTER TABLE orders_2025_11
ADD CONSTRAINT orders_2025_11_bounds
CHECK (order_date >= DATE '2025-11-01'
AND order_date < DATE '2025-12-01');
ALTER TABLE orders_new
ATTACH PARTITION orders_2025_11
FOR VALUES FROM ('2025-11-01') TO ('2025-12-01');The check constraint helps PostgreSQL verify that existing rows satisfy the partition bounds without scanning the table in the same way. Plan the attach operation during a controlled period because it still requires locks.
Validate the cutover
Compare row counts and representative aggregates between the old and new tables. Run important queries with EXPLAIN and confirm that the expected partitions are scanned. During the final switch, pause writes, copy rows written since the initial load, rename the tables inside a transaction, and update dependent objects if necessary.
Exercise
Why must a migration define partitions before copying rows?
Show answer
PostgreSQL routes each inserted row to a partition immediately. A row with no matching partition causes the insert to fail, so the partition bounds must cover the source data first.