PostgreSQL sessions (Effect's @effect/sql-pg PgConnection) on
psycopg's asyncio API, driven on a loop thread of its own (_loop), so
they work under run_blocking and run_async alike and no fiber blocks.
A session runs one statement at a time; fibers sharing it wait in turn.
pin reserves it for one scope (transactions, streams and listeners
pin), and statements through the pinned connection skip the line.
Interrupting a statement sends PostgreSQL a cancel request and waits for
the statement to end. Unless PostgreSQL confirms the cancel (57014),
a pool replaces the session before its next use, since a late cancel
could hit the next statement; a statement still running five seconds
after the cancel closes the session.
Re-exports#
- From
pyfect.sql.error:SqlError - From
pyfect.sql.pg:Field,Notification,Result
Values#
| Attribute | Type | Description |
|---|---|---|
DEFAULT_LISTEN_CAPACITY | Literal[1024] | How many notifications a |
Types#
| Type | Definition | Description |
|---|---|---|
Row | type Row = dict[str, Any] |
Classes#
Functions#
make#
make(config) -> Effect[PgConnection, SqlError, Scope]Connect and authenticate one session; it closes with the scope.
Resolving the password, connecting and authenticating are bounded by
connect_timeout.
| Parameter | Type | Description |
|---|---|---|
config | PgConnectionConfig |
Returns Effect[PgConnection, SqlError, Scope]
def make(config: PgConnectionConfig) -> Effect[PgConnection, SqlError, Scope]:
"""Connect and authenticate one session; it closes with the scope.
Resolving the password, connecting and authenticating are bounded by
``connect_timeout``."""
def opened(resolved: ResolvedConfig) -> Effect[PgConnection, SqlError, Scope]:
def body(restore: Restore) -> Effect[PgConnection, SqlError, Scope]:
connected: Effect[PgConnection, SqlError] = restore(_open(config, resolved))
return connected.flat_map(
lambda conn: add_finalizer(lambda _: conn._shared.close()).as_(conn)
)
def timed_out() -> Effect[Never, SqlError]:
return fail(connection_error("PgConnection: Connection timed out", "connect"))
return uninterruptible_mask(body).timeout_or_else(resolved.connect_timeout, timed_out)
return resolve(config).flat_map(opened)