Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

perf(search): keyword statement runs with work_mem 32MB so a common word's bitmap stays exact and the bounded sort stays in memory

garrytan8861b33b0c1d
diff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.ts
index 5c6be409d6..79b56b5411 100644
--- a/src/core/postgres-engine.ts
+++ b/src/core/postgres-engine.ts
@@ -116,7 +116,7 @@ import { validateSlug, contentHash, isBlankBody, rowToPage, rowToStalePage, rowT
import { resolveBoostMap, resolveHardExcludes } from './search/source-boost.ts';
import { buildSourceFactorCase, buildHardExcludeClause, buildVisibilityClause, buildBestPerPagePoolCte, buildOrFallbackWebsearchQuery, boundWebsearchQuery, collapseWebsearchDashRuns } from './search/sql-ranking.ts';
import { narrowedOrFallbackQuery } from './search/keyword-or-narrow.ts';
-import { buildKeywordChunkMatch, KEYWORD_RANK_CANDIDATES } from './search/keyword-chunk-match.ts';
+import { buildKeywordChunkMatch, KEYWORD_RANK_CANDIDATES, KEYWORD_STATEMENT_SETTINGS } from './search/keyword-chunk-match.ts';
import { privatePagesFilterFragment, privateSnapshotFilterFragment, privateLinkOriginFilterFragment, privateTimelineEventFilterFragment, privateProvenanceFilterFragment } from './search/private-visibility.ts';
import { DEFAULT_EMBEDDING_MODEL, DEFAULT_EMBEDDING_DIMENSIONS } from './ai/defaults.ts';
import { readStoredEmbeddingIdentity } from './stored-embedding-identity.ts';
@@ -1155,7 +1155,7 @@ export class PostgresEngine implements BrainEngine {
this.withScopedReadTransaction(opts?.sourceIds, opts?.sourceId, (tx) => {
const boundParams = params.map((v, i) => i === 0 ? collapseWebsearchDashRuns(queryText) : i === rankIdx && !opts?.rankQuery ? collapseWebsearchDashRuns(rankText) : v);
return tx.unsafe(rawQuery, boundParams as Parameters<typeof tx.unsafe>[1]);
- }, { alwaysTransaction: true, jitOff: true, settings: relaxed ? { statement_timeout: statementTimeout, enable_seqscan: 'off' } : { statement_timeout: statementTimeout } }));
+ }, { alwaysTransaction: true, jitOff: true, settings: relaxed ? { statement_timeout: statementTimeout, enable_seqscan: 'off', ...KEYWORD_STATEMENT_SETTINGS } : { statement_timeout: statementTimeout, ...KEYWORD_STATEMENT_SETTINGS } }));
let rows = await runKeyword(query);
// D2 fix (fix/title-retrieval-arm): websearch AND semantics at chunk
// grain mean one non-co-occurring token zeroes keyword recall. When the
diff --git a/src/core/search/keyword-chunk-match.ts b/src/core/search/keyword-chunk-match.ts
index 596c3d9851..8529f60dcc 100644
--- a/src/core/search/keyword-chunk-match.ts
+++ b/src/core/search/keyword-chunk-match.ts
@@ -29,6 +29,16 @@
*/
export const KEYWORD_RANK_CANDIDATES = 50_000;
+/**
+ * Transaction-local settings for the keyword statement. At the 4 MB
+ * `work_mem` default, the bitmap over a common word's matches goes lossy, so
+ * the heap scan rechecks every row's tsvector, and the bounded candidate sort
+ * spills to disk: a remote caller's bounded any-word retry took 6.6 s on the
+ * 2M-chunk bench against 1.4 s at 32 MB (128 MB measured the same). The
+ * bound keeps the memory this can use per statement small.
+ */
+export const KEYWORD_STATEMENT_SETTINGS = { work_mem: '32MB' } as const;
+
export function buildKeywordChunkMatch(ftsLang: string, excludePrivate: boolean | undefined): { from: string; match: string; modality: string } {
const tsq = `websearch_to_tsquery('${ftsLang}', $1)`;
if (!excludePrivate) return { from: 'content_chunks cc', match: `cc.search_vector @@ ${tsq}`, modality: `AND cc.modality = 'text'` };