Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# The documentation site on GitHub Pages, rebuilt on every push to main:
# The documentation site on GitHub Pages: built on every pull request, which
# fails if agents/ is not what the pages write, and deployed from main.
# odoc's reference from the .mli files, then Starlight's pages around it.
name: docs

on:
push:
branches: [main]
pull_request:

# A build reads the repository and nothing more; the deploy job alone is
# given the rights to publish.
permissions:
contents: read
pages: write
id-token: write

# A newer push to a pull request replaces its build; main's deploys queue
# and always finish.
concurrency:
group: pages
cancel-in-progress: true
group: pages-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
deploy:
build:
runs-on: ubuntu-24.04
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/configure-pages@v6
- uses: actions/checkout@v5
- uses: ocaml/setup-ocaml@v3
with:
Expand All @@ -38,8 +38,23 @@ jobs:
# the token, past its limit for a runner's shared address.
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# The build writes agents/ from the pages: a page changed without it
# differs here.
- run: git add --intent-to-add agents/ && git diff --exit-code agents/
- uses: actions/upload-pages-artifact@v5
with:
path: _build/docs
deploy:
needs: build
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-24.04
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/configure-pages@v6
- uses: actions/deploy-pages@v5
id: deployment
15 changes: 13 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ make release VERSION=1.2.3 # once CHANGES.md names it: tag and release with ope
`make test` needs `podman compose`. Without
`POSTGRES_EIO_TEST_PG` the cases that need a server are skipped, and the test
output says so. `make docs` needs Bun. CI runs `make lint` and `make test`
on OCaml 5.5, and `docs.yml` publishes the site to GitHub Pages on every
push to `main`.
on OCaml 5.5, and `docs.yml` builds the site on every pull request and
publishes it to GitHub Pages from `main`.

## Layout

Expand All @@ -49,6 +49,10 @@ docs/ the documentation site, a Starlight site in Hypha's look,
as @examples/<file>.ml?raw into Starlight's <Code>
examples/ the programs and modules the site shows, each a whole
file, each built
agents/ the documentation for a user's agent, generated by
starlight-hypha-agents from the pages, `make docs`:
llms.txt, the catalogue, and pages/, each page as
Markdown; installed by the postgres-eio package
```

`make docs` builds the site into `_build/docs`, so it adds nothing to the
Expand All @@ -63,6 +67,13 @@ module.

Each rule comes with why it exists and the test that catches a break.

- **`agents/` is generated, never edited.** starlight-hypha-agents writes
it from the site's pages as `make docs` builds them: a page changed is
`make docs` run in the same commit. Why: a user's agent reads what the
release installs, offline, and a page it reads must be the release's.
Test: the docs workflow builds the pages on every pull request and fails
if `agents/` then differs.

- **Every protocol requirement the driver meets has a test case named by
its section** of chapter 54 of the Postgres 18 docs (e.g. `54.2.4 ...`).
Backend messages are read whole, a byte at a time, and at generated
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changes

## Unreleased

- postgres-eio installs its documentation for agents: each page of the
site as Markdown, and `llms.txt`, in `$(opam var postgres-eio:doc)`.

## 0.5.2 (2026-11-10)

- postgres-eio is published to open-opam, `opam repo add open-opam
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ let () =
Postgres_eio.close conn)
```

## With an agent

postgres-eio installs its documentation at `$(opam var postgres-eio:doc)`:
each page of the site as Markdown, and `llms.txt`, which lists them. Your
agent reads it when told to, in a line in your project's `AGENTS.md`:

```md
Before writing code with postgres-eio, read `$(opam var postgres-eio:doc)/llms.txt` and
the pages it lists for the parts you need, and use those parts rather than
writing your own.
```

## Contributing

See [AGENTS.md](AGENTS.md).
Expand Down
25 changes: 25 additions & 0 deletions agents/llms.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# postgres-eio

> The Postgres driver for modern OCaml.

## Start here

- [Getting Started](pages/index.md): The Postgres driver for modern OCaml.
- [Quick start](pages/quick-start.md)

## Guides

- [Connection strings](pages/guide/connection-strings.md)
- [TLS and signing in](pages/guide/tls.md)
- [Several hosts](pages/guide/several-hosts.md)
- [Queries and values](pages/guide/queries.md)
- [Errors](pages/guide/errors.md)
- [Pool](pages/guide/pool.md)
- [Pipelining and batches](pages/guide/pipelining.md)
- [COPY](pages/guide/copy.md)
- [LISTEN/NOTIFY](pages/guide/listen-notify.md)
- [Performance](pages/guide/performance.md)

## Reference

- [Reference](pages/reference.md)
114 changes: 114 additions & 0 deletions agents/pages/guide/connection-strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Connection strings

Both libpq forms are accepted: `postgres://user:pass@host:5432/db?sslmode=require`
and `host=db user=app sslmode=require`. A URL may start `postgresql://` too.
Unknown keys are an error, not ignored.

```ocaml
let url =
Postgres_eio.Conninfo.of_string
"postgres://app:secret@db.internal:5432/shop?sslmode=verify-full"

let keywords =
Postgres_eio.Conninfo.of_string
"host=db.internal port=5432 user=app password='a secret' dbname=shop \
sslmode=verify-full"
```

`Conninfo.of_string` answers the connection, or a sentence saying what is
wrong with the string. A URL is percent-decoded. A keyword value may be
single-quoted, with backslash escapes, so it can hold a space. An empty
value unsets a key.

## The keys

| Key | Values | Default |
|---|---|---|
| `host` | name, address, or a Unix socket directory (starts with `/`); comma-separated for several | `localhost` |
| `hostaddr` | IP address to connect to without a lookup; `host` stays the name TLS verifies | |
| `port` | one for all hosts, or one per host | `5432` |
| `user` | | required |
| `password` | | |
| `dbname` | | `user` |
| `sslmode` | `disable`, `allow`, `prefer`, `require`, `verify-ca`, `verify-full` | `prefer` on this machine, `require` elsewhere |
| `sslrootcert` | PEM file of trusted CAs, for `verify-ca` and `verify-full` | system CAs |
| `sslcert`, `sslkey` | PEM client certificate and unencrypted key | |
| `sslnegotiation` | `postgres`, or `direct` for TLS from the first byte (needs `require` or stronger) | `postgres` |
| `channel_binding` | `disable`, `prefer`, `require` | `prefer` |
| `require_auth` | list of `password`, `md5`, `scram-sha-256`, `none`, or each negated with `!` | all |
| `connect_timeout` | whole seconds per address attempt | the connection's `timeout` (30 s) |
| `application_name` | | |
| `keepalives` | `1`, `0` | `1` |
| `target_session_attrs` | `any`, `read-write`, `read-only`, `primary`, `standby`, `prefer-standby` | `any` |
| `load_balance_hosts` | `disable`, `random` | `disable` |
| `min_protocol_version`, `max_protocol_version` | `3.0`, `3.2`, `latest` | `3.0` |
| `options` | `-c name=value` settings, sent as startup parameters | |

The TLS keys are explained in [TLS and signing in](tls.md),
and `target_session_attrs` and `load_balance_hosts` in
[several hosts](several-hosts.md). `sslpassword` is not
supported, and is an error like any other unknown key: a client key is read
unencrypted.

The driver speaks protocol 3.0 and 3.2, over TCP, a Unix socket or TLS.
`max_protocol_version` is `3.0` by default, since many poolers speak only
3.0; `latest` is 3.2. `options` reads `-c name=value`, `-cname=value` and
`--name=value`. `connect_timeout` of `0` or less is absent, as libpq reads
it, and the connection's `timeout` bounds each attempt instead.

## The environment and .pgpass

The driver reads no environment variables or files unless you pass them in:

```ocaml
let from_environment pgpass_contents =
Postgres_eio.Conninfo.of_string
~defaults:(Postgres_eio.Conninfo.environment Sys.getenv_opt)
"dbname=shop"
|> Result.map (Postgres_eio.Conninfo.passfile pgpass_contents)
```

`Conninfo.environment` reads libpq's `PG*` variables for the supported keys
(`PGHOST`, `PGPORT`, `PGUSER`, `PGPASSWORD`, ...), ignoring an empty one,
and hands them to `of_string` as defaults for the keys the string leaves
out. It takes the function that reads a variable, so a test can hand it
another.

`Conninfo.passfile` takes the contents of a `.pgpass` file and fills each
host's password from it where the string gave none, as libpq does:
`host:port:database:user:password`, `*` wildcards, backslash escapes, first
match wins. Hosts match as written, a socket by its directory. Finding the
file and checking its permissions is the program's:

```ocaml
(* libpq refuses a .pgpass that others can read; checking that is the
program's, as is finding the file. *)
let contents () =
match Sys.getenv_opt "HOME" with
| None -> ""
| Some home -> (
match
In_channel.with_open_bin
(Filename.concat home ".pgpass")
In_channel.input_all
with
| contents -> contents
| exception Sys_error _ -> "")
```

## Changing a connection

A connection string is read by its fields (`hosts`, `user`, `database`,
...) and made by `of_string`. Two keys can be changed after:

```ocaml
let maintenance conninfo =
let conninfo = Postgres_eio.Conninfo.with_database conninfo "postgres" in
Postgres_eio.Conninfo.with_connect_timeout conninfo Mtime.Span.(10 * s)
```

`with_database` connects to another database, the empty string meaning the
user's name. `with_connect_timeout` bounds each address attempt by a span.
`to_url` writes a URL that parses back to the same connection, minus the
passwords `.pgpass` gave; it writes `connect_timeout` in whole seconds,
rounded up.
68 changes: 68 additions & 0 deletions agents/pages/guide/copy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# COPY

COPY moves bulk data in both directions, streamed, without holding a whole
table. It takes no parameters.

## Rows in

```ocaml
let load conn rows =
Postgres_eio.copy_in_rows conn ~table:"orders" ~columns:[ "id"; "total" ]
(Seq.map
(fun (id, total) -> [| Some id; Some (string_of_int total) |])
rows)
```

`copy_in_rows` runs `COPY "schema"."table" ("a", "b") FROM STDIN` in text
format, escaping each row. Cells follow `columns`' order, `None` for NULL;
empty `columns` means every column, in the table's order. The sequence is
read lazily. Names are quoted as given, so `"Jobs"` is not `jobs`.

If the producer raises, the COPY is aborted, nothing is written and the
connection stays usable; the exception propagates, and the server is sent
a fixed message, never the exception. A NUL in a name is refused before
anything is sent; a NUL in a value fails the COPY, the connection still
usable.

## Rows out

`copy_out_rows` streams a select the other way:

```ocaml
let sum conn =
Postgres_eio.copy_out_rows conn ~select:"select total from orders" ~init:0
~row:(fun sum row ->
match row with
| [| Some total |] ->
sum + Option.value ~default:0 (Postgres_eio.Text.to_int total)
| _ -> sum)
```

It runs `COPY (select) TO STDOUT` and decodes each row for `row`, `None`
for NULL, like `query`, for a very large result.

## Bytes in and out

`copy_in` and `copy_out` run a COPY statement of your own, in its own
format, CSV say, and move its bytes unchanged:

```ocaml
let import conn source =
Postgres_eio.copy_in conn
"copy orders (id, total) from stdin with (format csv, header)" source
```

```ocaml
let export conn sink =
Postgres_eio.copy_out conn "copy orders to stdout with (format csv)" ~init:()
~chunk:(fun () data -> Eio.Flow.copy_string data sink)
```

`copy_in` streams an Eio source, and `copy_out` folds each chunk into
`chunk` as it arrives. Any other statement is refused, after it has run.
If `chunk` raises, the connection is closed, the statement cancelled at
the server, and the exception propagates. A cancellation of `copy_in`
closes the connection.

A COPY given to `query` or `script` is refused, and the connection stays
usable.
Loading