# postgres

Postgres over its own wire protocol (version 3), written out in Mote on
`tcp.open`: no driver. The whole of it is [docs/postgres.md](../../docs/postgres.md).

```text
use "pkg:postgres/postgres.mote"
use "pkg:postgres/scram.mote"
use "pkg:postgres/types.mote"

let rows: Order[] = pgRows("select id, total from orders where customer = $1", [who])?
let n = pgExec("update orders set paid = true where id = $1", [id])?
```

Settings: `PG_URL` (a secret: the connection string as the host gives it),
`PG_CA` (PEM, for `sslmode=verify-full` with an authority of the server's own).

| File | |
|---|---|
| `postgres.mote` | The connection string; startup, TLS and the password exchange; the extended query protocol; the pool (`tcp.take`); transactions and savepoints; LISTEN/NOTIFY; COPY in; a cancel. |
| `scram.mote` | SCRAM (RFC 5802; SHA-256, RFC 7677): the client's side. |
| `types.mote` | Values both ways, by the column's type OID. |
| `tests.mote` | Its tests, with RFC 7677's and RFC 5802's exchanges: `use "pkg:postgres/tests.mote"`, then `mote test`. |

**Covered**: what a product sends week to week. Parameters (never in the
text), the common types, prepared statements kept per connection, a pool
with a size and an idle time, `pgAtomic` and `pgBegin`/`pgCommit` with
savepoints, refusals as failures whose kind is the SQLSTATE's and whose data
is every field, statement timeouts and cancels, LISTEN/NOTIFY, bulk loads
by COPY, Supabase, Neon, RDS and transaction poolers.

**Left out on purpose**: pipelining, the binary format, COPY out, channel
binding and Kerberos, several hosts, logical replication. See the last part
of the docs page.

**Run against** a stand-in that speaks the protocol and can be made to
misbehave (`tests/all.sh real_svc_postgres`, `real_pg_live`), and a real
PostgreSQL 17 the test starts itself (`real_pg_real`: SCRAM, TLS each way,
values at their edges, large results, calls at once, a restart). One command
runs `real_pg_live` against a server of yours: `MOTE_PG_URL=… tests/all.sh real_pg_live`.

## Mote's own models in Postgres

Built: `MOTE_SQL=postgres://…` keeps a program's models, its jobs queue and
the back end's other stores that go with them in Postgres
([docs/back-end.md](../../docs/back-end.md#models-in-postgres)). Of the
seven steps this page once listed:

1. **The client in the runtime**: `runtime/server/pg.mote`, the same
   protocol written again in the runtime's dialect (on part TCP's
   connections, SCRAM by node's crypto). This extension does not stand on
   it yet: the two are kept apart.
2. **A second dialect**: the engine still writes SQLite's SQL, and
   `pg.mote` says each statement again as Postgres's, word by word (`$n`,
   `IS NOT DISTINCT FROM`, or `=` and `IS NOT NULL` where a value is given,
   so an index answers; `mote_rowid` for `rowid`; the types; `TEXT COLLATE
   "C"`). The full-text index (FTS5) has no Postgres form yet: a program
   with one is refused at start.
3. **Waiting**: the engine already waited for every statement (the page's
   SQLite is in a worker); nothing to do.
4. **`atomic`**: a turn holds one connection from BEGIN to COMMIT and takes
   `pg_advisory_xact_lock` first, so turns are one at a time across every
   back end, as SQLite's one writer.
5. **Migrations**: one transaction under that lock; `schema.lock`'s steps as
   for SQLite.
6. **What changed**: `mote_changes` and NOTIFY in the turn, LISTEN on a
   connection of its own (or polled behind a pooler).
7. **The runtime's own databases**: the jobs queue, answered calls, leases,
   `server at`, `migrate` blocks, `webhook.once`, accounts and sessions,
   and the counts of `limit` are in Postgres. The inbox and devices of
   `notify` are not yet.
