|
| 1 | +# Migrating an existing partitioner |
| 2 | + |
| 3 | +This guide covers the traps we have seen real projects hit when replacing a hand-rolled |
| 4 | +partition maintenance script with pg-partsmith. Read it before the first production tick. |
| 5 | + |
| 6 | +## Retention: count, not distance |
| 7 | + |
| 8 | +Hand-rolled pruners usually express retention as a *distance*: "drop everything older than |
| 9 | +`N` periods from now", which keeps `N + 1` partitions on disk (the current one plus `N` |
| 10 | +past). pg-partsmith's `retention_count` is a *count*: "keep exactly `N` newest periods, |
| 11 | +current one included". |
| 12 | + |
| 13 | +Passing a distance straight into `retention_count` silently drops one extra period of data |
| 14 | +on the first tick after deploy. Convert once, at the boundary: |
| 15 | + |
| 16 | +```python |
| 17 | +config = TablePartitionConfig( |
| 18 | + ..., |
| 19 | + # old semantics: "keep current + N past periods" |
| 20 | + retention_count=old_retention_distance + 1, |
| 21 | +) |
| 22 | +``` |
| 23 | + |
| 24 | +Verify on a staging copy: run one maintenance tick and compare the set of surviving |
| 25 | +partitions with what the old pruner would have kept. |
| 26 | + |
| 27 | +## Adopting legacy detached partitions |
| 28 | + |
| 29 | +pg-partsmith only ever drops tables that carry its `COMMENT` marker (see |
| 30 | +[Advanced](advanced.md)). The marker is stamped automatically when *the library* detaches a |
| 31 | +partition — so partitions that are attached at migration time need nothing: the first |
| 32 | +maintenance tick detaches and marks them itself. |
| 33 | + |
| 34 | +The one case that needs action is tables your *old* partitioner already detached and never |
| 35 | +dropped (a failed export, a crashed run). They carry no marker, so they are invisible to |
| 36 | +orphan discovery and will sit there forever. **Do not reach for `drop_allow_unmanaged=True` |
| 37 | +to handle them** — that disables the safe-drop guard for every drop, permanently, and does |
| 38 | +not even help: unmarked detached tables are never discovered in the first place. Adopt them |
| 39 | +once instead: |
| 40 | + |
| 41 | +```python |
| 42 | +repo.adopt_partition("events", "events__2024_01") # stamps the marker; True when done |
| 43 | +``` |
| 44 | + |
| 45 | +Adoption is idempotent, refuses attached partitions (`PartitionAttachedError`), and returns |
| 46 | +`False` for names that do not resolve. The next maintenance tick collects adopted tables |
| 47 | +like any other orphan: `before_drop` hooks run, then they are dropped. |
| 48 | + |
| 49 | +## Partition names are schema-qualified |
| 50 | + |
| 51 | +`list_partitions` returns names as `schema.relname` (e.g. `public.events__2024_01`) — a |
| 52 | +partition may live in a different schema than its parent, and a bare name could resolve to |
| 53 | +an unrelated table through `search_path`. Code that works with bare names (period parsing, |
| 54 | +export layouts) should use the accessors instead of splitting strings: |
| 55 | + |
| 56 | +```python |
| 57 | +for p in metadata.list_partitions("events"): |
| 58 | + p.name # "public.events__2024_01" — use for DDL and library calls |
| 59 | + p.relname # "events__2024_01" — use for parsing / external layouts |
| 60 | + p.schema_name # "public" (None if the name is unqualified) |
| 61 | +``` |
| 62 | + |
| 63 | +## Mapping partitions back to periods |
| 64 | + |
| 65 | +There is no need for custom catalogue queries to answer "which period does this partition |
| 66 | +hold" — combine `list_partitions` with the calculator you already have: |
| 67 | + |
| 68 | +```python |
| 69 | +calc = get_period_calculator(config.granularity) |
| 70 | +by_period = { |
| 71 | + period: p |
| 72 | + for p in metadata.list_partitions("events") |
| 73 | + if p.is_attached and (period := calc.parse_partition_name(p.relname)) is not None |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## Who takes the lock |
| 78 | + |
| 79 | +`maintain_lifecycle` (and the maintainer on top of it) takes the distributed lock itself. |
| 80 | +The granular service methods — `create_future_partitions`, `ensure_partition`, |
| 81 | +`get_partitions_for_pruning`, `detach_old_partitions`, `drop_detached_partitions` — do |
| 82 | +**not**: when you orchestrate them yourself, hold the lock around the whole sequence: |
| 83 | + |
| 84 | +```python |
| 85 | +with locks.acquire_lock("events"): |
| 86 | + service.create_future_partitions(config) |
| 87 | + ... |
| 88 | +``` |
| 89 | + |
| 90 | +The advisory lock is non-blocking (`pg_try_advisory_lock`): a tick that collides with |
| 91 | +another replica raises `LockAcquisitionError` immediately. Catch it and skip the tick — |
| 92 | +the schedule brings it back. |
| 93 | + |
| 94 | +## Creating one specific partition |
| 95 | + |
| 96 | +`create_future_partitions` covers the scheduled create-ahead path. When a *writer* must |
| 97 | +guarantee a partition exists before an insert (an hourly buffer, a backfill for a past |
| 98 | +period), use `ensure_partition` — it targets exactly one period and runs the same DEFAULT |
| 99 | +reconciliation and attach-race handling: |
| 100 | + |
| 101 | +```python |
| 102 | +service.ensure_partition(config, Period(year=2026, month=8, day=27, hour=14)) |
| 103 | +``` |
| 104 | + |
| 105 | +Do not hand-roll `create_partition` + `attach_partition` for this: the raw repository |
| 106 | +calls skip reconciliation, so rows sitting in a DEFAULT partition or a concurrent worker |
| 107 | +will fail the attach. |
| 108 | + |
| 109 | +## One failed step should not stop the tick |
| 110 | + |
| 111 | +By default `maintain_lifecycle` aborts on the first error. For a scheduled tick you |
| 112 | +usually want the opposite — a failed create must not prevent pruning (which may free the |
| 113 | +very space create needs): |
| 114 | + |
| 115 | +```python |
| 116 | +result = maintainer.run_maintenance_safe(config, continue_on_error=True) |
| 117 | +for issue in result.issues: |
| 118 | + log.warning("step failed", step=issue.step, error=issue.error) |
| 119 | +``` |
| 120 | + |
| 121 | +Step failures land in `result.issues` (`MaintenanceIssue`) instead of aborting; |
| 122 | +validation and lock failures are still fatal. |
| 123 | + |
| 124 | +## Export pipelines: when is a partition finished? |
| 125 | + |
| 126 | +Incremental exporters need to know when a partition can no longer receive rows so they can |
| 127 | +finalize it. `is_partition_closed` answers this with a single server-side check — `now()` |
| 128 | +and the partition bound come from the same query, so replica lag and app-clock skew do not |
| 129 | +skew the answer: |
| 130 | + |
| 131 | +```python |
| 132 | +if metadata.is_partition_closed("public.events__2026_07", settle_seconds=900): |
| 133 | + finalize_export(...) |
| 134 | +``` |
| 135 | + |
| 136 | +Exports that must happen *before* a partition is dropped belong in a `before_drop` hook: |
| 137 | +raising there aborts that partition's drop, and the orphan marker retries it next tick. |
0 commit comments