Skip to content
// EFFECT DAYS 2026

PgConnection unstable

Native PostgreSQL sessions built on the PgProtocol wire codec.

Details

Sessions support queries, streaming, LISTEN/NOTIFY, cancellation, and exclusive ownership for transactions.

12 exports Since v4.0.0 Source

Constructors

make

unstable
Since v4.0.0

Connects and authenticates a single PostgreSQL session.

Details

Password resolution, transport, optional SSLRequest, startup, and authentication run under connectTimeout (5 seconds by default). The effect resolves when the backend sends ReadyForQuery. Closing the scope sends Terminate, ends the socket, and destroys the transport (a custom stream included) once the message has flushed, or after one second if the flush stalls. Release does not wait for the peer.

Use sslmode=require or explicit ssl: true to require encryption. Unix sockets and custom streams should set ssl.servername explicitly.

When tracing is enabled, each session attempt emits a sql.connect client span with error.type on non-interrupt failures and db.response.status_code when the server supplies a valid SQLSTATE.

Signature

declare function make(options: Config): Effect<PgConnection, SqlError, Scope>

Models

BatchStatement

interface unstable
Since v4.2.0

One statement of a batch.

Signature

interface BatchStatement {
readonly params?: readonly Array<unknown>;
readonly sql: string;
}

Config

interface unstable
Since v4.0.0

Connection settings for a PostgreSQL session.

Details

A url is parsed as a libpq URI (postgres:// or postgresql://); explicit fields win over anything the URL carries. A stream factory wins over host, port, and path. A path is used verbatim as the unix socket path, while a host beginning with / is treated as a socket directory and expands to ${host}/.s.PGSQL.${port}.

URL modes sslmode=prefer and sslmode=allow try TLS first, falling back to plaintext only when the server answers SSLRequest with N. Unlike libpq, allow also tries TLS first. Certificate verification stays enabled unless explicitly disabled through ssl options.

Prepared statements are enabled by default and limited by preparedStatementCacheSize. Disable them for poolers that drop named statements, such as PgBouncer with max_prepared_statements = 0, or for workloads that generate unique SQL. Streams always use unnamed statements. With multiplex, PgBouncer before 1.26.0 with max_prepared_statements > 0 can misorder or drop responses to pipelined Close messages and fail connections with Unexpected ParseComplete during close; set prepare: false or upgrade PgBouncer.

With multiplex enabled, unpinned queries from multiple fibers are pipelined. Transactions, streams, and listeners remain exclusive. An unpinned multiplexed connection cannot be interrupted because cancellation could affect another fiber's query.

Signature

interface Config {
readonly applicationName?: string;
readonly connectTimeout?: Input;
readonly database?: string;
readonly host?: string;
readonly keepAlive?: Input;
readonly maxMessageSize?: number;
readonly multiplex?: boolean;
readonly password?: Redacted<string> | Effect<Redacted<string>, never, never>;
readonly path?: string;
readonly port?: number;
readonly prepare?: boolean;
readonly preparedStatementCacheSize?: number;
readonly ssl?: boolean | ConnectionOptions;
readonly startupOptions?: string;
readonly startupParameters?: Readonly<Record<string, string>>;
readonly stream?: () => Duplex;
readonly types?: Registry;
readonly url?: Redacted<string>;
readonly username?: string;
}

Field

interface unstable
Since v4.0.0

Metadata for one result column.

Signature

interface Field {
readonly dataTypeId: number;
readonly name: string;
}

Notification

interface unstable
Since v4.0.0

A NOTIFY message received while listening on a channel.

Signature

interface Notification {
readonly channel: string;
readonly payload: string;
readonly processId: number;
}

PgConnection

interface unstable
Since v4.0.0

A connected and authenticated PostgreSQL session.

Signature

interface PgConnection {
readonly "~@effect/sql-pg/PgConnection": "~@effect/sql-pg/PgConnection";
readonly batch: (statements: readonly Array<BatchStatement>) => Effect<readonly Array<Result>, SqlError>;
readonly config: Config;
readonly interrupt: Effect<void>;
readonly listen: (channel: string) => Effect<Dequeue<Notification, SqlError>, SqlError, Scope>;
readonly pin: Effect<PgConnection, never, Scope>;
readonly processId: number;
readonly query: (sql: string, params?: readonly Array<unknown>, prepare?: boolean) => Effect<Result, SqlError>;
readonly queryTextValues: (sql: string, params?: readonly Array<unknown>, prepare?: boolean) => Effect<TextResult, SqlError>;
readonly queryValues: (sql: string, params?: readonly Array<unknown>, prepare?: boolean) => Effect<readonly Array<readonly Array<unknown>>, SqlError>;
readonly stream: (sql: string, params?: readonly Array<unknown>) => Stream<Row, SqlError>;
}

Result

interface unstable
Since v4.0.0

The result of a query.

Signature

interface Result {
readonly command: string;
readonly fields: readonly Array<Field>;
readonly oid: number | null;
readonly rowCount: number;
readonly rows: readonly Array<Row>;
}

Row

interface unstable
Since v4.0.0

An object result row keyed by column name.

Signature

interface Row {
[column: string]: unknown;
}

TextResult

interface unstable
Since v4.1.6

The result of queryTextValues: positional rows holding each value as the server's text, or null for SQL NULL.

Signature

interface TextResult {
readonly command: string;
readonly fields: readonly Array<Field>;
readonly oid: number | null;
readonly rowCount: number;
readonly values: readonly Array<readonly Array<string | null>>;
}

Services

Since v4.0.0

The service tag for PgConnection.

Signature

declare const PgConnection: Service<PgConnection, PgConnection>

Type IDs

TypeId

unstable
Since v4.0.0

The runtime type identifier for PgConnection.

Signature

declare const TypeId: TypeId

TypeId

type unstable
Since v4.0.0

The type-level identifier for PgConnection.

Signature

type TypeId = "~@effect/sql-pg/PgConnection"