Events¶
Pub/sub event channel system with database-backed queue support. Provides both sync and async channels with listener management and native backend integration for databases that support LISTEN/NOTIFY.
Native LISTEN/NOTIFY model¶
Native PG event backends (asyncpg, psycopg async/sync, psqlpy)
hold a single persistent LISTEN connection per backend instance. Each
backend owns its own listener hub that:
Acquires the dedicated LISTEN connection lazily on first subscribe.
Emits
LISTEN <channel>exactly once per channel andUNLISTENon unsubscribe / shutdown.Dispatches incoming notifications into per-channel
asyncio.Queueinstances (orqueue.Queuefor the sync psycopg variant).Serializes subscribe / unsubscribe under a lock so concurrent callers cannot race on driver-level statements that share the connection.
The Oracle Advanced Queuing backend uses an analogous pattern: a
per-channel queue-handle cache backed by a single dedicated session per
backend instance. dequeue honors min(poll_interval, aq_wait_seconds)
as its wait bound so the caller's polling cadence is respected.
ack / nack semantics are unchanged. Native backends remain
fire-and-forget; hybrid (listen_notify_durable) backends acknowledge
through the durable table queue.
Channels¶
- class sqlspec.extensions.events.AsyncEventChannel[source]¶
Bases:
objectEvent channel for asynchronous database configurations.
- async iter_events(channel, *, poll_interval=None)[source]¶
Yield events as they become available.
- Return type:
- class sqlspec.extensions.events.SyncEventChannel[source]¶
Bases:
objectEvent channel for synchronous database configurations.
- iter_events(channel, *, poll_interval=None)[source]¶
Yield events as they become available.
- Return type:
Listeners¶
Event Queue¶
- final class sqlspec.extensions.events.AsyncTableEventQueue[source]¶
Bases:
_BaseTableEventQueueAsync table queue implementation.
- final class sqlspec.extensions.events.SyncTableEventQueue[source]¶
Bases:
_BaseTableEventQueueSync table queue implementation.
Store¶
- class sqlspec.extensions.events.BaseEventQueueStore[source]¶
-
Base class for adapter-specific event queue DDL generators.
This class provides a hook-based pattern for DDL generation. Adapters only need to override _column_types() and optionally any hook methods for dialect-specific variations:
_string_type(length): String type syntax (default: VARCHAR(N))
_integer_type(): Integer type syntax (default: INTEGER)
_timestamp_default(): Timestamp default expression (default: CURRENT_TIMESTAMP)
_primary_key_syntax(): Inline PRIMARY KEY clause (default: empty, PK on column)
_table_clause(): Additional table options (default: empty)
For complex dialects (Oracle PL/SQL, BigQuery CLUSTER BY), adapters may override _build_create_table_sql() directly.
- property settings: dict[str, TypeAliasForwardRef('typing.Any')]¶
Return extension settings for adapters to inspect.
Models¶
Protocols¶
- class sqlspec.extensions.events.AsyncEventBackendProtocol[source]¶
Bases:
ProtocolProtocol for async event backends.
All async event backends (native or queue-based) must implement these methods.
- __init__(*args, **kwargs)¶
- class sqlspec.extensions.events.SyncEventBackendProtocol[source]¶
Bases:
ProtocolProtocol for sync event backends.
All sync event backends (native or queue-based) must implement these methods.
- __init__(*args, **kwargs)¶
Payload Helpers¶
- sqlspec.extensions.events.encode_notify_payload(event_id, payload, metadata)[source]¶
Encode event data as JSON for NOTIFY payload.
- Raises:
EventChannelError -- If the encoded payload exceeds PostgreSQL's 8KB limit.
- Return type:
Utility Functions¶
- sqlspec.extensions.events.load_native_backend(config, backend_name, extension_settings, adapter_name=None)[source]¶
Load adapter-specific native backend if available.
- sqlspec.extensions.events.resolve_poll_interval(poll_interval, default)[source]¶
Resolve poll interval with validation.
- Return type: