@@ -15,6 +15,10 @@ A single library that covers the full PostgreSQL partition lifecycle: creating p
1515
1616- ** Async and sync** — ` pg_partsmith.aio ` on the SQLAlchemy async engine, ` pg_partsmith.sync ` on the classic sync engine
1717- ** Full lifecycle** — create ahead, detach expired, drop orphans in one call
18+ - ** Nested partitioning** — ` RANGE(time) → HASH(column) ` and ` → LIST(column) ` trees, reconciled towards the configured shape on every run
19+ - ** Time-free tables** — HASH or LIST roots with a fixed partition set, managed by the same reconciler
20+ - ** Composite keys** — multi-column partition keys, with trailing columns bounded by ` MINVALUE `
21+ - ** Encoded partition keys** — partition by time even when the key is a UUIDv7 or another sortable id
1822- ** Extensible hooks** — 6 hook points (` before ` /` after ` create, detach, drop)
1923- ** Multiple strategies** — hourly, daily, weekly, monthly, quarterly, yearly + fully custom
2024- ** Distributed locking** — PostgreSQL advisory locks (built-in) or Redis
@@ -89,6 +93,8 @@ async def run_maintenance(engine: AsyncEngine) -> None:
8993
9094> ** Transaction semantics** — every DDL operation (CREATE, ATTACH, DETACH, DROP)
9195> runs in its own connection and commits immediately. Use ` AsyncEngine ` , not ` AsyncSession ` .
96+ > A subpartitioned branch is built detached and attached last, so an interrupted run leaves
97+ > an unreachable table rather than a live partition that cannot route part of its keyspace.
9298
9399> ** Timezones** — everything is UTC by default. For calendar partitions in a business
94100> timezone pass the same zone to both sides: ` MonthPeriodCalculator(tz=ZoneInfo("Europe/Moscow")) `
@@ -136,6 +142,94 @@ Hooks and custom lock managers implement the sync protocols from `pg_partsmith.s
136142- The Redis lock renews its TTL from a background thread; on renewal failure it logs a
137143 warning but cannot cancel the running maintenance (the TTL bounds a stale holder).
138144
145+ ## Nested partitioning
146+
147+ A time partition can itself be a partitioned table. Add a ` subpartition ` spec and each
148+ period is split along a second dimension:
149+
150+ ``` python
151+ from pg_partsmith import HashSubpartitionSpec
152+
153+ config = TablePartitionConfig(
154+ schema = " public" ,
155+ table_name = " events" ,
156+ partition_type = PartitionType.RANGE ,
157+ partition_strategy = PartitionStrategy.TIME_BASED ,
158+ partition_column = " created_at" ,
159+ granularity = PartitionGranularity.WEEK ,
160+ create_ahead_count = 3 ,
161+ retention_count = 12 ,
162+ subpartition = HashSubpartitionSpec(column = " tenant_id" , modulus = 4 ),
163+ )
164+ ```
165+
166+ ``` text
167+ events
168+ ├── events__2026_w35 PARTITION BY HASH (tenant_id)
169+ │ ├── events__2026_w35__h0
170+ │ ├── events__2026_w35__h1
171+ │ ├── events__2026_w35__h2
172+ │ └── events__2026_w35__h3
173+ └── events__2026_w36 …
174+ ```
175+
176+ The two dimensions stay separate: ** time is the lifecycle dimension** (create-ahead,
177+ retention, detach, drop, hooks all operate on the whole week) and the nested one is the
178+ distribution dimension. ` retention_count=12 ` keeps twelve weeks, not twelve leaves.
179+
180+ ` ListSubpartitionSpec ` splits a period into named value sets instead
181+ (` RANGE(time) → LIST(region) ` ), with an optional DEFAULT catch-all; the strategies also
182+ nest into each other.
183+
184+ Maintenance converges the actual tree towards the configured one on every run: missing
185+ buckets are created, a converged tree costs ** zero DDL** , and anything that cannot be
186+ repaired safely — a bucket set at an older modulus, a partition from a previous policy, a
187+ shape the config does not describe — is left intact and reported through
188+ ` MaintenanceResult.issues ` . See the
189+ [ subpartitioning guide] ( https://bedrock-python.github.io/pg-partsmith/guide/subpartitioning/ ) .
190+
191+ ## Tables partitioned without a time dimension
192+
193+ A table divided only by tenant or region has a fixed set of partitions — nothing
194+ is created ahead and nothing ages out. Configure it with ` root_layout ` :
195+
196+ ``` python
197+ config = TablePartitionConfig(
198+ table_name = " issue_index" ,
199+ partition_type = PartitionType.HASH ,
200+ partition_strategy = PartitionStrategy.HASH_BASED ,
201+ partition_column = " organization_id" ,
202+ root_layout = HashSubpartitionSpec(column = " organization_id" , modulus = 16 ),
203+ )
204+ ```
205+
206+ Such a table needs no period calculator, and maintenance is purely
207+ reconciliation: missing partitions are created, nothing is ever pruned.
208+
209+ ## Partitioning by an encoded key
210+
211+ When the partition key is a time-sortable identifier rather than a timestamp — a UUIDv7, a
212+ ULID, an epoch bigint — a boundary codec keeps the lifecycle reasoning in calendar periods
213+ while the DDL speaks the key's own language:
214+
215+ ``` python
216+ from pg_partsmith.boundaries import UUIDv7BoundaryCodec
217+
218+ codec = UUIDv7BoundaryCodec()
219+
220+ service = PartitionLifecycleService(
221+ repo = PostgresPartitionRepository(engine),
222+ metadata = PostgresMetadataProvider(engine, boundary_codec = codec),
223+ locks = PostgresAdvisoryLockManager(engine),
224+ period_calculator = WeekPeriodCalculator(boundary_codec = codec),
225+ )
226+ ```
227+
228+ Partition names, create-ahead, and retention are unchanged; only the `FOR VALUES FROM … TO
229+ …` literals become UUIDs. Codecs are bidirectional, so retention and ` is_partition_closed`
230+ keep working. See the
231+ [ boundary codec guide] ( https://bedrock-python.github.io/pg-partsmith/guide/boundary-codecs/ ) .
232+
139233## Multi-schema databases
140234
141235If your database uses multiple schemas, set ` schema ` in ` TablePartitionConfig ` . The library
@@ -306,19 +400,25 @@ scheduler.add_job(
306400
307401** Entities** — ` Period ` , ` PartitionInfo ` , ` TablePartitionConfig ` , ` MaintenanceResult ` , ` MaintenanceIssue ` , ` MaintenanceIssueStep `
308402
403+ ** Topology** — ` HashSubpartitionSpec ` , ` ListSubpartitionSpec ` , ` ListGroup ` , ` SubpartitionSpec ` , ` PartitionNode ` , ` RangeBounds ` , ` HashBounds ` , ` ListBounds ` , ` DefaultBounds ` , ` PartitionBounds ` , ` SubpartitionBounds `
404+
405+ ** Reconciliation** — ` SubpartitionPlan ` , ` SubpartitionAction ` , ` SubpartitionReconcileResult ` , ` TopologyFinding ` , ` TopologyReason ` , ` plan_subpartitions `
406+
407+ ** Boundary codecs** — ` RangeBoundaryCodec ` , ` UUIDv7BoundaryCodec `
408+
309409** Helpers** — ` qualify ` , ` split_qualified_name `
310410
311411** Enums** — ` PartitionType ` , ` PartitionGranularity ` , ` PartitionStrategy `
312412
313- ** Exceptions** — ` PartitionError ` , ` PartitionAlreadyExistsError ` , ` PartitionNotFoundError ` , ` PartitionAttachedError ` , ` PartitionDetachInProgressError ` , ` InvalidPartitionConfigError ` , ` LockAcquisitionError ` , ` DropRetryExhaustedError ` , ` UnmanagedPartitionDropError `
413+ ** Exceptions** — ` PartitionError ` , ` PartitionAlreadyExistsError ` , ` PartitionNotFoundError ` , ` PartitionAttachedError ` , ` PartitionDetachInProgressError ` , ` InvalidPartitionConfigError ` , ` LockAcquisitionError ` , ` DropRetryExhaustedError ` , ` UnmanagedPartitionDropError ` , ` PartitionTopologyError ` , ` UnsupportedCapabilityError `
314414
315- ** Protocols** — ` PeriodCalculator ` , ` TimezoneAwareCalculator ` , ` DdlTimezoneAware `
415+ ** Protocols** — ` PeriodCalculator ` , ` TimezoneAwareCalculator ` , ` DdlTimezoneAware ` , ` BoundaryDecoder `
316416
317417** Strategies** — ` BasePeriodCalculator ` , ` HourPeriodCalculator ` , ` DayPeriodCalculator ` , ` WeekPeriodCalculator ` , ` MonthPeriodCalculator ` , ` QuarterPeriodCalculator ` , ` YearPeriodCalculator ` , ` get_period_calculator `
318418
319419### ` pg_partsmith.aio `
320420
321- ** Protocols** — ` PartitionRepository ` , ` PartitionMetadataProvider ` , ` LockManager `
421+ ** Protocols** — ` PartitionRepository ` , ` PartitionMetadataProvider ` , ` LockManager ` , ` SubpartitionRepository ` , ` NestedPartitionMetadata ` , ` CompositeKeyRepository ` , ` CompositeKeyMetadata `
322422
323423** Hooks** — ` BasePartitionLifecycleHooks `
324424
@@ -343,7 +443,13 @@ detached partitions are adopted with `repo.adopt_partition(...)` instead of disa
343443safe-drop via ` drop_allow_unmanaged ` ; writers that need one specific partition use
344444` service.ensure_partition(config, period) ` ; scheduled ticks isolate step failures with
345445` maintain_lifecycle(..., continue_on_error=True) ` → ` result.issues ` ; export pipelines
346- check ` metadata.is_partition_closed(name, settle_seconds=...) ` before finalizing.
446+ check ` metadata.is_partition_closed(name, settle_seconds=...) ` before finalizing; data that
447+ predates the create-ahead window gets partitions via
448+ ` service.ensure_partitions(config, periods) ` .
449+
450+ For a worked example of adopting a ` RANGE(UUIDv7) → HASH(tenant) ` event store — including a
451+ non-uniform bucket history and custom partition names — see
452+ [ Example: an event store with TIME → HASH] ( https://bedrock-python.github.io/pg-partsmith/guide/nested-migration/ ) .
347453
348454## Development
349455
0 commit comments