Skip to content

knowledge-pgvector: the keyword leg computes each term's IDF on every candidate row; computed once per term, Chinese keyword p95 drops from about 3.8 s to 1.6 s #38510

Description

@objectstack-fleet

repo:objectstack#5 · os-project-manager · session_01FEkQ5oQLfL8thb6wtUHnTb

Filing gate: ② seat-ruled follow-up (seat repo:objectstack#5 ruling A on #38473's needs_decision; #38473 closed not planned per its Ask 3) · Source: fleet, measured in dev report 7251984363 on #38473.
Grading: area:ai · Bug (performance) · p2 · Tier S · reach: every pgvector deployment searched in Chinese, Japanese or Korean since PR #38469 (keyword pieces) · evidence: measured (below).
Part of #37000.

维护者速读

  • 问题:PostgreSQL 存储上,中文整句检索的关键词一路 p95 约 3.8 秒,时间主要花在给每一行候选重复算每个词的权重(IDF)上。
  • 做什么:每个词的权重只算一次,再拿去给候选打分;不加索引、不加扩展,排序结果不变。
  • 效果(实测):p95 约 3.8 秒降到约 1.6 秒,六次排名完全一致。
  • 要你做的:无。

What happens

The pgvector keyword score evaluates ln(1.0 + (n - d + 0.5) / (d + 0.5)) for every term (up to 16 CJK pieces) on every candidate row, in numeric arithmetic. When a piece most passages hold (公司 is in 99.7% of the bench passages) makes nearly every row a candidate, scoring takes about 2.4 s of a 3.9 s search (EXPLAIN ANALYZE in 7251984363). The candidate scan itself is about 0.6 s.

Ask

  1. Compute each term's IDF once per search, for example in a materialized stats CTE, and score candidates against it. The change is in the search query of packages/plugins/knowledge-pgvector/src/pgvector-adapter.ts (and sql.ts if the query text lives there). Keep numeric arithmetic: the float8 form measured p95 1.39 s but changed 1 of 218 rankings (tie order), which fails the bar.
  2. knowledge-turso evaluates the same per-row expression in SQLite float math (turso-adapter.ts, its keyword score). Measure it in the same PR, or write NOT MEASURED in the PR and file a card for it.

Acceptance

Related: #38473 (closed, not planned), #38411, PR #38469, #37000, #38346.

Activity

  1. added
    priority:p2
    pm:queue
    area:ai
    domain:server
    on Nov 29, 2026
  2. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack#5 · os-project-manager · session_01FEkQ5oQLfL8thb6wtUHnTb
    Claim: PM loop round 1 · 2026-11-29T06:43Z
    Session: session_01FEkQ5oQLfL8thb6wtUHnTb
    Account: os-project-manager
    Branch: claude/issue-38510-pgvector-idf-once
    Worktree: objectstack-issue-38510
    Domain: domain:server
    Seat: repo:objectstack#5
    Area: o5-ai37000
    File surface: packages/plugins/knowledge-pgvector/src/pgvector-adapter.ts (the keyword score in the search query only), packages/plugins/knowledge-turso/src/turso-adapter.ts (its keyword score only, if measured), new tests packages/plugins/knowledge-pgvector/src/__tests__/idf-once.test.ts and packages/plugins/knowledge-pgvector/src/__tests__/idf-once.live-postgres.test.ts (and a turso pin beside its suite if turso changes), one changeset .changeset/38510-keyword-idf-once.md (stop on breach; explain in the report). Both sql.ts files are not in it: PR #38498 (#38411) edits them.
    Container & model: S, mode:subagent, model: default (dispatch-gates --tier at dccf836, "Model tier — no path-derived mandate"), effort: medium (S)
    Clause-②: no
    Metadata-protocol: none
    Responsibility: introduced by PR #38469 (#38411): its up-to-16 CJK pieces multiply the per-row ln() terms; the per-row form predates it.
    Thread-read: none
    Premise: holds at dccf836 — the keyword score evaluates ln(1.0 + (s.n - s.d${i} + 0.5) / (s.d${i} + 0.5)) per candidate row at pgvector-adapter.ts:679; turso's twin is turso-adapter.ts:805; measured p95 3.8 s → 1.6 s with the IDF computed once (dev report 7251984363 on #38473).
    Serial constraints cleared: check-handover --inflight at dccf836a86: clear for both adapters; PR #38498 (#38411 follow-up) touches only the two sql.ts files and pins; overlap-check before every push
    Pre-claim lane: packages/plugins/knowledge-* ⇒ domain:server (file ownership only); filed by this area on the seat's ruling A for #38473 under #37000.
    Pre-claim deps: none.
    Prior rulings read: the seat's ruling A on #38473 (close it per Ask 3; file this card; Tier S; acceptance as on the card); #38473's close record 7252016704.

  3. added
    pm:dispatched
    and removed
    pm:queue
    on Nov 29, 2026
  4. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

    triage#1 · sunhaolin · session_017xRBcSxvUE3fDrox79CL9t

    Triage (record; in flight, labels untouched): lands in packages/plugins/knowledge-pgvector/src/pgvector-adapter.ts (the keyword-score query computes each term's IDF on every candidate row), optionally the turso adapter, with an IDF pin and a live-postgres test · area:ai · domain:server · Bug (a shipped search slow enough to hurt use: Chinese keyword p95 ~3.8 s) · p2 · size S. The cause came with PR #38469 (#38411). The body says "Part of #37000": attached as a sub-issue in this pass. ⚠️ The live-postgres job is red on main (anchor #38514): this card's own live test runs in it.
    Recorded 2026-11-29T06:58Z.

  5. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack#5 · os-project-manager · session_01FEkQ5oQLfL8thb6wtUHnTb
    os-dev-report
    {
    "issue": 38510,
    "status": "done",
    "branch": "claude/issue-38510-pgvector-idf-once",
    "pr": "#38533",
    "session": "session_01FEkQ5oQLfL8thb6wtUHnTb",
    "premise_still_valid": true,
    "summary": "The pgvector keyword leg now computes each term's IDF once per search. A materialized idf CTE reads the one-row stats, and kw scores candidates as the sum of h.hN * w.wN. The arithmetic stays numeric, so every score is digit-for-digit the per-row one. The only source change is the keyword-leg hunk in pgvector-adapter.ts (sql.ts untouched). Same bench as before (PostgreSQL 16.15 + pgvector 0.6.0, 67,447 passages, 218 zh questions, 3 interleaved runs per side, origin/main dccf836 vs branch): p95 went from 3,840-3,910 ms to 1,569-1,622 ms and p50 from 488-596 ms to 354-448 ms, with 0 of 218 rankings differing in all 6 runs. The box was shared, load average 4-7. Turso twin, measured and not changed: the same corpus went through TursoKnowledgeAdapter.upsert into a local libSQL file, and a scratch once-form gave p95 973-999 ms against 1,026-1,066 ms (about 5%), rankings identical. Its time is in the 2-character LIKE scan, not the double ln(), so turso-adapter.ts is untouched. One thing will show red that this diff did not cause: capability-mount.live-postgres.test.ts is already red on main (details in out_of_scope_findings and open_questions). Because this PR touches knowledge-pgvector/, the required check 'Temporal Conformance (live PG + MySQL)' will run that file and go red. The card assignee was not touched.",
    "tests": "Head 623da38 (live server: OS_TEST_POSTGRES_URL set, OS_EXPECT_LIVE_DIALECT_MATRIX=1, all under os-verify-lock). New pins: vitest run src/__tests__/idf-once.test.ts gave Tests 2 passed (2). vitest run src/__tests__/idf-once.live-postgres.test.ts gave Tests 2 passed (2), VERDICT command-exit 0. Full package suite pnpm --filter @objectstack/knowledge-pgvector exec vitest run --maxWorkers=2: Test Files 1 failed | 12 passed (13), Tests 112 passed | 2 skipped (114), VERDICT command-exit 1. The failure is capability-mount.live-postgres.test.ts:119 (expected { deploymentAdapter: 'pgvector' }, received the same plus runtimeComposed: { declared: false }). With the adapter restored to dccf836 that file fails identically (VERDICT command-exit 1, same AssertionError), so it is not caused by this diff. pnpm --filter @objectstack/knowledge-pgvector typecheck: VERDICT command-exit 0, and --listFiles counts both new files in tsconfig.json and tsconfig.test.json. Ablations (committed first, restored by blob hash c73f0f0a0b, git diff HEAD empty): (1) adapter restored to dccf836 made all 4 new cases red (unit: 'the statement holds a CTE named idf: expected null not to be null'; live: the steps regex fails). (2) ablation-replace changed the weight to (d + 1.5), the live equality assertion went red on both cases, and the file was restored. Tests import ../index.js from src, so no dist leg applies. Before the closure build, pnpm --filter '@objectstack/knowledge-pgvector^...' --filter '@objectstack/knowledge-turso^...' build gave VERDICT command-exit 0.",
    "gates": "node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack at 623da38 derived 67 families. I ran all 67 locally with each exit captured before any pipe: 66 exited 0. check:dual-build-cjs-loads exited 3 ('PREREQUISITE NOT MET', it needs a full pnpm build), so NOT MEASURED locally; CI's Lint & Repo Gates runs it. --ran with exit codes recorded: '67 derived famil(ies) accounted for, 66 run, 1 NOT-MEASURED (1 DERIVED from a recorded exit 3)', exit 0. overlap-check --branch=HEAD gave 'VERDICT clean exit 0' before every push. PR check-runs were not read: the report is delivered when local verification ends, and CI convergence is the seat's. Expected: 'Temporal Conformance (live PG + MySQL)' goes red on capability-mount.live-postgres.test.ts (already red on main, see open_questions). check-platform-checklist was not owed (no export renamed or removed).",
    "dogfood_battery": "n/a: no ! in the PR title and no (narrowing) arm",
    "mcp_calls": "0",
    "api_writes": "3 REST writes through the fleet-write relay / post-stamped: POST /repos/objectstack-ai/objectstack/pulls (draft #38533) plus its assignee leg POST /repos//issues/38533/assignees (os-project-manager), both in one pr_create action; read-back reported 7234 bytes sent and 7234 stored, identical. Then POST /repos//issues/38510/comments (this os-dev-report, via post-stamped.mjs). Plus git pushes, which are not REST writes: the dev-worktree --probe and three commits pushed to claude/issue-38510-pgvector-idf-once.",
    "open_questions": [
    {
    "question": "capability-mount.live-postgres.test.ts is already red on main. It pins resolveCapabilityArgument('knowledge') as { deploymentAdapter: 'pgvector' }, and core now also answers runtimeComposed: { declared: false } (merge 91fc398). The required check 'Temporal Conformance (live PG + MySQL)' runs the pgvector live files only when a PR touches knowledge-pgvector/
    , so this PR will be the one that shows it red. How does this PR land?",
    "options": [
    "A: The seat widens this claim's file surface by that one test file. I change line 119 to the full answer (deploymentAdapter plus runtimeComposed { declared: false }), push to this PR, and it lands green. Cost: one line and a re-run of the pgvector live suite.",
    "B: The seat files a separate card for the stale pin, lands it first, and this PR merges main afterwards. Cost: one more S card and one more round before this lands."
    ],
    "recommendation": "A, because it is a one-line pin update that this PR's own required check cannot pass without, and the cause (the new runtimeComposed field) is already landed and intended."
    }
    ],
    "security_findings": "none",
    "out_of_scope_findings": [
    "class: a · reach: required CI check 'Temporal Conformance (live PG + MySQL)' goes red on every PR that touches packages/plugins/knowledge-pgvector/** (temporal_live paths filter), this PR first · evidence: packages/plugins/knowledge-pgvector/src/tests/capability-mount.live-postgres.test.ts:119 expects toEqual({ deploymentAdapter: 'pgvector' }); packages/core/src/capability-composition.ts:309 now returns runtimeComposed: { declared: ... } beside it (merge 91fc398). Reproduced on a live PostgreSQL 16 with the adapter at dccf836: same AssertionError, VERDICT command-exit 1 · dedupe words: capability-mount live-postgres runtimeComposed, knowledge deploymentAdapter pin, temporal conformance pgvector red",
    "carrier: none · noted, not filed: knowledge-turso was measured (once-form p95 973-999 ms vs 1,026-1,066 ms, 3 interleaved runs per side on the same 67k corpus in a local libSQL file, rankings identical). The lever is worth about 5% there, because turso's p95 is set by the 2-character LIKE candidate scan. No card is owed: the card asked for a measurement or a card, and it was measured."
    ]
    }

  6. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack#5 · os-project-manager · session_01FEkQ5oQLfL8thb6wtUHnTb

    Delivered: PR #38533 merged into main as 4a43d6c (2026-11-29T09:08Z). The pgvector keyword leg computes each term's IDF once per search (a materialized idf CTE, numeric kept): p95 3.84–3.91 s → 1.57–1.62 s on 67,447 passages and 218 Chinese questions, 0 of 218 rankings changed across 6 interleaved runs. Turso measured (about 5%; its time is the 2-character LIKE scan), unchanged, so no card is owed. L1-a ACCEPT 7252834317. Clause-②: no, Metadata-protocol: none, not governed. pm:dispatched removed in this stroke.

  7. removed
    pm:dispatched
    on Nov 29, 2026
  8. added a commit that references this issue on Dec 1, 2026
    4a43d6c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area:aidomain:serverpriority:p2

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions