Files
PaperClipAI/packages/db/src/prepared-queries-postgres.test.ts
T
DottaandClaude Opus 5.5 b1e70ab6dd fix(db): recover prepared statements that a pooler or schema change invalidates (#21718)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The server runs every database statement from one Node main thread.
With `prepare: true`, postgres.js sends a statement in one round trip
instead of two, and the CPU cost of each statement is about half
(benchmark from #21707).
> - `DATABASE_PREPARED_STATEMENTS=true` turns on preparation for ORM
queries (#17673). Hosted instances connect through a transaction-mode
pooler (PgBouncer 1.22 or later, Neon `-pooler` hosts), which supports
protocol-level prepared statements.
> - I tested the flag locally through PgBouncer 1.26.1 in transaction
mode. Evictions, server connection churn, `RECONNECT`, transactions,
pipelining, `SET LOCAL`, advisory transaction locks and savepoints all
work. One failure is not acceptable: after a migration changes a column
type, every prepared statement that reads that column fails, for every
client and for every new process.
> - PgBouncer shares one server-side prepared statement by SQL text.
PostgreSQL never repairs a named statement whose result type changed, so
it fails with `cached plan must not change result type` until PgBouncer
drops it. The postgres.js retry prepares the same text again, and
PgBouncer gives it the same broken statement. postgres.js also caches
the failed statement without columns, so the connection throws
`TypeError: Cannot read properties of undefined (reading 'length')` for
that text even after the schema is correct again.
> - This pull request makes the client recover. When a prepared
statement fails with a prepared-statement error, the client stops
preparing that text, retries once without preparation where that is
safe, and counts it. It also caps the number of prepared texts.
> - The benefit is that the flag can be turned on behind a pooler
without a deploy migration that breaks reads. Behavior with the flag
unset or `false` does not change.

## Linked Issues or Issue Description

Refs #17673 (the opt-in), #10795 (the flag), #21707 (the CPU benchmark).

**What happened?**

With `DATABASE_PREPARED_STATEMENTS=true` behind PgBouncer 1.26.1 in
transaction mode (`max_prepared_statements=5`, local PostgreSQL 17), a
change of a column type made every later execution of an affected
statement fail. A new client process failed in the same way. Measured
with the local probe, 20 executions after the change and 10 from a new
process:

| Change | Before: failures | After: failures |
| --- | --- | --- |
| `int` to `bigint`, ORM read outside a transaction | 20/20, new process
10/10 (0A000) | 0/20, 0/10 |
| `varchar` length change, ORM read | 20/20, 10/10 | 0/20, 0/10 |
| Add column, `select *` | 20/20, 10/10 | 0/20, 0/10 |
| `int` to `bigint`, first statement of a transaction | 20/20, 10/10 |
0/20, 0/10 |
| `int` to `bigint`, second statement of a transaction | 20/20, 10/10 |
1/20, 1/10 |
| Table recreated with its original shape, same client | 10/10
(TypeError in `DataRow`) | 0/10 |
| Add column, explicit-column ORM read | 0/20 | 0/20 |

**Expected behavior**

A statement whose pooled prepared statement is stale or lost runs
anyway, and the failure does not reach the caller.

**Steps to reproduce**

1. Run PostgreSQL behind PgBouncer 1.22 or later with `pool_mode =
transaction` and `max_prepared_statements` above 0.
2. Start Paperclip with `DATABASE_PREPARED_STATEMENTS=true` and run a
read of a table a few times.
3. Change the type of a column that the read returns, for example `alter
table ... alter column n type bigint`.
4. Run the read again from the same process or from a new process: it
fails with `cached plan must not change result type`.

## What Changed

- `packages/db/src/prepared-queries.ts`:
- The prepare decision for each `unsafe` query is made when the driver
builds the query, not when the caller creates it, so a query that waits
on a busy connection sees a text that was demoted after it was queued.
- A failure with 26000 (`prepared statement does not exist`), 42P05
(duplicate statement name), or 0A000 `cached plan must not change result
type` demotes the text: the client does not prepare it again in that
process.
- A pooled statement that fails in that way is retried once with
`prepare: false`. Statements in a reserved connection, cursors,
`forEach`, `describe` and queries with `onexecute` are not retried.
- When the failed statement is the first statement that the transaction
ran (only `SET TRANSACTION` before it), the client restarts the
transaction on the same connection: `ROLLBACK`, `BEGIN` with the same
options, the same `SET TRANSACTION` statements, then the statement
unprepared. The callback runs once and receives the result. Tagged
statements and statements in savepoints count toward the execution
order. The client does not restart when another statement of the
transaction is queued, and it never restarts inside a savepoint.
- At most 1,000 distinct texts are prepared for each client and its
private pools. Texts longer than 8 KB are not prepared. Explicit
per-query `prepare` options, simple queries and the flag-off paths are
unchanged.
- `packages/db/src/client.ts`, `packages/db/src/index.ts`: a client and
its dedicated, liveness, checkpoint, file-storage and listener pools
share one state. `preparedStatementStats(db)` returns the prepared,
unprepared, demoted and retried counts.
- `packages/db/src/prepared-queries-postgres.test.ts`: nine tests
against embedded PostgreSQL: restart after a stale plan and after a lost
statement (the callback runs once and its later work commits),
`repeatable read read only` kept after a restart for driver and ORM
transactions, no restart after a later statement, after a tagged first
statement, or for an out-of-order await, a savepoint whose first
statement fails while the outer transaction continues once, the pooled
retry with `.values()`, and the cap.
- `doc/DATABASE.md`, `docs/deploy/database.md`: the cap and the
recovery.

## Verification

- Red/green, `pnpm --filter @paperclipai/db exec vitest run
src/prepared-queries-postgres.test.ts`. With the old
`prepared-queries.ts` (stub stats exports only), the five new tests
fail: two transactions fail with the stale-plan and the
missing-statement errors, the ORM read fails with 42P05, the invalidated
text is prepared again, and 5 texts are prepared where the cap is 2.
With this change, 23/23 pass.
- `pnpm --filter @paperclipai/db exec vitest run` on
`prepared-queries-postgres`, `client-options`, `client`,
`dedicated-connection-pool`, `dedicated-connection-admission`,
`response-capture-postgres`, `lazy-transaction-queries`,
`client-disconnect-replay`: 91/91 pass on the final revision. `tsc
--noEmit -p packages/db` passes.
- 31 server test files with `DATABASE_PREPARED_STATEMENTS=true`
(services, routes, inbox, recovery, heartbeat events, agent auth,
prepared-statement helpers): 829/830 pass. The one failure
(`execution-workspaces-service`, a git-scan timing test) passes alone
with the flag on and off. The machine load average was 9 to 20.
- Local PgBouncer probe (not in CI, because CI has no PgBouncer):
PgBouncer 1.26.1, PostgreSQL 17, postgres.js 3.4.9 (patched), Node 26.4.
The results in the table above, plus 0 failures for 4,800 statements
over 60 distinct texts with `max_prepared_statements=5`, 0 failures with
`pg_terminate_backend` and `RECONNECT` every 25 statements, and 0
failures for transactions with `SET LOCAL`, pipelines of 20 statements,
`pg_advisory_xact_lock` and savepoint rollback.

- Through PgBouncer 1.26.1 (transaction mode,
`max_prepared_statements=5`, 4 server connections) and PostgreSQL 17, at
head 84732c8f67, compared with the master `prepared-queries.ts`. Each
row ran 10 times on its own client, warmed before the migration. "Type"
is `int` to `bigint` under an ORM read; "star" is an added column under
`select *`. Failures before → after:

| Row | Type change | Added column, `select *` |
| --- | --- | --- |
| (a) outside a transaction, same process | 10 → 0 | 10 → 0 |
| (b) first statement of an ORM transaction (callback runs once) | 10 →
0 | 10 → 0 |
| (b) first statement, `begin isolation level repeatable read read only`
| 0 → 0 (the statement was evicted and prepared again) | 10 → 0 |
| (b) first statement, ORM `repeatable read`, `read only` (`SET
TRANSACTION` repeated) | 10 → 0 | 10 → 0 |
| (c) later statement of a transaction, same process | 10 → 1 | 10 → 1 |
| (d) new process after the migration: (a) / (b) / (b) driver / (b) ORM
/ (c) | 10/10/0/0/0 → 0/0/0/0/1 | 10/10/10/10/10 → 0/0/0/0/1 |
| (e) same client after the table is restored: (a) / (b) / (b) driver /
(b) ORM / (c) | 10/10/0/10/10 → 0 | 10 TypeError each → 0 |

Every restarted transaction checked `transaction_isolation = repeatable
read` and `transaction_read_only = on` after the restart. The PostgreSQL
log shows each restart as `ROLLBACK`, `BEGIN` with the same options,
then the statement as an unnamed statement. With 12 clients restarting
at once over 2 server connections and 6 clients of background load,
12/12 transactions succeeded. In the 15 restarts I paired by
`application_name`, the second `BEGIN` ran on the same server connection
as the `ROLLBACK`, because both are sent together. A different server
connection is also safe: only `SET TRANSACTION` ran before the restart
point, and the client repeats it after the new `BEGIN`.
- Silent rollback check through PgBouncer with
`max_prepared_statements=2`: 3 clients ran 600 transactions, each with
an insert and 6 statements over 40 distinct texts, and more distinct
statements between transactions. PgBouncer parsed 6,409 statements on
the server for 1,089 client parses, so it evicted and prepared again the
shared `commit` (`PGBOUNCER_15`) many times. All 600 `COMMIT`s
succeeded, 600/600 rows were present, and no 26000 error was logged. The
silent rollback needs a `DEALLOCATE` on the session, which PgBouncer
does not allow (it closes the client) and Paperclip does not send.

## Risks

- A transaction restart happens below the callback. Code in the callback
before its first statement runs once. The restarted transaction has a
new transaction id.
- A postgres.js behavior that this PR does not change: with driver-level
preparation (the flag `true`, or unset for tagged SQL), the tagged
`commit` of `sql.begin` is a prepared statement. If something
deallocates it on the session, the driver retries `COMMIT` in the
aborted transaction, which PostgreSQL turns into a rollback without an
error. Through PgBouncer this did not happen in 600 transactions with
the `commit` statement evicted repeatedly (see Verification). It needs a
manual `DEALLOCATE` on a direct session.
- A later statement in a transaction still fails once for each affected
text in each process after a migration that changes a column type. The
text then runs unprepared.
- A demoted text stays unprepared until the process restarts. The cap
admits texts in first-use order.
- A client that sends `DEALLOCATE ALL` through PgBouncer is disconnected
by PgBouncer. Paperclip does not send it.
- With the flag unset or `false`, the code path is the same as before.

## Model Used

Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, via Claude Code with
tool use (shell, local PostgreSQL and PgBouncer in a throwaway
container, test execution).

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 21:57:04 -06:00

358 lines
20 KiB
TypeScript

import { eq, sql } from "drizzle-orm";
import postgres from "postgres";
import { integer, jsonb, pgTable, text } from "drizzle-orm/pg-core";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { closeLivenessDb, createDb, databaseClientOptionsFromEnv, livenessDb, preparedStatementStats, withDedicatedDbConnection, type DatabaseClientOptions, type Db } from "./client.js";
import { createPreparedQueryState, installPreparedQueries, preparedQueryStats } from "./prepared-queries.js";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./test-embedded-postgres.js";
import { runEventPayload } from "./run-event-payload.js";
const records = pgTable("prepared_query_fixture", { id: text("id").primaryKey(), companyId: text("company_id").notNull(), value: integer("value").notNull(), payload: jsonb("payload") });
const stale = pgTable("prepared_stale_fixture", { id: text("id").primaryKey(), value: integer("value").notNull() });
const encoded = pgTable("prepared_encoded_fixture", { id: text("id").primaryKey(), payload: runEventPayload("payload") });
const support = await getEmbeddedPostgresTestSupport();
describe.skipIf(!support.supported)("explicit ORM prepared statements on PostgreSQL", () => {
let temporary: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
let setup: Db;
const clients: Db[] = [];
beforeAll(async () => {
temporary = await startEmbeddedPostgresTestDatabase("prepared-orm-");
setup = createDb(temporary.connectionString, { prepare: false, maxConnections: 1 });
await setup.execute(sql`create table prepared_query_fixture(id text primary key, company_id text not null, value integer not null, payload jsonb)`);
await setup.execute(sql`create table prepared_encoded_fixture(id text primary key, payload jsonb)`);
await setup.execute(sql`create table prepared_schema_change_fixture(id text primary key)`);
await setup.execute(sql`create table prepared_stale_fixture(id text primary key, value integer not null)`);
await setup.execute(sql`insert into prepared_stale_fixture(id, value) values ('stale', 5)`);
await setup.execute(sql`insert into prepared_schema_change_fixture(id) values ('retained')`);
}, 90_000);
beforeEach(async () => {
await setup.execute(sql`truncate prepared_query_fixture, prepared_encoded_fixture`);
await setup.insert(records).values([
{ id: "a", companyId: "company-a", value: 1, payload: { owner: "a" } },
{ id: "b", companyId: "company-b", value: 2, payload: { owner: "b" } },
]);
});
afterEach(async () => {
for (const db of clients.splice(0)) {
await closeLivenessDb(db, 2);
await db.$client.end();
}
});
afterAll(async () => { await setup?.$client.end(); await temporary?.cleanup(); });
function fixture(options: DatabaseClientOptions = {}) {
const db = createDb(temporary.connectionString, { ...options, maxConnections: 1 });
clients.push(db); return db;
}
async function statements(db: Db, pattern = '%"prepared_query_fixture"%') {
return db.$client.unsafe("select name,statement from pg_prepared_statements where statement like $1", [pattern]);
}
it.each([undefined, false])("keeps the existing ORM mode when prepare is %s", async prepare => {
const db = fixture({ prepare });
expect(await db.select().from(records).where(eq(records.companyId, "company-a"))).toEqual([
{ id: "a", companyId: "company-a", value: 1, payload: { owner: "a" } },
]);
expect(await statements(db)).toHaveLength(0);
});
it("honors the explicit environment opt-in for ORM queries with fresh company bindings", async () => {
const db = fixture(databaseClientOptionsFromEnv({ DATABASE_PREPARED_STATEMENTS: "true" }));
for (let i = 0; i < 12; i++) {
const companyId = i % 2 ? "company-b" : "company-a";
const rows = await db.select().from(records).where(eq(records.companyId, companyId));
expect(rows).toEqual([{ id: i % 2 ? "b" : "a", companyId, value: i % 2 ? 2 : 1, payload: { owner: i % 2 ? "b" : "a" } }]);
}
expect(await statements(db)).toHaveLength(1);
});
it("preserves explicit per-query opt-out", async () => {
const db = fixture({ prepare: true });
expect((await db.$client.unsafe("select $1::integer as explicit_opt_out_marker", [7], { prepare: false }))[0].explicit_opt_out_marker).toBe(7);
expect(await statements(db, "%explicit_opt_out_marker%")).toHaveLength(0);
});
it("preserves simple queries and the two-argument options overload", async () => {
const db = fixture({ prepare: true });
const simpleOptions = { prepare: true, simple: true };
const simple = await db.$client.unsafe("select 1 as first; select 2 as second", [], simpleOptions);
expect(simple).toEqual([[{ first: 1 }], [{ second: 2 }]]);
const unsafe = db.$client.unsafe as unknown as (query: string, options: { simple: boolean }) => Promise<unknown>;
expect(await unsafe("select 3 as third; select 4 as fourth", { simple: true }))
.toEqual([[{ third: 3 }], [{ fourth: 4 }]]);
});
it("does not replay a rejected prepared write", async () => {
const db = fixture({ prepare: true });
await expect(db.insert(records).values({ id: "a", companyId: "company-b", value: 9 }))
.rejects.toMatchObject({ cause: { code: "23505" } });
expect(await db.select({ companyId: records.companyId, value: records.value }).from(records).where(eq(records.id, "a")))
.toEqual([{ companyId: "company-a", value: 1 }]);
});
it("keeps write queries lazy and repeated awaits from repeating writes", async () => {
const db = fixture({ prepare: true });
const pending = db.$client.unsafe("insert into prepared_query_fixture(id,company_id,value) values ($1,$2,$3) returning id", ["lazy", "company-a", 3]);
expect(typeof pending.values).toBe("function");
expect(await setup.select().from(records).where(eq(records.id, "lazy"))).toEqual([]);
expect(await pending).toEqual([{ id: "lazy" }]);
expect(await pending).toEqual([{ id: "lazy" }]);
expect(await setup.select({ id: records.id }).from(records).where(eq(records.id, "lazy"))).toEqual([{ id: "lazy" }]);
expect(await statements(db, "%insert into prepared_query_fixture%")).toHaveLength(1);
});
it("preserves transaction commits, nested savepoint rollback, and scoped reads", async () => {
const db = fixture({ prepare: true });
await db.transaction(async tx => {
expect(await tx.select({ value: records.value }).from(records).where(eq(records.companyId, "company-a"))).toEqual([{ value: 1 }]);
await expect(tx.transaction(async nested => {
await nested.update(records).set({ value: 99 }).where(eq(records.companyId, "company-a"));
throw new Error("rollback savepoint");
})).rejects.toThrow("rollback savepoint");
expect(await tx.select({ value: records.value }).from(records).where(eq(records.companyId, "company-a"))).toEqual([{ value: 1 }]);
await tx.insert(records).values({ id: "committed", companyId: "company-b", value: 3 });
const names = await tx.execute(sql`select statement from pg_prepared_statements`);
expect(names.some(row => String(row.statement).includes('"prepared_query_fixture"'))).toBe(true);
}, { isolationLevel: "serializable" });
expect(await db.select({ value: records.value }).from(records).where(eq(records.id, "committed"))).toEqual([{ value: 3 }]);
});
it("rolls back failed outer transactions without replay", async () => {
const db = fixture({ prepare: true });
await expect(db.transaction(async tx => {
await tx.update(records).set({ value: 42 }).where(eq(records.id, "a"));
throw new Error("rollback outer transaction");
})).rejects.toThrow("rollback outer transaction");
expect(await db.select({ value: records.value }).from(records).where(eq(records.id, "a"))).toEqual([{ value: 1 }]);
});
it("prepares reserved-client queries and keeps their bound values fresh", async () => {
const db = fixture({ prepare: true });
const reserved = await db.$client.reserve();
try {
for (const marker of [1, 2, 3]) {
expect((await reserved.unsafe("select $1::integer as reserved_prepared_marker", [marker]))[0].reserved_prepared_marker).toBe(marker);
}
const names = await reserved.unsafe("select statement from pg_prepared_statements where statement like $1", ["%reserved_prepared_marker%"]);
expect(names).toHaveLength(1);
} finally { reserved.release(); }
});
it("preserves object and array results for the same prepared shape", async () => {
const db = fixture({ prepare: true });
expect(await db.$client.unsafe("select $1::integer as result_mode_marker", [41]).values()).toEqual([[41]]);
expect(await db.$client.unsafe("select $1::integer as result_mode_marker", [42])).toEqual([{ result_mode_marker: 42 }]);
});
it("keeps custom stored-payload decoding fresh", async () => {
const db = fixture({ prepare: true });
const first = { text: "encoded first payload🙂".repeat(2048) };
const second = { text: "encoded second payload🙂".repeat(2048) };
await setup.insert(encoded).values({ id: "encoded", payload: first });
expect((await db.select().from(encoded).where(eq(encoded.id, "encoded")))[0].payload).toEqual(first);
await setup.update(encoded).set({ payload: second }).where(eq(encoded.id, "encoded"));
expect((await db.select().from(encoded).where(eq(encoded.id, "encoded")))[0].payload).toEqual(second);
expect(await statements(db, '%"prepared_encoded_fixture"%')).toHaveLength(1);
});
it("inherits the opt-in in dedicated and liveness clients", async () => {
const db = fixture({ prepare: true });
await withDedicatedDbConnection(db, async dedicated => {
expect(await dedicated.select({ value: records.value }).from(records).where(eq(records.companyId, "company-b"))).toEqual([{ value: 2 }]);
expect(await statements(dedicated)).toHaveLength(1);
});
const live = livenessDb(db);
expect(await live.select({ value: records.value }).from(records).where(eq(records.companyId, "company-a"))).toEqual([{ value: 1 }]);
expect(await statements(live)).toHaveLength(1);
});
it("refreshes prepared result metadata after a schema change", async () => {
const db = fixture({ prepare: true });
const query = "select * from prepared_schema_change_fixture where id=$1";
expect(await db.$client.unsafe(query, ["retained"])).toEqual([{ id: "retained" }]);
try {
await setup.execute(sql`alter table prepared_schema_change_fixture add column extra text`);
expect(await db.$client.unsafe(query, ["retained"])).toEqual([{ id: "retained", extra: null }]);
} finally {
await setup.execute(sql`alter table prepared_schema_change_fixture drop column if exists extra`);
}
});
async function changeStaleValueType(type: "bigint" | "integer") {
await setup.execute(sql.raw(`alter table prepared_stale_fixture alter column value type ${type}`));
}
it("restarts a transaction whose first statement meets an invalidated plan without running the callback again", async () => {
const db = fixture({ prepare: true });
let calls = 0;
const read = () => db.transaction(tx => { calls++; return tx.select().from(stale).where(eq(stale.id, "stale")); });
expect(await read()).toEqual([{ id: "stale", value: 5 }]);
await changeStaleValueType("bigint");
try {
expect(await read()).toEqual([{ id: "stale", value: 5 }]);
expect(calls).toBe(2);
expect(preparedStatementStats(db)).toMatchObject({ restartedTransactions: 1, demoted: 1 });
} finally {
await changeStaleValueType("integer");
}
});
it("restarts a transaction whose first statement lost its prepared statement and keeps its later work", async () => {
const db = fixture({ prepare: true });
const run = (value: number) => db.transaction(async tx => {
const [row] = await tx.select({ value: records.value }).from(records).where(eq(records.id, "a"));
await tx.execute(sql`update prepared_query_fixture set value = ${value} where id = ${"b"} ${sql.raw(`/* run ${value} */`)}`);
return row;
});
expect(await run(3)).toEqual({ value: 1 });
const [{ name }] = await statements(db, 'select "value" from "prepared_query_fixture"%');
await db.$client.unsafe(`deallocate "${String(name)}"`);
expect(await run(4)).toEqual({ value: 1 });
expect(await setup.select({ value: records.value }).from(records).where(eq(records.id, "b"))).toEqual([{ value: 4 }]);
expect(preparedStatementStats(db)).toMatchObject({ restartedTransactions: 1, demoted: 1 });
});
it("keeps the isolation level and access mode of a restarted transaction", async () => {
const db = fixture({ prepare: true });
const settings = "select current_setting('transaction_isolation') as isolation, current_setting('transaction_read_only') as read_only, $1::integer as marker";
const snapshot = { isolation: "repeatable read", read_only: "on", marker: 1 };
const readDriver = () => db.$client.begin("isolation level repeatable read read only", async tx => {
const rows = await tx.unsafe("select id, value from prepared_stale_fixture where id = $1", ["stale"]);
return [rows, await tx.unsafe(settings, [1])];
});
const readOrm = () => db.transaction(async tx => [
await tx.select().from(stale).where(eq(stale.id, "stale")),
await tx.execute(sql`select current_setting('transaction_isolation') as isolation, current_setting('transaction_read_only') as read_only, ${1}::integer as marker`),
], { isolationLevel: "repeatable read", accessMode: "read only" });
expect(await readDriver()).toEqual([[{ id: "stale", value: 5 }], [snapshot]]);
expect(await readOrm()).toEqual([[{ id: "stale", value: 5 }], [snapshot]]);
await changeStaleValueType("bigint");
try {
expect(await readDriver()).toEqual([[{ id: "stale", value: "5" }], [snapshot]]);
expect(await readOrm()).toEqual([[{ id: "stale", value: 5 }], [snapshot]]);
expect(preparedStatementStats(db)).toMatchObject({ restartedTransactions: 2, demoted: 2 });
} finally {
await changeStaleValueType("integer");
}
});
it("does not restart a transaction after its first statement and runs the invalidated text unprepared", async () => {
const db = fixture({ prepare: true });
let calls = 0;
const update = () => db.transaction(async tx => {
calls++;
await tx.update(records).set({ value: 7 }).where(eq(records.id, "b"));
return tx.select().from(stale).where(eq(stale.id, "stale"));
});
expect(await update()).toEqual([{ id: "stale", value: 5 }]);
await changeStaleValueType("bigint");
try {
await expect(update()).rejects.toMatchObject({ cause: { code: "0A000" } });
expect(calls).toBe(2);
expect(await update()).toEqual([{ id: "stale", value: 5 }]);
expect(await statements(db, '%"prepared_stale_fixture"%')).toHaveLength(1);
expect(preparedStatementStats(db)).toMatchObject({ restartedTransactions: 0, demoted: 1 });
} finally {
await changeStaleValueType("integer");
}
});
it("counts a tagged statement as the first statement of a transaction", async () => {
const db = fixture({ prepare: true });
const read = "select id, value from prepared_stale_fixture where id = $1";
expect(await db.$client.unsafe(read, ["stale"])).toEqual([{ id: "stale", value: 5 }]);
await changeStaleValueType("bigint");
try {
await expect(db.$client.begin(async tx => {
await tx`update prepared_query_fixture set value = value + 1 where id = 'a'`;
return tx.unsafe(read, ["stale"]);
})).rejects.toMatchObject({ code: "0A000" });
expect(await setup.select({ value: records.value }).from(records).where(eq(records.id, "a"))).toEqual([{ value: 1 }]);
expect(preparedStatementStats(db)).toMatchObject({ restartedTransactions: 0, demoted: 1 });
} finally {
await changeStaleValueType("integer");
}
});
it("rolls back only the savepoint whose first statement fails and continues the outer transaction once", async () => {
const db = fixture({ prepare: true });
const nestedRead = (tx: Parameters<Parameters<Db["transaction"]>[0]>[0]) =>
tx.transaction(nested => nested.select().from(stale).where(eq(stale.id, "stale")));
await db.transaction(async tx => { await nestedRead(tx); });
await changeStaleValueType("bigint");
let outerCalls = 0;
try {
const result = await db.transaction(async tx => {
outerCalls++;
await tx.execute(sql`update prepared_query_fixture set value = value + 10 where id = ${"a"}`);
const nested = await nestedRead(tx).catch((error: { cause?: { code?: string } }) => error.cause?.code);
await tx.execute(sql`update prepared_query_fixture set value = value + 100 where id = ${"b"}`);
return [nested, await tx.select().from(stale).where(eq(stale.id, "stale"))];
});
expect(result).toEqual(["0A000", [{ id: "stale", value: 5 }]]);
expect(outerCalls).toBe(1);
expect(await setup.select({ id: records.id, value: records.value }).from(records).orderBy(records.id))
.toEqual([{ id: "a", value: 11 }, { id: "b", value: 102 }]);
expect(preparedStatementStats(db)).toMatchObject({ restartedTransactions: 0, demoted: 1 });
} finally {
await changeStaleValueType("integer");
}
});
async function occupyNextStatementName(db: Db) {
const names = (await statements(db, "%")).map(row => String(row.name));
const prefix = names.find(name => /\d$/.test(name) && !name.includes(" "))!.replace(/\d+$/, "");
const counters = names.filter(name => name.startsWith(prefix)).map(name => Number(name.slice(prefix.length)));
await db.$client.unsafe(`prepare "${prefix}${Math.max(...counters) + 1}" as select 1`, [], { prepare: false });
}
it("retries a pooled statement unprepared when its prepared statement cannot be created", async () => {
const db = fixture({ prepare: true });
await statements(db, "%");
await occupyNextStatementName(db);
expect(await db.select({ value: records.value }).from(records).where(eq(records.id, "b"))).toEqual([{ value: 2 }]);
await occupyNextStatementName(db);
expect(await db.$client.unsafe("select $1::integer as raw_retry_marker", [3]).values()).toEqual([[3]]);
expect(await statements(db)).toHaveLength(0);
expect(await statements(db, "%raw_retry_marker%")).toHaveLength(0);
expect(preparedStatementStats(db)).toMatchObject({ retried: 2, demoted: 2 });
});
it("does not restart a transaction when a query created first executes after another statement", async () => {
const db = fixture({ prepare: true });
const read = "select id, value from prepared_stale_fixture where id = $1";
expect(await db.$client.unsafe(read, ["stale"])).toEqual([{ id: "stale", value: 5 }]);
await changeStaleValueType("bigint");
let calls = 0;
try {
await expect(db.$client.begin(async tx => {
calls++;
const saved = tx.unsafe(read, ["stale"]);
await tx.unsafe("select $1::integer as ordering_marker", [1]);
return await saved;
})).rejects.toMatchObject({ code: "0A000" });
expect(calls).toBe(1);
expect(preparedStatementStats(db)).toMatchObject({ restartedTransactions: 0, demoted: 1 });
} finally {
await changeStaleValueType("integer");
}
});
it("prepares at most the configured number of distinct texts and skips long texts", async () => {
const raw = postgres(temporary.connectionString, { max: 1, prepare: true, onnotice: () => {} });
const state = createPreparedQueryState({ limit: 2, maxLength: 80 });
installPreparedQueries(raw, state);
try {
for (const marker of ["cap_a", "cap_b", "cap_c", "cap_d"]) {
expect(await raw.unsafe(`select $1::integer as ${marker}`, [1])).toEqual([{ [marker]: 1 }]);
}
const long = `select $1::integer as cap_long /* ${"x".repeat(80)} */`;
expect(await raw.unsafe(long, [2])).toEqual([{ cap_long: 2 }]);
expect((await raw.unsafe("select name from pg_prepared_statements where statement like 'select $1::integer as cap_%'", [], { prepare: false })).length).toBe(2);
expect(preparedQueryStats(state)).toMatchObject({ texts: 2, unprepared: 3 });
} finally {
await raw.end();
}
});
});