Skip to content

Add option to detach old partitions instead of dropping them - #301

Open
Mohith26 wants to merge 1 commit into
SectorLabs:masterfrom
Mohith26:detach-partitions
Open

Mohith26 wants to merge 1 commit into
SectorLabs:masterfrom
Mohith26:detach-partitions

Conversation

@Mohith26

@Mohith26 Mohith26 commented Nov 9, 2026

Copy link
Copy Markdown

Adds a way to detach old partitions instead of dropping them, so time-based retention doesn't have to throw data away.

What

  • PostgresSchemaEditor.detach_partition(model, name, *, concurrently=False) runs ALTER TABLE ... DETACH PARTITION ..., optionally with CONCURRENTLY (PostgreSQL 14+). Asking for concurrently=True inside a transaction raises TransactionManagementError up front instead of a Postgres error.
  • PostgresPartition.detach() (default implementation, so existing partition classes keep working).
  • PostgresPartitioningPlan.apply(detach=..., concurrently=...). With concurrently=True the new partitions are created in a transaction first and the old ones are then detached one by one outside it, since Postgres doesn't allow DETACH ... CONCURRENTLY in a transaction block.
  • pgpartition --detach {no,sequentially,concurrently} (default no, so nothing changes for existing users). The plan output says "will be detached" when it applies.
  • Docs: new "Detaching instead of deleting" section and a row in the CLI options table.

Why

Dropping partitions is irreversible, and the docs already warn against running deletions unattended. Detaching keeps the table and its rows as a standalone table that can be archived, exported or re-attached. CONCURRENTLY only takes a SHARE UPDATE EXCLUSIVE lock on the parent, so it can run on busy tables. The flag shape matches a patch Airspace Intelligence carries in their fork (https://github.com/uni-intelligence/django-postgres-extra-fork), so they could drop it.

Small related change: PostgresModelPartitioningPlan.apply() now opens its transaction on the using connection instead of always on default.

Tests

  • schema editor: detach keeps the table and rows, concurrent detach outside a transaction, clear error inside one
  • time strategy: max_age partitions are detached (not dropped), rows survive, re-planning ignores them; concurrent detach together with creations in one plan
  • pgpartition --detach sequentially|concurrently and --dry --detach

I don't have Docker here, so I ran the suite against PGlite (WASM Postgres) with a local tablespace path: 357 passed. The 6 failures I saw (test_settings.py search_path assertions and test_schema_editor_clone_model_to_schema) fail the same way on master under PGlite because its default search_path and constraint naming differ; CI on real Postgres should be the judge there. black, flake8, isort, docformatter and mypy are clean.

AI-assisted; tested locally as described above.

Adds PostgresSchemaEditor.detach_partition() and PostgresPartition.detach(),
lets PostgresPartitioningPlan.apply() detach partitions (optionally with
DETACH PARTITION ... CONCURRENTLY on PostgreSQL 14+) and exposes it as
`pgpartition --detach {no,sequentially,concurrently}`. The detached
tables and their rows are kept.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant