Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test: #6930 connection, pg_type and transaction pipelining proofs (scripted server, Postgres, PgBouncer)

garrytan14befb2ff99c
diff --git a/.github/workflows/persistence-validation.yml b/.github/workflows/persistence-validation.yml
index 5fa7553a06..40b02bf865 100644
--- a/.github/workflows/persistence-validation.yml
+++ b/.github/workflows/persistence-validation.yml
@@ -172,6 +172,7 @@ jobs:
test/e2e/publication-deadline-pgbouncer.test.ts
test/e2e/transaction-cancel-held-lock.test.ts
test/e2e/pgbouncer-pipelined-read-pool.test.ts
+ test/e2e/pgbouncer-transaction-pipeline-6930.test.ts
test/e2e/extract-atoms-page-state.test.ts
- if: always()
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
diff --git a/test/e2e/pg-round-trips-6930-postgres.test.ts b/test/e2e/pg-round-trips-6930-postgres.test.ts
new file mode 100644
index 0000000000..e87848b9d3
--- /dev/null
+++ b/test/e2e/pg-round-trips-6930-postgres.test.ts
@@ -0,0 +1,3 @@
+import { registerPostgresTests } from '../helpers/test-backends.ts';
+
+await registerPostgresTests(() => import('../pathological/pg-round-trips-6930.test.ts'));
diff --git a/test/e2e/pgbouncer-transaction-pipeline-6930.test.ts b/test/e2e/pgbouncer-transaction-pipeline-6930.test.ts
new file mode 100644
index 0000000000..aa826a7268
--- /dev/null
+++ b/test/e2e/pgbouncer-transaction-pipeline-6930.test.ts
@@ -0,0 +1,151 @@
+/**
+ * #6930: behind a transaction-mode PgBouncer, the statements one transaction issues together are pipelined again.
+ *
+ * #6543 turned pipelining off on a transaction-mode pooler URL (`?prepare=false` or port 6543) because statements
+ * pipelined across a transaction boundary went to the wrong server connection. That also cost every transaction one
+ * round trip per statement, though the pooler pins one server connection from BEGIN until the transaction ends: a
+ * one-file managed commit took 354-500 dependent round trips behind PgBouncer against about 230 direct. The driver
+ * now pipelines on a connection its owner holds while every statement in flight leaves it inside a transaction, and
+ * never sends anything behind a statement that may end one.
+ *
+ * Each case reads the socket trace (GBRAIN_SQL_TRACE): a statement sent before the previous one's answer arrived was
+ * pipelined, a client decision rather than a timing. Safety: every statement of a pipelined transaction runs in that
+ * transaction on one backend; a failure inside one rolls it back; autocommit statements and anything after COMMIT
+ * still wait; prepared statements stay off.
+ *
+ * Runs when the CI PgBouncer fixture is configured (GBRAIN_PGBOUNCER_URL + GBRAIN_PGBOUNCER_DIRECT_URL), against a
+ * database of its own.
+ */
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import postgres from '#postgres';
+import { PostgresEngine } from '../../src/core/postgres-engine.ts';
+import { assertSafeE2eDatabaseUrl } from '../helpers/db-guard.ts';
+import { readSqlTrace, type StatementRecord } from '../helpers/statement-counter.ts';
+import { withEnv } from '../helpers/with-env.ts';
+
+const pooled = process.env.GBRAIN_PGBOUNCER_URL;
+const pooledAdmin = process.env.GBRAIN_PGBOUNCER_DIRECT_URL;
+if (process.env.GBRAIN_CI_REQUIRE_PGBOUNCER === '1' && !(pooled && pooledAdmin)) throw new Error('The transaction pipeline PgBouncer test requires the configured CI PgBouncer fixture.');
+const database = `gbrain_test_txn_pipeline_${crypto.randomUUID().replace(/-/g, '').slice(0, 12)}`;
+const pooledUrl = (query: string) => { const url = new URL(pooled!); url.pathname = `/${database}`; url.search = query; return url.toString(); };
+const dir = mkdtempSync(join(tmpdir(), 'gbrain-6930-pgbouncer-'));
+const trace = join(dir, 'trace.jsonl');
+
+beforeAll(async () => {
+ if (!(pooled && pooledAdmin)) return;
+ assertSafeE2eDatabaseUrl(pooledAdmin);
+ const admin = postgres(pooledAdmin, { max: 1, prepare: false, onnotice: () => {} });
+ try { await admin.unsafe(`CREATE DATABASE ${database}`); } finally { await admin.end(); }
+}, 60_000);
+
+afterAll(async () => {
+ rmSync(dir, { recursive: true, force: true });
+ if (!(pooled && pooledAdmin)) return;
+ const admin = postgres(pooledAdmin, { max: 1, prepare: false, onnotice: () => {} });
+ try { await admin.unsafe(`DROP DATABASE IF EXISTS ${database} WITH (FORCE)`); } finally { await admin.end(); }
+});
+
+/** Runs `run` on a traced pooled engine and returns the trace records it produced, in send order. */
+async function traced(label: string, poolSize: number, run: (engine: PostgresEngine) => Promise<void>, env: Record<string, string | undefined> = {}): Promise<StatementRecord[]> {
+ const url = pooledUrl('?prepare=false');
+ assertSafeE2eDatabaseUrl(url);
+ await withEnv({ PGMAX_PIPELINE: undefined, GBRAIN_PREPARE: undefined, GBRAIN_DISABLE_DIRECT_POOL: '1', GBRAIN_PG_TXN_PIPELINE: undefined,
+ GBRAIN_SQL_TRACE: trace, GBRAIN_SQL_TRACE_LABEL: label, ...env }, async () => {
+ const engine = new PostgresEngine();
+ await engine.connect({ engine: 'postgres', database_url: url, poolSize });
+ try { await run(engine); } finally { await engine.disconnect(); }
+ });
+ await Bun.sleep(1200);
+ return readSqlTrace(trace).filter(r => (r as { label?: string }).label === label && r.kind !== 'connect').sort((a, b) => a.t - b.t);
+}
+const end = (r: StatementRecord) => r.t + (r.ms ?? 0);
+/** For each record matching `re` after the first on its connection: was it sent before the previous one there was answered? */
+const sentBeforeAnswer = (records: StatementRecord[], re: RegExp, limit = Infinity) => {
+ const last = new Map<number, StatementRecord>();
+ const out: boolean[] = [];
+ for (const r of records.filter(x => re.test(x.sql)).slice(0, limit)) {
+ const prev = last.get(r.conn);
+ if (prev) out.push(r.t < end(prev));
+ last.set(r.conn, r);
+ }
+ return out;
+};
+
+describe.skipIf(!(pooled && pooledAdmin))('#6930 transaction pipelining through transaction-mode PgBouncer', () => {
+ test('a transaction\'s concurrent statements are pipelined, each reads its own rows, all on one backend in one transaction', async () => {
+ let rows: Array<{ n: number; pid: number; xid: string }> = [];
+ let prepare: unknown;
+ const records = await traced('txn-pipeline', 1, async engine => {
+ prepare = (engine.sql as unknown as { options: { prepare: boolean } }).options.prepare;
+ rows = (await engine.sql.begin(tx => Promise.all(Array.from({ length: 12 }, (_, n) =>
+ tx.unsafe(`SELECT ${n}::int AS n, pg_backend_pid() AS pid, txid_current()::text AS xid /* pipelined */`))))).map(r => r[0] as unknown as { n: number; pid: number; xid: string });
+ });
+ expect(prepare).toBe(false);
+ expect(rows.map(r => r.n)).toEqual(Array.from({ length: 12 }, (_, n) => n));
+ expect(new Set(rows.map(r => r.pid)).size).toBe(1);
+ expect(new Set(rows.map(r => r.xid)).size).toBe(1);
+ expect(sentBeforeAnswer(records, /\/\* pipelined \*\/$/)).toEqual(Array.from({ length: 11 }, () => true));
+ }, 60_000);
+
+ test('a bounded read sends BEGIN; SET LOCAL, the read and COMMIT before any answer, and the bound still applies server-side', async () => {
+ let timedOut: string | undefined;
+ const records = await traced('txn-bounded', 1, async engine => {
+ await engine.executeRaw('SELECT 1 AS bounded_read', [], { timeoutMs: 5000 });
+ timedOut = await engine.executeRaw('SELECT pg_sleep(2) AS slept', [], { timeoutMs: 200 }).then(() => 'none', (e: { code?: string }) => e.code);
+ });
+ expect(sentBeforeAnswer(records, /^BEGIN; SET LOCAL statement_timeout|^SELECT 1 AS bounded_read$|^COMMIT$/, 3)).toEqual([true, true]);
+ expect(timedOut).toBe('57014');
+ }, 60_000);
+
+ test('a failure inside a pipelined transaction rolls the whole transaction back, and the pool answers the next statement with its own rows', async () => {
+ const table = `txn_pipeline_${crypto.randomUUID().replace(/-/g, '').slice(0, 8)}`;
+ let failed: string | undefined;
+ let count = -1;
+ let after: unknown;
+ await traced('txn-failure', 1, async engine => {
+ await engine.sql.unsafe(`CREATE TABLE ${table} (n int)`);
+ failed = await engine.sql.begin(tx => Promise.all([tx.unsafe(`INSERT INTO ${table} VALUES (1)`), tx.unsafe('SELECT 1/0 AS boom'), tx.unsafe(`INSERT INTO ${table} VALUES (2)`)]))
+ .then(() => 'none', (e: { code?: string }) => e.code);
+ count = Number((await engine.sql.unsafe(`SELECT count(*)::int AS c FROM ${table}`))[0]!.c);
+ after = (await engine.sql.unsafe('SELECT 42 AS after_failure'))[0]!.after_failure;
+ });
+ expect(failed).toBe('22012');
+ expect(count).toBe(0);
+ expect(after).toBe(42);
+ }, 60_000);
+
+ test('#6543 kept: autocommit statements are not pipelined, nothing is sent behind COMMIT, and concurrent transactions over a small pool never share a backend mid-transaction', async () => {
+ let txns: Array<Array<{ pid: number; xid: string; tag: number }>> = [];
+ const records = await traced('txn-safety', 2, async engine => {
+ await Promise.all(Array.from({ length: 6 }, (_, n) => engine.sql.unsafe(`SELECT ${n} AS autocommit_read`)));
+ const reserved = await engine.sql.reserve();
+ try {
+ await Promise.all([reserved.unsafe('BEGIN').simple().execute(), reserved.unsafe('SELECT 1 AS inside_reserved').execute(),
+ reserved.unsafe('COMMIT').execute(), reserved.unsafe('SELECT 2 AS after_commit').execute()]);
+ } finally { reserved.release(); }
+ txns = await Promise.all(Array.from({ length: 16 }, (_, tag) => engine.sql.begin(tx => Promise.all(Array.from({ length: 4 }, () =>
+ tx.unsafe(`SELECT pg_backend_pid() AS pid, txid_current()::text AS xid, ${tag}::int AS tag`)))).then(rs => rs.map(r => r[0] as unknown as { pid: number; xid: string; tag: number }))));
+ });
+ const autocommit = sentBeforeAnswer(records, /AS autocommit_read$/);
+ expect(autocommit.length).toBeGreaterThanOrEqual(4);
+ expect(autocommit.filter(Boolean)).toEqual([]);
+ const commit = records.find(r => r.sql === 'COMMIT')!;
+ const afterCommit = records.find(r => /AS after_commit$/.test(r.sql))!;
+ expect(afterCommit.t >= end(commit)).toBe(true);
+ for (const [tag, rows] of txns.entries()) {
+ expect({ tag, tags: [...new Set(rows.map(r => r.tag))], pids: new Set(rows.map(r => r.pid)).size, xids: new Set(rows.map(r => r.xid)).size })
+ .toEqual({ tag, tags: [tag], pids: 1, xids: 1 });
+ }
+ expect(new Set(txns.map(rows => rows[0]!.xid)).size).toBe(16);
+ }, 60_000);
+
+ test('GBRAIN_PG_TXN_PIPELINE=0 restores one round trip per statement inside a transaction', async () => {
+ const records = await traced('txn-off', 1, async engine => {
+ await engine.sql.begin(tx => Promise.all(Array.from({ length: 4 }, (_, n) => tx.unsafe(`SELECT ${n} AS switched_off`))));
+ }, { GBRAIN_PG_TXN_PIPELINE: '0' });
+ expect(sentBeforeAnswer(records, /AS switched_off$/)).toEqual([false, false, false]);
+ }, 60_000);
+});
diff --git a/test/pathological/pg-round-trips-6930.test.ts b/test/pathological/pg-round-trips-6930.test.ts
new file mode 100644
index 0000000000..15a64b15d9
--- /dev/null
+++ b/test/pathological/pg-round-trips-6930.test.ts
@@ -0,0 +1,249 @@
+/**
+ * GBRA-133 PR 2b (#6930): the managed engine's Postgres round trips.
+ *
+ * 1. Array types. postgres.js runs a pg_type query on every new connection before its first statement. A sync run
+ * opens its pool's connections in a burst (sync-drain's pre-warm), so every one of them paid that round trip, on
+ * every pool of the process. The array types are now fetched by the process's first connection to a database and
+ * applied by every later one, of any pool.
+ * 2. Transaction pipelining. Behind a transaction-mode pooler a read pool does not pipeline (#6543), which also kept
+ * the statements of one transaction, which the pooler pins to one server connection, at one round trip each. They
+ * are pipelined again on the connection their owner holds, never behind a statement that may end the transaction.
+ *
+ * The unit arm runs on a scripted server (no database) and records, for every statement, how many earlier ones were
+ * still unanswered when it arrived: a decision the client made, not a timing. The Postgres arm runs a managed sync
+ * through a fresh traced pool and counts its connections and pg_type queries.
+ */
+import { afterEach, describe, expect, test } from 'bun:test';
+import { execFileSync } from 'node:child_process';
+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { createServer, type Server, type Socket } from 'node:net';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { PostgresEngine } from '../../src/core/postgres-engine.ts';
+import { resolvePoolSize } from '../../src/core/db.ts';
+import { _resetSharedParameterTypesForTests } from '../../src/core/pg-type-cache.ts';
+import { drainManagedSync } from '../../src/core/persistence/sync-drain.ts';
+import { readSqlTrace } from '../helpers/statement-counter.ts';
+import { withEnv } from '../helpers/with-env.ts';
+import { managedBrain, pathologicalArms } from './harness.ts';
+
+const NO_OVERRIDES = { PGMAX_PIPELINE: undefined, GBRAIN_PREPARE: undefined, GBRAIN_DISABLE_DIRECT_POOL: '1', GBRAIN_SQL_TRACE: undefined,
+ GBRAIN_PG_TYPE_CACHE: undefined, GBRAIN_PG_TXN_PIPELINE: undefined, GBRAIN_DIRECT_DATABASE_URL: undefined };
+const ANSWER_DELAY_MS = 25;
+
+interface Arrival { conn: number; sql: string; unanswered: number }
+
+/**
+ * A server that speaks just enough of the protocol for gbrain's pool: trust auth, empty results, a transaction status
+ * that follows BEGIN / COMMIT / ROLLBACK, and every answer held ANSWER_DELAY_MS so a pipelining client is seen sending
+ * its next statement before the previous answer.
+ */
+async function scriptedServer(): Promise<{ url: (query: string) => string; arrivals: Arrival[]; connections: () => number; close: () => Promise<void> }> {
+ const arrivals: Arrival[] = [];
+ let connections = 0;
+ const frame = (type: string, body: Buffer = Buffer.alloc(0)) => {
+ const header = Buffer.alloc(5);
+ header.write(type, 0, 'ascii'); header.writeInt32BE(body.length + 4, 1);
+ return Buffer.concat([header, body]);
+ };
+ const cstr = (s: string) => Buffer.from(`${s}\0`, 'utf8');
+ const sockets = new Set<Socket>();
+ const server: Server = createServer(socket => {
+ const conn = ++connections;
+ sockets.add(socket);
+ socket.on('close', () => sockets.delete(socket));
+ let buffered = Buffer.alloc(0), started = false, status = 'I', text = '';
+ let reply: Buffer[] = [];
+ const queue: Array<{ frames: Buffer; status: string }> = [];
+ let answering = false;
+ const pump = () => {
+ if (answering || !queue.length) return;
+ answering = true;
+ setTimeout(() => {
+ const next = queue.shift()!;
+ if (!socket.destroyed) socket.write(Buffer.concat([next.frames, frame('Z', Buffer.from(next.status))]));
+ answering = false;
+ pump();
+ }, ANSWER_DELAY_MS);
+ };
+ const statement = (sqlText: string, frames: Buffer[]) => {
+ arrivals.push({ conn, sql: sqlText.replace(/\s+/g, ' ').trim(), unanswered: queue.length });
+ for (const part of sqlText.split(';')) {
+ const s = part.trim().toLowerCase();
+ if (/^(begin|start transaction)\b/.test(s)) status = 'T';
+ else if (/^(commit|rollback|end|abort)\b/.test(s)) status = 'I';
+ }
+ queue.push({ frames: Buffer.concat(frames), status });
+ pump();
+ };
+ socket.on('data', (chunk: Buffer) => {
+ buffered = Buffer.concat([buffered, chunk]);
+ if (!started) {
+ if (buffered.length < 8 || buffered.length < buffered.readInt32BE(0)) return;
+ buffered = buffered.subarray(buffered.readInt32BE(0)); started = true;
+ socket.write(Buffer.concat([frame('R', Buffer.from([0, 0, 0, 0])), frame('S', Buffer.concat([cstr('server_version'), cstr('16.0')])),
+ frame('K', Buffer.from([0, 0, 0, 7, 0, 0, 0, 9])), frame('Z', Buffer.from('I'))]));
+ }
+ while (buffered.length >= 5) {
+ const type = String.fromCharCode(buffered[0]!), length = buffered.readInt32BE(1);
+ if (buffered.length < length + 1) break;
+ const body = buffered.subarray(5, length + 1);
+ buffered = buffered.subarray(length + 1);
+ if (type === 'P') { const nameEnd = body.indexOf(0); text = body.subarray(nameEnd + 1, body.indexOf(0, nameEnd + 1)).toString('utf8'); reply.push(frame('1')); }
+ else if (type === 'B') reply.push(frame('2'));
+ else if (type === 'D') reply.push(frame('n'));
+ else if (type === 'E') reply.push(frame('C', cstr('SELECT 0')));
+ else if (type === 'S') { statement(text, reply); reply = []; }
+ else if (type === 'Q') { const q = body.subarray(0, body.indexOf(0)).toString('utf8'); statement(q, q.split(';').map(() => frame('C', cstr('SELECT 0')))); }
+ else if (type === 'X') socket.end();
+ }
+ });
+ socket.on('error', () => undefined);
+ });
+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', () => resolve()));
+ const port = (server.address() as { port: number }).port;
+ return {
+ url: query => `postgresql://u:[email protected]:${port}/d${query}`,
+ arrivals,
+ connections: () => connections,
+ close: () => new Promise<void>(resolve => { for (const s of sockets) s.destroy(); server.close(() => resolve()); }),
+ };
+}
+
+const engines: PostgresEngine[] = [];
+let closeServer: (() => Promise<void>) | undefined;
+afterEach(async () => {
+ while (engines.length) await engines.pop()!.disconnect().catch(() => undefined);
+ await closeServer?.();
+ closeServer = undefined;
+});
+
+async function engineOn(url: string, poolSize: number): Promise<PostgresEngine> {
+ const engine = new PostgresEngine();
+ engines.push(engine);
+ await engine.connect({ engine: 'postgres', database_url: url, poolSize });
+ return engine;
+}
+async function serverFor(): Promise<Awaited<ReturnType<typeof scriptedServer>>> {
+ const server = await scriptedServer();
+ closeServer = server.close;
+ return server;
+}
+const isTypeFetch = (sql: string) => /pg_catalog\.pg_type/.test(sql) && /typarray/.test(sql);
+const unansweredOf = (arrivals: Arrival[], re: RegExp) => arrivals.filter(a => re.test(a.sql)).map(a => a.unanswered);
+
+describe('#6930 unit: scripted server', () => {
+ test('array types are fetched by the process\'s first connection to a database, not by every connection of every pool', async () => withEnv(NO_OVERRIDES, async () => {
+ _resetSharedParameterTypesForTests();
+ const server = await serverFor();
+ const a = await engineOn(server.url(''), 3);
+ const b = await engineOn(server.url(''), 3);
+ await Promise.all([a, b].flatMap(engine => Array.from({ length: 6 }, () => engine.sql.unsafe('SELECT 1'))));
+ expect(server.connections()).toBeGreaterThanOrEqual(4);
+ expect({ connections: server.connections(), typeFetches: server.arrivals.filter(x => isTypeFetch(x.sql)).length })
+ .toEqual({ connections: server.connections(), typeFetches: 1 });
+ }));
+
+ test('GBRAIN_PG_TYPE_CACHE=0 keeps the stock fetch on every connection', async () => withEnv({ ...NO_OVERRIDES, GBRAIN_PG_TYPE_CACHE: '0' }, async () => {
+ _resetSharedParameterTypesForTests();
+ const server = await serverFor();
+ const a = await engineOn(server.url(''), 3);
+ await Promise.all(Array.from({ length: 6 }, () => a.sql.unsafe('SELECT 1')));
+ expect(server.connections()).toBeGreaterThanOrEqual(2);
+ expect(server.arrivals.filter(x => isTypeFetch(x.sql)).length).toBe(server.connections());
+ }));
+
+ test('behind a transaction pooler, the statements one transaction issues together share a round trip', async () => withEnv(NO_OVERRIDES, async () => {
+ const server = await serverFor();
+ const engine = await engineOn(server.url('?prepare=false'), 1);
+ await engine.sql.begin(tx => Promise.all([1, 2, 3, 4].map(n => tx.unsafe(`SELECT ${n} AS in_tx`))));
+ expect(unansweredOf(server.arrivals, /^SELECT \d AS in_tx$/)).toEqual([0, 1, 2, 3]);
+ expect(unansweredOf(server.arrivals, /^commit$/)).toEqual([0]);
+ }));
+
+ test('behind a transaction pooler, a bounded read sends BEGIN; SET LOCAL, the read and COMMIT in one round trip', async () => withEnv(NO_OVERRIDES, async () => {
+ const server = await serverFor();
+ const engine = await engineOn(server.url('?prepare=false'), 1);
+ await engine.executeRaw('SELECT 1 AS bounded', [], { timeoutMs: 5000 });
+ const sent = server.arrivals.filter(a => /^BEGIN; SET LOCAL statement_timeout|^SELECT 1 AS bounded$|^COMMIT$/.test(a.sql));
+ expect(sent.map(a => a.unanswered)).toEqual([0, 1, 2]);
+ }));
+
+ test('#6543 kept: autocommit statements are never pipelined, and nothing is sent behind COMMIT', async () => withEnv(NO_OVERRIDES, async () => {
+ const server = await serverFor();
+ const engine = await engineOn(server.url('?prepare=false'), 1);
+ await Promise.all(Array.from({ length: 5 }, (_, n) => engine.sql.unsafe(`SELECT ${n} AS autocommit`)));
+ expect(unansweredOf(server.arrivals, /AS autocommit$/)).toEqual([0, 0, 0, 0, 0]);
+ const reserved = await engine.sql.reserve();
+ try {
+ await Promise.all([reserved.unsafe('BEGIN').simple().execute(), reserved.unsafe('SELECT 1 AS before_commit').execute(),
+ reserved.unsafe('COMMIT').execute(), reserved.unsafe('SELECT 2 AS after_commit').execute()]);
+ } finally { reserved.release(); }
+ expect(unansweredOf(server.arrivals, /AS after_commit$/)).toEqual([0]);
+ }));
+
+ test('behind a transaction pooler, a reserved connection pipelines from BEGIN through COMMIT', async () => withEnv(NO_OVERRIDES, async () => {
+ const server = await serverFor();
+ const engine = await engineOn(server.url('?prepare=false'), 1);
+ const reserved = await engine.sql.reserve();
+ try {
+ await Promise.all([reserved.unsafe('BEGIN').simple().execute(), reserved.unsafe('SELECT 1 AS reserved_tx').execute(), reserved.unsafe('COMMIT').execute()]);
+ } finally { reserved.release(); }
+ expect(server.arrivals.filter(a => /^BEGIN$|AS reserved_tx$|^COMMIT$/.test(a.sql)).map(a => a.unanswered)).toEqual([0, 1, 2]);
+ }));
+
+ test('a direct URL keeps the driver\'s pipelining, and GBRAIN_PG_TXN_PIPELINE=0 restores one round trip per statement', async () => {
+ await withEnv(NO_OVERRIDES, async () => {
+ const server = await serverFor();
+ const engine = await engineOn(server.url(''), 1);
+ await engine.sql.begin(tx => Promise.all([1, 2].map(n => tx.unsafe(`SELECT ${n} AS direct_tx`))));
+ expect(unansweredOf(server.arrivals, /AS direct_tx$/)).toEqual([0, 1]);
+ });
+ while (engines.length) await engines.pop()!.disconnect();
+ await closeServer?.();
+ await withEnv({ ...NO_OVERRIDES, GBRAIN_PG_TXN_PIPELINE: '0' }, async () => {
+ const server = await serverFor();
+ const engine = await engineOn(server.url('?prepare=false'), 1);
+ await engine.sql.begin(tx => Promise.all([1, 2].map(n => tx.unsafe(`SELECT ${n} AS switched_off`))));
+ expect(unansweredOf(server.arrivals, /AS switched_off$/)).toEqual([0, 0]);
+ });
+ });
+});
+
+const git = (root: string, ...args: string[]) => execFileSync('git', ['-c', 'user.name=Bench Example', '-c', '[email protected]', ...args], { cwd: root });
+
+for (const { backend, databaseUrl } of pathologicalArms().filter(arm => arm.backend === 'postgres')) {
+ describe(`#6930 ${backend}: a managed sync run`, () => {
+ test('opens at most its pool\'s connections, all on one pool, and runs the pg_type query once', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'gbrain-6930-trace-'));
+ const trace = join(dir, 'sync.jsonl');
+ try {
+ await managedBrain(async ({ engine, root }) => {
+ writeFileSync(join(root, 'alice-example.md'), '---\ntitle: Alice Example\ntype: note\n---\nRound trip probe.\n');
+ git(root, 'add', '-A');
+ git(root, 'commit', '-qm', 'probe');
+ const url = (engine as unknown as { _savedConfig: { database_url: string } })._savedConfig.database_url;
+ await withEnv({ ...NO_OVERRIDES, GBRAIN_SQL_TRACE: trace, GBRAIN_SQL_TRACE_LABEL: 'sync-6930' }, async () => {
+ _resetSharedParameterTypesForTests();
+ const run = new PostgresEngine();
+ await run.connect({ engine: 'postgres', database_url: url, poolSize: resolvePoolSize() });
+ try {
+ await drainManagedSync(run, { sourceId: 'default', noPull: true, noEmbed: true } as Parameters<typeof drainManagedSync>[1], false);
+ } finally { await run.disconnect(); }
+ });
+ await Bun.sleep(1200);
+ }, { databaseUrl, setup: ({ root }) => {
+ git(root, 'init', '-q');
+ writeFileSync(join(root, 'acme-example.md'), '---\ntitle: Acme Example\ntype: note\n---\nSeed.\n');
+ git(root, 'add', '-A');
+ git(root, 'commit', '-qm', 'seed');
+ } });
+ const records = readSqlTrace(trace).filter(r => (r as { label?: string }).label === 'sync-6930');
+ const connects = records.filter(r => r.kind === 'connect');
+ expect(connects.length).toBeGreaterThanOrEqual(2);
+ expect({ within_pool: connects.length <= resolvePoolSize(), pools: [...new Set(connects.map(r => (r as { pool?: string }).pool))], type_fetches: records.filter(r => isTypeFetch(r.sql)).length })
+ .toEqual({ within_pool: true, pools: ['instance'], type_fetches: 1 });
+ } finally { rmSync(dir, { recursive: true, force: true }); }
+ }, 180_000);
+ });
+}