# Module /postgres/client

Pooled PostgreSQL client with primary/replica routing, strong consistency reads (query-strong, query-one-strong), and ACID transactions.

```tangu
(use /postgres/client)
```

## Functions & symbols

### begin

`(begin conn callback) -> (Future Any)`

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` | no | Connection pool, writer, or parent Transaction (creates savepoint) |
| `callback` | `Func` | no | Transaction body `(Func tx ...)` — commits on normal return, rolls back on error or thrown panic |

```tangu
(await (db/begin conn (Func tx
    (await (db/exec tx #sql "UPDATE accounts SET balance = balance - 10 WHERE id = $1" [1]))
)))
```

### command

`(command conn sql [params]) -> (Future Int)`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/command conn "CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, name TEXT)"))
```

### connect

`(connect uri-or-config) -> (Future Pool)`

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` | no | Connection URI string or config Map {:uri ... :reader-uri ... :max-connections ...} |

```tangu
(let pool (await (pg/connect {:uri "postgres://app:secret@localhost:5432/mydb" :max-connections 16})))
```

### exec

`(exec conn sql [params]) -> (Future Int)`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/exec conn #sql "DELETE FROM sessions WHERE id = $1" [sid]))
```

### exec-batch

`(exec-batch conn sql) -> (Future None)`

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` | no | Connection pool or writer |
| `sql` | `String` | no | Multi-statement SQL script (e.g. schema migration) |

```tangu
(await (db/exec-batch conn migration-sql))
```

### get-reader

`(get-reader pool) -> Pool`

Returns the read-replica pool handle (or writer pool in single-node setups).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `pool` | `Pool` | no | Database cluster or pool |

```tangu
(let reader (db/get-reader pool))
```

### get-writer

`(get-writer pool) -> Pool`

Returns the underlying primary writer pool handle.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `pool` | `Pool` | no | Database cluster or pool |

```tangu
(let writer (db/get-writer pool))
```

### is-cluster?

`(is-cluster? pool) -> Bool`

Returns `true` if `pool` was configured with separate writer and read-replica pools.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `pool` | `Pool` | no | Database connection pool |

```tangu
(db/is-cluster? pool)
```

### pool-stats

`(pool-stats pool) -> Map`

Alias for `pool-status`: returns live connection pool utilization metrics.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `pool` | `Pool` | no | Database connection pool |

```tangu
(db/pool-stats pool)
```

### pool-status

`(pool-status pool) -> Map`

Returns connection pool health metrics (active connections, idle count, capacity).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `pool` | `Pool` | no | Database connection pool |

```tangu
(db/pool-status pool)
```

### query

`(query conn sql [params]) -> (Future (Array Map))`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query conn #sql "SELECT id, name FROM users WHERE active = $1" [true]))
```

### query-exists-strong?

`(query-exists-strong? conn sql [params]) -> (Future Bool)`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-exists-strong? conn #sql "SELECT 1 FROM users WHERE id = $1" [id]))
```

### query-exists?

`(query-exists? conn sql [params]) -> (Future Bool)`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-exists? conn #sql "SELECT 1 FROM users WHERE email = $1" [email]))
```

### query-maps

`(query-maps conn sql [params]) -> (Future (Array Map))`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-maps conn #sql "SELECT * FROM items WHERE org_id = $1" [org-id]))
```

### query-maps-strong

`(query-maps-strong conn sql [params]) -> (Future (Array Map))`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-maps-strong conn #sql "UPDATE items SET done = true WHERE id = $1 RETURNING *" [id]))
```

### query-one

`(query-one conn sql [params]) -> (Future (Or Map None))`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-one conn #sql "SELECT * FROM users WHERE id = $1" [id]))
```

### query-one-strong

`(query-one-strong conn sql [params]) -> (Future (Or Map None))`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-one-strong conn #sql "INSERT INTO users (name) VALUES ($1) RETURNING id, name" ["Ada"]))
```

### query-one-tuple

`(query-one-tuple conn sql [params]) -> (Future (Or Array None))`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-one-tuple conn #sql "SELECT id, email FROM users WHERE id = $1" [id]))
```

### query-scalar

`(query-scalar conn sql [params]) -> (Future Any)`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-scalar conn #sql "SELECT count(*) FROM users"))
```

### query-scalar-strong

`(query-scalar-strong conn sql [params]) -> (Future Any)`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-scalar-strong conn #sql "INSERT INTO jobs (name) VALUES ($1) RETURNING id" [name]))
```

### query-strong

`(query-strong conn sql [params]) -> (Future (Array Map))`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-strong conn #sql "INSERT INTO users (email) VALUES ($1) RETURNING *" [email]))
```

### query-tuples

`(query-tuples conn sql [params]) -> (Future (Array Array))`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-tuples conn #sql "SELECT id, title FROM posts"))
```

### query-tuples-one

`(query-tuples-one conn sql [params]) -> (Future (Or Array None))`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-tuples-one conn #sql "SELECT id, email FROM users WHERE id = $1" [id]))
```

### query-tuples-one-strong

`(query-tuples-one-strong conn sql [params]) -> (Future (Or Array None))`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-tuples-one-strong conn #sql "INSERT INTO tags (slug) VALUES ($1) RETURNING id, slug" [slug]))
```

### query-tuples-strong

`(query-tuples-strong conn sql [params]) -> (Future (Array Array))`

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` | no | Database connection pool, cluster, or active Transaction |
| `sql` | `String` | no | Parameterized `#sql` query using $1, $2 placeholders |
| `params` | `Array` | yes | Optional positional bind arguments |

```tangu
(await (db/query-tuples-strong conn #sql "SELECT id, title FROM posts"))
```
