Skip to content

Commit e7dde5e

Browse files
Merge pull request #31 from bedrock-python/fix/naive-bound-without-a-timezone
One event for every lifecycle hook, and the last silent disagreement from #27
2 parents 85b3d4f + 38d8c23 commit e7dde5e

41 files changed

Lines changed: 1899 additions & 537 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CHANGELOG.md‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,64 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 1.1.0 — one event for every hook
9+
10+
**This breaks every hook written against 1.0.** It is shipped in a minor version on
11+
purpose: the library is days old and has no dependants to protect, and carrying the old
12+
shape until a major would mean carrying it for years. Ported hooks are two lines of change
13+
each; unported ones are refused when the service is constructed, with the fix in the
14+
message, rather than failing at their first call in the middle of a maintenance run.
15+
16+
### What changed
17+
18+
Three of the six phases were handed a rich object and three a bare string, so a
19+
`before_drop` that archived a partition could not know which period it was archiving —
20+
and could not look it up either, because `DETACH` clears `relpartbound`. Every hook method
21+
now takes one `PartitionEvent`:
22+
23+
| Field | What it is |
24+
|---|---|
25+
| `phase` | which moment this is (`HookPhase`) |
26+
| `config` | the table's configuration — no longer something a hook has to be given separately |
27+
| `partition` | `PartitionInfo`: name, bounds, OID, `subpartition_type` |
28+
| `window` | the period covered; `None` for a member of a root `HASH` or `LIST` |
29+
| `operation` | the planned operation: `reason`, `detail`, `oid`, `size_bytes`, `row_estimate`, `detached_at` |
30+
| `table_name` | the root table, derived from `config` |
31+
32+
| Before | After |
33+
|---|---|
34+
| `before_create(config, partition)` | `before_create(event)` — `event.config`, `event.partition` |
35+
| `after_create(config, partition)` | `after_create(event)` |
36+
| `before_detach(table_name, partition)` | `before_detach(event)` — `event.table_name`, `event.partition` |
37+
| `after_detach(table_name, partition_name)` | `after_detach(event)` — `event.partition.name` |
38+
| `before_drop(table_name, partition_name)` | `before_drop(event)` — and now `event.window`, `event.operation.reason` |
39+
| `after_drop(table_name, partition_name)` | `after_drop(event)` |
40+
| — | `before_attach(event)` / `after_attach(event)`, new: a detached partition going back into the tree |
41+
| — | `on_event(event)`, new: fires for every phase, just before the method named for it |
42+
43+
### Also breaking, in the same area
44+
45+
- `PlanExecutor.detach_single_partition` and `drop_single_partition` take the
46+
`TablePartitionConfig` instead of the parent's name — it is what the event is built
47+
from, and every other entry point already took the config.
48+
- `PartitionLifecycleService.detach_old_partitions` and `drop_detached_partitions` take
49+
the config for the same reason: `detach_old_partitions(config, partitions)`,
50+
`drop_detached_partitions(config, names)`.
51+
52+
### Added
53+
54+
- `PartitionEvent`, `HookPhase` (exported from `pg_partsmith`), and `on_event` — one
55+
method for an audit trail or a metrics counter across every phase, instead of one
56+
identical delegating method per phase, plus another with every phase added. It runs
57+
before the method named for the phase, so a `before_*` that refuses an operation by
58+
raising still leaves the audit trail with the attempt in it.
59+
- `before_attach` / `after_attach`: a partition retention released is re-attached when its
60+
window is wanted again — retention grew, or create-ahead reached back to it — and that
61+
transition used to happen silently. An export taken while it was detached goes stale the
62+
moment it comes back, and this is where a hook is told.
63+
- Hook signatures are read when the service is constructed; one still taking the 1.0
64+
arguments is refused there, naming the class, the method and the shape to move to.
65+
866
## [1.0.0](https://github.com/bedrock-python/pg-partsmith/compare/pg-partsmith-v0.5.0...pg-partsmith-v1.0.0) (2026-09-01)
967

1068

‎README.md‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,30 @@ Two behavioural differences: `ddl_timeout_seconds` is enforced server-side via
223223
## Hooks
224224

225225
```python
226+
from pg_partsmith import PartitionEvent
226227
from pg_partsmith.aio import BasePartitionLifecycleHooks
227228

228229

229230
class ColdStorageHooks(BasePartitionLifecycleHooks):
230-
async def before_drop(self, table_name: str, partition_name: str) -> None:
231-
await export_to_object_storage(partition_name) # raising aborts the drop; retried next tick
231+
async def before_drop(self, event: PartitionEvent) -> None:
232+
await export_to_object_storage(event.partition.name, covering=event.window)
233+
# raising aborts the drop; retried next tick
232234

233235

234236
service = PartitionLifecycleService(repo, metadata, locks, hooks=[ColdStorageHooks()])
235237
```
236238

239+
Every method takes one `PartitionEvent`: the `phase`, the `config`, the `partition`, the
240+
`window` it covers, and the `operation` being carried out — with the `reason` it was
241+
planned and the size the policy measured, when it asked for one.
242+
237243
| Method | When |
238244
|--------|------|
239-
| `before_create(config, partition)` / `after_create(config, partition)` | around the creation of a partition directly under the root (its subtree included) |
240-
| `before_detach(table_name, partition)` / `after_detach(table_name, partition_name)` | around a detach |
241-
| `before_drop(table_name, partition_name)` / `after_drop(table_name, partition_name)` | around a drop — `before_drop` is the last chance to read the data |
245+
| `before_create` / `after_create` | around the creation of a partition directly under the root (its subtree included) |
246+
| `before_attach` / `after_attach` | around bringing a detached partition back into the tree |
247+
| `before_detach` / `after_detach` | around a detach |
248+
| `before_drop` / `after_drop` | around a drop — `before_drop` is the last chance to read the data |
249+
| `on_event` | every one of the above, for an audit trail or metrics in one method |
242250

243251
`before_*` exceptions abort that operation; `after_*` exceptions are logged.
244252

‎docs/agents.md‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ Every method takes the `TablePartitionConfig` as its first argument.
239239
| `unpartition(config, into, *, batch_rows=10_000, max_batches=None, drop_emptied=False)` | **yes** | yes | `MigrationResult` |
240240
| `create_future_partitions(config)` | no | yes | `list[PartitionInfo]` |
241241
| `get_partitions_for_pruning(config)` | no | no | `list[PartitionInfo]` |
242-
| `detach_old_partitions(table_name, partitions)` | no | yes | `list[str]` |
243-
| `drop_detached_partitions(table_name, partition_names)` | no | yes | `int` |
242+
| `detach_old_partitions(config, partitions)` | no | yes | `list[str]` |
243+
| `drop_detached_partitions(config, partition_names)` | no | yes | `int` |
244244

245245
`maintain_lifecycle` is the same method as `maintain`. `PartitionMaintainer(service)` adds
246246
`run_maintenance(...)` and `run_maintenance_safe(...)` — same keywords; the *safe* one never
@@ -268,13 +268,16 @@ Results:
268268

269269
```python
270270
class ColdStorageHooks(BasePartitionLifecycleHooks):
271-
async def before_drop(self, table_name: str, partition_name: str) -> None:
272-
await export(partition_name) # raising aborts this drop; it is retried next tick
271+
async def before_drop(self, event: PartitionEvent) -> None:
272+
await export(event.partition.name) # raising aborts this drop; it is retried next tick
273273
```
274274

275-
Six methods: `before_create(config, partition)` / `after_create(config, partition)`,
276-
`before_detach(table_name, partition)` / `after_detach(table_name, partition_name)`,
277-
`before_drop(table_name, partition_name)` / `after_drop(table_name, partition_name)`.
275+
Nine methods, each taking one `PartitionEvent(phase, config, partition, window, operation)`:
276+
`before_create` / `after_create`, `before_attach` / `after_attach` (a detached partition
277+
coming back), `before_detach` / `after_detach`, `before_drop` / `after_drop`, and
278+
`on_event`, which fires for every phase in addition to the named method.
279+
`event.table_name` is the root; `event.operation.reason` says why the operation is in the
280+
plan; `event.window` is the period, or None for a member of a root HASH or LIST.
278281
`before_*` exceptions abort that operation; `after_*` exceptions are logged. Hooks fire for
279282
lifecycle units — partitions directly under the root — never once per leaf of a subtree.
280283

‎docs/design/api-cheatsheet-1.0.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# pg-partsmith 1.0 — API cheat sheet (what changed from 0.5, for porting tests and docs)
22

3-
Source of truth is the code; this page is the map.
3+
Source of truth is the code; this page is the map. Two entries were superseded in 1.1 —
4+
the hook signatures and the executor's single-partition entry points — and say so where
5+
they stand; the [changelog](../changelog.md) has that migration.
46

57
## Pure core (shared by aio and sync)
68

@@ -44,15 +46,15 @@ TablePartitionConfig(table_name="regions", scheme=ListPartitioning(key="region",
4446

4547
Service wiring: `PartitionLifecycleService(repo, metadata, locks, hooks=None)` — **no `period_calculator` argument**; the calculator/tz/codec live in `TimeBoundaries`. `PostgresMetadataProvider(engine, marker_prefix=, boundary_codec=, ddl_timezone=)` (codec only for `is_partition_closed`).
4648

47-
Service methods: `inspect(config) -> ActualTree | None`, `plan(config, *, mode=, now=, windows=) -> MaintenancePlan` (no lock, no DDL), `apply(config, plan, *, continue_on_error=) -> MaintenanceResult` (lock), `maintain(config, *, skip_create, skip_detach, skip_drop, continue_on_error)` (= `maintain_lifecycle`, lock; plan + apply), `reconcile(config)`, `ensure_partition(config, period|Window) -> PartitionInfo | None`, `ensure_partitions(config, periods) -> list[PartitionInfo]`, granular `create_future_partitions(config)`, `get_partitions_for_pruning(config)`, `detach_old_partitions(table, partitions)`, `drop_detached_partitions(table, names)`; movers `partition_data(config, *, batch_rows, max_batches) -> MigrationResult` and `unpartition(config, into, *, batch_rows, max_batches, drop_emptied)` (both take the lock).
49+
Service methods: `inspect(config) -> ActualTree | None`, `plan(config, *, mode=, now=, windows=) -> MaintenancePlan` (no lock, no DDL), `apply(config, plan, *, continue_on_error=) -> MaintenanceResult` (lock), `maintain(config, *, skip_create, skip_detach, skip_drop, continue_on_error)` (= `maintain_lifecycle`, lock; plan + apply), `reconcile(config)`, `ensure_partition(config, period|Window) -> PartitionInfo | None`, `ensure_partitions(config, periods) -> list[PartitionInfo]`, granular `create_future_partitions(config)`, `get_partitions_for_pruning(config)`, `detach_old_partitions(config, partitions)`, `drop_detached_partitions(config, names)` (both took the parent's name until 1.1); movers `partition_data(config, *, batch_rows, max_batches) -> MigrationResult` and `unpartition(config, into, *, batch_rows, max_batches, drop_emptied)` (both take the lock).
4850

49-
Executor (`aio/services/execution.py` `PlanExecutor(repo, metadata, hooks)`): `apply(config, plan, continue_on_error)`, extension points `create_partition(config, plan, op, *, issues, fill=None) -> bool`, `detach_single_partition(table_name, DetachPartition)`, `drop_single_partition(table_name, DropPartition)`. Mover (`services/migration.py` `DataMover(repo, metadata, executor)`). Inspector (`services/inspection.py` `PartitionInspector(metadata)`): `inspect(config, measure=)`, `context(config, now=, mode=, explicit_windows=)`. Validation unchanged in spirit (`PartitionValidationService(metadata).validate_config(config)`).
51+
Executor (`aio/services/execution.py` `PlanExecutor(repo, metadata, hooks)`): `apply(config, plan, continue_on_error)`, extension points `create_partition(config, plan, op, *, issues, fill=None) -> bool`, `detach_single_partition(config, DetachPartition)`, `drop_single_partition(config, DropPartition)` (both took the parent's name until 1.1). Mover (`services/migration.py` `DataMover(repo, metadata, executor)`). Inspector (`services/inspection.py` `PartitionInspector(metadata)`): `inspect(config, measure=)`, `context(config, now=, mode=, explicit_windows=)`. Validation unchanged in spirit (`PartitionValidationService(metadata).validate_config(config)`).
5052

5153
Repository protocol (`PartitionRepository`): `create_table_like(template_name, table_name, partition_by: PartitionBy | None, *, physical: LocalLeaves | None = None)`, `create_foreign_table_like(template_name, table_name, *, server, options)`, `move_rows(source, target, *, limit=None) -> int`, `attach_partition(parent, partition, bounds: PartitionBounds, *, key_arity=1)`, `detach_partition(parent, partition, *, mode=DetachMode.AUTO)`, `drop_partition(partition, *, expected_oid=None)`, `adopt_partition(table, partition) -> bool`, `reconcile_default_rows(*, default_partition_name, target_partition_name, key_columns, from_value, to_value, limit=None) -> int`. Old `create_partition/create_branch/create_subpartition_table/attach_subpartition/attach_composite_partition` are gone.
5254

5355
Metadata protocol (`PartitionMetadataProvider`): `get_partition_type`, `get_partition_columns`, `get_actual_tree(table) -> ActualTree | None`, `measure(tree, *, targets, facts, sql_predicates) -> ActualTree`, `get_partition_tree(name) -> PartitionNode | None`, `get_default_partition`, `partition_exists`, `is_partition_attached`, `get_relation_oid(name)`, `get_unique_constraint_columns`, `get_key_high_water_mark(table, column, *, sequence=False)`, `get_leading_key_minimum(table, key_columns)`, `list_partitions`. Plus on the Postgres implementation: `get_partition_column` (single-column only), `get_partition_boundaries`, `is_partition_closed`, `evaluate_sql_predicate`.
5456

55-
Hooks: `before_create(config, partition: PartitionInfo)` (was `(config, name, from_value, to_value)`); the other five unchanged. Hooks fire for partitions directly under the root only (lifecycle units / root set members), never per leaf of a subtree.
57+
Hooks: in 1.0 `before_create(config, partition: PartitionInfo)` (was `(config, name, from_value, to_value)`) and the other five unchanged. **Superseded in 1.1**: every method takes one `PartitionEvent`, and there are eight phases plus `on_event` — see the changelog. Hooks fire for partitions directly under the root only (lifecycle units / root set members), never per leaf of a subtree.
5658

5759
Semantics to test: attach-last subtree creation; a converged tree costs zero DDL; ownership by alignment (an attached partition whose bounds are not on the scheme's grid is `UNMANAGED_PARTITION` INFO and never touched; a finer-than-grid one is managed); orphan `COMMENT` marker line 1 unchanged, line 2 `pg-partsmith:detached-at=<iso>`; `DropAfter(grace)` keeps orphans until `detached_at + grace`, unknown instant = eligible; `DropNever` leaves detached tables alone; drops revalidate OID (`PlanStaleError` on a recreated table, recorded as an issue with `continue_on_error`, otherwise raised); detach revalidates OID and attachment; topology conflicts at apply time (`PartitionTopologyError`, e.g. DEFAULT holding rows for a hash bucket) are recorded as issues and never abort; other errors abort unless `continue_on_error`.
5860

‎docs/design/final-report.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,11 @@ the point of the architecture:
185185
- **Later, if asked for.** Explicit migration tooling for a topology change that *does* need
186186
a rewrite (hash 4 → 8 across history), kept separate from maintenance so that the safety
187187
rule — maintenance never rewrites history — stays absolute.
188+
- **Done in 1.1.** One event for every lifecycle hook. Three of the six phases used to be
189+
handed a `PartitionInfo` and three a bare name, so a drop hook could not know the period
190+
it was archiving without decoding the name itself. Every method now takes one
191+
`PartitionEvent` — phase, config, partition, window, and the planned operation with the
192+
reason and the size behind it — and `on_event` serves every phase at once, which is what
193+
an audit or metrics hook wanted all along. It breaks every hook written against 1.0;
194+
shipped in a minor deliberately, while the library has no dependants to protect, rather
195+
than carried as a wart to the next major.

0 commit comments

Comments
 (0)