Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)runsALTER TABLE ... DETACH PARTITION ..., optionally withCONCURRENTLY(PostgreSQL 14+). Asking forconcurrently=Trueinside a transaction raisesTransactionManagementErrorup front instead of a Postgres error.PostgresPartition.detach()(default implementation, so existing partition classes keep working).PostgresPartitioningPlan.apply(detach=..., concurrently=...). Withconcurrently=Truethe new partitions are created in a transaction first and the old ones are then detached one by one outside it, since Postgres doesn't allowDETACH ... CONCURRENTLYin a transaction block.pgpartition --detach {no,sequentially,concurrently}(defaultno, so nothing changes for existing users). The plan output says "will be detached" when it applies.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.
CONCURRENTLYonly takes aSHARE UPDATE EXCLUSIVElock 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 theusingconnection instead of always ondefault.Tests
max_agepartitions are detached (not dropped), rows survive, re-planning ignores them; concurrent detach together with creations in one planpgpartition --detach sequentially|concurrentlyand--dry --detachI 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.pysearch_path assertions andtest_schema_editor_clone_model_to_schema) fail the same way onmasterunder PGlite because its defaultsearch_pathand 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.