# ADR 0016 — Keep Bun.SQL as the PostgreSQL driver

**Status:** accepted. **Date:** 2026-12-03.

## Context
The framework runs on Bun.SQL (ADR 0002). Choosing a driver by preference or performance expectation is not enough:
keeping it required proof of compatibility, features, stability and performance against postgres.js.

## Decision
Keep Bun.SQL. The access layer stays behind `@fellowframework/core` (`sql`, `query`, `transaction`, `afterCommit`,
`dbHealth`, `closeDb`), so the driver remains replaceable without touching modules.

## Evidence (evaluation suite run on Bun 1.4.4 and PostgreSQL 16 with no Node.js process; the suite is not shipped)
- **Compatibility and features** (30/30 checks):
  - Types round-trip: unicode text, bigint above 2^53 (returned as a string), exact numeric, jsonb, text[] with
    commas and quotes, timestamptz with milliseconds, bytea, boolean, null.
  - Row-level security through transaction-local `set_config`: isolation without WHERE, no rows without a tenant,
    cross-tenant write rejected (42501).
  - 200 concurrent tenant transactions on a pool of 4: each sees its own tenant; nothing leaks into later pooled
    queries. A negative control (session-level setting) proves the probe detects a leak.
  - Rollback on throw, savepoints, SQLSTATE on errors (23505, 42601, 57014), `unsafe()` with bound `$1`
    parameters, `set local statement_timeout`, cached statements surviving `ALTER TABLE`, pool recovery after
    `pg_terminate_backend` (negative control: backends were actually killed).
- **Stability** (soak test): 24 workers of mixed transactions, savepoint failures and leak probes, with every
  pooled backend killed every 5 s. 30 s run: 125 549 operations, 0 tenant leaks, 0 unexpected errors (23
  connection errors caused by 30 kills), 15 MB RSS growth, pool usable afterwards.
- **Performance** (same process, same pool of 4, best of 3, against postgres.js 3): Bun.SQL was
  ahead in every run on simple queries (≈1.6–2.7×), tenant transactions (≈1.4–1.7×) and reading 100k rows
  (≈2–3×). Local loopback only; this is a baseline, not a production benchmark.
- **Core suite:** `packages/core/tests/db.test.ts` and `tenancy.test.ts` cover transactions, context and row-level security.

## Consequences
- **Minimum Bun is 1.4.4.** Bun 1.4.4 reworked Bun.SQL and fixed serious defects present before it (transactions
  inside `Promise.all` falling back to autocommit after a ROLLBACK, a `release()` race, the pool not growing back
  after lost connections, leaked server-side prepared statements, a no-op `cancel()`); see the Bun 1.4.4 release
  notes. `fellow doctor` enforces this minimum.
- No migration work. postgres.js is not a dependency of the framework (it was only used for the comparison).
- Errors carry SQLSTATE in `errno` (`PostgresError`), not in `code` as postgres.js does. Code that maps database
  errors must read `errno`.
- Connection drops surface as errors to the in-flight query (the pool reconnects); callers that need retries
  must implement them explicitly.

## Conditions for reassessment
- A Bun release regresses any of the checks above (run `bun test packages` on every Bun upgrade).
- A required feature is missing or broken in Bun.SQL: COPY, LISTEN/NOTIFY under load (`sql.listen` exists but
  is not covered by the evaluation), or a pooler in transaction mode (PgBouncer) breaking transaction-local `set_config`.
  None is used today.
- Production measurements contradict the local baseline.
