Spec v0.1 & CI
Normative spec/, JSON schemas, shared fixtures, Cargo & pnpm CI.
Auditstream is an open-source audit trail engine in Rust. Adapters attach the actor as a
trailing sqlcommenter tag, generated triggers record
before and after in
auditstream.changes, and the CLI exports on demand with a lossless
xid8 cursor.
1UPDATE invoice
2SET status = $1
3WHERE id = $2
4/*auditstream='eyJ0eXBlIjoidXNlciIsImlkIjoidXNyXzQyIn0'*/*/ or inject SQL. IDs only — no names, emails, or IPs leak into pg_stat_activity.
Pick a scenario or type any actor.id — even */ DROP TABLE -- — to watch real unpadded base64url encoding, byte-cap enforcement, and PostgreSQL trigger capture in action.
Application code never writes audit rows itself. audit.as() awaits your
writes inside AsyncLocalStorage.run() and appends an unpadded base64url
sqlcommenter tag — supporting humans, API keys, services, and
AI agents acting onBehalfOf a user.
Because comments stay on the statement, standalone Prisma 7 queries need no explicit transaction and never pin connections on RDS Proxy. Malformed payloads never abort your write.
1import { PrismaClient } from "@prisma/client";
2import { audit } from "@auditstream/node";
3import { auditstream } from "@auditstream/prisma";
4
5const db = new PrismaClient({
6 comments: [auditstream()],
7});
8
9await audit.as(
10 {
11 type: "ai_agent",
12 id: "billing-agent",
13 onBehalfOf: { type: "user", id: "usr_42" },
14 tenant: "org_acme",
15 },
16 async () => {
17 await db.invoice.update({
18 where: { id: "inv_901" },
19 data: { status: "paid" },
20 });
21 }
22); auditstream.changes is partitioned by month on occurred_at,
owned by a dedicated NOLOGIN role, and written exclusively through
SECURITY DEFINER triggers. Your application role receives
SELECT only — its INSERT, UPDATE,
and DELETE tamper attempts are rejected by Postgres itself.
INSERT and DELETE fire once per statement via transition tables (REFERENCING NEW/OLD TABLE);
UPDATE stays row-level with WHEN (OLD.* IS DISTINCT FROM NEW.*) so no-op and
excluded-column writes record nothing. Lookups use a partial HASH index on
record_id alongside a monthly BRIN on occurred_at.
Logical replication needs an always-on consumer — a stopped slot retains WAL and can fill your disk.
Auditstream exports pull-based batches on demand using
xact_id >= cursor AND xact_id < pg_snapshot_xmin(),
acknowledging the cursor only after the sink flushes.
Normative RFC 2119 spec with language-neutral conformance fixtures in Rust & TS.
Statement-level INSERT/DELETE (3× faster bulk), row-level UPDATE, TRUNCATE trigger.
Hexagonal Cargo workspace (unsafe_code = forbid). Runs on demand or cron.
JSON Lines & CSV in v0.1; Webhooks, S3, SIEM, and MCP (rmcp) later.
Why teams choose Auditstream over pgaudit, Debezium/CDC, or hand-rolled triggers — backed by
PostgreSQL 19 durable disk (fsync=on) and tmpfs ceiling suites,
PgBouncer 1.26 contention tests, 500k-row CLI scale runs, Node 26 adapter timings, and Neon/Supabase verification.
Read the full Evidence log →
| Requirement | Auditstream (v0.1) | pgaudit | Debezium / CDC (pgoutput) | Hand-rolled / App logging |
|---|---|---|---|---|
| Structured before / after diffs | OLD/NEW jsonb + changed[] | SQL log lines only (no diffs) | Needs REPLICA IDENTITY FULL | Misses direct SQL & migrations |
| User & AI agent attribution | Trailing comment + onBehalfOf | DB role (session_user) only | Needs explicit tx + emit_message | Custom per ORM; leaks under pools |
| New infra & WAL slots | Zero daemon · any PG 15–19 | Extension + log shipping pipeline | wal_level=logical + 24/7 consumer | None (you own schema & ops) |
| Poolers (PgBouncer / Neon) | Pool-safe (0 leaks / 2.4M tx) | Safe (no per-user context) | Dedicated replication conn | Session vars pin RDS Proxy |
| Drift & partition hygiene | auditstream verify --fix in CI | Manual log retention rules | Slot lag alerts (disk-full risk) | Silent drift when tables change |
| Measured write overhead | +3.2% (+5.1 µs) durable UPDATE · 211k/s bulk INSERT | Text log I/O overhead | Lower write CPU · higher WAL | Varies; often row-level slow |
pgbench -M prepared -c 8 -j 4 (3-run medians, tmpfs isolates CPU/PLpgSQL cost from disk fsync). Shaded bar = prior baseline; colored segment = current gain.
Process-wide WeakMap caching per immutable audit.as() scope + O(1) trailing-character scan. Shaded = before; emerald = current latency.
With WAL fsync=on, commit latency dominates: single-row UPDATE adds +3.2% (+5.1 µs) and 10k bulk INSERT takes 60.5 ms (+3.8 µs/row). On tmpfs, bulk 100k INSERT hits 211k/s while row-level 10k UPDATE caps honestly at ~15k/s.
64 clients over 8 PgBouncer 1.26 transaction-mode slots (8× oversubscribed): comment sustains 42.5k–53.5k tps (1.8×–2.4× faster than set_config), with 272,053 mixed leak-test writes and zero crossover. Across 500,000 rows (348 MB), partial hash lookups execute in 0.101 ms (157k concurrent QPS).
Run auditstream verify in CI or cron to catch disabled triggers,
modified trigger functions, rows stuck in the DEFAULT partition, or
missing future monthly partitions — and auto-create upcoming partitions with
--fix.
Read CLI reference →
$auditstream verify --format json
# [ok] public.invoice 4 triggers intact
# [error] public.users disabled (tgenabled = 'D')
# [warn] partitions <2 future months (--fix)
# exit 3 -> blocks CI before an audit gap shipsNormative spec/, JSON schemas, shared fixtures, Cargo & pnpm CI.
Domain types, port traits, cached base64url codec passing shared Rust & TS fixtures.
auditstream-pg, JSONL/CSV sinks, Prisma 7, Drizzle 0.45 & 1.x.
init, status, verify, history, export + Next.js & Hono apps.
Prebuilt binaries via shell/PowerShell installers, Homebrew, npm, and ghcr.io.
rmcp) · Optional WAL replication via pg_logical_emit_message
Single static binary via shell/PowerShell installer, Homebrew, npm, or Docker (ghcr.io). Need a 60-second Docker walkthrough? See Getting Started →
$# macOS / Linux shell installer
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/auditstream/auditstream/releases/latest/download/auditstream-installer.sh | sh
# Or: brew install auditstream/tap/auditstream · npm i -g auditstream · ghcr.io/auditstream/auditstream
Writes the monthly-partitioned auditstream.changes table, partial HASH lookup index, and hybrid triggers for Prisma, Drizzle, or plain SQL.
$auditstream init --migrations prisma \
--table public.invoice --table public.users \
--exclude public.users.password_hash
# partitioned auditstream.changes + partial HASH idxESM-only packages for Node 22.12+, Bun, Deno, and Cloudflare Workers.
$pnpm add @auditstream/node @auditstream/prisma
Query history from the CLI or export JSON Lines / CSV using the commit-safe xid8 cursor.
$auditstream history --table public.invoice --limit 10
auditstream export --format jsonl \
--output audit.jsonl --cursor-file cursor.json
# xid8 window exported · acked after sink flush