API Reference¶
Auto-generated from source using mkdocstrings.
pg_partsmith¶
Top-level public API: entities, enums, exceptions, and period calculators.
Entities¶
Represents a time period for partition boundaries.
Attributes:
| Name | Type | Description |
|---|---|---|
year |
int
|
Year component. |
month |
int | None
|
Month component (1-12), optional. |
day |
int | None
|
Day component (1-31), optional. |
week |
int | None
|
ISO week number (1-53), optional. |
Source code in pg_partsmith/entities.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
__add__(offset)
¶
Add offset to period (implementation depends on granularity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
int
|
Number of periods to add. |
required |
Returns:
| Type | Description |
|---|---|
Period
|
New Period object. |
Source code in pg_partsmith/entities.py
__lt__(other)
¶
Compare periods.
Source code in pg_partsmith/entities.py
__post_init__()
¶
__str__()
¶
String representation.
Source code in pg_partsmith/entities.py
__sub__(offset)
¶
to_date(day=1)
¶
Convert period to date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
day
|
int
|
Day of month (default: 1). Ignored for weekly periods and when self.day is already set. |
1
|
Returns:
| Type | Description |
|---|---|
date
|
Date object representing this period. For weekly periods returns the |
date
|
Monday of the ISO week. |
Source code in pg_partsmith/entities.py
Bases: BaseModel
Metadata about a partition.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
StrippedNonEmptyStr
|
Partition table name. |
partition_type |
PartitionType
|
Type of partition (RANGE, LIST, HASH). |
from_value |
str | None
|
Start boundary value (for RANGE). |
to_value |
str | None
|
End boundary value (for RANGE). |
boundaries_expr |
str | None
|
Raw boundary expression as reported by PostgreSQL
( |
is_attached |
bool
|
Whether partition is currently attached to parent table. |
is_default |
bool
|
Whether this is the DEFAULT partition (no explicit boundaries). |
parent_table |
StrippedNonEmptyStr | None
|
Name of parent partitioned table. |
Source code in pg_partsmith/entities.py
validate_range_boundaries()
¶
Validate that attached RANGE partitions have boundaries.
Detached (orphaned) partitions may have lost their boundary metadata
from the catalog and are allowed to carry None boundaries.
For attached partitions we accept either parsed boundaries
(from_value + to_value) OR a raw boundaries expression so that
callers can still reason about partitions even when expression parsing
fails.
Source code in pg_partsmith/entities.py
Bases: BaseModel
Configuration for table partitioning maintenance.
Only TIME_BASED (RANGE by date/time) partitioning is currently supported. VALUE_BASED and HASH_BASED strategies are reserved for future use and will raise a ValueError at construction time.
Attributes:
| Name | Type | Description |
|---|---|---|
schema |
Optional schema name for the partitioned table. When set, all DDL and catalogue queries are schema-qualified, making behaviour deterministic in databases with multiple schemas. |
|
table_name |
StrippedNonEmptyStr
|
Name of the partitioned table (lowercase, max 63 chars minus the longest generated partition suffix). |
partition_type |
PartitionType
|
Type of partitioning (RANGE, LIST, HASH). |
partition_strategy |
PartitionStrategy
|
Strategy for partitioning. |
partition_column |
StrippedNonEmptyStr
|
Column used for partitioning. |
granularity |
PartitionGranularity | None
|
Time granularity (for TIME_BASED strategy). |
create_ahead_count |
PositiveInt
|
Number of periods to ensure exist, including the current period. |
retention_count |
PositiveInt
|
Number of partitions to retain. |
auto_attach_after_create |
bool
|
Whether to attach immediately after creation. |
Source code in pg_partsmith/entities.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
db_schema
property
¶
PostgreSQL schema name.
validate_identifier(v)
classmethod
¶
Validate and normalise SQL identifiers.
Source code in pg_partsmith/entities.py
validate_schema(v)
classmethod
¶
validate_strategy_requirements()
¶
Validate strategy-specific requirements.
Source code in pg_partsmith/entities.py
Bases: BaseModel
Result of partition maintenance operation.
Attributes:
| Name | Type | Description |
|---|---|---|
created_count |
NonNegativeInt
|
Number of partitions created. |
detached_count |
NonNegativeInt
|
Number of partitions detached in this run. |
dropped_count |
NonNegativeInt
|
Number of partitions dropped. |
duration_ms |
NonNegativeInt
|
Duration of maintenance in milliseconds. |
error |
str | None
|
Fatal error message (set when the whole maintenance run fails). |
Source code in pg_partsmith/entities.py
success
property
¶
True only when there is no fatal error.
Exceptions¶
Domain exceptions for partition management.
PartitionError
¶
PartitionAlreadyExistsError
¶
Bases: PartitionError
Raised when attempting to create a partition that already exists.
Source code in pg_partsmith/exceptions.py
PartitionNotFoundError
¶
Bases: PartitionError
Raised when a partition is not found.
Source code in pg_partsmith/exceptions.py
PartitionAttachedError
¶
Bases: PartitionError
Raised when attempting to drop an attached partition.
Source code in pg_partsmith/exceptions.py
PartitionDetachInProgressError
¶
Bases: PartitionError
Raised when detach operation is in progress.
Source code in pg_partsmith/exceptions.py
InvalidPartitionConfigError
¶
Bases: PartitionError
Raised when partition configuration is invalid.
Source code in pg_partsmith/exceptions.py
LockAcquisitionError
¶
Bases: PartitionError
Raised when unable to acquire lock for partition operation.
Source code in pg_partsmith/exceptions.py
DropRetryExhaustedError
¶
Bases: PartitionError
Raised when all drop_partition retry attempts are exhausted.
This means PostgreSQL returned a retryable error (deadlock, lock timeout,
or query cancellation) on every attempt. Inspect cause for the last
underlying error.
Source code in pg_partsmith/exceptions.py
UnmanagedPartitionDropError
¶
Bases: PartitionError
Raised when attempting to drop a table not managed by this library.
Source code in pg_partsmith/exceptions.py
Period strategies¶
Bases: ABC
Base class for all period calculators.
Implements common logic for period calculations and defines the interface for granularity-specific strategies. Subclass and override any method to customise behaviour.
Subclasses must define _NAME_PATTERN (a compiled regex) and implement
_period_from_match to construct a Period from regex groups.
Group 1 is conventionally the table name; subsequent groups encode the period.
Source code in pg_partsmith/strategies/base.py
current_period()
abstractmethod
¶
format_partition_name(table_name, period)
abstractmethod
¶
get_boundaries(period)
abstractmethod
¶
next_periods(count)
¶
Generate N periods starting from the current period (inclusive).
Source code in pg_partsmith/strategies/base.py
parse_partition_name(partition_name)
¶
Parse period from a partition name.
Returns None if the name does not match _NAME_PATTERN or encodes
an invalid calendar value (e.g. month 13).
Source code in pg_partsmith/strategies/base.py
period_after(reference, offset)
¶
Return the period offset steps after reference.
period_before(reference, offset)
¶
Return the period offset steps before reference.
Bases: BasePeriodCalculator
Calculator for daily partitions.
Generates partitions with day granularity.
Partition naming: {table}__{YYYY}_{MM}_{DD}
Source code in pg_partsmith/strategies/day.py
current_period()
¶
format_partition_name(table_name, period)
¶
Format partition name: table__YYYY_MM_DD.
Source code in pg_partsmith/strategies/day.py
get_boundaries(period)
¶
Get day boundaries as (start_date, end_date) in ISO format.
Source code in pg_partsmith/strategies/day.py
next_periods(count)
¶
Generate N periods starting from the current period (inclusive).
Source code in pg_partsmith/strategies/base.py
parse_partition_name(partition_name)
¶
Parse period from a partition name.
Returns None if the name does not match _NAME_PATTERN or encodes
an invalid calendar value (e.g. month 13).
Source code in pg_partsmith/strategies/base.py
period_after(reference, offset)
¶
Return the period offset steps after reference.
period_before(reference, offset)
¶
Return the period offset steps before reference.
Bases: BasePeriodCalculator
Calculator for weekly partitions.
Generates partitions with ISO-week granularity.
Partition naming: {table}__{YYYY}_w{WW}
Source code in pg_partsmith/strategies/week.py
current_period()
¶
format_partition_name(table_name, period)
¶
Format partition name: table__YYYY_wWW.
Source code in pg_partsmith/strategies/week.py
get_boundaries(period)
¶
Get ISO-week boundaries (Monday to Monday) as ISO date strings.
Source code in pg_partsmith/strategies/week.py
next_periods(count)
¶
Generate N periods starting from the current period (inclusive).
Source code in pg_partsmith/strategies/base.py
parse_partition_name(partition_name)
¶
Parse period from a partition name.
Returns None if the name does not match _NAME_PATTERN or encodes
an invalid calendar value (e.g. month 13).
Source code in pg_partsmith/strategies/base.py
period_after(reference, offset)
¶
Return the period offset steps after reference.
period_before(reference, offset)
¶
Return the period offset steps before reference.
Bases: BasePeriodCalculator
Calculator for monthly partitions.
Generates partitions with month granularity.
Partition naming: {table}__{YYYY}_{MM}
Source code in pg_partsmith/strategies/month.py
current_period()
¶
format_partition_name(table_name, period)
¶
Format partition name: table__YYYY_MM.
Source code in pg_partsmith/strategies/month.py
get_boundaries(period)
¶
Get month boundaries as (start_date, end_date) in ISO format.
Source code in pg_partsmith/strategies/month.py
next_periods(count)
¶
Generate N periods starting from the current period (inclusive).
Source code in pg_partsmith/strategies/base.py
parse_partition_name(partition_name)
¶
Parse period from a partition name.
Returns None if the name does not match _NAME_PATTERN or encodes
an invalid calendar value (e.g. month 13).
Source code in pg_partsmith/strategies/base.py
period_after(reference, offset)
¶
Return the period offset steps after reference.
period_before(reference, offset)
¶
Return the period offset steps before reference.
Bases: BasePeriodCalculator
Calculator for yearly partitions.
Generates partitions with year granularity.
Partition naming: {table}__{YYYY}
Source code in pg_partsmith/strategies/year.py
current_period()
¶
format_partition_name(table_name, period)
¶
get_boundaries(period)
¶
Get year boundaries as (start_date, end_date) in ISO format.
Source code in pg_partsmith/strategies/year.py
next_periods(count)
¶
Generate N periods starting from the current period (inclusive).
Source code in pg_partsmith/strategies/base.py
parse_partition_name(partition_name)
¶
Parse period from a partition name.
Returns None if the name does not match _NAME_PATTERN or encodes
an invalid calendar value (e.g. month 13).
Source code in pg_partsmith/strategies/base.py
period_after(reference, offset)
¶
Return the period offset steps after reference.
period_before(reference, offset)
¶
Return the period offset steps before reference.
pg_partsmith.aio¶
Async implementations: service, maintainer, repositories, lock managers, and hooks.
Service and maintainer¶
Service for managing the full partition lifecycle.
Orchestrates partition creation, detachment, and deletion by delegating to specialized component services.
Source code in pg_partsmith/aio/service.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
__init__(repo, metadata, locks, period_calculator, hooks=None)
¶
Initialize the partition lifecycle service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo
|
PartitionRepository
|
DDL operations on partitions (create / attach / detach / drop). |
required |
metadata
|
PartitionMetadataProvider
|
Read-only access to PostgreSQL catalog data. |
required |
locks
|
LockManager
|
Distributed lock manager preventing concurrent maintenance runs. |
required |
period_calculator
|
PeriodCalculator[Period]
|
Strategy for determining partition names and boundaries. |
required |
hooks
|
list[PartitionLifecycleHooks] | None
|
Optional list of lifecycle hooks called around each step. |
None
|
Source code in pg_partsmith/aio/service.py
create_future_partitions(config)
async
¶
Create partitions for future periods.
Ensures partitions exist for the next config.create_ahead_count periods
starting from the current period (inclusive). Idempotent: existing partitions
are skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TablePartitionConfig
|
Table partitioning configuration. |
required |
Returns:
| Type | Description |
|---|---|
list[PartitionInfo]
|
List of newly created partitions (empty if all already existed). |
Raises:
| Type | Description |
|---|---|
PartitionAlreadyExistsError
|
If a partition exists with conflicting boundaries. |
InvalidPartitionConfigError
|
If |
Source code in pg_partsmith/aio/service.py
detach_old_partitions(table_name, partitions)
async
¶
Detach attached partitions from their parent table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Qualified parent table name. |
required |
partitions
|
list[PartitionInfo]
|
Attached partitions to detach. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Names of successfully detached partitions. |
Raises:
| Type | Description |
|---|---|
PartitionDetachInProgressError
|
If a concurrent detach is in progress. |
Source code in pg_partsmith/aio/service.py
drop_detached_partitions(table_name, partition_names)
async
¶
Drop previously detached, marker-tagged partitions.
Attached partitions are skipped with a warning (they raise
PartitionAttachedError internally). Unmanaged tables are refused
unless the underlying repository is configured otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Qualified parent table name (used for hook context). |
required |
partition_names
|
list[str]
|
Names of partitions to drop. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of partitions actually dropped. |
Source code in pg_partsmith/aio/service.py
get_partitions_for_pruning(config)
async
¶
Return partitions older than config.retention_count periods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TablePartitionConfig
|
Table partitioning configuration. |
required |
Returns:
| Type | Description |
|---|---|
list[PartitionInfo]
|
Partitions that are eligible for detach + drop, sorted oldest first. |
Source code in pg_partsmith/aio/service.py
maintain_lifecycle(config, *, skip_create=False, skip_detach=False, skip_drop=False)
async
¶
Run create + detach + drop in a single locked maintenance window.
The whole sequence runs under a single distributed lock acquired through
the configured :class:LockManager, so concurrent maintainers do not
race on the same parent table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TablePartitionConfig
|
Table partitioning configuration. |
required |
skip_create
|
bool
|
Skip the create-ahead step. |
False
|
skip_detach
|
bool
|
Skip detaching old partitions (orphans are still dropped). |
False
|
skip_drop
|
bool
|
Skip dropping detached partitions. |
False
|
Returns:
| Type | Description |
|---|---|
MaintenanceResult
|
|
MaintenanceResult
|
because exceptions propagate from this method (the maintainer is |
MaintenanceResult
|
responsible for catching them). |
Raises:
| Type | Description |
|---|---|
LockAcquisitionError
|
If the table-level maintenance lock is unavailable. |
InvalidPartitionConfigError
|
If |
Source code in pg_partsmith/aio/service.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
Orchestrator for partition lifecycle maintenance.
Wraps a lifecycle service with timing, logging, and error handling.
Operational failures are logged and re-raised by run_maintenance.
Use run_maintenance_safe (or the maintain_partitions helper) when
you need a scheduler-friendly API that always returns MaintenanceResult.
Source code in pg_partsmith/aio/maintainer.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
run_maintenance(config, *, skip_create=False, skip_detach=False, skip_drop=False)
async
¶
Execute full partition lifecycle maintenance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TablePartitionConfig
|
Table partition configuration. |
required |
skip_create
|
bool
|
Skip creating future partitions. |
False
|
skip_detach
|
bool
|
Skip detaching old partitions. |
False
|
skip_drop
|
bool
|
Skip dropping detached partitions. |
False
|
Returns:
| Type | Description |
|---|---|
MaintenanceResult
|
Maintenance result with counts and duration. |
Raises:
| Type | Description |
|---|---|
CancelledError
|
Propagated after being logged. |
Exception
|
Propagated after being logged. |
Source code in pg_partsmith/aio/maintainer.py
run_maintenance_safe(config, *, skip_create=False, skip_detach=False, skip_drop=False)
async
¶
Run maintenance and always return MaintenanceResult, never raise.
Scheduler-friendly wrapper around :meth:run_maintenance. Any exception
— including asyncio.CancelledError — is captured and reported via
result.error; the duration_ms field always reflects the elapsed
time even on failure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TablePartitionConfig
|
Table partitioning configuration. |
required |
skip_create
|
bool
|
Skip creating future partitions. |
False
|
skip_detach
|
bool
|
Skip detaching old partitions. |
False
|
skip_drop
|
bool
|
Skip dropping detached partitions. |
False
|
Returns:
| Type | Description |
|---|---|
MaintenanceResult
|
|
Source code in pg_partsmith/aio/maintainer.py
PostgreSQL implementations¶
PostgreSQL implementation of partition repository.
Facade that delegates to specialized helper classes for improved maintenance and SRP.
Source code in pg_partsmith/aio/repositories/repository.py
Provider for PostgreSQL partition metadata.
Queries pg_catalog to retrieve information about table partitioning. Override any method to customise catalog queries for your schema setup.
Each method opens its own read-only connection from the engine pool so it is safe to call outside any existing transaction.
Source code in pg_partsmith/aio/metadata.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
__init__(engine, *, marker_prefix=None)
¶
Initialize provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
AsyncEngine
|
SQLAlchemy async engine. |
required |
marker_prefix
|
str | None
|
Optional COMMENT marker prefix for orphaned partitions. When None, the library default prefix is used. Pass the same value to both repository and metadata provider if you override it. |
None
|
Source code in pg_partsmith/aio/metadata.py
get_default_partition(table_name)
async
¶
Get DEFAULT partition for a table if it exists and is attached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Parent table name. |
required |
Returns:
| Type | Description |
|---|---|
PartitionInfo | None
|
PartitionInfo with is_default=True, or None if no default partition exists. |
Source code in pg_partsmith/aio/metadata.py
get_partition_boundaries(partition_name)
async
¶
Get partition boundaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partition_name
|
str
|
Partition table name. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str] | None
|
Tuple of (from_value, to_value) or None if not a range partition. |
Source code in pg_partsmith/aio/metadata.py
get_partition_column(table_name)
async
¶
Get partition column for a table.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the table uses a composite (multi-column) partition key. Only single-column keys are supported by this library. |
Source code in pg_partsmith/aio/metadata.py
get_partition_type(table_name)
async
¶
Get partition type for a table.
Source code in pg_partsmith/aio/metadata.py
is_partition_attached(table_name, partition_name)
async
¶
Check if a partition is currently attached to its parent via pg_inherits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Parent table name. |
required |
partition_name
|
str
|
Partition table name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the partition is attached. |
Source code in pg_partsmith/aio/metadata.py
list_partitions(table_name)
async
¶
List all partitions for a table, including orphaned detached ones.
Orphaned partitions are detached-but-not-dropped tables previously
detached by this library. They are detected by a COMMENT marker set on
successful detach and returned with is_attached=False and None
boundaries.
Source code in pg_partsmith/aio/metadata.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
partition_exists(partition_name)
async
¶
Check if a partition table exists in pg_class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partition_name
|
str
|
Partition table name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the table exists as a regular table. |
Source code in pg_partsmith/aio/metadata.py
Lock managers¶
Lock manager using PostgreSQL advisory locks.
Holds the advisory lock on a dedicated AUTOCOMMIT connection from the engine pool. This guarantees the lock survives any number of commits or rollbacks on the caller's session, which is required when the caller needs to commit DDL (e.g. ATTACH PARTITION) before running DETACH PARTITION CONCURRENTLY.
Override _compute_lock_id to customise the lock ID derivation.
Source code in pg_partsmith/aio/lock/postgres.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
__init__(engine, prefix='partitioner', acquire_min_interval_seconds=0.0)
¶
Initialize lock manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
AsyncEngine
|
SQLAlchemy async engine used to open a dedicated connection for the advisory lock. |
required |
prefix
|
str
|
Prefix for lock key generation. |
'partitioner'
|
acquire_min_interval_seconds
|
float
|
Minimum seconds between acquire attempts per table (rate limiting). 0 disables. |
0.0
|
Source code in pg_partsmith/aio/lock/postgres.py
acquire_lock(table_name)
¶
Acquire advisory lock for a table.
Opens a dedicated AUTOCOMMIT connection from the engine pool and acquires a session-level advisory lock on it. The lock is released when the context manager exits, with cancellation-safe cleanup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Table name to lock. |
required |
Returns:
| Type | Description |
|---|---|
AbstractAsyncContextManager[None]
|
Async context manager for the lock. |
Raises:
| Type | Description |
|---|---|
LockAcquisitionError
|
If the lock cannot be acquired. |
Source code in pg_partsmith/aio/lock/postgres.py
is_locked(table_name)
async
¶
Check if lock is held by any session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Table name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the advisory lock for the given table is currently held. |
Source code in pg_partsmith/aio/lock/postgres.py
Lock manager using Redis for distributed coordination.
Uses SET NX EX to acquire the lock and a background renewal task to
extend the TTL while the lock is held, preventing expiry during long DDL
operations (e.g. DETACH PARTITION CONCURRENTLY). The lock is released
atomically via a Lua script that checks the ownership token, so it is safe
even if Redis restarts during the renewal window.
The renewal interval is ttl_seconds // 3 (with random jitter to avoid
thundering herds). If renewal fails — e.g. Redis is unreachable or another
holder takes over — the watchdog logs a warning and cancels the holder
task, forcing the maintenance run to stop (fail-safe).
For production use you may want to subclass and override acquire_lock
to use Redlock or another algorithm with stronger guarantees.
Raises:
| Type | Description |
|---|---|
ImportError
|
If the |
Source code in pg_partsmith/aio/lock/redis.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
__init__(redis_client, prefix='partitioner:lock', ttl_seconds=300, acquire_min_interval_seconds=0.0)
¶
Initialize lock manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
redis_client
|
RedisClientProtocol
|
Redis client instance. |
required |
prefix
|
str
|
Prefix for Redis keys. |
'partitioner:lock'
|
ttl_seconds
|
int
|
Lock time-to-live in seconds. The lock is automatically
renewed every |
300
|
acquire_min_interval_seconds
|
float
|
Minimum seconds between acquire attempts per table (rate limiting). 0 disables. |
0.0
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
ValueError
|
If |
Source code in pg_partsmith/aio/lock/redis.py
acquire_lock(table_name)
¶
Acquire Redis lock with automatic TTL renewal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Table name. |
required |
Returns:
| Type | Description |
|---|---|
AbstractAsyncContextManager[None]
|
Async context manager for the lock. |
Raises:
| Type | Description |
|---|---|
LockAcquisitionError
|
If the lock is already held. |
Source code in pg_partsmith/aio/lock/redis.py
is_locked(table_name)
async
¶
Return True if the Redis lock for table_name is currently held.
Hooks¶
No-op base implementation of partition lifecycle hooks.
Subclass and override only the methods you need. All methods are no-ops by default so you can selectively add behaviour without implementing every step.
Source code in pg_partsmith/aio/hooks.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
after_create(config, partition)
async
¶
Called after a partition has been created (and optionally attached).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TablePartitionConfig
|
Table partition configuration. |
required |
partition
|
PartitionInfo
|
Info about the newly created partition. |
required |
Source code in pg_partsmith/aio/hooks.py
after_detach(table_name, partition_name)
async
¶
after_drop(table_name, partition_name)
async
¶
before_create(config, partition_name, from_value, to_value)
async
¶
Called before a partition is created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TablePartitionConfig
|
Table partition configuration. |
required |
partition_name
|
str
|
Name the new partition will be given. |
required |
from_value
|
str
|
Start boundary value. |
required |
to_value
|
str
|
End boundary value. |
required |
Source code in pg_partsmith/aio/hooks.py
before_detach(table_name, partition)
async
¶
Called before a partition is detached from its parent table.
This is a good place to export or archive data while the partition is still accessible via the parent table's indexes and constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Parent table name. |
required |
partition
|
PartitionInfo
|
Info about the partition being detached. |
required |
Source code in pg_partsmith/aio/hooks.py
before_drop(table_name, partition_name)
async
¶
Called before a partition table is dropped.
This is the last chance to read or export data from the partition before it is permanently destroyed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Parent table name. |
required |
partition_name
|
str
|
Name of the partition about to be dropped. |
required |