Files
Mark KalsbeekandClaude Opus 5.5 43adca39b5 smarm-pg 0.1.0: the Postgres client from the urus + smarm Conduit server
Extracted from rust-urus-markk116 (db/wire.rs, db/types.rs). Changes on the way:
- Config (was DbConfig) takes startup options; plan_cache_mode/jit are now the app's choice
- PgConn::query for single statements
- bool parameters and results; FromSql for time::OffsetDateTime (feature time)
  replaces the app-specific Timestamp
- a parameter count mismatch is refused before sending and leaves the connection usable
- lib denies unwrap/expect, as smarm does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011iFkM6QYfk9aJxpog3uuTj
2026-10-20 21:39:01 +02:00

2.6 KiB

smarm-pg

A small Postgres client for the smarm actor runtime. Calls are plain blocking functions: the socket is read and written optimistically and the calling actor parks on smarm's fd readiness only on EAGAIN. No async, no runtime of its own.

Extracted from the urus + smarm Conduit server, where it replaced tokio-based drivers that don't fit smarm.

use smarm_pg::{oid, Config, PgConn, Stmt};

static ADD: Stmt = Stmt::read("SELECT $1::int8 + 1", &[oid::INT8]);

smarm::run(|| {
    let cfg = Config::parse("postgres://app:secret@127.0.0.1/app")
        .unwrap()
        .option("plan_cache_mode", "force_generic_plan");
    let mut conn = PgConn::connect(&cfg).unwrap();
    let rows = conn.query(&ADD, &[&41i64]).unwrap();
    assert_eq!(rows.first::<i64>().unwrap(), Some(42));
});

What it does

  • Extended protocol only. Statements are static Stmts. A connection prepares each one the first time it runs it, under a name of its own, and reuses it afterwards. The Parse goes out in the same write as the Bind/Execute, so a cold statement still costs one round trip.
  • Binary parameters and results.
  • Pipelines. PgConn::pipeline sends several statements behind one Sync: one round trip, one implicit transaction. If one fails, the rest are skipped and none of them took effect.
  • Startup options. Config::option puts session settings in the startup message.
  • Auth: trust, cleartext, MD5, SCRAM-SHA-256 (no channel binding).
  • Failure: a server error (PgError::Db) leaves the connection usable; anything else (PgError::fatal) marks it broken for good (is_healthy), and the owner should drop it.

What it doesn't do (yet)

  • Pooling. One PgConn is one connection, used by one actor at a time. In smarm the natural pool is an actor per connection under a supervisor, sending batches by message; the application owns that, along with routing (Stmt::writes is there for read/write splitting).
  • Types: bool, int8 (int4 reads too), text, text[], timestamptz (as time::OffsetDateTime, feature time, on by default). Everything else is Rows::raw.
  • No TLS, no COPY, no LISTEN/NOTIFY, no explicit transactions beyond a pipeline, no percent-decoding in URLs, no Unix sockets.

Tests

TEST_DATABASE_URL=postgres://user:pw@127.0.0.1/db cargo test. Without it, the tests that need a server print a note and pass. They only create session-local temp tables.

cargo clippy --lib must be clean: as in smarm, the library denies unwrap/expect.

License

MIT