/postgres/client
Pooled PostgreSQL client with primary/replica routing, strong consistency reads (query-strong, query-one-strong), and ACID transactions.
Functions & symbols#
begin#
Runs `callback` inside an ACID database transaction (or nested SAVEPOINT if already in a transaction), automatically committing on success or rolling back on error. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Connection pool, writer, or parent Transaction (creates savepoint) |
callback | Func | required | Transaction body `(Func tx ...)` — commits on normal return, rolls back on error or thrown panic |
Any(await (db/begin conn (Func tx
(await (db/exec tx #sql "UPDATE accounts SET balance = balance - 10 WHERE id = $1" [1]))
)))command#
Executes a SQL command/DDL statement on the primary writer and returns the affected row count. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/command conn "CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, name TEXT)"))connect#
Opens a SQL connection pool (or primary/reader cluster when `:reader-uri` / `:reader` is provided). Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
config | String | Map | required | Connection URI string or config Map {:uri ... :reader-uri ... :max-connections ...} |
Any(let pool (await (pg/connect {:uri "postgres://app:secret@localhost:5432/mydb" :max-connections 16})))exec#
Executes a DML statement (`INSERT`, `UPDATE`, `DELETE`) on the primary writer and returns affected rows. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/exec conn #sql "DELETE FROM sessions WHERE id = $1" [sid]))exec-batch#
Executes a batch of raw semicolon-separated SQL statements on the primary writer (useful for migrations). Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Connection pool or writer |
sql | String | required | Multi-statement SQL script (e.g. schema migration) |
Any(await (db/exec-batch conn migration-sql))get-reader#
Returns the read-replica pool handle (or writer pool in single-node setups).
| Parameter | Type | Optional | Description |
|---|---|---|---|
pool | Pool | required | Database cluster or pool |
Any(let reader (db/get-reader pool))get-writer#
Returns the underlying primary writer pool handle.
| Parameter | Type | Optional | Description |
|---|---|---|---|
pool | Pool | required | Database cluster or pool |
Any(let writer (db/get-writer pool))is-cluster?#
Returns `true` if `pool` was configured with separate writer and read-replica pools.
| Parameter | Type | Optional | Description |
|---|---|---|---|
pool | Pool | required | Database connection pool |
Any(db/is-cluster? pool)pool-stats#
Alias for `pool-status`: returns live connection pool utilization metrics.
| Parameter | Type | Optional | Description |
|---|---|---|---|
pool | Pool | required | Database connection pool |
Any(db/pool-stats pool)pool-status#
Returns connection pool health metrics (active connections, idle count, capacity).
| Parameter | Type | Optional | Description |
|---|---|---|---|
pool | Pool | required | Database connection pool |
Any(db/pool-status pool)query#
Executes a `SELECT` query (routed to read replicas when configured, so reads are eventually consistent) and returns an Array of row Maps. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query conn #sql "SELECT id, name FROM users WHERE active = $1" [true]))query-exists-strong?#
Primary-writer variant of `query-exists?` guaranteeing strong consistency. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-exists-strong? conn #sql "SELECT 1 FROM users WHERE id = $1" [id]))query-exists?#
Returns `true` if the SQL query matches at least one row. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-exists? conn #sql "SELECT 1 FROM users WHERE email = $1" [email]))query-maps#
Executes a `SELECT` query and returns each row as a Map. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-maps conn #sql "SELECT * FROM items WHERE org_id = $1" [org-id]))query-maps-strong#
Primary-writer variant of `query-maps` guaranteeing linearizable read-after-write visibility. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-maps-strong conn #sql "UPDATE items SET done = true WHERE id = $1 RETURNING *" [id]))query-one#
Executes a query and returns the first row as a `Map`, or `()` (`None`) if zero rows matched. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-one conn #sql "SELECT * FROM users WHERE id = $1" [id]))query-one-strong#
Executes a single-row query on the primary writer (`INSERT/UPDATE ... RETURNING` or immediate post-write lookup), returning a `Map` or `()`. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-one-strong conn #sql "INSERT INTO users (name) VALUES ($1) RETURNING id, name" ["Ada"]))query-one-tuple#
Alias for `query-tuples-one`: returns the first row as an Array or `()`. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-one-tuple conn #sql "SELECT id, email FROM users WHERE id = $1" [id]))query-scalar#
Executes a query expecting one column from the first row and returns that scalar value (or `()` if empty). Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-scalar conn #sql "SELECT count(*) FROM users"))query-scalar-strong#
Primary-writer variant of `query-scalar` for strong read-after-write scalar queries. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-scalar-strong conn #sql "INSERT INTO jobs (name) VALUES ($1) RETURNING id" [name]))query-strong#
Executes a query strictly on the primary writer for strong read-after-write consistency (required for `INSERT/UPDATE/DELETE ... RETURNING`). Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-strong conn #sql "INSERT INTO users (email) VALUES ($1) RETURNING *" [email]))query-tuples#
Executes a query and returns rows as positional Arrays of column values. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-tuples conn #sql "SELECT id, title FROM posts"))query-tuples-one#
Executes a query and returns the first row as an Array of positional columns, or `()`. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-tuples-one conn #sql "SELECT id, email FROM users WHERE id = $1" [id]))query-tuples-one-strong#
Primary-writer variant of `query-tuples-one` guaranteeing strong consistency. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-tuples-one-strong conn #sql "INSERT INTO tags (slug) VALUES ($1) RETURNING id, slug" [slug]))query-tuples-strong#
Primary-writer variant of `query-tuples` for read-after-write consistency. Executes asynchronously on `*runtime*` and returns a `Future` resolved via `(await ...)`.
| Parameter | Type | Optional | Description |
|---|---|---|---|
conn | Pool | Transaction | required | Database connection pool, cluster, or active Transaction |
sql | String | required | Parameterized `#sql` query using $1, $2 placeholders |
params | Array | optional | Optional positional bind arguments |
Any(await (db/query-tuples-strong conn #sql "SELECT id, title FROM posts"))