// SQL snippet

Rotate a table partition

Instead of deleting old rows, rotate whole partitions: attach an empty one for the new period, detach the full one for the expired period. Both steps are metadata operations, no row copying.

Main statement
PostgreSQL
还支持
MySQL

The task

A time-series table grows without bound and the monthly purge has become a multi-hour, lock-holding DELETE. Partition rotation turns it into two instant catalog operations.

The snippet

Written for PostgreSQL. Versions for MySQL are under Variations.

PostgreSQL
-- attach the new period's partition
CREATE TABLE measurements_2026_12
  PARTITION OF measurements
  FOR VALUES FROM ('2026-12-01') TO ('2027-01-01');

-- detach an expired one; its rows stay in what becomes a
-- standalone table. CONCURRENTLY (PostgreSQL 14+) avoids
-- blocking queries on the parent, but cannot run in a transaction.
ALTER TABLE measurements
  DETACH PARTITION measurements_2024_11;

工作原理

CREATE TABLE ... PARTITION OF registers a new empty partition for the declared range, and DETACH PARTITION removes an old one from the parent without moving a row; both are catalog changes. The detached table becomes an ordinary table you can dump, archive or drop. Plain DETACH takes an ACCESS EXCLUSIVE lock on the parent, so it waits for running queries and blocks new ones until it gets the lock; DETACH ... CONCURRENTLY, added in PostgreSQL 14, uses a weaker lock.

Variations

MySQL

MySQL
-- table PARTITION BY RANGE COLUMNS (ts); split the catch-all
-- partition (instant while it is empty), then drop the oldest
ALTER TABLE measurements
  REORGANIZE PARTITION p_future INTO (
    PARTITION p2026_12 VALUES LESS THAN ('2027-01-01'),
    PARTITION p_future VALUES LESS THAN (MAXVALUE));
ALTER TABLE measurements DROP PARTITION p2024_11;
-- DROP PARTITION deletes its rows; to keep them, first move
-- them out with EXCHANGE PARTITION ... WITH TABLE

PostgreSQL: attach an existing filled table

PostgreSQL
-- ATTACH scans the table to prove every row fits the bounds;
-- a matching valid CHECK constraint lets it skip the scan
ALTER TABLE measurements_2024_11
  ADD CONSTRAINT m_2024_11_bounds
  CHECK (ts IS NOT NULL AND ts >= '2024-11-01' AND ts < '2024-12-01');
ALTER TABLE measurements
  ATTACH PARTITION measurements_2024_11
  FOR VALUES FROM ('2024-11-01') TO ('2024-12-01');
ALTER TABLE measurements_2024_11
  DROP CONSTRAINT m_2024_11_bounds;

Questions that come up

What happens to queries that hit the detached partition?

They stop seeing its rows immediately: the parent's data now excludes that period while the rows sit in the standalone table. If reports still need the expired period, either keep the partition attached until its data leaves all service-level queries, or archive it somewhere the reports can reach.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.