Postgres
A Postgres database from the back end, over Postgres's own wire protocol
(version 3), written out in Mote on tcp.open:
no driver, no library. TLS, SCRAM-SHA-256 (and MD5, and cleartext where a
server asks for it), statements and their parameters always apart, values
made Mote's by their column's type, a pool, transactions and savepoints,
LISTEN/NOTIFY, COPY, a statement cancelled when it runs too long. An
extension: ordinary Mote in extensions/postgres/.
Back to the map.
use "pkg:postgres/postgres.mote"use "pkg:postgres/scram.mote"use "pkg:postgres/types.mote"shape Order id: number total: string paid: bool at: numberserver fn unpaid(customer: string): Order[] requires signedIn let rows: Order[] = pgRows("select id, total, paid, created_at as at from orders where customer = $1 and not paid order by id", [customer])? return rowsserver fn pay(id: number): number requires signedIn let tx = pgBegin()? let order = pgOne("select total from orders where id = $1 for update", [id], {tx})? if order == none pgRollback(tx) fail("no such order", {kind: "missing"})? pgExec("update orders set paid = true, paid_at = $2 where id = $1", [id, pgTime(now())], {tx})? pgExec("insert into ledger (order_id, amount) values ($1, $2)", [id, order?.total], {tx})? pgCommit(tx)? return idPG_URL='postgresql://app:…@db.example.com:5432/shop?sslmode=verify-full' bun dist/server/main.jsWhat was checked. SCRAM is checked against RFC 7677's own exchange
(SHA-256) and RFC 5802's (SHA-1), message for message, in
extensions/postgres/tests.mote. Everything else runs against a stand-in
server (tests/pg_standin.mjs) that speaks the protocol as its
documentation says and checks the client's SCRAM proof for itself:
tests/all.sh real_svc_postgres (the ways in, TLS each way, what goes on
the wire, a connection dropped in the middle of the rows, an error after
rows, idle connections the server closed, a statement timeout, the pool)
and real_pg_live (types, parameters, rows, refusals, transactions,
savepoints, COPY, LISTEN/NOTIFY, a cancel). And at a real PostgreSQL 17,
started by the test itself (tests/all.sh real_pg_real, which needs
initdb and pg_ctl: PG_BIN, default Homebrew's postgresql@17): the
same checks print the same, signed in by SCRAM-SHA-256 over TLS checked
against the server's certificate (verify-full), over TLS checking nothing
(require) and without TLS; then values at their edges (numeric past a
float's precision, the least int8, -infinity, microseconds, arrays empty
and nested), NULL of each type, a megabyte of bytea each way, a result of
100,000 rows, refusals, forty calls at once through a pool of four, a wrong
password, and the server restarted under the back end (the next read and
writes answer, on new connections). It has not been run against a hosted
Postgres or a transaction pooler (PgBouncer, Supavisor):
the checks to run there.
The settings#
One, PG_URL, a secret (it holds the password: the
back end does not start without it, and the log never holds it): the
connection string as the host gives it.
postgres://user:password@host:port/database?sslmode=require&…A user or password with @, : or / in it is percent-encoded
(p%40ss), as every host's console does. Besides libpq's own sslmode,
application_name, connect_timeout (seconds), options=-c statement_timeout=…, hostaddr (connect to this address, and check the
certificate against host), host, port, user, password and dbname:
| In the query | |
|---|---|
pool | How many connections to keep, 10 unsaid (also PG_POOL). |
idle | How long one stands idle before it is closed, in ms (30 000). |
pool_wait | How long a call waits for a connection when all are out, in ms (10 000); then it fails, busy. |
query_timeout | How long a call waits for its answer before the statement is cancelled, in ms (also PG_QUERY_TIMEOUT; unsaid: no limit of its own). A call may say its own: {timeout: 2000}. |
pooler | transaction when a transaction pooler stands before the server. Said by itself for a host with pooler in its name, or port 6543. |
prepare | false: no statement is kept by name on its connection. |
numeric | number: a numeric column is read as a number, not as its exact text. |
PG_CA, an env setting: the PEM of the authority
that signs the server's certificate, for sslmode=verify-full where this
machine's own authorities do not (RDS's bundle, a self-signed server). Only
the URL form is read; a libpq key=value string, and several hosts, are not.
sslmode#
Hosted Postgres#
The calls#
What a column becomes#
Failures#
Transactions#
LISTEN and NOTIFY#
Checking it against a real server#
What it leaves out#
docs/postgres.md