Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
perf(search): send a top-level search read behind its settings statement without waiting for the answer
garrytan76791a8ab2a6
diff --git a/src/core/postgres-engine/search-settings.ts b/src/core/postgres-engine/search-settings.tsindex 3fed4d2e06..6c2ce43c93 100644--- a/src/core/postgres-engine/search-settings.ts+++ b/src/core/postgres-engine/search-settings.ts@@ -18,7 +18,12 @@* `SELECT set_config(...), set_config(...)`: each separate statement was a* round trip in series with the query, about 30 ms each to a remote pooler.* At the top level nothing is read first or restored after: `set_config(...,- * true)` ends with the transaction. Inside a caller's transaction the read+ * true)` ends with the transaction, and the read is sent behind the settings+ * statement without waiting for its answer (one round trip for both: the+ * transaction pins its connection, the driver sends in order and budgets the+ * read by the `statement_timeout` it just sent). A failed settings statement+ * aborts the transaction, so the read fails behind it and the settings error+ * is the one raised. Inside a caller's transaction the read* runs in a savepoint, and a released savepoint keeps the settings, so one* statement reads the caller's values first and one restores them after; a* failed read rolls the savepoint back, which reverts them too.@@ -77,8 +82,14 @@ export async function withSearchSettings<T>(if (keys.length === 0) return run();const previous = nested ? (await tx.unsafe(currentSettingsStatement(keys)))[0] as Record<string, string | null> : undefined;const set = searchSettingsStatement(settings);- await tx.unsafe(set.sql, set.params);- const result = await run();+ const applied = tx.unsafe(set.sql, set.params);+ if (previous) await applied;+ else applied.catch(() => undefined);+ const result = await run().catch(async error => {+ await applied;+ throw error;+ });+ await applied;if (previous) {const restore = searchSettingsStatement(Object.fromEntries(keys.map(name => [name, previous[name] ?? fallback[name] ?? ''])));await tx.unsafe(restore.sql, restore.params);diff --git a/test/search-settings-one-statement.test.ts b/test/search-settings-one-statement.test.tsindex a256f1ba7a..af965bba8c 100644--- a/test/search-settings-one-statement.test.ts+++ b/test/search-settings-one-statement.test.ts@@ -53,6 +53,25 @@ describe('withSearchSettings', () => {expect(Connection.statementTimeoutChanges(statements[0]!.sql, statements[0]!.params)).toEqual([{ kind: 'set', local: true, ms: 8000 }]);});+ test('top level: the read is sent behind the settings statement without waiting for its answer', async () => {+ const sent: string[] = [];+ let answer!: () => void;+ const tx = { unsafe: (sql: string) => { sent.push(sql); return new Promise<unknown[]>(resolve => { answer = () => resolve([]); }); } } as never;+ let readStarted = false;+ const pending = withSearchSettings(tx, false, { jit: 'off' }, async () => { readStarted = true; return 'rows'; });+ await Promise.resolve();+ expect(readStarted).toBe(true);+ expect(sent).toEqual(["SELECT set_config('jit', $1, true)"]);+ answer();+ expect(await pending).toBe('rows');+ });++ test('top level: a refused settings statement is the error raised, ahead of the read that failed behind it', async () => {+ const tx = { unsafe: async () => { throw new Error('settings sentinel'); } } as never;+ await expect(withSearchSettings(tx, false, { jit: 'off' }, async () => { throw new Error('current transaction is aborted'); })).rejects.toThrow('settings sentinel');+ await expect(withSearchSettings(tx, false, { jit: 'off' }, async () => 'rows')).rejects.toThrow('settings sentinel');+ });+test("nested: reads the caller's values, sets its own, restores the caller's (fallback for an unloaded extension setting)", async () => {const { tx, statements } = recordingTx({ jit: 'on', 'hnsw.ef_search': null });let during = -1;