@vibeorm/adapter-pg 2.0.2 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -45,7 +45,7 @@ const db = VibeClient({ adapter: pgAdapter({ pool }) });
45
45
  | `statementTimeout` | `number` (ms) | — | Pool-wide `statement_timeout`; a per-transaction `timeout` overrides it |
46
46
  | `connectionTimeout` | `number` (ms) | — | Wait for a pool checkout before failing (pg's `connectionTimeoutMillis`) |
47
47
  | `preparedStatements` | `boolean` | `false` | Send ORM-path queries as named prepared statements. Measured −17…−25 % on reads and −6 % on a by-key update (see below). Off by default because of transaction-mode poolers — turn it on for direct connections and session pooling |
48
- | `stmtCacheMax` | `number` | `1000` | Maximum entries in the prepared-statement name cache (LRU) |
48
+ | `stmtCacheMax` | `number` | `1000` | Budget of prepared-statement names per pool. Names never evict (an evicted name stays prepared on the backend), so this bounds server-side retention per connection; a text arriving after the budget is spent runs unnamed |
49
49
  | `planCacheMode` | `"auto" \| "force_custom_plan" \| "force_generic_plan"` | `"auto"` | PostgreSQL `plan_cache_mode` for every connection of the pool the adapter builds; sent in the `options` startup parameter, merged with an `options` the URL already carries (a URL that sets `plan_cache_mode` itself wins). Refused with `VIBE_CONFIG` next to a caller-supplied `pool` |
50
50
 
51
51
  ### Prepared statements: the measured trade
@@ -61,6 +61,8 @@ Interleaved rounds on this repository's benchmark database (postgres 16, `apps/b
61
61
 
62
62
  The saving is the server's parse/analyze and the protocol Parse/Describe round on every repeat, not the plan — which is why nearly all of it survives `force_custom_plan`, the setting that removes the classic downside of prepared statements (a generic plan after ~5 executions picking worse indexes for range, `LIKE` or skewed filters).
63
63
 
64
+ A name is allocated on a text's **second** sighting, never its first: statement names never evict (an evicted name stays prepared on every backend that saw it), so first-sight allocation let a stream of one-off texts — offset pagination emits a distinct text per page — spend the whole `stmtCacheMax` budget for the pool's lifetime and leave the genuinely hot statements unnamed. A repeated statement therefore runs unprepared exactly once; a one-off statement costs the server nothing.
65
+
64
66
  Why it stays opt-in: named statements live on the **backend** connection. Behind a transaction-mode pooler (PgBouncer, Supavisor, Neon's pooler) that is not configured for protocol-level prepared statements, a client that remembers a statement name from an earlier transaction is handed a different backend later. The outcomes are SQLSTATE `26000` (statement missing), `42P05` (name collision on Parse) or — the one no error reveals — another client's statement executed under the same `vibeorm_N` name when its parameter types happen to be compatible. Enable it only where every connection maps to one backend for the process lifetime.
65
67
 
66
68
  ## Runtime support
@@ -85,6 +87,8 @@ Transactions run on one checked-out client; nested `transaction()` calls become
85
87
  - `serializeParams` (ORM path): plain objects **and** plain arrays are JSON-stringified, since at that point they can only be `json`/`jsonb` values. Scalar-list values are tagged by `formatArrayParam` with `PgArrayParam` and unwrapped, reaching the driver as the untouched array.
86
88
  - `serializeParamsRaw` (raw path, `$queryRaw*` / `$executeRaw*` / DDL): plain JS arrays pass straight through, so `$queryRawUnsafe('… WHERE "id" = ANY($1)', [1, 2, 3])` matches rows. Binding an array *into* a `jsonb` column from a raw query means calling `JSON.stringify` yourself.
87
89
 
90
+ A string bound to a `::jsonb` slot on the raw path is sent as text and parsed by the server, so pre-encoded JSON text stores the document here — on `@vibeorm/adapter-bun` the same statement stores a jsonb *string*, because bun:sql JSON-encodes whatever is bound to a jsonb slot. Spell the slot `::text::jsonb` when a raw statement must run on both drivers; that is what the ORM path renders for `Json` fields (pinned live in `tests/raw-jsonb.live.test.ts` on both adapters).
91
+
88
92
  On both paths a `Date` is spelled as UTC ISO text (`2026-03-08T19:38:08.538Z`) rather than node-postgres' host-local form, and the result side attaches per-query parsers (never `pg.types` globally) that read `timestamp without time zone`, `date` and their arrays as **UTC** — pg-types reads them as host-local time. Together they give zone-less columns (Prisma's `@db.Timestamp(n)` / `@db.Date`) the UTC wall-clock contract shared with bun:sql, PGlite and Prisma, whatever the process `TZ` is; `timestamptz` is unaffected. See `docs/dialect-notes.md` "DateTime without time zone on postgres".
89
93
 
90
94
  ## Error mapping
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAE,KAAK,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAIhE;;;GAGG;AACH,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CA2B1E,CAAC;AAEH,4EAA4E;AAC5E,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAE/D,CAAC;AAYH;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE;IAAE,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG,MAAM,GAAG,SAAS,CAK9E;AAID;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE;IAAE,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG,SAAS,CA0BtE;AAED,wEAAwE;AACxE,wBAAsB,SAAS,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAMnE"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAE,KAAK,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAIhE;;;GAGG;AACH,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAwC1E,CAAC;AAEH,4EAA4E;AAC5E,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAO/D,CAAC;AAYH;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE;IAAE,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG,MAAM,GAAG,SAAS,CAK9E;AAID;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE;IAAE,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG,SAAS,CA0BtE;AAED,wEAAwE;AACxE,wBAAsB,SAAS,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAMnE"}
package/dist/index.d.ts CHANGED
@@ -92,7 +92,15 @@ export type PgAdapterOptions = {
92
92
  * without any error. Turn it on for direct connections and session pooling.
93
93
  */
94
94
  readonly preparedStatements?: boolean;
95
- /** Maximum entries in the prepared-statement name cache (default 1000, LRU). */
95
+ /**
96
+ * Budget of prepared-statement NAMES per pool (default 1000). Names never
97
+ * evict — a name evicted here would stay prepared on every backend that saw
98
+ * it — so this is a bound on the server's retention per connection, and a
99
+ * text arriving after the budget is spent runs unnamed (it still binds its
100
+ * parameters; only the server-side plan cache is skipped). A name is
101
+ * allocated on a text's SECOND sighting, so one-off texts — offset pagination
102
+ * emits one per page — never spend the budget.
103
+ */
96
104
  readonly stmtCacheMax?: number;
97
105
  /**
98
106
  * PostgreSQL `plan_cache_mode` for every connection of the pool this adapter
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAEV,eAAe,EAIhB,MAAM,kBAAkB,CAAC;AAmB1B,OAAO,EAAwC,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAW5F,qFAAqF;AACrF,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;IAC5B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,aAAa,CAAC;CAChC,CAAC;AAE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EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC"}
package/dist/index.js CHANGED
@@ -17,18 +17,25 @@ import pg from "pg";
17
17
  // packages/adapter-pg/src/errors.ts
18
18
  import { VibeError } from "@vibeorm/schema";
19
19
  var PG_SQLSTATE_ERROR_CODES = Object.freeze({
20
+ "23P01": "VIBE_EXCLUSION_VIOLATION",
20
21
  "23505": "VIBE_UNIQUE_VIOLATION",
21
22
  "23503": "VIBE_FK_VIOLATION",
23
+ "23001": "VIBE_FK_VIOLATION",
22
24
  "23502": "VIBE_VALIDATION",
23
25
  "23514": "VIBE_VALIDATION",
24
26
  "22P02": "VIBE_VALIDATION",
25
27
  "42501": "VIBE_PERMISSION_DENIED",
26
28
  "40001": "VIBE_TRANSACTION",
27
29
  "40P01": "VIBE_TRANSACTION",
28
- "57014": "VIBE_TRANSACTION"
30
+ "57014": "VIBE_TRANSACTION",
31
+ "55P03": "VIBE_LOCK_NOT_AVAILABLE"
29
32
  });
30
33
  var PG_SQLSTATE_REASONS = Object.freeze({
31
- "57014": "timeout"
34
+ "23P01": "exclusion-violation",
35
+ "57014": "timeout",
36
+ "55P03": "lock-not-available",
37
+ "40P01": "deadlock",
38
+ "40001": "serialization-failure"
32
39
  });
33
40
  var SQLSTATE_PATTERN = /^[0-9A-Z]{5}$/;
34
41
  function readStringField(params) {
@@ -177,9 +184,14 @@ function buildPoolConfig(params) {
177
184
  }
178
185
 
179
186
  // packages/adapter-pg/src/statement-cache.ts
187
+ var DEFAULT_PROMOTE_AFTER = 2;
188
+ var SIGHTINGS_PER_NAME = 4;
180
189
  function createStatementNameCache(params) {
181
190
  const max = Math.max(0, params.max);
191
+ const promoteAfter = Math.max(1, params.promoteAfter ?? DEFAULT_PROMOTE_AFTER);
192
+ const sightingsMax = max * SIGHTINGS_PER_NAME;
182
193
  const names = new Map;
194
+ const sightings = new Map;
183
195
  let counter = 0;
184
196
  return {
185
197
  getName(nameParams) {
@@ -189,12 +201,28 @@ function createStatementNameCache(params) {
189
201
  return existing;
190
202
  if (names.size >= max)
191
203
  return;
204
+ const seen = (sightings.get(text) ?? 0) + 1;
205
+ if (seen < promoteAfter) {
206
+ sightings.delete(text);
207
+ sightings.set(text, seen);
208
+ while (sightings.size > sightingsMax) {
209
+ const oldest = sightings.keys().next();
210
+ if (oldest.done === true)
211
+ break;
212
+ sightings.delete(oldest.value);
213
+ }
214
+ return;
215
+ }
216
+ sightings.delete(text);
192
217
  const name = `vibeorm_${counter++}`;
193
218
  names.set(text, name);
194
219
  return name;
195
220
  },
196
221
  size() {
197
222
  return names.size;
223
+ },
224
+ sightings() {
225
+ return sightings.size;
198
226
  }
199
227
  };
200
228
  }
@@ -579,4 +607,4 @@ export {
579
607
  DEFAULT_DEADLINE_CLOCK
580
608
  };
581
609
 
582
- //# debugId=79BF9E77B467A34F64756E2164756E21
610
+ //# debugId=990A1E48379577DE64756E2164756E21
package/dist/index.js.map CHANGED
@@ -2,14 +2,14 @@
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts", "../src/errors.ts", "../src/params.ts", "../src/pool-config.ts", "../src/statement-cache.ts", "../src/transaction-sql.ts"],
4
4
  "sourcesContent": [
5
- "/**\n * @vibeorm/adapter-pg — node-postgres (`pg`) adapter for VibeORM v2.\n *\n * Runs on Bun and Node. Owns pooling, optional named prepared statements,\n * transactions (nested → savepoints on the same client) and driver-error\n * mapping. Only `VibeError` ever escapes.\n */\n\nimport type {\n AdapterBudgetSupport,\n DatabaseAdapter,\n QueryResult,\n TransactionOptions,\n WireFidelity,\n} from \"@vibeorm/runtime\";\nimport {\n createSavepointScope,\n engineStatementBudgetMs,\n isTransactionDeadlineError,\n runNestedSavepoint,\n runRootScope,\n type SavepointScope,\n scopeTurn,\n startTransactionBudget,\n type TransactionBudget,\n transactionOutcomeUnknownError,\n validateTransactionDeadline,\n} from \"@vibeorm/runtime/adapter-kit\";\nimport { VibeError } from \"@vibeorm/schema\";\nimport type { Dialect, Provider } from \"@vibeorm/schema\";\nimport pg from \"pg\";\nimport { mapPgDriverError, runMapped } from \"./errors.ts\";\nimport { PgArrayParam, serializeParams, serializeParamsRaw } from \"./params.ts\";\nimport { assertPlanCacheMode, buildPoolConfig, type PlanCacheMode } from \"./pool-config.ts\";\nimport { createStatementNameCache, type StatementNameCache } from \"./statement-cache.ts\";\nimport {\n beginStatement,\n classifyRawTransactionControl,\n refuseNestedTransactionOptions,\n statementTimeoutStatement,\n} from \"./transaction-sql.ts\";\n\n// ─── Minimal pg surface ───────────────────────────────────────────\n\n/** The single `pg` query form this adapter uses (config object, never overloads). */\nexport type PgQueryConfig = {\n readonly text: string;\n readonly values?: unknown[];\n readonly name?: string;\n /**\n * QUERY-LOCAL result parsers ({@link EXACT_TYPES}). node-postgres reads this\n * off the query config, so nothing outside VibeORM's own statements is\n * affected — a caller's pool keeps the driver's documented behaviour.\n */\n readonly types?: PgTypeParsers;\n};\n\n/** The `pg-types` surface a query config may carry. */\nexport type PgTypeParsers = {\n getTypeParser(oid: number, format?: \"text\" | \"binary\"): (value: string) => unknown;\n};\n\n/** The parts of a `pg` result this adapter reads. */\nexport type PgQueryResultLike = {\n readonly rows: Record<string, unknown>[];\n readonly rowCount: number | null;\n};\n\n/** The parts of a `pg` PoolClient this adapter uses. */\nexport type PgPoolClientLike = {\n query(config: PgQueryConfig): Promise<PgQueryResultLike>;\n /**\n * Hand the client back. A TRUTHY argument tells node-postgres to DESTROY it\n * instead of pooling it — used when a rollback failed and the session state\n * can no longer be asserted (EPIC 6). Optional so existing test doubles that\n * declare `release(): void` still satisfy this type.\n */\n release(destroy?: boolean): void;\n /**\n * EventEmitter surface. A real `pg.PoolClient` has it; test doubles need not,\n * so both members are optional and the adapter feature-detects them.\n */\n on?(event: \"error\", listener: (error: unknown) => void): void;\n removeListener?(event: \"error\", listener: (error: unknown) => void): void;\n};\n\n/** The parts of a `pg` Pool this adapter uses — satisfied by a real `pg.Pool`. */\nexport type PgPoolLike = {\n connect(): Promise<PgPoolClientLike>;\n query(config: PgQueryConfig): Promise<PgQueryResultLike>;\n end(): Promise<void>;\n /** EventEmitter surface; optional for the same reason as on {@link PgPoolClientLike}. */\n on?(event: \"error\", listener: (error: unknown, client: PgPoolClientLike) => void): void;\n};\n\n/** Anything that can run a query: the pool itself or a checked-out client. */\ntype Queryable = { query(config: PgQueryConfig): Promise<PgQueryResultLike> };\n\n/**\n * Keep a CHECKED-OUT client's background failures from killing the process,\n * returning the detach to run before the client goes back to the pool.\n *\n * node-postgres listens for `'error'` on a pooled client only while that client\n * sits IDLE, and removes the listener the moment the client is checked out\n * (restoring it on release). A client this adapter holds — for a transaction,\n * or as a pinned raw session — therefore has NO listener, so a backend that\n * dies mid-transaction reaches `client.emit('error', …)` with none. An\n * `'error'` event without a listener is an uncaught exception: fatal under\n * Node, which turns a database failover into a dead application process.\n *\n * The listener deliberately does nothing with the error and is NOT an error\n * channel: the in-flight query already rejects on its own with the mapped\n * `VibeError`, and the pool discards the broken client. Its only job is to keep\n * the process alive. Detaching before `release()` matters just as much — a\n * listener left attached would shadow pg's own idle listener and stop the pool\n * evicting a client that fails later, while idle.\n */\n/**\n * Prepared-statement name registries, one per POOL (F16/F18) — see\n * ./statement-cache.ts for why they are shared and bounded. Weakly held, so a\n * pool that goes out of scope takes its registry with it.\n */\nconst POOL_STATEMENT_NAMES: WeakMap<PgPoolLike, StatementNameCache> = new WeakMap();\n\nfunction guardBackgroundErrors(params: { client: PgPoolClientLike }): () => void {\n const { client } = params;\n if (typeof client.on !== \"function\") return (): void => undefined;\n const swallow = (): void => {\n /* the failing call rejects with a mapped VibeError; nothing to report here */\n };\n client.on(\"error\", swallow);\n return (): void => {\n client.removeListener?.(\"error\", swallow);\n };\n}\n\n// ─── Options ──────────────────────────────────────────────────────\n\n/**\n * Options for {@link pgAdapter}. Either point it at a connection string (the\n * adapter then owns the pool it creates) or hand it a pool you already own.\n */\nexport type PgAdapterOptions = {\n /** PostgreSQL connection URL. Defaults to `process.env.DATABASE_URL`. */\n readonly connectionString?: string;\n /**\n * An existing `pg.Pool` to use instead of creating one. The adapter never\n * ends a pool it did not create, so `disconnect()` is a no-op in this mode.\n */\n readonly pool?: PgPoolLike;\n /** Maximum pooled connections (default 10). Ignored when `pool` is given. */\n readonly max?: number;\n /**\n * Pool-wide `statement_timeout` in milliseconds. Every connection inherits\n * it; a per-transaction `timeout` overrides it for that transaction.\n */\n readonly statementTimeout?: number;\n /** Milliseconds to wait for a pool checkout before failing (pg's `connectionTimeoutMillis`). */\n readonly connectionTimeout?: number;\n /**\n * Send ORM-path queries as named prepared statements (default false).\n *\n * Measured on this repo's benchmark (postgres 16, interleaved rounds):\n * −17…−25 % on reads, −6 % on a by-key update — the server skips the\n * parse/analyze and the protocol Parse/Describe round on every repeat. The\n * gain survives {@link PgAdapterOptions.planCacheMode} `force_custom_plan`,\n * which removes the classic downside (a generic plan after ~5 executions\n * picking worse indexes for range/LIKE/skewed filters).\n *\n * Off by default because named statements live on the BACKEND connection:\n * behind a transaction-mode pooler (PgBouncer, Supavisor, Neon's pooler)\n * without prepared-statement support a client that remembers a name from an\n * earlier transaction is handed another backend later — SQLSTATE 26000,\n * 42P05, or, worst, another client's statement under the same name executed\n * without any error. Turn it on for direct connections and session pooling.\n */\n readonly preparedStatements?: boolean;\n /** Maximum entries in the prepared-statement name cache (default 1000, LRU). */\n readonly stmtCacheMax?: number;\n /**\n * PostgreSQL `plan_cache_mode` for every connection of the pool this adapter\n * builds (default `\"auto\"`, the server default — nothing is sent). Pair\n * `\"force_custom_plan\"` with {@link PgAdapterOptions.preparedStatements}: the\n * statement is still prepared once, but planned with the real parameter\n * values on every execution. Rides the `options` startup parameter, merged\n * with whatever the connection string already carries (a URL that sets\n * `plan_cache_mode` itself wins). Refused with `VIBE_CONFIG` together with\n * a caller-supplied `pool` — the adapter never reconfigures a pool it does\n * not own; set it on that pool's own `options` instead.\n */\n readonly planCacheMode?: PlanCacheMode;\n};\n\n// ─── Adapter ──────────────────────────────────────────────────────\n\nconst DIALECT: Dialect = \"postgres\";\nconst PROVIDER: Provider = \"postgresql\";\n\n/**\n * node-postgres wire (board #30, live-verified on both execute paths):\n * timestamptz → `Date`, json/jsonb → parsed values, int4/float8 → numbers.\n * (int8 stays a string — BigInt decoding is real work and never pruned.)\n */\nconst WIRE: WireFidelity = Object.freeze({ dateTime: \"native\", json: \"parsed\", numbers: \"native\" } as const);\n\n/**\n * Timing budgets this adapter genuinely enforces (EPIC 6).\n *\n * - `acquireTimeout: true` — {@link PgAdapterOptions.connectionTimeout} maps to\n * node-postgres' `connectionTimeoutMillis`, which bounds the checkout wait\n * and rejects when the pool stays exhausted. The capability is declared here;\n * it is OFF until the option is set (pg's own default is \"wait forever\").\n * - `statementTimeout: \"all-statements\"` — PostgreSQL's `statement_timeout`\n * cancels ANY over-long statement, writes included (SQLSTATE 57014).\n * - `transactionDeadline: \"cancel-running-statements\"` — the strongest level:\n * the remaining budget is handed to the SERVER before each statement, so the\n * database itself stops work that would outlive the deadline. Nothing here is\n * a `Promise.race` over a statement that keeps running.\n */\nconst BUDGETS: AdapterBudgetSupport = Object.freeze({\n acquireTimeout: true,\n statementTimeout: \"all-statements\",\n transactionDeadline: \"cancel-running-statements\",\n} as const);\n\n// ─── Exact numeric arrays (F12) ───────────────────────────────\n\n/** `_numeric`: the array form of `numeric`/`DECIMAL`. */\nconst NUMERIC_ARRAY_OID = 1231;\n/** `_text`: its parser splits a postgres array literal into RAW element strings. */\nconst TEXT_ARRAY_OID = 1009;\n/** `timestamp without time zone` and `date` — the zone-less types — and their array forms. */\nconst TIMESTAMP_OID = 1114;\nconst TIMESTAMP_ARRAY_OID = 1115;\nconst DATE_OID = 1082;\nconst DATE_ARRAY_OID = 1182;\n\n/**\n * `pg.types.getTypeParser` behind a plain numeric-OID signature. `@types/pg`\n * narrows the two-argument overload to its own `TypeId` union of well-known\n * OIDs, which does not include every array OID this function must look up.\n */\nconst driverTypeParser: (oid: number, format?: \"text\" | \"binary\") => (value: string) => unknown =\n pg.types.getTypeParser as unknown as (oid: number, format?: \"text\" | \"binary\") => (value: string) => unknown;\n\n/** postgres' text output for the zone-less types: `2026-03-08 19:38:08.538` or `2026-03-08`. */\nconst ZONE_LESS_TEXT = /^\\d{4}-\\d{2}-\\d{2}(?: \\d{2}:\\d{2}:\\d{2}(?:\\.\\d{1,6})?)?$/;\n\n/**\n * Zone-less date/timestamp text read as **UTC** — the same rule as the codec\n * table's `parseDateTimeText` (@vibeorm/sql), applied at the driver level\n * because node-postgres hands these columns to the ORM as `Date` objects\n * already (`wire.dateTime: \"native\"` prunes the codec decode). pg-types reads\n * them as host-LOCAL time, so one stored row decoded to a different instant\n * on every host TZ, and to a different instant than bun:sql, PGlite's ORM\n * path and Prisma (all UTC) return. `infinity`, BC dates and anything else\n * outside the plain form keep the driver's own parser.\n */\nfunction parseZoneLessAsUtc(value: string): unknown {\n if (ZONE_LESS_TEXT.test(value)) {\n const iso = value.includes(\" \") ? `${value.replace(\" \", \"T\").replace(/(\\.\\d{3})\\d+$/, \"$1\")}Z` : value;\n const parsed = new Date(iso);\n if (!Number.isNaN(parsed.getTime())) return parsed;\n }\n return driverTypeParser(TIMESTAMP_OID, \"text\")(value);\n}\n\n/** The array forms: the driver's `_text` parser splits the literal, elements go through {@link parseZoneLessAsUtc}. */\nfunction parseZoneLessArrayAsUtc(value: string): unknown {\n const mapElements = (element: unknown): unknown => {\n if (element === null) return null;\n if (Array.isArray(element)) return element.map(mapElements);\n return parseZoneLessAsUtc(String(element));\n };\n return mapElements(driverTypeParser(TEXT_ARRAY_OID, \"text\")(value));\n}\n\n/**\n * Query-local type parsers that keep `numeric[]` EXACT and read zone-less\n * timestamps as UTC.\n *\n * pg-types decodes `_numeric` with `parseFloat` per element, so\n * `9007199254740993` comes back as `9007199254740992` and `12.340` as the\n * number `12.34` — while the SCALAR `numeric` correctly stays a string and\n * VibeORM's Decimal contract is \"string in, string out\" on every dialect. The\n * array is the anomaly, and the loss happens inside the driver, before any\n * codec can see the value.\n *\n * The fix reuses the driver's OWN array-literal parser (`_text`), so array\n * syntax, quoting and NULL elements stay node-postgres' business; only the\n * per-element float conversion is dropped. It is attached PER QUERY (F12's\n * acceptance criterion), never by mutating `pg.types` globally: an injected\n * pool is shared property, and a global override would silently change every\n * other consumer in the process. The zone-less parsers ride the same hook for\n * the same reason.\n */\nconst EXACT_TYPES: PgTypeParsers = {\n getTypeParser(oid: number, format?: \"text\" | \"binary\"): (value: string) => unknown {\n if (format === undefined || format === \"text\") {\n if (oid === NUMERIC_ARRAY_OID) return driverTypeParser(TEXT_ARRAY_OID, \"text\");\n if (oid === TIMESTAMP_OID || oid === DATE_OID) return parseZoneLessAsUtc;\n if (oid === TIMESTAMP_ARRAY_OID || oid === DATE_ARRAY_OID) return parseZoneLessArrayAsUtc;\n }\n return driverTypeParser(oid, format);\n },\n};\n\n/**\n * Create a VibeORM adapter backed by node-postgres.\n *\n * @example\n * ```ts\n * const adapter = pgAdapter({ connectionString: process.env.DATABASE_URL, max: 20 });\n * await adapter.connect();\n * const rows = await adapter.execute({ text: 'SELECT * FROM \"User\" WHERE \"id\" = $1', values: [1] });\n * ```\n *\n * @example Reusing a pool you already own\n * ```ts\n * import pg from \"pg\";\n * const pool = new pg.Pool({ connectionString });\n * const adapter = pgAdapter({ pool });\n * ```\n */\nexport function pgAdapter(options?: PgAdapterOptions): DatabaseAdapter {\n const usePrepared = options?.preparedStatements ?? false;\n const stmtCacheMax = options?.stmtCacheMax ?? 1000;\n const planCacheMode = options?.planCacheMode === undefined ? \"auto\" : assertPlanCacheMode({ value: options.planCacheMode });\n if (planCacheMode !== \"auto\" && options?.pool !== undefined) {\n throw new VibeError({\n code: \"VIBE_CONFIG\",\n message:\n \"planCacheMode configures the pool the adapter builds — it cannot be applied to a caller-supplied `pool`; \" +\n \"set `options: \\\"-c plan_cache_mode=…\\\"` on that pool instead\",\n meta: { planCacheMode },\n });\n }\n\n let pool: PgPoolLike | null = null;\n let ownsPool = false;\n\n /**\n * The checked-out client of an OPEN raw transaction — session affinity for\n * the raw path. @vibeorm/migrate wraps transactional DDL in literal `BEGIN`\n * / `COMMIT` / `ROLLBACK` sent through `executeUnsafe`; without pinning,\n * `pool.query(\"BEGIN\")` sequences stay on one client only by LIFO checkout\n * luck, and any concurrent traffic silently splits the transaction across\n * connections. A pure `BEGIN` checks a client out, every raw statement until\n * the matching `COMMIT`/`ROLLBACK` rides it, and the close releases it. The\n * ORM path (`execute`) and `transaction()` stay on the pool. Concurrent raw\n * transactions on one adapter are unsupported.\n */\n let rawSession: PgPoolClientLike | null = null;\n /** Detach for {@link rawSession}'s background-error guard; runs before its release. */\n let rawSessionUnguard: (() => void) | null = null;\n\n function getPool(): PgPoolLike {\n if (pool !== null) return pool;\n\n if (options?.pool !== undefined) {\n pool = options.pool;\n ownsPool = false;\n return pool;\n }\n\n const config = buildPoolConfig({\n connectionString: options?.connectionString ?? process.env.DATABASE_URL,\n max: options?.max ?? 10,\n ...(options?.statementTimeout === undefined ? {} : { statementTimeout: options.statementTimeout }),\n ...(options?.connectionTimeout === undefined ? {} : { connectionTimeout: options.connectionTimeout }),\n planCacheMode,\n });\n\n const PoolCtor = pg.Pool as unknown as new (poolConfig: Record<string, unknown>) => PgPoolLike;\n pool = new PoolCtor(config);\n /**\n * The IDLE half of the same hazard {@link guardBackgroundErrors} covers: a\n * pooled client that loses its socket while idle makes pg re-emit on the\n * POOL, and an unhandled `'error'` there is equally fatal. The pool already\n * evicts that client, so this listener only keeps the process alive.\n *\n * Attached ONLY to a pool this adapter constructed. A caller-supplied pool\n * (the `pool` option) has its own owner: the adapter never ends it either,\n * node-postgres documents attaching an `'error'` listener as that owner's\n * job, and registering one here that is never removed would stack up a\n * listener per adapter sharing that pool. The dangerous case — a client\n * checked out for a transaction or a raw session, which pg leaves\n * unlistened by construction — is guarded in BOTH modes.\n */\n pool.on?.(\"error\", () => {\n /* the pool has already discarded the failed client */\n });\n ownsPool = true;\n return pool;\n }\n\n /**\n * Prepared-statement names for the pool this adapter runs on (F18): the\n * registry is keyed by the POOL, so two adapters sharing one pool share one\n * name space instead of both starting at `vibeorm_0` and colliding on a\n * connection. It also bounds how many names that pool can ever put on a\n * backend (F16) no matter how many adapters come and go over it.\n */\n function statementNamesFor(params: { pool: PgPoolLike }): StatementNameCache {\n const existing = POOL_STATEMENT_NAMES.get(params.pool);\n if (existing !== undefined) return existing;\n const created = createStatementNameCache({ max: stmtCacheMax });\n POOL_STATEMENT_NAMES.set(params.pool, created);\n return created;\n }\n\n /** Build the config for one ORM-path query (statement name only when enabled). */\n function buildQueryConfig(params: { text: string; values: unknown[] }): PgQueryConfig {\n const values = serializeParams({ values: params.values });\n if (!usePrepared) return { text: params.text, values, types: EXACT_TYPES };\n // `undefined` once the pool's name budget is spent: the statement runs\n // unnamed rather than growing the server's prepared-statement retention.\n const name = statementNamesFor({ pool: getPool() }).getName({ text: params.text });\n return name === undefined\n ? { text: params.text, values, types: EXACT_TYPES }\n : { name, text: params.text, values, types: EXACT_TYPES };\n }\n\n async function runExecute(params: {\n queryable: Queryable;\n text: string;\n values: unknown[];\n }): Promise<Record<string, unknown>[]> {\n const config = buildQueryConfig({ text: params.text, values: params.values });\n const result = await runMapped(() => params.queryable.query(config));\n return result.rows;\n }\n\n async function runExecuteUnsafe(params: {\n queryable: Queryable;\n text: string;\n values?: unknown[];\n }): Promise<QueryResult> {\n const values = params.values === undefined ? undefined : serializeParamsRaw({ values: params.values });\n const result = await runMapped(() => params.queryable.query({ text: params.text, values, types: EXACT_TYPES }));\n return { rows: result.rows, affectedRows: result.rowCount ?? 0 };\n }\n\n /**\n * Adapter bound to one checked-out client that is already inside a\n * transaction. Nested `transaction()` calls open savepoints on that same\n * client, and their LIFETIMES are serialized by the savepoint gate (F14):\n * overlapping siblings share one savepoint stack, so the earlier one's\n * rollback would erase the later one's already released work.\n */\n function createClientAdapter(params: {\n client: PgPoolClientLike;\n scope: SavepointScope;\n budget: TransactionBudget;\n }): DatabaseAdapter {\n const { client, scope, budget } = params;\n\n /**\n * Hand the SERVER the remaining budget before a statement runs, when the\n * caller bought `\"cancel-running-statements\"`. `SET LOCAL` is\n * transaction-scoped, so PostgreSQL reverts it at COMMIT/ROLLBACK and the\n * connection returns to the pool with its session settings intact.\n *\n * Costs one extra round trip per statement, and only while a deadline is\n * active — the price of a deadline the database itself enforces.\n */\n async function applyEngineBudget(): Promise<void> {\n const ms = engineStatementBudgetMs({ budget });\n if (ms === null) return;\n await runMapped(() => client.query({ text: statementTimeoutStatement({ timeout: ms }) }));\n }\n\n return {\n dialect: DIALECT,\n provider: PROVIDER,\n wire: WIRE,\n budgets: BUDGETS,\n\n async execute(execParams) {\n const turn = scopeTurn({ scope });\n if (turn !== undefined) await turn;\n // After the turn, not before: waiting for a sibling savepoint spends\n // the same budget the statement is about to run against.\n budget.assertUsable({ stage: \"statement\" });\n await applyEngineBudget();\n return runExecute({ queryable: client, ...execParams });\n },\n\n async executeUnsafe(execParams) {\n const turn = scopeTurn({ scope });\n if (turn !== undefined) await turn;\n budget.assertUsable({ stage: \"statement\" });\n await applyEngineBudget();\n return runExecuteUnsafe({ queryable: client, ...execParams });\n },\n\n async transaction<T>(\n fn: (txAdapter: DatabaseAdapter) => Promise<T>,\n nestedOptions?: TransactionOptions,\n ): Promise<T> {\n refuseNestedTransactionOptions({ options: nestedOptions, provider: PROVIDER });\n budget.assertUsable({ stage: \"nested\" });\n return runNestedSavepoint({\n scope,\n execute: (statement) => runMapped(() => client.query({ text: statement })),\n // The child shares the ROOT budget by reference: a savepoint never\n // gets an independent clock or session setting.\n body: (child) => fn(createClientAdapter({ client, scope: child, budget })),\n });\n },\n\n async connect(): Promise<void> {\n // Already connected — this adapter is bound to a live client.\n },\n\n async disconnect(): Promise<void> {\n // The pool owns the connection lifecycle; releasing happens at the\n // top-level transaction boundary.\n },\n\n formatArrayParam(values: unknown[]): unknown {\n return new PgArrayParam(values);\n },\n };\n }\n\n return {\n dialect: DIALECT,\n provider: PROVIDER,\n wire: WIRE,\n budgets: BUDGETS,\n\n async execute(params) {\n return runExecute({ queryable: getPool(), ...params });\n },\n\n async executeUnsafe(params) {\n const control = classifyRawTransactionControl({ text: params.text });\n if (rawSession !== null) {\n if (control === \"close\") {\n const session = rawSession;\n const unguard = rawSessionUnguard;\n rawSession = null;\n rawSessionUnguard = null;\n try {\n return await runExecuteUnsafe({ queryable: session, ...params });\n } finally {\n unguard?.();\n session.release();\n }\n }\n // A nested raw BEGIN lands here too — postgres treats it as a warning\n // no-op on the open session, exactly like a single connection would.\n return runExecuteUnsafe({ queryable: rawSession, ...params });\n }\n if (control === \"open\") {\n const client = await runMapped(() => getPool().connect());\n const unguard = guardBackgroundErrors({ client });\n try {\n const result = await runExecuteUnsafe({ queryable: client, ...params });\n rawSession = client;\n rawSessionUnguard = unguard;\n return result;\n } catch (error) {\n unguard();\n client.release();\n throw error;\n }\n }\n return runExecuteUnsafe({ queryable: getPool(), ...params });\n },\n\n async transaction<T>(\n fn: (txAdapter: DatabaseAdapter) => Promise<T>,\n transactionOptions?: TransactionOptions,\n ): Promise<T> {\n // Refuse an unsupportable budget BEFORE checking a connection out, so a\n // bad option never costs a checkout and never leaves a transaction open.\n validateTransactionDeadline({\n options: transactionOptions,\n support: BUDGETS.transactionDeadline,\n provider: PROVIDER,\n });\n const client = await runMapped(() => getPool().connect());\n const unguard = guardBackgroundErrors({ client });\n const scope = createSavepointScope();\n // The deadline clock starts HERE: the connection is already in hand, so\n // acquisition (bounded separately by `connectionTimeout`) is not charged\n // against the caller's transaction budget.\n const budget = startTransactionBudget({ options: transactionOptions, provider: PROVIDER });\n /** Set when the session's state can no longer be asserted (see below). */\n let discard = false;\n try {\n await runMapped(() =>\n client.query({ text: beginStatement({ isolationLevel: transactionOptions?.isolationLevel }) }),\n );\n if (transactionOptions?.timeout !== undefined) {\n const text = statementTimeoutStatement({ timeout: transactionOptions.timeout });\n await runMapped(() => client.query({ text }));\n }\n const result = await runRootScope({\n scope,\n body: () => fn(createClientAdapter({ client, scope, budget })),\n });\n // An expired budget must never commit, however the callback ended.\n budget.assertUsable({ stage: \"commit\" });\n await runMapped(() => client.query({ text: \"COMMIT\" }));\n budget.close({ outcome: \"committed\" });\n return result;\n } catch (error) {\n try {\n await client.query({ text: \"ROLLBACK\" });\n budget.close({ outcome: \"rolled-back\" });\n } catch {\n // The rollback that should have cleaned up failed: this connection may\n // still be inside a transaction with `SET LOCAL` settings that were\n // never reverted, so it is DESTROYED rather than pooled, and the\n // commit state is reported as genuinely unknown.\n discard = true;\n budget.close({ outcome: \"unknown\" });\n if (budget.totalMs !== null && isTransactionDeadlineError(error)) {\n throw transactionOutcomeUnknownError({\n provider: PROVIDER,\n totalMs: budget.totalMs,\n cause: error,\n });\n }\n }\n throw error;\n } finally {\n unguard();\n client.release(discard);\n }\n },\n\n async connect(): Promise<void> {\n const client = await runMapped(() => getPool().connect());\n client.release();\n },\n\n async disconnect(): Promise<void> {\n if (rawSession !== null) {\n // An abandoned raw transaction must not leak an in-transaction client;\n // roll it back best-effort before the pool ends.\n const session = rawSession;\n const unguard = rawSessionUnguard;\n rawSession = null;\n rawSessionUnguard = null;\n try {\n await session.query({ text: \"ROLLBACK\" });\n } catch {\n /* the pool is closing; the session is going away regardless */\n }\n unguard?.();\n session.release();\n }\n if (pool !== null && ownsPool) {\n const closing = pool;\n pool = null;\n await runMapped(() => closing.end());\n }\n },\n\n formatArrayParam(values: unknown[]): unknown {\n return new PgArrayParam(values);\n },\n };\n}\n\n// ─── Re-exports (driver-error table, markers, helpers) ────────────\n\nexport { PG_SQLSTATE_ERROR_CODES, PG_SQLSTATE_REASONS, mapPgDriverError, pgErrorSqlstate } from \"./errors.ts\";\nexport { PgArrayParam, serializeParams, serializeParamsRaw } from \"./params.ts\";\nexport { createSavepointCounter, nextSavepointName } from \"@vibeorm/runtime/adapter-kit\";\nexport type { SavepointCounter } from \"@vibeorm/runtime/adapter-kit\";\nexport { createStatementNameCache } from \"./statement-cache.ts\";\nexport type { StatementNameCache } from \"./statement-cache.ts\";\nexport { buildPoolConfig, placePlanCacheMode } from \"./pool-config.ts\";\nexport type { PlanCacheMode, StartupOptionsPlacement } from \"./pool-config.ts\";\nexport {\n ISOLATION_LEVEL_SQL,\n beginStatement,\n classifyRawTransactionControl,\n refuseNestedTransactionOptions,\n statementTimeoutStatement,\n} from \"./transaction-sql.ts\";\nexport type { RawTransactionControl } from \"./transaction-sql.ts\";\n// ─── EPIC 6: transaction budgets (shared, @vibeorm/runtime/adapter-kit) ───\nexport {\n DEFAULT_DEADLINE_CLOCK,\n engineStatementBudgetMs,\n isTransactionDeadlineError,\n startTransactionBudget,\n transactionClosedError,\n transactionDeadlineError,\n transactionOutcomeUnknownError,\n validateTransactionDeadline,\n} from \"@vibeorm/runtime/adapter-kit\";\nexport type { BudgetStage, DeadlineClock, TransactionBudget, TransactionOutcome } from \"@vibeorm/runtime/adapter-kit\";\n",
6
- "/**\n * Driver-error mapping for node-postgres (`pg`).\n *\n * Constitution rule 5: a raw driver error never escapes an adapter. Every\n * failure becomes a `VibeError` with a stable code, the driver error attached\n * as `cause`, and the SQLSTATE (plus constraint/table/column when the server\n * reported them) in `meta`.\n */\n\nimport { VibeError, type VibeErrorCode } from \"@vibeorm/schema\";\n\n// ─── SQLSTATE table ───────────────────────────────────────────────\n\n/**\n * PostgreSQL SQLSTATE → stable VibeError code. Frozen and mirrored by tests;\n * anything absent from this table maps to `VIBE_ADAPTER`.\n */\nexport const PG_SQLSTATE_ERROR_CODES: Readonly<Record<string, VibeErrorCode>> = Object.freeze({\n /** unique_violation */\n \"23505\": \"VIBE_UNIQUE_VIOLATION\",\n /** foreign_key_violation */\n \"23503\": \"VIBE_FK_VIOLATION\",\n /** not_null_violation */\n \"23502\": \"VIBE_VALIDATION\",\n /** check_violation */\n \"23514\": \"VIBE_VALIDATION\",\n /**\n * invalid_text_representation — a value the server cannot parse into the\n * column's type (out-of-range enum value, `\"abc\"` for an integer). Bad input,\n * so the same code sqlite reports for its enum CHECK violation.\n */\n \"22P02\": \"VIBE_VALIDATION\",\n /**\n * insufficient_privilege — a denied table/column privilege, and the SQLSTATE a\n * native row-level-security policy raises when its WITH CHECK refuses a write.\n * Callers identify it by this code, never by message text.\n */\n \"42501\": \"VIBE_PERMISSION_DENIED\",\n /** serialization_failure */\n \"40001\": \"VIBE_TRANSACTION\",\n /** deadlock_detected */\n \"40P01\": \"VIBE_TRANSACTION\",\n /** query_canceled — statement_timeout fired */\n \"57014\": \"VIBE_TRANSACTION\",\n});\n\n/** SQLSTATEs that additionally explain themselves through `meta.reason`. */\nexport const PG_SQLSTATE_REASONS: Readonly<Record<string, string>> = Object.freeze({\n \"57014\": \"timeout\",\n});\n\n// ─── Reading driver errors ────────────────────────────────────────\n\n/** A PostgreSQL SQLSTATE is exactly five digits/uppercase letters (e.g. `40P01`). */\nconst SQLSTATE_PATTERN = /^[0-9A-Z]{5}$/;\n\nfunction readStringField(params: { source: Record<string, unknown>; key: string }): string | undefined {\n const value = params.source[params.key];\n return typeof value === \"string\" && value.length > 0 ? value : undefined;\n}\n\n/**\n * Extract the SQLSTATE from a pg driver error. `pg` puts the server's SQLSTATE\n * on `error.code`; client-side failures (`ECONNREFUSED`, `ETIMEDOUT`, …) use\n * the same field for a non-SQLSTATE string, which this rejects.\n */\nexport function pgErrorSqlstate(params: { error: unknown }): string | undefined {\n const { error } = params;\n if (error === null || typeof error !== \"object\") return undefined;\n const code = readStringField({ source: error as Record<string, unknown>, key: \"code\" });\n return code !== undefined && SQLSTATE_PATTERN.test(code) ? code : undefined;\n}\n\n// ─── Mapping ──────────────────────────────────────────────────────\n\n/**\n * Map any error thrown by `pg` onto a `VibeError`.\n *\n * A `VibeError` passes through unchanged, so an error raised deeper inside the\n * ORM (or by a nested adapter call) is never re-wrapped.\n */\nexport function mapPgDriverError(params: { error: unknown }): VibeError {\n const { error } = params;\n if (error instanceof VibeError) return error;\n\n const source: Record<string, unknown> =\n error !== null && typeof error === \"object\" ? (error as Record<string, unknown>) : {};\n const sqlstate = pgErrorSqlstate({ error });\n const code: VibeErrorCode =\n (sqlstate === undefined ? undefined : PG_SQLSTATE_ERROR_CODES[sqlstate]) ?? \"VIBE_ADAPTER\";\n\n const meta: Record<string, unknown> = {};\n if (sqlstate !== undefined) meta.sqlstate = sqlstate;\n const constraint = readStringField({ source, key: \"constraint\" });\n if (constraint !== undefined) meta.constraint = constraint;\n const table = readStringField({ source, key: \"table\" });\n if (table !== undefined) meta.table = table;\n const column = readStringField({ source, key: \"column\" });\n if (column !== undefined) meta.column = column;\n const reason = sqlstate === undefined ? undefined : PG_SQLSTATE_REASONS[sqlstate];\n if (reason !== undefined) meta.reason = reason;\n\n const message =\n readStringField({ source, key: \"message\" }) ??\n (typeof error === \"string\" ? error : \"unknown postgres driver error\");\n\n return new VibeError({ code, message, meta, cause: error });\n}\n\n/** Run a driver call, mapping anything it throws onto a `VibeError`. */\nexport async function runMapped<T>(fn: () => Promise<T>): Promise<T> {\n try {\n return await fn();\n } catch (error) {\n throw mapPgDriverError({ error });\n }\n}\n",
5
+ "/**\n * @vibeorm/adapter-pg — node-postgres (`pg`) adapter for VibeORM v2.\n *\n * Runs on Bun and Node. Owns pooling, optional named prepared statements,\n * transactions (nested → savepoints on the same client) and driver-error\n * mapping. Only `VibeError` ever escapes.\n */\n\nimport type {\n AdapterBudgetSupport,\n DatabaseAdapter,\n QueryResult,\n TransactionOptions,\n WireFidelity,\n} from \"@vibeorm/runtime\";\nimport {\n createSavepointScope,\n engineStatementBudgetMs,\n isTransactionDeadlineError,\n runNestedSavepoint,\n runRootScope,\n type SavepointScope,\n scopeTurn,\n startTransactionBudget,\n type TransactionBudget,\n transactionOutcomeUnknownError,\n validateTransactionDeadline,\n} from \"@vibeorm/runtime/adapter-kit\";\nimport { VibeError } from \"@vibeorm/schema\";\nimport type { Dialect, Provider } from \"@vibeorm/schema\";\nimport pg from \"pg\";\nimport { mapPgDriverError, runMapped } from \"./errors.ts\";\nimport { PgArrayParam, serializeParams, serializeParamsRaw } from \"./params.ts\";\nimport { assertPlanCacheMode, buildPoolConfig, type PlanCacheMode } from \"./pool-config.ts\";\nimport { createStatementNameCache, type StatementNameCache } from \"./statement-cache.ts\";\nimport {\n beginStatement,\n classifyRawTransactionControl,\n refuseNestedTransactionOptions,\n statementTimeoutStatement,\n} from \"./transaction-sql.ts\";\n\n// ─── Minimal pg surface ───────────────────────────────────────────\n\n/** The single `pg` query form this adapter uses (config object, never overloads). */\nexport type PgQueryConfig = {\n readonly text: string;\n readonly values?: unknown[];\n readonly name?: string;\n /**\n * QUERY-LOCAL result parsers ({@link EXACT_TYPES}). node-postgres reads this\n * off the query config, so nothing outside VibeORM's own statements is\n * affected — a caller's pool keeps the driver's documented behaviour.\n */\n readonly types?: PgTypeParsers;\n};\n\n/** The `pg-types` surface a query config may carry. */\nexport type PgTypeParsers = {\n getTypeParser(oid: number, format?: \"text\" | \"binary\"): (value: string) => unknown;\n};\n\n/** The parts of a `pg` result this adapter reads. */\nexport type PgQueryResultLike = {\n readonly rows: Record<string, unknown>[];\n readonly rowCount: number | null;\n};\n\n/** The parts of a `pg` PoolClient this adapter uses. */\nexport type PgPoolClientLike = {\n query(config: PgQueryConfig): Promise<PgQueryResultLike>;\n /**\n * Hand the client back. A TRUTHY argument tells node-postgres to DESTROY it\n * instead of pooling it — used when a rollback failed and the session state\n * can no longer be asserted (EPIC 6). Optional so existing test doubles that\n * declare `release(): void` still satisfy this type.\n */\n release(destroy?: boolean): void;\n /**\n * EventEmitter surface. A real `pg.PoolClient` has it; test doubles need not,\n * so both members are optional and the adapter feature-detects them.\n */\n on?(event: \"error\", listener: (error: unknown) => void): void;\n removeListener?(event: \"error\", listener: (error: unknown) => void): void;\n};\n\n/** The parts of a `pg` Pool this adapter uses — satisfied by a real `pg.Pool`. */\nexport type PgPoolLike = {\n connect(): Promise<PgPoolClientLike>;\n query(config: PgQueryConfig): Promise<PgQueryResultLike>;\n end(): Promise<void>;\n /** EventEmitter surface; optional for the same reason as on {@link PgPoolClientLike}. */\n on?(event: \"error\", listener: (error: unknown, client: PgPoolClientLike) => void): void;\n};\n\n/** Anything that can run a query: the pool itself or a checked-out client. */\ntype Queryable = { query(config: PgQueryConfig): Promise<PgQueryResultLike> };\n\n/**\n * Keep a CHECKED-OUT client's background failures from killing the process,\n * returning the detach to run before the client goes back to the pool.\n *\n * node-postgres listens for `'error'` on a pooled client only while that client\n * sits IDLE, and removes the listener the moment the client is checked out\n * (restoring it on release). A client this adapter holds — for a transaction,\n * or as a pinned raw session — therefore has NO listener, so a backend that\n * dies mid-transaction reaches `client.emit('error', …)` with none. An\n * `'error'` event without a listener is an uncaught exception: fatal under\n * Node, which turns a database failover into a dead application process.\n *\n * The listener deliberately does nothing with the error and is NOT an error\n * channel: the in-flight query already rejects on its own with the mapped\n * `VibeError`, and the pool discards the broken client. Its only job is to keep\n * the process alive. Detaching before `release()` matters just as much — a\n * listener left attached would shadow pg's own idle listener and stop the pool\n * evicting a client that fails later, while idle.\n */\n/**\n * Prepared-statement name registries, one per POOL (F16/F18) — see\n * ./statement-cache.ts for why they are shared and bounded. Weakly held, so a\n * pool that goes out of scope takes its registry with it.\n */\nconst POOL_STATEMENT_NAMES: WeakMap<PgPoolLike, StatementNameCache> = new WeakMap();\n\nfunction guardBackgroundErrors(params: { client: PgPoolClientLike }): () => void {\n const { client } = params;\n if (typeof client.on !== \"function\") return (): void => undefined;\n const swallow = (): void => {\n /* the failing call rejects with a mapped VibeError; nothing to report here */\n };\n client.on(\"error\", swallow);\n return (): void => {\n client.removeListener?.(\"error\", swallow);\n };\n}\n\n// ─── Options ──────────────────────────────────────────────────────\n\n/**\n * Options for {@link pgAdapter}. Either point it at a connection string (the\n * adapter then owns the pool it creates) or hand it a pool you already own.\n */\nexport type PgAdapterOptions = {\n /** PostgreSQL connection URL. Defaults to `process.env.DATABASE_URL`. */\n readonly connectionString?: string;\n /**\n * An existing `pg.Pool` to use instead of creating one. The adapter never\n * ends a pool it did not create, so `disconnect()` is a no-op in this mode.\n */\n readonly pool?: PgPoolLike;\n /** Maximum pooled connections (default 10). Ignored when `pool` is given. */\n readonly max?: number;\n /**\n * Pool-wide `statement_timeout` in milliseconds. Every connection inherits\n * it; a per-transaction `timeout` overrides it for that transaction.\n */\n readonly statementTimeout?: number;\n /** Milliseconds to wait for a pool checkout before failing (pg's `connectionTimeoutMillis`). */\n readonly connectionTimeout?: number;\n /**\n * Send ORM-path queries as named prepared statements (default false).\n *\n * Measured on this repo's benchmark (postgres 16, interleaved rounds):\n * −17…−25 % on reads, −6 % on a by-key update — the server skips the\n * parse/analyze and the protocol Parse/Describe round on every repeat. The\n * gain survives {@link PgAdapterOptions.planCacheMode} `force_custom_plan`,\n * which removes the classic downside (a generic plan after ~5 executions\n * picking worse indexes for range/LIKE/skewed filters).\n *\n * Off by default because named statements live on the BACKEND connection:\n * behind a transaction-mode pooler (PgBouncer, Supavisor, Neon's pooler)\n * without prepared-statement support a client that remembers a name from an\n * earlier transaction is handed another backend later — SQLSTATE 26000,\n * 42P05, or, worst, another client's statement under the same name executed\n * without any error. Turn it on for direct connections and session pooling.\n */\n readonly preparedStatements?: boolean;\n /**\n * Budget of prepared-statement NAMES per pool (default 1000). Names never\n * evict — a name evicted here would stay prepared on every backend that saw\n * it — so this is a bound on the server's retention per connection, and a\n * text arriving after the budget is spent runs unnamed (it still binds its\n * parameters; only the server-side plan cache is skipped). A name is\n * allocated on a text's SECOND sighting, so one-off texts — offset pagination\n * emits one per page — never spend the budget.\n */\n readonly stmtCacheMax?: number;\n /**\n * PostgreSQL `plan_cache_mode` for every connection of the pool this adapter\n * builds (default `\"auto\"`, the server default — nothing is sent). Pair\n * `\"force_custom_plan\"` with {@link PgAdapterOptions.preparedStatements}: the\n * statement is still prepared once, but planned with the real parameter\n * values on every execution. Rides the `options` startup parameter, merged\n * with whatever the connection string already carries (a URL that sets\n * `plan_cache_mode` itself wins). Refused with `VIBE_CONFIG` together with\n * a caller-supplied `pool` — the adapter never reconfigures a pool it does\n * not own; set it on that pool's own `options` instead.\n */\n readonly planCacheMode?: PlanCacheMode;\n};\n\n// ─── Adapter ──────────────────────────────────────────────────────\n\nconst DIALECT: Dialect = \"postgres\";\nconst PROVIDER: Provider = \"postgresql\";\n\n/**\n * node-postgres wire (board #30, live-verified on both execute paths):\n * timestamptz → `Date`, json/jsonb → parsed values, int4/float8 → numbers.\n * (int8 stays a string — BigInt decoding is real work and never pruned.)\n */\nconst WIRE: WireFidelity = Object.freeze({ dateTime: \"native\", json: \"parsed\", numbers: \"native\" } as const);\n\n/**\n * Timing budgets this adapter genuinely enforces (EPIC 6).\n *\n * - `acquireTimeout: true` — {@link PgAdapterOptions.connectionTimeout} maps to\n * node-postgres' `connectionTimeoutMillis`, which bounds the checkout wait\n * and rejects when the pool stays exhausted. The capability is declared here;\n * it is OFF until the option is set (pg's own default is \"wait forever\").\n * - `statementTimeout: \"all-statements\"` — PostgreSQL's `statement_timeout`\n * cancels ANY over-long statement, writes included (SQLSTATE 57014).\n * - `transactionDeadline: \"cancel-running-statements\"` — the strongest level:\n * the remaining budget is handed to the SERVER before each statement, so the\n * database itself stops work that would outlive the deadline. Nothing here is\n * a `Promise.race` over a statement that keeps running.\n */\nconst BUDGETS: AdapterBudgetSupport = Object.freeze({\n acquireTimeout: true,\n statementTimeout: \"all-statements\",\n transactionDeadline: \"cancel-running-statements\",\n} as const);\n\n// ─── Exact numeric arrays (F12) ───────────────────────────────\n\n/** `_numeric`: the array form of `numeric`/`DECIMAL`. */\nconst NUMERIC_ARRAY_OID = 1231;\n/** `_text`: its parser splits a postgres array literal into RAW element strings. */\nconst TEXT_ARRAY_OID = 1009;\n/** `timestamp without time zone` and `date` — the zone-less types — and their array forms. */\nconst TIMESTAMP_OID = 1114;\nconst TIMESTAMP_ARRAY_OID = 1115;\nconst DATE_OID = 1082;\nconst DATE_ARRAY_OID = 1182;\n\n/**\n * `pg.types.getTypeParser` behind a plain numeric-OID signature. `@types/pg`\n * narrows the two-argument overload to its own `TypeId` union of well-known\n * OIDs, which does not include every array OID this function must look up.\n */\nconst driverTypeParser: (oid: number, format?: \"text\" | \"binary\") => (value: string) => unknown =\n pg.types.getTypeParser as unknown as (oid: number, format?: \"text\" | \"binary\") => (value: string) => unknown;\n\n/** postgres' text output for the zone-less types: `2026-03-08 19:38:08.538` or `2026-03-08`. */\nconst ZONE_LESS_TEXT = /^\\d{4}-\\d{2}-\\d{2}(?: \\d{2}:\\d{2}:\\d{2}(?:\\.\\d{1,6})?)?$/;\n\n/**\n * Zone-less date/timestamp text read as **UTC** — the same rule as the codec\n * table's `parseDateTimeText` (@vibeorm/sql), applied at the driver level\n * because node-postgres hands these columns to the ORM as `Date` objects\n * already (`wire.dateTime: \"native\"` prunes the codec decode). pg-types reads\n * them as host-LOCAL time, so one stored row decoded to a different instant\n * on every host TZ, and to a different instant than bun:sql, PGlite's ORM\n * path and Prisma (all UTC) return. `infinity`, BC dates and anything else\n * outside the plain form keep the driver's own parser.\n */\nfunction parseZoneLessAsUtc(value: string): unknown {\n if (ZONE_LESS_TEXT.test(value)) {\n const iso = value.includes(\" \") ? `${value.replace(\" \", \"T\").replace(/(\\.\\d{3})\\d+$/, \"$1\")}Z` : value;\n const parsed = new Date(iso);\n if (!Number.isNaN(parsed.getTime())) return parsed;\n }\n return driverTypeParser(TIMESTAMP_OID, \"text\")(value);\n}\n\n/** The array forms: the driver's `_text` parser splits the literal, elements go through {@link parseZoneLessAsUtc}. */\nfunction parseZoneLessArrayAsUtc(value: string): unknown {\n const mapElements = (element: unknown): unknown => {\n if (element === null) return null;\n if (Array.isArray(element)) return element.map(mapElements);\n return parseZoneLessAsUtc(String(element));\n };\n return mapElements(driverTypeParser(TEXT_ARRAY_OID, \"text\")(value));\n}\n\n/**\n * Query-local type parsers that keep `numeric[]` EXACT and read zone-less\n * timestamps as UTC.\n *\n * pg-types decodes `_numeric` with `parseFloat` per element, so\n * `9007199254740993` comes back as `9007199254740992` and `12.340` as the\n * number `12.34` — while the SCALAR `numeric` correctly stays a string and\n * VibeORM's Decimal contract is \"string in, string out\" on every dialect. The\n * array is the anomaly, and the loss happens inside the driver, before any\n * codec can see the value.\n *\n * The fix reuses the driver's OWN array-literal parser (`_text`), so array\n * syntax, quoting and NULL elements stay node-postgres' business; only the\n * per-element float conversion is dropped. It is attached PER QUERY (F12's\n * acceptance criterion), never by mutating `pg.types` globally: an injected\n * pool is shared property, and a global override would silently change every\n * other consumer in the process. The zone-less parsers ride the same hook for\n * the same reason.\n */\nconst EXACT_TYPES: PgTypeParsers = {\n getTypeParser(oid: number, format?: \"text\" | \"binary\"): (value: string) => unknown {\n if (format === undefined || format === \"text\") {\n if (oid === NUMERIC_ARRAY_OID) return driverTypeParser(TEXT_ARRAY_OID, \"text\");\n if (oid === TIMESTAMP_OID || oid === DATE_OID) return parseZoneLessAsUtc;\n if (oid === TIMESTAMP_ARRAY_OID || oid === DATE_ARRAY_OID) return parseZoneLessArrayAsUtc;\n }\n return driverTypeParser(oid, format);\n },\n};\n\n/**\n * Create a VibeORM adapter backed by node-postgres.\n *\n * @example\n * ```ts\n * const adapter = pgAdapter({ connectionString: process.env.DATABASE_URL, max: 20 });\n * await adapter.connect();\n * const rows = await adapter.execute({ text: 'SELECT * FROM \"User\" WHERE \"id\" = $1', values: [1] });\n * ```\n *\n * @example Reusing a pool you already own\n * ```ts\n * import pg from \"pg\";\n * const pool = new pg.Pool({ connectionString });\n * const adapter = pgAdapter({ pool });\n * ```\n */\nexport function pgAdapter(options?: PgAdapterOptions): DatabaseAdapter {\n const usePrepared = options?.preparedStatements ?? false;\n const stmtCacheMax = options?.stmtCacheMax ?? 1000;\n const planCacheMode = options?.planCacheMode === undefined ? \"auto\" : assertPlanCacheMode({ value: options.planCacheMode });\n if (planCacheMode !== \"auto\" && options?.pool !== undefined) {\n throw new VibeError({\n code: \"VIBE_CONFIG\",\n message:\n \"planCacheMode configures the pool the adapter builds — it cannot be applied to a caller-supplied `pool`; \" +\n \"set `options: \\\"-c plan_cache_mode=…\\\"` on that pool instead\",\n meta: { planCacheMode },\n });\n }\n\n let pool: PgPoolLike | null = null;\n let ownsPool = false;\n\n /**\n * The checked-out client of an OPEN raw transaction — session affinity for\n * the raw path. @vibeorm/migrate wraps transactional DDL in literal `BEGIN`\n * / `COMMIT` / `ROLLBACK` sent through `executeUnsafe`; without pinning,\n * `pool.query(\"BEGIN\")` sequences stay on one client only by LIFO checkout\n * luck, and any concurrent traffic silently splits the transaction across\n * connections. A pure `BEGIN` checks a client out, every raw statement until\n * the matching `COMMIT`/`ROLLBACK` rides it, and the close releases it. The\n * ORM path (`execute`) and `transaction()` stay on the pool. Concurrent raw\n * transactions on one adapter are unsupported.\n */\n let rawSession: PgPoolClientLike | null = null;\n /** Detach for {@link rawSession}'s background-error guard; runs before its release. */\n let rawSessionUnguard: (() => void) | null = null;\n\n function getPool(): PgPoolLike {\n if (pool !== null) return pool;\n\n if (options?.pool !== undefined) {\n pool = options.pool;\n ownsPool = false;\n return pool;\n }\n\n const config = buildPoolConfig({\n connectionString: options?.connectionString ?? process.env.DATABASE_URL,\n max: options?.max ?? 10,\n ...(options?.statementTimeout === undefined ? {} : { statementTimeout: options.statementTimeout }),\n ...(options?.connectionTimeout === undefined ? {} : { connectionTimeout: options.connectionTimeout }),\n planCacheMode,\n });\n\n const PoolCtor = pg.Pool as unknown as new (poolConfig: Record<string, unknown>) => PgPoolLike;\n pool = new PoolCtor(config);\n /**\n * The IDLE half of the same hazard {@link guardBackgroundErrors} covers: a\n * pooled client that loses its socket while idle makes pg re-emit on the\n * POOL, and an unhandled `'error'` there is equally fatal. The pool already\n * evicts that client, so this listener only keeps the process alive.\n *\n * Attached ONLY to a pool this adapter constructed. A caller-supplied pool\n * (the `pool` option) has its own owner: the adapter never ends it either,\n * node-postgres documents attaching an `'error'` listener as that owner's\n * job, and registering one here that is never removed would stack up a\n * listener per adapter sharing that pool. The dangerous case — a client\n * checked out for a transaction or a raw session, which pg leaves\n * unlistened by construction — is guarded in BOTH modes.\n */\n pool.on?.(\"error\", () => {\n /* the pool has already discarded the failed client */\n });\n ownsPool = true;\n return pool;\n }\n\n /**\n * Prepared-statement names for the pool this adapter runs on (F18): the\n * registry is keyed by the POOL, so two adapters sharing one pool share one\n * name space instead of both starting at `vibeorm_0` and colliding on a\n * connection. It also bounds how many names that pool can ever put on a\n * backend (F16) no matter how many adapters come and go over it.\n */\n function statementNamesFor(params: { pool: PgPoolLike }): StatementNameCache {\n const existing = POOL_STATEMENT_NAMES.get(params.pool);\n if (existing !== undefined) return existing;\n const created = createStatementNameCache({ max: stmtCacheMax });\n POOL_STATEMENT_NAMES.set(params.pool, created);\n return created;\n }\n\n /** Build the config for one ORM-path query (statement name only when enabled). */\n function buildQueryConfig(params: { text: string; values: unknown[] }): PgQueryConfig {\n const values = serializeParams({ values: params.values });\n if (!usePrepared) return { text: params.text, values, types: EXACT_TYPES };\n // `undefined` on a text's first sighting and once the pool's name budget is\n // spent: the statement runs unnamed rather than growing the server's\n // prepared-statement retention.\n const name = statementNamesFor({ pool: getPool() }).getName({ text: params.text });\n return name === undefined\n ? { text: params.text, values, types: EXACT_TYPES }\n : { name, text: params.text, values, types: EXACT_TYPES };\n }\n\n async function runExecute(params: {\n queryable: Queryable;\n text: string;\n values: unknown[];\n }): Promise<Record<string, unknown>[]> {\n const config = buildQueryConfig({ text: params.text, values: params.values });\n const result = await runMapped(() => params.queryable.query(config));\n return result.rows;\n }\n\n async function runExecuteUnsafe(params: {\n queryable: Queryable;\n text: string;\n values?: unknown[];\n }): Promise<QueryResult> {\n const values = params.values === undefined ? undefined : serializeParamsRaw({ values: params.values });\n const result = await runMapped(() => params.queryable.query({ text: params.text, values, types: EXACT_TYPES }));\n return { rows: result.rows, affectedRows: result.rowCount ?? 0 };\n }\n\n /**\n * Adapter bound to one checked-out client that is already inside a\n * transaction. Nested `transaction()` calls open savepoints on that same\n * client, and their LIFETIMES are serialized by the savepoint gate (F14):\n * overlapping siblings share one savepoint stack, so the earlier one's\n * rollback would erase the later one's already released work.\n */\n function createClientAdapter(params: {\n client: PgPoolClientLike;\n scope: SavepointScope;\n budget: TransactionBudget;\n }): DatabaseAdapter {\n const { client, scope, budget } = params;\n\n /**\n * Hand the SERVER the remaining budget before a statement runs, when the\n * caller bought `\"cancel-running-statements\"`. `SET LOCAL` is\n * transaction-scoped, so PostgreSQL reverts it at COMMIT/ROLLBACK and the\n * connection returns to the pool with its session settings intact.\n *\n * Costs one extra round trip per statement, and only while a deadline is\n * active — the price of a deadline the database itself enforces.\n */\n async function applyEngineBudget(): Promise<void> {\n const ms = engineStatementBudgetMs({ budget });\n if (ms === null) return;\n await runMapped(() => client.query({ text: statementTimeoutStatement({ timeout: ms }) }));\n }\n\n return {\n dialect: DIALECT,\n provider: PROVIDER,\n wire: WIRE,\n budgets: BUDGETS,\n\n async execute(execParams) {\n const turn = scopeTurn({ scope });\n if (turn !== undefined) await turn;\n // After the turn, not before: waiting for a sibling savepoint spends\n // the same budget the statement is about to run against.\n budget.assertUsable({ stage: \"statement\" });\n await applyEngineBudget();\n return runExecute({ queryable: client, ...execParams });\n },\n\n async executeUnsafe(execParams) {\n const turn = scopeTurn({ scope });\n if (turn !== undefined) await turn;\n budget.assertUsable({ stage: \"statement\" });\n await applyEngineBudget();\n return runExecuteUnsafe({ queryable: client, ...execParams });\n },\n\n async transaction<T>(\n fn: (txAdapter: DatabaseAdapter) => Promise<T>,\n nestedOptions?: TransactionOptions,\n ): Promise<T> {\n refuseNestedTransactionOptions({ options: nestedOptions, provider: PROVIDER });\n budget.assertUsable({ stage: \"nested\" });\n return runNestedSavepoint({\n scope,\n execute: (statement) => runMapped(() => client.query({ text: statement })),\n // The child shares the ROOT budget by reference: a savepoint never\n // gets an independent clock or session setting.\n body: (child) => fn(createClientAdapter({ client, scope: child, budget })),\n });\n },\n\n async connect(): Promise<void> {\n // Already connected — this adapter is bound to a live client.\n },\n\n async disconnect(): Promise<void> {\n // The pool owns the connection lifecycle; releasing happens at the\n // top-level transaction boundary.\n },\n\n formatArrayParam(values: unknown[]): unknown {\n return new PgArrayParam(values);\n },\n };\n }\n\n return {\n dialect: DIALECT,\n provider: PROVIDER,\n wire: WIRE,\n budgets: BUDGETS,\n\n async execute(params) {\n return runExecute({ queryable: getPool(), ...params });\n },\n\n async executeUnsafe(params) {\n const control = classifyRawTransactionControl({ text: params.text });\n if (rawSession !== null) {\n if (control === \"close\") {\n const session = rawSession;\n const unguard = rawSessionUnguard;\n rawSession = null;\n rawSessionUnguard = null;\n try {\n return await runExecuteUnsafe({ queryable: session, ...params });\n } finally {\n unguard?.();\n session.release();\n }\n }\n // A nested raw BEGIN lands here too — postgres treats it as a warning\n // no-op on the open session, exactly like a single connection would.\n return runExecuteUnsafe({ queryable: rawSession, ...params });\n }\n if (control === \"open\") {\n const client = await runMapped(() => getPool().connect());\n const unguard = guardBackgroundErrors({ client });\n try {\n const result = await runExecuteUnsafe({ queryable: client, ...params });\n rawSession = client;\n rawSessionUnguard = unguard;\n return result;\n } catch (error) {\n unguard();\n client.release();\n throw error;\n }\n }\n return runExecuteUnsafe({ queryable: getPool(), ...params });\n },\n\n async transaction<T>(\n fn: (txAdapter: DatabaseAdapter) => Promise<T>,\n transactionOptions?: TransactionOptions,\n ): Promise<T> {\n // Refuse an unsupportable budget BEFORE checking a connection out, so a\n // bad option never costs a checkout and never leaves a transaction open.\n validateTransactionDeadline({\n options: transactionOptions,\n support: BUDGETS.transactionDeadline,\n provider: PROVIDER,\n });\n const client = await runMapped(() => getPool().connect());\n const unguard = guardBackgroundErrors({ client });\n const scope = createSavepointScope();\n // The deadline clock starts HERE: the connection is already in hand, so\n // acquisition (bounded separately by `connectionTimeout`) is not charged\n // against the caller's transaction budget.\n const budget = startTransactionBudget({ options: transactionOptions, provider: PROVIDER });\n /** Set when the session's state can no longer be asserted (see below). */\n let discard = false;\n try {\n await runMapped(() =>\n client.query({ text: beginStatement({ isolationLevel: transactionOptions?.isolationLevel }) }),\n );\n if (transactionOptions?.timeout !== undefined) {\n const text = statementTimeoutStatement({ timeout: transactionOptions.timeout });\n await runMapped(() => client.query({ text }));\n }\n const result = await runRootScope({\n scope,\n body: () => fn(createClientAdapter({ client, scope, budget })),\n });\n // An expired budget must never commit, however the callback ended.\n budget.assertUsable({ stage: \"commit\" });\n await runMapped(() => client.query({ text: \"COMMIT\" }));\n budget.close({ outcome: \"committed\" });\n return result;\n } catch (error) {\n try {\n await client.query({ text: \"ROLLBACK\" });\n budget.close({ outcome: \"rolled-back\" });\n } catch {\n // The rollback that should have cleaned up failed: this connection may\n // still be inside a transaction with `SET LOCAL` settings that were\n // never reverted, so it is DESTROYED rather than pooled, and the\n // commit state is reported as genuinely unknown.\n discard = true;\n budget.close({ outcome: \"unknown\" });\n if (budget.totalMs !== null && isTransactionDeadlineError(error)) {\n throw transactionOutcomeUnknownError({\n provider: PROVIDER,\n totalMs: budget.totalMs,\n cause: error,\n });\n }\n }\n throw error;\n } finally {\n unguard();\n client.release(discard);\n }\n },\n\n async connect(): Promise<void> {\n const client = await runMapped(() => getPool().connect());\n client.release();\n },\n\n async disconnect(): Promise<void> {\n if (rawSession !== null) {\n // An abandoned raw transaction must not leak an in-transaction client;\n // roll it back best-effort before the pool ends.\n const session = rawSession;\n const unguard = rawSessionUnguard;\n rawSession = null;\n rawSessionUnguard = null;\n try {\n await session.query({ text: \"ROLLBACK\" });\n } catch {\n /* the pool is closing; the session is going away regardless */\n }\n unguard?.();\n session.release();\n }\n if (pool !== null && ownsPool) {\n const closing = pool;\n pool = null;\n await runMapped(() => closing.end());\n }\n },\n\n formatArrayParam(values: unknown[]): unknown {\n return new PgArrayParam(values);\n },\n };\n}\n\n// ─── Re-exports (driver-error table, markers, helpers) ────────────\n\nexport { PG_SQLSTATE_ERROR_CODES, PG_SQLSTATE_REASONS, mapPgDriverError, pgErrorSqlstate } from \"./errors.ts\";\nexport { PgArrayParam, serializeParams, serializeParamsRaw } from \"./params.ts\";\nexport { createSavepointCounter, nextSavepointName } from \"@vibeorm/runtime/adapter-kit\";\nexport type { SavepointCounter } from \"@vibeorm/runtime/adapter-kit\";\nexport { createStatementNameCache } from \"./statement-cache.ts\";\nexport type { StatementNameCache } from \"./statement-cache.ts\";\nexport { buildPoolConfig, placePlanCacheMode } from \"./pool-config.ts\";\nexport type { PlanCacheMode, StartupOptionsPlacement } from \"./pool-config.ts\";\nexport {\n ISOLATION_LEVEL_SQL,\n beginStatement,\n classifyRawTransactionControl,\n refuseNestedTransactionOptions,\n statementTimeoutStatement,\n} from \"./transaction-sql.ts\";\nexport type { RawTransactionControl } from \"./transaction-sql.ts\";\n// ─── EPIC 6: transaction budgets (shared, @vibeorm/runtime/adapter-kit) ───\nexport {\n DEFAULT_DEADLINE_CLOCK,\n engineStatementBudgetMs,\n isTransactionDeadlineError,\n startTransactionBudget,\n transactionClosedError,\n transactionDeadlineError,\n transactionOutcomeUnknownError,\n validateTransactionDeadline,\n} from \"@vibeorm/runtime/adapter-kit\";\nexport type { BudgetStage, DeadlineClock, TransactionBudget, TransactionOutcome } from \"@vibeorm/runtime/adapter-kit\";\n",
6
+ "/**\n * Driver-error mapping for node-postgres (`pg`).\n *\n * Constitution rule 5: a raw driver error never escapes an adapter. Every\n * failure becomes a `VibeError` with a stable code, the driver error attached\n * as `cause`, and the SQLSTATE (plus constraint/table/column when the server\n * reported them) in `meta`.\n */\n\nimport { VibeError, type VibeErrorCode } from \"@vibeorm/schema\";\n\n// ─── SQLSTATE table ───────────────────────────────────────────────\n\n/**\n * PostgreSQL SQLSTATE → stable VibeError code. Frozen and mirrored by tests;\n * anything absent from this table maps to `VIBE_ADAPTER`.\n */\nexport const PG_SQLSTATE_ERROR_CODES: Readonly<Record<string, VibeErrorCode>> = Object.freeze({\n /** exclusion_violation */\n \"23P01\": \"VIBE_EXCLUSION_VIOLATION\",\n /** unique_violation */\n \"23505\": \"VIBE_UNIQUE_VIOLATION\",\n /** foreign_key_violation */\n \"23503\": \"VIBE_FK_VIOLATION\",\n /** restrict_violation — PostgreSQL 18+ ON DELETE/UPDATE RESTRICT */\n \"23001\": \"VIBE_FK_VIOLATION\",\n /** not_null_violation */\n \"23502\": \"VIBE_VALIDATION\",\n /** check_violation */\n \"23514\": \"VIBE_VALIDATION\",\n /**\n * invalid_text_representation — a value the server cannot parse into the\n * column's type (out-of-range enum value, `\"abc\"` for an integer). Bad input,\n * so the same code sqlite reports for its enum CHECK violation.\n */\n \"22P02\": \"VIBE_VALIDATION\",\n /**\n * insufficient_privilege — a denied table/column privilege, and the SQLSTATE a\n * native row-level-security policy raises when its WITH CHECK refuses a write.\n * Callers identify it by this code, never by message text.\n */\n \"42501\": \"VIBE_PERMISSION_DENIED\",\n /** serialization_failure */\n \"40001\": \"VIBE_TRANSACTION\",\n /** deadlock_detected */\n \"40P01\": \"VIBE_TRANSACTION\",\n /** query_canceled — statement_timeout fired */\n \"57014\": \"VIBE_TRANSACTION\",\n /**\n * lock_not_available — a `nowait` locking read whose rows were already\n * locked, OR a `lock_timeout` that expired while blocking. The server reports\n * both with this ONE SQLSTATE (verified on postgres 18.6: \"could not obtain\n * lock on row in relation\" and \"canceling statement due to lock timeout\"), so\n * VibeORM does not invent a distinction the server does not make. Nothing is\n * retried automatically — waiting, skipping or failing is the caller's policy.\n */\n \"55P03\": \"VIBE_LOCK_NOT_AVAILABLE\",\n});\n\n/** SQLSTATEs that additionally explain themselves through `meta.reason`. */\nexport const PG_SQLSTATE_REASONS: Readonly<Record<string, string>> = Object.freeze({\n \"23P01\": \"exclusion-violation\",\n \"57014\": \"timeout\",\n \"55P03\": \"lock-not-available\",\n /** Same reason string mysql already uses for errno 1213 — one word per dialect. */\n \"40P01\": \"deadlock\",\n \"40001\": \"serialization-failure\",\n});\n\n// ─── Reading driver errors ────────────────────────────────────────\n\n/** A PostgreSQL SQLSTATE is exactly five digits/uppercase letters (e.g. `40P01`). */\nconst SQLSTATE_PATTERN = /^[0-9A-Z]{5}$/;\n\nfunction readStringField(params: { source: Record<string, unknown>; key: string }): string | undefined {\n const value = params.source[params.key];\n return typeof value === \"string\" && value.length > 0 ? value : undefined;\n}\n\n/**\n * Extract the SQLSTATE from a pg driver error. `pg` puts the server's SQLSTATE\n * on `error.code`; client-side failures (`ECONNREFUSED`, `ETIMEDOUT`, …) use\n * the same field for a non-SQLSTATE string, which this rejects.\n */\nexport function pgErrorSqlstate(params: { error: unknown }): string | undefined {\n const { error } = params;\n if (error === null || typeof error !== \"object\") return undefined;\n const code = readStringField({ source: error as Record<string, unknown>, key: \"code\" });\n return code !== undefined && SQLSTATE_PATTERN.test(code) ? code : undefined;\n}\n\n// ─── Mapping ──────────────────────────────────────────────────────\n\n/**\n * Map any error thrown by `pg` onto a `VibeError`.\n *\n * A `VibeError` passes through unchanged, so an error raised deeper inside the\n * ORM (or by a nested adapter call) is never re-wrapped.\n */\nexport function mapPgDriverError(params: { error: unknown }): VibeError {\n const { error } = params;\n if (error instanceof VibeError) return error;\n\n const source: Record<string, unknown> =\n error !== null && typeof error === \"object\" ? (error as Record<string, unknown>) : {};\n const sqlstate = pgErrorSqlstate({ error });\n const code: VibeErrorCode =\n (sqlstate === undefined ? undefined : PG_SQLSTATE_ERROR_CODES[sqlstate]) ?? \"VIBE_ADAPTER\";\n\n const meta: Record<string, unknown> = {};\n if (sqlstate !== undefined) meta.sqlstate = sqlstate;\n const constraint = readStringField({ source, key: \"constraint\" });\n if (constraint !== undefined) meta.constraint = constraint;\n const table = readStringField({ source, key: \"table\" });\n if (table !== undefined) meta.table = table;\n const column = readStringField({ source, key: \"column\" });\n if (column !== undefined) meta.column = column;\n const reason = sqlstate === undefined ? undefined : PG_SQLSTATE_REASONS[sqlstate];\n if (reason !== undefined) meta.reason = reason;\n\n const message =\n readStringField({ source, key: \"message\" }) ??\n (typeof error === \"string\" ? error : \"unknown postgres driver error\");\n\n return new VibeError({ code, message, meta, cause: error });\n}\n\n/** Run a driver call, mapping anything it throws onto a `VibeError`. */\nexport async function runMapped<T>(fn: () => Promise<T>): Promise<T> {\n try {\n return await fn();\n } catch (error) {\n throw mapPgDriverError({ error });\n }\n}\n",
7
7
  "/**\n * Parameter serialization for node-postgres — the ORM path and the raw path\n * differ, deliberately (see LEARNINGS.md).\n *\n * `pg` natively turns a JS array into a PostgreSQL array literal (`{1,2,3}`),\n * which is exactly what a raw query wants. The ORM hot path cannot tell whether\n * a JS array is destined for a `jsonb` column or for a scalar-list / `= ANY($n)`\n * parameter, so it JSON-stringifies arrays and objects — scalar-list values are\n * tagged with `PgArrayParam` by `formatArrayParam` and unwrapped here, reaching\n * the driver as the untouched JS array.\n */\n\n// ─── Scalar-list marker ───────────────────────────────────────────\n\n/**\n * Opaque token returned by `pgAdapter().formatArrayParam` marking a value as a\n * PostgreSQL array parameter (rather than a JSON value that happens to be an\n * array). The wrapped array reaches `pg` as-is; the driver serializes it.\n */\nexport class PgArrayParam {\n /** The untouched scalar-list values. */\n readonly values: unknown[];\n\n constructor(values: unknown[]) {\n this.values = values;\n }\n}\n\n// ─── Serialization ────────────────────────────────────────────────\n\n/**\n * `Date` → UTC ISO-8601 text (`2026-03-08T19:38:08.538Z`).\n *\n * node-postgres' own serializer writes the host's LOCAL wall-clock plus its\n * offset (`2026-03-08T20:38:08.538+01:00` under Europe/Oslo). A `timestamptz`\n * parameter keeps the instant either way, but a `timestamp without time zone`\n * or `date` parameter DISCARDS the offset (\"PostgreSQL will silently ignore\n * any time zone indication\"), so the stored wall-clock would depend on the\n * host's TZ — and differ from what bun:sql, PGlite and Prisma store for the\n * same `Date` (all UTC). The UTC spelling stores one value on every host and\n * matches the UTC read side (`EXACT_TYPES` in index.ts). Dates the ISO form\n * cannot spell (invalid, BC, beyond year 9999) keep the driver's serializer.\n */\nexport function serializeDate(params: { value: Date }): unknown {\n const { value } = params;\n const time = value.getTime();\n if (Number.isNaN(time)) return value;\n const year = value.getUTCFullYear();\n if (year < 1 || year > 9999) return value;\n return value.toISOString();\n}\n\n/** True for values `pg` must receive verbatim (binary, primitives, null). */\nfunction isDriverNativeValue(value: unknown): boolean {\n return (\n value === null ||\n value === undefined ||\n typeof value !== \"object\" ||\n ArrayBuffer.isView(value) ||\n value instanceof ArrayBuffer\n );\n}\n\n/**\n * ORM hot path: unwrap scalar-list markers, spell `Date` as UTC, JSON-stringify\n * plain objects **and** plain arrays (they can only be `json`/`jsonb` values\n * at this point).\n */\nexport function serializeParams(params: { values: readonly unknown[] }): unknown[] {\n return params.values.map((value) => {\n if (value instanceof PgArrayParam) return serializeList({ values: value.values });\n if (value instanceof Date) return serializeDate({ value });\n if (isDriverNativeValue(value)) return value;\n return JSON.stringify(value);\n });\n}\n\n/**\n * A scalar-list parameter: `Date` elements spelled as UTC (the driver's array\n * serializer would otherwise write them in local time); a list without dates\n * reaches `pg` as the untouched array.\n */\nfunction serializeList(params: { values: unknown[] }): unknown[] {\n const { values } = params;\n return values.some((value) => value instanceof Date)\n ? values.map((value) => (value instanceof Date ? serializeDate({ value }) : value))\n : values;\n}\n\n/**\n * Raw path (`$queryRaw*` / `$executeRaw*` / DDL): identical to\n * {@link serializeParams} except that plain JS arrays pass straight through, so\n * `db.$queryRawUnsafe('… WHERE \"id\" = ANY($1)', [1, 2, 3])` matches rows.\n *\n * Trade-off (v1 lesson): binding a JS array **into** a `jsonb` column from a raw\n * query means calling `JSON.stringify` yourself.\n */\nexport function serializeParamsRaw(params: { values: readonly unknown[] }): unknown[] {\n return params.values.map((value) => {\n if (value instanceof PgArrayParam) return serializeList({ values: value.values });\n if (Array.isArray(value)) return serializeList({ values: value });\n if (value instanceof Date) return serializeDate({ value });\n if (isDriverNativeValue(value)) return value;\n return JSON.stringify(value);\n });\n}\n",
8
8
  "/**\n * Pool configuration for the pool {@link pgAdapter} builds itself.\n *\n * Per-connection PostgreSQL settings travel in the `options` startup\n * parameter (`-c key=value` pairs the server parses at connect time), so every\n * pooled connection inherits them without a per-query round trip. Two\n * node-postgres facts shape the merge below, both read from the installed\n * driver (pg 8.23):\n *\n * - `ConnectionParameters` parses `connectionString` OVER the explicit config\n * (`Object.assign({}, config, parse(connectionString))`), so a URL that\n * already carries `?options=…` silently wins over `config.options`.\n * - `pg-connection-string` turns EVERY query parameter into a config key, so\n * `?options=-c search_path=x` arrives as `config.options`.\n *\n * Hence: with no `options` in the URL the setting rides `config.options`\n * (nothing to collide with); with one, the URL's value is EXTENDED in place\n * and a `plan_cache_mode` the URL already sets is left alone — the caller's\n * connection string is the more explicit statement of intent.\n */\n\nimport { VibeError } from \"@vibeorm/schema\";\n\n/**\n * PostgreSQL plan-cache behaviour for named prepared statements\n * (`preparedStatements: true`). `auto` (the server default) lets PostgreSQL\n * switch to a generic plan after ~5 executions — cheaper per call, but it can\n * pick worse indexes for range/LIKE/skewed filters; `force_custom_plan`\n * replans with the real parameter values every time and keeps almost the whole\n * prepared-statement gain (the saving is the parse/analyze and the protocol\n * round, not the plan — measured −17…−25 % on reads either way, see\n * `.ai/plans/26.09.08 - performance-join-strategy.md` §1.3).\n */\nexport type PlanCacheMode = \"auto\" | \"force_custom_plan\" | \"force_generic_plan\";\n\nconst PLAN_CACHE_MODES: readonly PlanCacheMode[] = [\"auto\", \"force_custom_plan\", \"force_generic_plan\"];\n\nfunction isPlanCacheMode(value: unknown): value is PlanCacheMode {\n return typeof value === \"string\" && (PLAN_CACHE_MODES as readonly string[]).includes(value);\n}\n\n/** Validate a `planCacheMode` option value; the string ends up inside a startup parameter verbatim. */\nexport function assertPlanCacheMode(params: { value: unknown }): PlanCacheMode {\n const { value } = params;\n if (isPlanCacheMode(value)) return value;\n throw new VibeError({\n code: \"VIBE_CONFIG\",\n message: `planCacheMode must be one of ${PLAN_CACHE_MODES.map((mode) => `\"${mode}\"`).join(\", \")}, received ${JSON.stringify(value)}`,\n meta: { planCacheMode: value, allowed: PLAN_CACHE_MODES },\n });\n}\n\n/** The `options` startup parameter and (possibly rewritten) connection string carrying it. */\nexport type StartupOptionsPlacement = {\n readonly connectionString: string | undefined;\n /** `undefined` when nothing needs setting, or the URL already sets the parameter itself. */\n readonly options: string | undefined;\n};\n\n/**\n * Place `plan_cache_mode` into the startup options, honouring what the URL\n * already carries (see the module header for the precedence rule).\n */\nexport function placePlanCacheMode(params: {\n connectionString: string | undefined;\n planCacheMode: PlanCacheMode;\n}): StartupOptionsPlacement {\n const { connectionString, planCacheMode } = params;\n if (planCacheMode === \"auto\") return { connectionString, options: undefined };\n const setting = `-c plan_cache_mode=${planCacheMode}`;\n\n if (connectionString === undefined || !URL.canParse(connectionString)) {\n return { connectionString, options: setting };\n }\n const url = new URL(connectionString);\n const existing = url.searchParams.get(\"options\");\n if (existing === null) {\n // Absent from the URL → the parsed URL carries no `options` key and\n // `config.options` survives the driver's merge untouched.\n return { connectionString, options: setting };\n }\n if (/(^|\\s)-c\\s*plan_cache_mode=/.test(existing)) {\n // The URL is the more explicit statement of intent; leave it alone.\n return { connectionString, options: undefined };\n }\n const merged = existing.trim().length === 0 ? setting : `${existing.trim()} ${setting}`;\n // The URL's own value wins in the driver, so the merge has to live THERE.\n url.searchParams.set(\"options\", merged);\n return { connectionString: url.toString(), options: merged };\n}\n\n/** Build the `pg.Pool` constructor config for an adapter-owned pool. */\nexport function buildPoolConfig(params: {\n connectionString: string | undefined;\n max: number;\n statementTimeout?: number;\n connectionTimeout?: number;\n planCacheMode?: PlanCacheMode;\n}): Record<string, unknown> {\n const { max, statementTimeout, connectionTimeout } = params;\n const placement = placePlanCacheMode({\n connectionString: params.connectionString,\n planCacheMode: params.planCacheMode ?? \"auto\",\n });\n const config: Record<string, unknown> = { connectionString: placement.connectionString, max };\n if (statementTimeout !== undefined) config.statement_timeout = statementTimeout;\n if (connectionTimeout !== undefined) config.connectionTimeoutMillis = connectionTimeout;\n if (placement.options !== undefined) config.options = placement.options;\n return config;\n}\n",
9
- "/**\n * Bounded prepared-statement NAME registry, shared per pool.\n *\n * With `preparedStatements: true` every distinct SQL text gets a stable name, so\n * PostgreSQL keeps a cached plan for it. Two properties matter, and the first\n * version of this file had neither:\n *\n * 1. **The bound has to be a bound on the SERVER** (F16). Evicting a local\n * mapping and minting a fresh name leaves the old prepared statement on\n * every backend that ever saw it — the local map stayed small while server\n * retention grew without limit. Nor can eviction be repaired with\n * `DEALLOCATE`: a name is prepared per CONNECTION, the adapter does not know\n * which pooled connections hold it, and deallocating behind node-postgres'\n * back invalidates its own per-client name registry (it would keep sending\n * the name and the server would refuse it).\n *\n * So the registry never re-uses a name for another text and never evicts:\n * it allocates at most `max` names, and once that budget is spent a new text\n * runs UNNAMED — bounded preparation fallback. Server retention is then\n * `max` per connection, permanently. Statements still bind their parameters;\n * only the server-side plan cache is skipped.\n *\n * 2. **Adapters sharing one pool must not collide** (F18). Each adapter used to\n * start its own counter at zero, so two adapters on one pool both sent\n * `vibeorm_0` for different SQL and postgres refused the second\n * (\"Prepared statements must be unique\"). Names are therefore allocated by a\n * registry shared per pool, which also stops N adapters from preparing the\n * same text N times.\n */\n\n/** Registry mapping SQL text to the prepared-statement name `pg` should send. */\nexport type StatementNameCache = {\n /**\n * Stable statement name for `text`, allocating one on first sight — or\n * `undefined` once the name budget is spent, meaning \"send this one unnamed\".\n */\n getName(params: { text: string }): string | undefined;\n /** Number of names allocated — diagnostics and tests. */\n size(): number;\n};\n\n/**\n * Create a statement-name registry that allocates at most `max` names\n * (`vibeorm_0`, `vibeorm_1`, …). Texts beyond the budget get no name and run\n * unprepared, so the server's prepared-statement retention stays bounded.\n */\nexport function createStatementNameCache(params: { max: number }): StatementNameCache {\n const max = Math.max(0, params.max);\n const names = new Map<string, string>();\n let counter = 0;\n\n return {\n getName(nameParams: { text: string }): string | undefined {\n const { text } = nameParams;\n const existing = names.get(text);\n if (existing !== undefined) return existing;\n if (names.size >= max) return undefined;\n const name = `vibeorm_${counter++}`;\n names.set(text, name);\n return name;\n },\n\n size(): number {\n return names.size;\n },\n };\n}\n",
9
+ "/**\n * Bounded prepared-statement NAME registry, shared per pool.\n *\n * With `preparedStatements: true` every SQL text that proves itself repeated\n * gets a stable name, so PostgreSQL keeps a cached plan for it. Three\n * properties matter, and the first version of this file had none of them:\n *\n * 1. **The bound has to be a bound on the SERVER** (F16). Evicting a local\n * mapping and minting a fresh name leaves the old prepared statement on\n * every backend that ever saw it — the local map stayed small while server\n * retention grew without limit. Nor can eviction be repaired with\n * `DEALLOCATE`: a name is prepared per CONNECTION, the adapter does not know\n * which pooled connections hold it, and deallocating behind node-postgres'\n * back invalidates its own per-client name registry (it would keep sending\n * the name and the server would refuse it).\n *\n * So the registry never re-uses a name for another text and never evicts:\n * it allocates at most `max` names, and once that budget is spent a new text\n * runs UNNAMED — bounded preparation fallback. Server retention is then\n * `max` per connection, permanently. Statements still bind their parameters;\n * only the server-side plan cache is skipped.\n *\n * 2. **Adapters sharing one pool must not collide** (F18). Each adapter used to\n * start its own counter at zero, so two adapters on one pool both sent\n * `vibeorm_0` for different SQL and postgres refused the second\n * (\"Prepared statements must be unique\"). Names are therefore allocated by a\n * registry shared per pool, which also stops N adapters from preparing the\n * same text N times.\n *\n * 3. **A one-off text must not spend the budget.** Because names never evict\n * (property 1), allocating on FIRST sight hands the whole budget to whatever\n * arrives first. Offset pagination emits a distinct text per page, so a\n * paginating workload used to burn all `max` names for the pool's lifetime\n * and leave every genuinely hot text unnamed thereafter. A name is therefore\n * allocated on the `promoteAfter`-th sighting of a text. Sightings live in\n * their own bounded LRU (capped at {@link SIGHTINGS_PER_NAME} × `max`);\n * evicting a sighting only delays a promotion, it can never name the wrong\n * text or exceed the budget. A hot text loses exactly one prepared\n * execution; a one-off text costs the server nothing.\n */\n\n/** Sightings of a text before it earns a name. */\nconst DEFAULT_PROMOTE_AFTER = 2;\n\n/**\n * Sighting slots per name of budget. The sighting map is a pure heuristic —\n * over-sizing it only wastes memory, under-sizing it only delays promotions —\n * so a small multiple of the name budget is enough to see a repeat through a\n * burst of one-off texts.\n */\nconst SIGHTINGS_PER_NAME = 4;\n\n/** Registry mapping SQL text to the prepared-statement name `pg` should send. */\nexport type StatementNameCache = {\n /**\n * Stable statement name for `text` — or `undefined` meaning \"send this one\n * unnamed\", which is the answer until the text has been seen `promoteAfter`\n * times and again once the name budget is spent.\n */\n getName(params: { text: string }): string | undefined;\n /** Number of names allocated — diagnostics and tests. */\n size(): number;\n /** Number of texts tracked toward promotion — diagnostics and tests. */\n sightings(): number;\n};\n\n/**\n * Create a statement-name registry that allocates at most `max` names\n * (`vibeorm_0`, `vibeorm_1`, …), each on the `promoteAfter`-th sighting of its\n * text (default 2). Texts seen fewer times, and texts arriving after the budget\n * is spent, get no name and run unprepared, so the server's prepared-statement\n * retention stays bounded and a stream of one-off texts cannot consume it.\n */\nexport function createStatementNameCache(params: { max: number; promoteAfter?: number }): StatementNameCache {\n const max = Math.max(0, params.max);\n const promoteAfter = Math.max(1, params.promoteAfter ?? DEFAULT_PROMOTE_AFTER);\n const sightingsMax = max * SIGHTINGS_PER_NAME;\n /** Never evicted, never re-used: this map is the server-side bound. */\n const names = new Map<string, string>();\n /** Insertion-ordered, so the first key is the least recently seen text. */\n const sightings = new Map<string, number>();\n let counter = 0;\n\n return {\n getName(nameParams: { text: string }): string | undefined {\n const { text } = nameParams;\n const existing = names.get(text);\n if (existing !== undefined) return existing;\n // Budget spent (and the `max: 0` case): nothing left to promote into, so\n // do not spend memory counting sightings either.\n if (names.size >= max) return undefined;\n\n const seen = (sightings.get(text) ?? 0) + 1;\n if (seen < promoteAfter) {\n // Re-insert to move the text to the most-recently-seen end.\n sightings.delete(text);\n sightings.set(text, seen);\n while (sightings.size > sightingsMax) {\n const oldest = sightings.keys().next();\n if (oldest.done === true) break;\n sightings.delete(oldest.value);\n }\n return undefined;\n }\n\n sightings.delete(text);\n const name = `vibeorm_${counter++}`;\n names.set(text, name);\n return name;\n },\n\n size(): number {\n return names.size;\n },\n\n sightings(): number {\n return sightings.size;\n },\n };\n}\n",
10
10
  "/**\n * Transaction-control SQL text (postgres dialect).\n *\n * Kept apart from the adapter so the mapping from `TransactionOptions` to SQL\n * is unit-testable without a server, and so the timeout value is validated\n * before it is ever interpolated into a statement.\n */\n\nimport type { IsolationLevel, TransactionOptions } from \"@vibeorm/runtime\";\nimport { VibeError } from \"@vibeorm/schema\";\n\n/** VibeORM isolation level → PostgreSQL isolation level keyword. */\nexport const ISOLATION_LEVEL_SQL: Readonly<Record<IsolationLevel, string>> = Object.freeze({\n ReadCommitted: \"READ COMMITTED\",\n RepeatableRead: \"REPEATABLE READ\",\n Serializable: \"SERIALIZABLE\",\n});\n\n/** `BEGIN`, with an `ISOLATION LEVEL` clause when the caller asked for one. */\nexport function beginStatement(params: { isolationLevel?: IsolationLevel }): string {\n const { isolationLevel } = params;\n return isolationLevel === undefined\n ? \"BEGIN\"\n : `BEGIN ISOLATION LEVEL ${ISOLATION_LEVEL_SQL[isolationLevel]}`;\n}\n\n/**\n * `SET LOCAL statement_timeout = <ms>` — transaction-scoped, so PostgreSQL\n * cancels an over-long statement with SQLSTATE 57014 (mapped to\n * `VIBE_TRANSACTION` with `meta.reason = \"timeout\"`).\n *\n * @throws VibeError `VIBE_VALIDATION` when the timeout is not a non-negative\n * integer — the value is interpolated into SQL, so it is never trusted blindly.\n */\nexport function statementTimeoutStatement(params: { timeout: number }): string {\n const { timeout } = params;\n if (!Number.isInteger(timeout) || timeout < 0) {\n throw new VibeError({\n code: \"VIBE_VALIDATION\",\n message: `transaction timeout must be a non-negative integer of milliseconds, received ${String(timeout)}`,\n meta: { timeout },\n });\n }\n return `SET LOCAL statement_timeout = ${timeout}`;\n}\n\n// ─── Raw transaction control (session affinity) ─────────────────\n\n/** How a raw statement steers the adapter's session-affine raw transaction. */\nexport type RawTransactionControl = \"open\" | \"close\" | null;\n\nconst RAW_OPEN_PATTERN: RegExp = /^\\s*(?:begin|start\\s+transaction)\\b[^;]*;?\\s*$/i;\nconst RAW_CLOSE_PATTERN: RegExp = /^\\s*(?:commit|end|rollback|abort)(?:\\s+(?:transaction|work))?\\s*;?\\s*$/i;\n\n/**\n * Classify a raw statement as transaction control.\n *\n * @vibeorm/migrate drives transactional DDL by sending literal `BEGIN` /\n * `COMMIT` / `ROLLBACK` through its stateless `SqlExecutor` — sound only when\n * every statement in between rides the SAME connection. Without pinning, a\n * pooled `pool.query(\"BEGIN\")` sequence stays on one client only by LIFO\n * checkout luck; any concurrent traffic silently splits the transaction\n * across connections. The adapter uses this to pin one client for the whole\n * raw transaction (\"open\" checks out, \"close\" releases).\n *\n * Deliberately strict: only PURE single-statement control text matches\n * (`BEGIN`, `BEGIN ISOLATION LEVEL …;`, `COMMIT`, `ROLLBACK WORK`, `END`,\n * `ABORT`). Compound scripts, `ROLLBACK TO SAVEPOINT`, two-phase\n * `COMMIT PREPARED` and `COMMIT AND CHAIN` (which keeps a transaction open)\n * never engage the pinning.\n */\nexport function classifyRawTransactionControl(params: { text: string }): RawTransactionControl {\n if (RAW_OPEN_PATTERN.test(params.text)) return \"open\";\n if (RAW_CLOSE_PATTERN.test(params.text)) return \"close\";\n return null;\n}\n\n// ─── Nested transaction options (the cross-adapter contract) ──────\n\n/**\n * Refuse options on a NESTED `transaction()` call — the same contract on every\n * adapter (see the `DatabaseAdapter.transaction` JSDoc in @vibeorm/runtime):\n * a nested transaction is a SAVEPOINT, and a savepoint can neither change the\n * isolation level of the transaction it joins nor enforce its own timeout.\n * Silently dropping the option (the pre-#3 behaviour) hid exactly that.\n *\n * @throws VibeError `VIBE_VALIDATION` when `isolationLevel` or `timeout` is\n * present (an `undefined` or empty options object is accepted).\n */\nexport function refuseNestedTransactionOptions(params: {\n options?: TransactionOptions;\n provider: string;\n}): void {\n const { options, provider } = params;\n if (options === undefined) return;\n\n const passed: string[] = [];\n if (options.isolationLevel !== undefined) passed.push(\"isolationLevel\");\n if (options.timeout !== undefined) passed.push(\"timeout\");\n if (options.deadline !== undefined) passed.push(\"deadline\");\n if (passed.length === 0) return;\n\n const meta: Record<string, unknown> = { provider, nested: true };\n if (options.isolationLevel !== undefined) meta.isolationLevel = options.isolationLevel;\n if (options.timeout !== undefined) meta.timeout = options.timeout;\n if (options.deadline !== undefined) meta.deadline = options.deadline;\n\n throw new VibeError({\n code: \"VIBE_VALIDATION\",\n message: `nested transactions run as savepoints and cannot honor ${passed.join(\" or \")} — a savepoint cannot change the isolation level of the transaction it joins, a nested timeout is not enforceable, and a nested deadline would need its own session setting on a connection it does not own (it inherits the top-level one instead); pass options on the top-level $transaction`,\n meta,\n });\n}\n"
11
11
  ],
12
- "mappings": ";;AAeA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaA,sBAAS;AAET;;;ACrBA;AAQO,IAAM,0BAAmE,OAAO,OAAO;AAAA,EAE5F,SAAS;AAAA,EAET,SAAS;AAAA,EAET,SAAS;AAAA,EAET,SAAS;AAAA,EAMT,SAAS;AAAA,EAMT,SAAS;AAAA,EAET,SAAS;AAAA,EAET,SAAS;AAAA,EAET,SAAS;AACX,CAAC;AAGM,IAAM,sBAAwD,OAAO,OAAO;AAAA,EACjF,SAAS;AACX,CAAC;AAKD,IAAM,mBAAmB;AAEzB,SAAS,eAAe,CAAC,QAA8E;AAAA,EACrG,MAAM,QAAQ,OAAO,OAAO,OAAO;AAAA,EACnC,OAAO,OAAO,UAAU,YAAY,MAAM,SAAS,IAAI,QAAQ;AAAA;AAQ1D,SAAS,eAAe,CAAC,QAAgD;AAAA,EAC9E,QAAQ,UAAU;AAAA,EAClB,IAAI,UAAU,QAAQ,OAAO,UAAU;AAAA,IAAU;AAAA,EACjD,MAAM,OAAO,gBAAgB,EAAE,QAAQ,OAAkC,KAAK,OAAO,CAAC;AAAA,EACtF,OAAO,SAAS,aAAa,iBAAiB,KAAK,IAAI,IAAI,OAAO;AAAA;AAW7D,SAAS,gBAAgB,CAAC,QAAuC;AAAA,EACtE,QAAQ,UAAU;AAAA,EAClB,IAAI,iBAAiB;AAAA,IAAW,OAAO;AAAA,EAEvC,MAAM,SACJ,UAAU,QAAQ,OAAO,UAAU,WAAY,QAAoC,CAAC;AAAA,EACtF,MAAM,WAAW,gBAAgB,EAAE,MAAM,CAAC;AAAA,EAC1C,MAAM,QACH,aAAa,YAAY,YAAY,wBAAwB,cAAc;AAAA,EAE9E,MAAM,OAAgC,CAAC;AAAA,EACvC,IAAI,aAAa;AAAA,IAAW,KAAK,WAAW;AAAA,EAC5C,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AM,EAAE,SAAS,UAAU,CAAC;AAAA,UACnC,IAAI,OAAO,YAAY,QAAQ,2BAA2B,KAAK,GAAG;AAAA,YAChE,MAAM,+BAA+B;AAAA,cACnC,UAAU;AAAA,cACV,SAAS,OAAO;AAAA,cAChB,OAAO;AAAA,YACT,CAAC;AAAA,UACH;AAAA;AAAA,QAEF,MAAM;AAAA,gBACN;AAAA,QACA,QAAQ;AAAA,QACR,OAAO,QAAQ,OAAO;AAAA;AAAA;AAAA,SAIpB,QAAO,GAAkB;AAAA,MAC7B,MAAM,SAAS,MAAM,UAAU,MAAM,QAAQ,EAAE,QAAQ,CAAC;AAAA,MACxD,OAAO,QAAQ;AAAA;AAAA,SAGX,WAAU,GAAkB;AAAA,MAChC,IAAI,eAAe,MAAM;AAAA,QAGvB,MAAM,UAAU;AAAA,QAChB,MAAM,UAAU;AAAA,QAChB,aAAa;AAAA,QACb,oBAAoB;AAAA,QACpB,IAAI;AAAA,UACF,MAAM,QAAQ,MAAM,EAAE,MAAM,WAAW,CAAC;AAAA,UACxC,MAAM;AAAA,QAGR,UAAU;AAAA,QACV,QAAQ,QAAQ;AAAA,MAClB;AAAA,MACA,IAAI,SAAS,QAAQ,UAAU;AAAA,QAC7B,MAAM,UAAU;AAAA,QAChB,OAAO;AAAA,QACP,MAAM,UAAU,MAAM,QAAQ,IAAI,CAAC;AAAA,MACrC;AAAA;AAAA,IAGF,gBAAgB,CAAC,QAA4B;AAAA,MAC3C,OAAO,IAAI,aAAa,MAAM;AAAA;AAAA,EAElC;AAAA;",
13
+ "debugId": "990A1E48379577DE64756E2164756E21",
14
14
  "names": []
15
15
  }
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Bounded prepared-statement NAME registry, shared per pool.
3
3
  *
4
- * With `preparedStatements: true` every distinct SQL text gets a stable name, so
5
- * PostgreSQL keeps a cached plan for it. Two properties matter, and the first
6
- * version of this file had neither:
4
+ * With `preparedStatements: true` every SQL text that proves itself repeated
5
+ * gets a stable name, so PostgreSQL keeps a cached plan for it. Three
6
+ * properties matter, and the first version of this file had none of them:
7
7
  *
8
8
  * 1. **The bound has to be a bound on the SERVER** (F16). Evicting a local
9
9
  * mapping and minting a fresh name leaves the old prepared statement on
@@ -26,25 +26,42 @@
26
26
  * ("Prepared statements must be unique"). Names are therefore allocated by a
27
27
  * registry shared per pool, which also stops N adapters from preparing the
28
28
  * same text N times.
29
+ *
30
+ * 3. **A one-off text must not spend the budget.** Because names never evict
31
+ * (property 1), allocating on FIRST sight hands the whole budget to whatever
32
+ * arrives first. Offset pagination emits a distinct text per page, so a
33
+ * paginating workload used to burn all `max` names for the pool's lifetime
34
+ * and leave every genuinely hot text unnamed thereafter. A name is therefore
35
+ * allocated on the `promoteAfter`-th sighting of a text. Sightings live in
36
+ * their own bounded LRU (capped at {@link SIGHTINGS_PER_NAME} × `max`);
37
+ * evicting a sighting only delays a promotion, it can never name the wrong
38
+ * text or exceed the budget. A hot text loses exactly one prepared
39
+ * execution; a one-off text costs the server nothing.
29
40
  */
30
41
  /** Registry mapping SQL text to the prepared-statement name `pg` should send. */
31
42
  export type StatementNameCache = {
32
43
  /**
33
- * Stable statement name for `text`, allocating one on first sight — or
34
- * `undefined` once the name budget is spent, meaning "send this one unnamed".
44
+ * Stable statement name for `text` — or `undefined` meaning "send this one
45
+ * unnamed", which is the answer until the text has been seen `promoteAfter`
46
+ * times and again once the name budget is spent.
35
47
  */
36
48
  getName(params: {
37
49
  text: string;
38
50
  }): string | undefined;
39
51
  /** Number of names allocated — diagnostics and tests. */
40
52
  size(): number;
53
+ /** Number of texts tracked toward promotion — diagnostics and tests. */
54
+ sightings(): number;
41
55
  };
42
56
  /**
43
57
  * Create a statement-name registry that allocates at most `max` names
44
- * (`vibeorm_0`, `vibeorm_1`, …). Texts beyond the budget get no name and run
45
- * unprepared, so the server's prepared-statement retention stays bounded.
58
+ * (`vibeorm_0`, `vibeorm_1`, …), each on the `promoteAfter`-th sighting of its
59
+ * text (default 2). Texts seen fewer times, and texts arriving after the budget
60
+ * is spent, get no name and run unprepared, so the server's prepared-statement
61
+ * retention stays bounded and a stream of one-off texts cannot consume it.
46
62
  */
47
63
  export declare function createStatementNameCache(params: {
48
64
  max: number;
65
+ promoteAfter?: number;
49
66
  }): StatementNameCache;
50
67
  //# sourceMappingURL=statement-cache.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"statement-cache.d.ts","sourceRoot":"","sources":["../src/statement-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,iFAAiF;AACjF,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;OAGG;IACH,OAAO,CAAC,MAAM,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,GAAG,SAAS,CAAC;IACtD,yDAAyD;IACzD,IAAI,IAAI,MAAM,CAAC;CAChB,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,kBAAkB,CAoBpF"}
1
+ {"version":3,"file":"statement-cache.d.ts","sourceRoot":"","sources":["../src/statement-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAaH,iFAAiF;AACjF,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;;OAIG;IACH,OAAO,CAAC,MAAM,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,GAAG,SAAS,CAAC;IACtD,yDAAyD;IACzD,IAAI,IAAI,MAAM,CAAC;IACf,wEAAwE;IACxE,SAAS,IAAI,MAAM,CAAC;CACrB,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,kBAAkB,CA8C3G"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vibeorm/adapter-pg",
3
- "version": "2.0.2",
3
+ "version": "2.1.1",
4
4
  "description": "node-postgres adapter for VibeORM v2 — Bun or Node.js",
5
5
  "keywords": [
6
6
  "orm",
@@ -43,8 +43,8 @@
43
43
  "bun": ">=1.2.0"
44
44
  },
45
45
  "dependencies": {
46
- "@vibeorm/runtime": "2.0.2",
47
- "@vibeorm/schema": "2.0.0"
46
+ "@vibeorm/runtime": "2.2.0",
47
+ "@vibeorm/schema": "2.1.1"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "pg": ">=8"