nl2data-workflow-postgres 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,25 @@
1
+ """PostgreSQL-backed durable workflow state backend for nl2data-core.
2
+
3
+ The durable workflow runtime composes this backend through the replaceable
4
+ store contract. Optional dependencies (``psycopg``/``psycopg_pool``) are
5
+ loaded lazily; importing this module never imports the driver.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from .client import build_pool, driver_available
11
+ from .config import WorkflowPostgresConfig
12
+ from .schema import MIGRATIONS, SUPPORTED_SCHEMA_VERSION
13
+ from .store import SQL_TEMPLATES, PostgreSQLStateStore
14
+
15
+ __version__ = "0.1.0"
16
+
17
+ __all__ = [
18
+ "MIGRATIONS",
19
+ "SUPPORTED_SCHEMA_VERSION",
20
+ "SQL_TEMPLATES",
21
+ "PostgreSQLStateStore",
22
+ "WorkflowPostgresConfig",
23
+ "build_pool",
24
+ "driver_available",
25
+ ]
@@ -0,0 +1,138 @@
1
+ """Lazy optional psycopg driver boundary for the PostgreSQL workflow backend.
2
+
3
+ The ``psycopg``/``psycopg_pool`` packages are loaded only inside this module
4
+ through :func:`importlib.import_module`, so importing
5
+ ``nl2data_workflow_postgres`` or the store module never imports a database
6
+ driver. The store accepts an injected pool (fake or host-managed) or a DSN;
7
+ the DSN path is constructed here with bounded connect/command timeouts and a
8
+ bounded pool, and DSNs are never included in errors. Driver errors are
9
+ classified by class name first (so injected fake clients work without the
10
+ driver installed) and by the real driver only when present.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from importlib import import_module
16
+ from importlib.util import find_spec
17
+ from typing import Any, cast
18
+
19
+ from nl2data_core.workflow.shared_errors import SharedStoreError, SharedStoreErrorCode
20
+
21
+
22
+ def driver_available() -> bool:
23
+ """Whether the optional ``psycopg`` driver (and pool) is installed."""
24
+ return find_spec("psycopg") is not None and find_spec("psycopg_pool") is not None
25
+
26
+
27
+ def build_pool(
28
+ dsn: str,
29
+ *,
30
+ pool_size: int,
31
+ connect_timeout_seconds: float,
32
+ command_timeout_seconds: float,
33
+ acquire_timeout_seconds: float,
34
+ schema: str,
35
+ ) -> Any:
36
+ """Lazily import the driver and build a bounded PostgreSQL connection pool.
37
+
38
+ Every checkout is pinned to the configured schema namespace through the
39
+ connection ``options`` so all unqualified table names resolve inside the
40
+ deployment's own schema. Raises a normalized ``STORE_UNAVAILABLE``
41
+ error when the driver is missing or the pool cannot be constructed; the
42
+ DSN and any driver exception text are never included in the error.
43
+ """
44
+ if not driver_available():
45
+ raise SharedStoreError(
46
+ SharedStoreErrorCode.STORE_UNAVAILABLE,
47
+ "the psycopg driver is not installed; install the 'postgres' extra",
48
+ details={"cause_type": "ImportError"},
49
+ )
50
+ try:
51
+ pool_module = cast(Any, import_module("psycopg_pool"))
52
+ driver = cast(Any, import_module("psycopg"))
53
+ pool = pool_module.ConnectionPool(
54
+ dsn,
55
+ min_size=1,
56
+ max_size=pool_size,
57
+ open=True,
58
+ timeout=acquire_timeout_seconds,
59
+ kwargs={
60
+ "connect_timeout": connect_timeout_seconds,
61
+ "options": f"-c search_path={schema}",
62
+ "row_factory": driver.rows.dict_row,
63
+ },
64
+ )
65
+ except SharedStoreError:
66
+ raise
67
+ except Exception as error:
68
+ raise SharedStoreError(
69
+ SharedStoreErrorCode.STORE_UNAVAILABLE,
70
+ "the PostgreSQL connection pool could not be constructed",
71
+ details={"cause_type": type(error).__name__},
72
+ ) from error
73
+ return pool
74
+
75
+
76
+ def is_connect_error(error: BaseException) -> bool:
77
+ """Whether the exception signals a lost or unavailable connection."""
78
+ if error.__class__.__name__ in {
79
+ "OperationalError",
80
+ "InterfaceError",
81
+ "ConnectionError",
82
+ "PoolTimeout",
83
+ "PoolClosed",
84
+ }:
85
+ return True
86
+ try:
87
+ pool_module = cast(Any, import_module("psycopg_pool"))
88
+ if isinstance(error, pool_module.PoolTimeout):
89
+ return True
90
+ except ImportError:
91
+ pass
92
+ try:
93
+ exceptions = cast(Any, import_module("psycopg"))
94
+ except ImportError:
95
+ return False
96
+ return isinstance(
97
+ error, (exceptions.OperationalError, exceptions.InterfaceError)
98
+ )
99
+
100
+
101
+ def is_timeout_error(error: BaseException) -> bool:
102
+ """Whether the exception signals a query canceled by its timeout."""
103
+ if error.__class__.__name__ in {
104
+ "TimeoutError",
105
+ "QueryCanceledError",
106
+ "QueryCanceled",
107
+ "Timeout",
108
+ }:
109
+ return True
110
+ try:
111
+ exceptions = cast(Any, import_module("psycopg.errors"))
112
+ except ImportError:
113
+ return False
114
+ return isinstance(error, exceptions.QueryCanceled)
115
+
116
+
117
+ def is_duplicate_key_error(error: BaseException) -> bool:
118
+ """Whether the exception signals a unique-key violation."""
119
+ if error.__class__.__name__ in {"UniqueViolation", "IntegrityError"}:
120
+ return True
121
+ try:
122
+ exceptions = cast(Any, import_module("psycopg.errors"))
123
+ except ImportError:
124
+ return False
125
+ return isinstance(error, exceptions.UniqueViolation)
126
+
127
+
128
+ def is_serialization_error(error: BaseException) -> bool:
129
+ """Whether the exception signals a retryable transaction conflict."""
130
+ if error.__class__.__name__ in {"SerializationFailure", "DeadlockDetected"}:
131
+ return True
132
+ try:
133
+ exceptions = cast(Any, import_module("psycopg.errors"))
134
+ except ImportError:
135
+ return False
136
+ return isinstance(
137
+ error, (exceptions.SerializationFailure, exceptions.DeadlockDetected)
138
+ )
@@ -0,0 +1,83 @@
1
+ """Bounded configuration for the PostgreSQL workflow state backend.
2
+
3
+ The configuration carries only behavior bounds - never connection strings
4
+ or credentials. A DSN (when the host manages its own pool) is passed to the
5
+ store constructor and is never part of the model, so a dumped configuration
6
+ cannot leak PostgreSQL endpoints or secrets. Every bound is validated at
7
+ construction so an empty schema namespace, an unsafe pool/timeout/TTL value,
8
+ or an impossible renewal margin fails before any client is built.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ from pydantic import BaseModel, ConfigDict, Field, model_validator
14
+
15
+ from .schema import SUPPORTED_SCHEMA_VERSION
16
+
17
+ #: Schema namespace: bounded identifier so every derived table name stays safe.
18
+ _SCHEMA_PATTERN = r"^[A-Za-z][A-Za-z0-9_]{0,63}$"
19
+
20
+ #: Hard limits for the validated bounds below.
21
+ _MAX_POOL_SIZE = 64
22
+ _MAX_CONNECT_TIMEOUT_SECONDS = 30.0
23
+ _MAX_COMMAND_TIMEOUT_SECONDS = 120.0
24
+ _MAX_ACQUIRE_TIMEOUT_SECONDS = 60.0
25
+ _MAX_CLEANUP_BATCH = 10_000
26
+ _MAX_LEASE_TTL_SECONDS = 86_400.0 # one day
27
+ _MAX_RENEWAL_MARGIN_SECONDS = 3_600.0
28
+ _MAX_CLOCK_TOLERANCE_SECONDS = 60.0
29
+
30
+
31
+ class WorkflowPostgresConfig(BaseModel):
32
+ """Immutable bounded configuration for the PostgreSQL workflow backend.
33
+
34
+ ``namespace`` is required and must be unique per application and
35
+ environment: it names the PostgreSQL schema that owns every workflow,
36
+ idempotency, and lease table, so two deployments sharing one database
37
+ service never observe each other's records. All other fields bound the
38
+ store's work so a pathological pool, timeout, cleanup pass, or lease
39
+ timing can never produce unbounded behavior.
40
+ """
41
+
42
+ model_config = ConfigDict(frozen=True, extra="forbid")
43
+
44
+ #: PostgreSQL schema owning all shared records (never a raw tenant id).
45
+ namespace: str = Field(pattern=_SCHEMA_PATTERN)
46
+ #: Bounded connection pool size of the lazy psycopg pool.
47
+ pool_size: int = Field(default=5, ge=1, le=_MAX_POOL_SIZE)
48
+ #: Bounded connect timeout for lazy pool connections (seconds).
49
+ connect_timeout_seconds: float = Field(
50
+ default=5.0, ge=0.1, le=_MAX_CONNECT_TIMEOUT_SECONDS
51
+ )
52
+ #: Bounded per-command timeout for every statement (seconds).
53
+ command_timeout_seconds: float = Field(
54
+ default=10.0, ge=0.1, le=_MAX_COMMAND_TIMEOUT_SECONDS
55
+ )
56
+ #: Bounded pool checkout timeout (seconds).
57
+ pool_acquire_timeout_seconds: float = Field(
58
+ default=5.0, ge=0.1, le=_MAX_ACQUIRE_TIMEOUT_SECONDS
59
+ )
60
+ #: Maximum schema version this store operates on; newer schemas reject.
61
+ schema_version: int = Field(
62
+ default=SUPPORTED_SCHEMA_VERSION, ge=1, le=SUPPORTED_SCHEMA_VERSION
63
+ )
64
+ #: Maximum records removed by one bounded cleanup pass.
65
+ cleanup_batch_size: int = Field(default=500, ge=1, le=_MAX_CLEANUP_BATCH)
66
+ #: Default lease TTL applied when an acquire call omits ``ttl_seconds``.
67
+ lease_ttl_seconds: float = Field(default=120.0, ge=1.0, le=_MAX_LEASE_TTL_SECONDS)
68
+ #: Renewal is attempted when less than this margin remains on the lease.
69
+ lease_renewal_margin_seconds: float = Field(
70
+ default=20.0, ge=0.1, le=_MAX_RENEWAL_MARGIN_SECONDS
71
+ )
72
+ #: Conservative clock-skew tolerance applied to every server-time check.
73
+ clock_tolerance_seconds: float = Field(
74
+ default=2.0, ge=0.0, le=_MAX_CLOCK_TOLERANCE_SECONDS
75
+ )
76
+
77
+ @model_validator(mode="after")
78
+ def _consistent_lease_timing(self) -> WorkflowPostgresConfig:
79
+ if self.lease_renewal_margin_seconds >= self.lease_ttl_seconds:
80
+ raise ValueError(
81
+ "lease renewal margin must be smaller than the lease TTL"
82
+ )
83
+ return self