wasi-pg-client
A production-grade PostgreSQL client library for WASI Preview 2.
Quick Start
use ;
async
Key Features
- Full PostgreSQL wire protocol v3 — simple and extended query protocols
- Parameterized queries — SQL injection prevention with
$1,$2syntax - Prepared statements — with automatic LRU caching
- Streaming results — O(1) memory for large queries via
RowStream - Parameterized streaming —
query_params_stream()for streaming with parameters - Transactions — RAII guards with automatic rollback on drop
- Savepoints — nested transaction scopes
- COPY protocol — bulk import/export with CSV and binary support
- LISTEN/NOTIFY — asynchronous pub/sub with timeout support
- TLS — via rustls (pure Rust, WASI-compatible)
- SCRAM-SHA-256 and MD5 authentication
- Connection pooling — via
wasi_pg_client::poolmodule withMutex-based thread safety - Automatic reconnection — with exponential backoff and session state rebuild
- Retry policies — for transient errors (serialization failures, deadlocks)
- Query cancellation — out-of-band via
CancelToken(with TLS support) - Runtime parameter setting —
set_param()with automatic re-application on reconnect - Structured logging — via
tracing - Compiles to
wasm32-wasip2and native targets
Feature Flags
| Feature | Default | Description |
|---|---|---|
tls |
✅ | TLS support via rustls |
scram |
✅ | SCRAM-SHA-256 authentication |
md5-auth |
❌ | MD5 authentication (legacy) |
pool |
❌ | Connection pooling |
tracing |
✅ | Structured logging via tracing |
uuid |
❌ | UUID type support via uuid crate |
serde-json |
❌ | JSON type support via serde_json |
chrono |
❌ | chrono integration for date/time |
test-native |
❌ | Native transport for testing |
tokio-transport |
❌ | Tokio async TCP transport for native builds |
WASI P2 Requirements
This library targets wasm32-wasip2. When running in wasmtime, use:
The getrandom crate must be configured with features = ["wasi"] for
cryptographic randomness (required for SCRAM auth and TLS).
Tracing
When the tracing feature is enabled, the library emits structured events
at the following levels:
| Level | What gets logged |
|---|---|
| ERROR | Fatal errors: auth failed, TLS handshake failed, reconnection failed |
| WARN | Recoverable problems: connection broken, transaction rolled back, dirty connection discarded |
| INFO | Normal operations: connection established/closed, query completed |
| DEBUG | Detailed info: TCP connect, auth method, pool acquire/release, connection state |
| TRACE | Wire-level detail: every protocol message, full SQL |
⚠️ TRACE may expose sensitive data. Use only in development, never in production.