PostgreSQL client for Hot.
Speaks the PostgreSQL v3 wire protocol directly over ::hot::tcp
(with optional ::hot::tls), so no native driver is required.
Supports trust, cleartext, MD5, and SCRAM-SHA-256 authentication,
simple and parameterized queries, and text-format result decoding.
A connection is a runtime-owned socket handle plus backend state; it lives for the duration of the run or task that opened it.
Error contract: expected failures — connection refused, TLS
negotiation, authentication, SQL errors — return a Result.Err;
branch with is-err / if-err. A socket failure mid-protocol (the
connection drops between request and reply) halts the run instead:
the connection state is unusable and the recovery unit is the run or
task, not the query.
Quick start
conn ::pg/connect({host: "localhost", port: 5432, user: "postgres",
password: ::hot::ctx/get("pg.password"), database: "app"})
users ::pg/query(conn, "SELECT id, email FROM users WHERE active = $1", [true])
::pg/execute(conn, "INSERT INTO events (name) VALUES ($1)", ["signup"])
::pg/close(conn)
Functions
close
fn (conn: PgConnection): Bool
Close a connection: send Terminate and shut down the socket. Safe to call more than once.
Example
conn ::pg/connect({user: "postgres", database: "postgres"})
::pg/close(conn)
connect
fn (options: Map): PgConnection
Open and authenticate a PostgreSQL connection.
Performs optional TLS negotiation, sends the startup message, runs the
authentication handshake, and consumes backend setup messages through
the first ReadyForQuery. Returns a PgConnection, or a Result.Err
carrying the error details when the connection, TLS negotiation, or
authentication fails. Branch on it with is-err / if-err; an
unhandled error halts at first use with the same message.
Example
conn ::pg/connect({user: "postgres", database: "postgres"})
if-err(conn, (e) { println(`Cannot connect: ${e}`) })
See PgOptions for the accepted fields.
execute
fn (conn: PgConnection, sql: Str): Map
fn (conn: PgConnection, sql: Str, params: Vec): Map
Run a statement for its effect and return
{command: Str, rows-affected: Int}. Use for INSERT/UPDATE/DELETE and
DDL. Accepts optional params like query.
Example
conn ::pg/connect({user: "postgres", database: "postgres"})
out ::pg/execute(conn, "UPDATE users SET active = $1 WHERE id = $2", [false, 42])
out.rows-affected
query
fn (conn: PgConnection, sql: Str): Vec
fn (conn: PgConnection, sql: Str, params: Vec): Vec
Run a query and return its rows as a Vec<Map> (column name -> value).
With params, the query runs through the extended protocol with
$1, $2, ... placeholders bound as parameters — always prefer this
over interpolating values into the SQL string. A query error returns
a Result.Err with the PostgreSQL error fields (code, message,
...) attached; branch on it with is-err / if-err. An unhandled
error halts at first use with the same message.
Example
conn ::pg/connect({user: "postgres", database: "postgres"})
::pg/query(conn, "SELECT id, email FROM users")
::pg/query(conn, "SELECT * FROM users WHERE id = $1", [42])
query-one
fn (conn: PgConnection, sql: Str): Any
fn (conn: PgConnection, sql: Str, params: Vec): Any
Run a query and return its first row as a Map, or null if there are
no rows. Accepts optional params like query.
Example
conn ::pg/connect({user: "postgres", database: "postgres"})
user ::pg/query-one(conn, "SELECT * FROM users WHERE id = $1", [42])
Types
PgConnection
PgConnection type {
socket: Any,
parameters: Map,
backend-pid: Int?,
backend-key: Int?
}
An open PostgreSQL connection.
Fields
socket— The underlying::hot::tcpconnectionparameters— Server ParameterStatus values (e.g.server_version)backend-pid/backend-key— BackendKeyData for query cancellation
PgOptions
PgOptions type {
host: Str?,
port: Int?,
user: Str,
password: Str?,
database: Str?,
ssl: Str?,
tls: Map?,
timeout: Int?
}
Connection options.
Fields
host— Server host (default"127.0.0.1")port— Server port (default5432)user— Role name (required)password— Password for cleartext/MD5/SCRAM auth (default"")database— Database name (default: theuservalue)ssl—"prefer"(default; TLS if offered, else plaintext),"require"(fail if the server refuses TLS), or"disable"tls— Options passed to::hot::tls/upgrade(e.g.ca-pem,mode)timeout— Connect/read timeout in milliseconds