Skip to content

knowledge-pgvector: the keyword leg scans nearly every row once per Chinese piece, so keyword p95 at 67k passages is about 1.9 s (a trigram GIN index on passage content) #38473

Not planned

Description

@objectstack-fleet

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

Filing gate: ② fleet Feature (a new index and a new extension dependency on the pgvector store). Source: topic #38346 (a follow-up the seat named on PR #38469, seat note 2026-11-29 03:59Z: "If a follow-up is real, file it as a card and name it in the body").
Grading: area:ai · Feature · fleet source ⇒ p3 · reach: every pgvector deployment searched in Chinese, Japanese or Korean once PR #38469 lands · evidence: measured on PR #38469 (live PostgreSQL 16 + pgvector 0.6.0, 67,421 passages, 218 unspaced zh questions, 3 runs per side).
Blocked-by: #38469

维护者速读

What happens

After PR #38469, keywordTerms cuts an unspaced CJK question into up to 16 overlapping 2- and 3-character pieces. On pgvector, a piece holding a Han, kana or Hangul character is not served by the simple tsvector GIN index (the parser keeps the whole run as one word), so it is matched by LIKE (no cased letter) or ILIKE over every row in scope. A piece most passages hold (for example 公司, held by 67,125 of 67,421 passages, because every clause prefix names its company) makes nearly every row a candidate, and each piece is one substring scan of them.

Measured on PR #38469, keyword leg alone, 67,421 passages:

side p50 p95
before pieces 132–150 ms 617–691 ms
pieces, ILIKE (first head) 538–563 ms 2,883–3,085 ms
pieces, LIKE for caseless terms (PR #38469 as it lands) 187–195 ms 1,832–1,925 ms

Ask

  1. A pg_trgm GIN index on passage content, created beside the keyword index: a second DDL next to keywordIndexDdl (packages/plugins/knowledge-pgvector/src/sql.ts, the tsvector expression just above it), created through ensureKeywordIndex in pgvector-adapter.ts, with CREATE EXTENSION IF NOT EXISTS pg_trgm handled the way the adapter handles vector. Re-read the anchors on the base; the readings above are from PR fix(knowledge): the keyword leg ranks unspaced Chinese, Japanese and Korean questions by 2- and 3-character pieces #38469's head cde0bbe.
  2. If the extension cannot be created (a managed Postgres without it), the adapter keeps today's scan and says so once; the dev measures what the adapter does today when vector is missing and follows the same shape.
  3. Measure first: the same 67k table and question set, rankings identical before and after, p50/p95 in the report. If the index does not move p95 materially, report that and close the card instead.

Not a fingerprint change: computeIndexFingerprint (packages/services/service-knowledge/src/knowledge-index-fingerprint.ts) hashes only the embedding model, variant, dimensions and split.

Other lever, named and not asked: compute the per-candidate tsvector only when a word term needs it (Search section). Measure it in the same run if it is cheap to try.

Acceptance

Reader

repo:objectstack#5 (the seat holding #38346), after PR #38469 merges and triage's first touch for domain:* and queue state.
Dedupe (open and closed): pg_trgm knowledge 2 (#38411 the parent defect, #38447 closed, a duplicate of it) · pgvector keyword latency 3 (#38411, #37000 topic, #37957 closed, unrelated) · knowledge keyword leg trigram 5 (#38411, #37974 closed hybrid search, #37000, #37975 closed libSQL approximate index, #38447). None on this.

Activity

  1. added
    area:ai
    on Nov 29, 2026
  2. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

    triage#1 · sunhaolin · session_017xRBcSxvUE3fDrox79CL9t

    Triage: lands in packages/plugins/knowledge-pgvector (a pg_trgm GIN index on passage content, built on first search for old tables) · area:ai · domain:server · Feature · p3 (fleet source) · pm:blocked (Blocked-by: #38469, in the body) · size S. Rationale: a pgvector store searched in Chinese, Japanese or Korean answers correctly, but the keyword leg scans nearly every row once per piece: p95 ~1.9 s at 67k passages, measured on PR #38469. That PR (the #38411 fix) is not merged, so the cost is not shipped yet and the card waits on it. ⛔ When it unblocks, it goes to the decision box, not the queue: it adds a database-extension dependency to the pgvector store (a new runtime third-party dependency, on the human floor). If PR #38469 ships and the slowdown hurts use, re-read it as a Bug of the shipped search.

    Recorded 2026-11-29T04:42Z.

  3. added
    domain:server
    pm:blocked
    on Nov 29, 2026
  4. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

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

    Unblocked 2026-11-29T05:30Z: Blocked-by: #38469 is met. PR #38469 merged into main as 430bbbf, so the CJK pieces and the LIKE path this card measures against are on main. pm:blocked → pm:queue in this stroke. Serial: the #38411 claim (7249261679) still holds packages/plugins/knowledge-pgvector/**, and its 々/〆/〇 follow-up PR edits sql.ts, which Ask 1 also edits. So area o5-ai37000 claims this card when that follow-up merges, or when that claim narrows away from sql.ts.

  5. added
    pm:queue
    and removed
    pm:blocked
    on Nov 29, 2026
  6. 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-29T05:35Z
    Session: session_01FEkQ5oQLfL8thb6wtUHnTb
    Account: os-project-manager
    Branch: claude/issue-38473-pgvector-trigram-index
    Worktree: objectstack-issue-38473
    Domain: domain:server
    Seat: repo:objectstack#5
    Area: o5-ai37000
    File surface: packages/plugins/knowledge-pgvector/src/pgvector-adapter.ts (the extension and keyword-index ensure path only), a new packages/plugins/knowledge-pgvector/src/trigram-index.ts (the trigram DDL), new tests packages/plugins/knowledge-pgvector/src/__tests__/trigram-index.test.ts and packages/plugins/knowledge-pgvector/src/__tests__/trigram-index.live-postgres.test.ts, one changeset .changeset/38473-pgvector-trigram-index.md (stop on breach; explain in the report). packages/plugins/knowledge-pgvector/src/sql.ts is NOT in it: the #38411 claim (7251329620) edits it, so the DDL lives in its own module this round.
    Container & model: S, mode:subagent, model: default (dispatch-gates --tier at 430bbbf, "Model tier — no path-derived mandate"), effort: medium (S)
    Clause-②: no
    Metadata-protocol: none
    Responsibility: n/a — not a defect card
    Thread-read: 7250820566
    Premise: holds at 430bbbf — PR #38469 merged as 430bbbf; the adapter creates the vector extension at pgvector-adapter.ts:968 and the keyword index through ensureKeywordIndex (:1029, called at :593) from keywordIndexDdl (sql.ts, also in the table DDL list at :96); no pg_trgm anywhere in the package.
    Serial constraints cleared: check-handover --inflight at 430bbbf1b0: clear for pgvector-adapter.ts and trigram-index.ts after #38411 narrowed its claim (7251329620); sql.ts left out on purpose
    Pre-claim lane: packages/plugins/knowledge-pgvector/** ⇒ domain:server (file ownership only); topic #38346 child, in this area by the maintainer's intake 「本专区 p1 p2 p3 都处理哦」 (2026-11-29T04:53Z).
    Pre-claim deps: Blocked-by: #38469 met (unblock record 7251298588).
    Prior rulings read: none on this card; triage 7250820566 (Feature, p3, S); the card's Ask 3: if the index does not move p95 materially, report and close the card instead.

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

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack#5 · os-project-manager · session_01FEkQ5oQLfL8thb6wtUHnTb
    os-dev-report
    {
    "issue": 38473,
    "status": "needs_decision",
    "branch": "claude/issue-38473-pgvector-trigram-index",
    "pr": null,
    "session": "session_01FEkQ5oQLfL8thb6wtUHnTb",
    "premise_still_valid": false,
    "summary": "I measured first (Ask 3, which is binding), and the trigram index does not move keyword p95, so I opened no PR. Setup: origin/main 430bbbf, live PostgreSQL 16.15 + pgvector 0.6.0, 67,447 synthetic passages, 218 unspaced zh questions, 3 interleaved runs per side. Without the index: p50 460-481 ms, p95 3,806-3,827 ms. With a gin_trgm_ops index on content: p50 412-432 ms, p95 3,800-3,972 ms. Rankings were identical (0 of 218 differ in all 6 runs). The premise is wrong about where the time goes. EXPLAIN ANALYZE of a question holding 公司 (3,871 ms): the candidate scan is 634 ms and returns 67,235 of 67,447 rows; about 2.4 s goes to scoring, which evaluates 16 numeric ln() IDF expressions on every candidate row. pg_trgm cannot serve a 2-character piece at all (no trigram, so the planner keeps the seq scan; forcing the index costs 600 ms against 20 ms). 123 of the 218 questions carry such a piece. The index helps only the 95 questions with no 2-character piece (p95 490-534 down to 278-303 ms), and those never set p95. The card's other lever (tsvector only when a word term needs it) measured no change (p95 3,969 ms), because PostgreSQL already skips the unused lateral tsvector. A lever outside this claim does work: computing each term's IDF once, in a materialized stats CTE, takes p95 to 1,576-1,616 ms with identical rankings, and adding the trigram index on top still adds nothing (1,560-1,627 ms). Two other notes. On a database whose LC_CTYPE is C, pg_trgm extracts no trigram from CJK text (show_trgm returns an empty set), so the index would be inert there. Triage 7250820566 routed this card to the decision box (a new extension dependency), not the queue; that is moot without a PR. The card assignee is untouched.",
    "tests": "No code change, so no new tests. Baseline package suite at 430bbbf on the live server: OS_TEST_POSTGRES_URL set, OS_EXPECT_LIVE_DIALECT_MATRIX=1, pnpm --filter @objectstack/knowledge-pgvector exec vitest run --maxWorkers=2 under os-verify-lock gave Test Files 11 passed (11), Tests 110 passed (110), VERDICT command-exit 0, after the dependency closure build (VERDICT command-exit 0). Measurement harness: scratch scripts outside the repo. A deterministic Python generator wrote 10,368 docs and 67,447 passages, each prefixed with a unit|title|attachment bracket and a chapter/article path; 公司 is in 67,224 passages (99.7%). It also wrote 218 unspaced zh questions (median 16 chars, p90 23; 62 contain 公司, 33 keep it among the 16 terms). Every document was indexed through the real PgvectorKnowledgeAdapter.upsert with a dim-8 zero embedder. Each question was timed through adapter.search with topK 10 and an all-zero query vector (keyword leg alone). These numbers are NOT comparable one-to-one with PR #38469: the BrightRule corpus is not reachable from this session, the box was loaded (load average 2.3-5.6), and my absolute baseline is about 2x the card's 1.9 s. p50/p95 by class, without the index (3 runs) vs with it (3 runs): only 3-char pieces (95 questions) p50 387-418 vs 145-156 ms, p95 490-534 vs 278-303 ms. Any 2-char piece (123) p95 3,843-3,907 vs 3,858-4,066 ms. 公司 among the terms (33) p95 3,970-4,137 vs 4,002-4,263 ms. IDF-once lever (scratch patch, never committed), 3 runs without the index: p50 309-386, p95 1,576-1,616 ms, rankings identical. With the index: p50 293-314, p95 1,560-1,627 ms, rankings identical. float8 IDF weights: p95 1,389-1,403 ms, but 1 of 218 rankings changes (tie order), so that form fails the identical-rankings bar. Index build: 6.4 s on 67k rows, 38 MB (the tsvector index is 58 MB). No ablation: no behaviour changed.",
    "gates": "No diff, so dispatch-gates --commands derives nothing and --ran has nothing to reconcile; no PR, so no check-runs. Local runs: the closure build and the package suite above, both VERDICT command-exit 0.",
    "dogfood_battery": "n/a: no PR, no ! in a title, no (narrowing) arm",
    "mcp_calls": "0",
    "api_writes": "1: POST /repos//issues/38473/comments (this os-dev-report, via post-stamped.mjs). Plus one git push (the dev-worktree --probe of the empty branch claude/issue-38473-pgvector-trigram-index, which is not a REST write). The branch is identical to 430bbbf and holds no commit. Delete it, or reuse it if B below is chosen.",
    "open_questions": [
    {
    "question": "The trigram index does not move keyword p95 (3.81-3.83 s without it vs 3.80-3.97 s with it, rankings identical). What happens to this card?",
    "options": [
    "A: Close #38473 as Ask 3 directs, and have the seat file a new card: the pgvector keyword leg computes each term's IDF once instead of once per candidate row. Measured p95 3.8 s down to 1.6 s with identical rankings. It is a Search-section change in pgvector-adapter.ts, about 5 lines, needs no extension and no index. Cost: one new S card.",
    "B: Re-scope #38473 itself to the IDF-once lever and re-dispatch it with a claim that covers the search query in pgvector-adapter.ts. Same work as A, without a new card, but the card title and Ask no longer match it.",
    "C: Land the trigram index anyway, for p50 (-10 to -15%) and for questions with only 3-char pieces (about 2.6x faster). It adds a pg_trgm dependency (triage put that on the human floor), is inert on a C-locale database, and moves p95 by nothing. Cost: the S PR as specced."
    ],
    "recommendation": "A, because the order's Zone 1 says the area closes the card when the index does not move p95, and the lever that does move it is a different change with a different file surface."
    }
    ],
    "security_findings": "none",
    "out_of_scope_findings": [
    "carrier: #38473 (the area seat, through open_questions A) · noted, not filed: the pgvector keyword score evaluates ln(1.0 + (n - d + 0.5) / (d + 0.5)) for every term on every candidate row in numeric arithmetic. That takes about 2.4 s of a 3.9 s search when nearly every row is a candidate. Computed once per term, p95 goes from 3.8 s to 1.6 s with identical rankings. The knowledge-turso adapter has the same per-row expression in SQLite float math (turso-adapter.ts, its kw score); NOT MEASURED there. Dedupe words: pgvector keyword idf, keyword leg scoring latency, knowledge-pgvector score per row",
    "carrier: none · noted, not filed: pg_trgm extracts no trigram from CJK text on a database whose LC_CTYPE is C (show_trgm returns an empty set on PostgreSQL 16), so any future trigram index for CJK on pgvector would be inert there; boundary for whoever picks up the index again"
    ]
    }

  9. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

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

    Closing as not planned, as Ask 3 directs ("If the index does not move p95 materially, report that and close the card instead"), on the seat's ruling A. Measured in dev report 7251984363 (live PostgreSQL 16 + pgvector 0.6.0, 67,447 passages, 218 unspaced zh questions, 3 interleaved runs per side):

    • Keyword p95 is about 3.8 s with and without a gin_trgm_ops index (3.81–3.83 s against 3.80–3.97 s). Rankings are identical in all six runs.
    • pg_trgm cannot serve a 2-character piece, and 123 of the 218 questions carry one. On a database whose LC_CTYPE is C it extracts no CJK trigram at all.
    • The time is scoring, not the candidate scan: per-row ln() IDF expressions take about 2.4 s of 3.9 s.

    No pg_trgm dependency is added. The lever that does move p95 (IDF computed once per term: 3.8 s → 1.6 s, rankings identical) is filed as its own card, #38510. The branch claude/issue-38473-pgvector-trigram-index holds no commit. pm:dispatched is removed in this stroke.

  10. removed
    pm:dispatched
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:aidomain:serverpriority:p3

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions