Skip to content

fix: MCP baselines empty store, why regexp perf, fk partial index - #170

Merged
alexshapalov merged 5 commits into
mainfrom
fix/mcp-why-fk
Nov 23, 2026
Merged

alexshapalov merged 5 commits into
mainfrom
fix/mcp-why-fk

Conversation

@alexshapalov

Copy link
Copy Markdown
Contributor

Fixes #157
Fixes #142
Fixes #129

One commit per issue.

#157: pgbot://baselines read as null on an empty store

store.List() returns a nil slice when there are no snapshots, and the MCP resource marshalled that slice directly, so the body was null. The resource now turns nil into []store.ListItem{} before marshalling, so it has the same array shape as a populated store. Regression test: TestBaselinesResource_emptyStoreIsArray (checks the empty store reads as [] and the seeded store reads as a non-empty array).

Not changed: the issue also suggests snake_case json tags on store.ListItem. That would change the key names a populated store already returns, so it is a contract change and is left for a separate decision.

#142: referencesTable compiled a regexp on every call

The regexp is replaced with an allocation-free scan that keeps the regexp's exact semantics. Go's \b is an ASCII [0-9A-Za-z_] boundary and (?i) is Unicode simple folding, so the scan compares runes by their SimpleFold orbit and checks neighbouring bytes for the boundary. Behaviour is deliberately identical. This includes the ASCII-only \b, which the issue's proposed patch would have changed for non-ASCII names.

  • The test file keeps the old regexp as an oracle. Table cases cover Kelvin sign / long s folding, invalid UTF-8, an empty name and quoted names. FuzzReferencesTable checks equivalence and ran about 620k inputs locally with no mismatch.
  • TestReferencesTable_doesNotAllocate fails on the old code (172 allocs).
  • BenchmarkReferencesTable (8 tables, M1 Pro): 42.6 µs, 18.6 KB, 172 allocs → 5.4 µs, 0 B, 0 allocs.

#129: fk_unindexed ignored partial indexes

A partial index does not contain every child row, so in general it cannot serve the RI check. The exception is the predicate the RI lookup guarantees on its own. The trigger queries WHERE $1 OPERATOR(=) fk_col, and that strict operator implies fk_col IS NOT NULL, so the planner uses such an index even in the generic plan. I confirmed this on PG 18 with auto_explain during a parent DELETE: Index Scan using c_parent.

The SQL now accepts a partial index whose pg_get_expr predicate is a conjunction of <fk column> IS NOT NULL terms (any subset of the FK columns, matched with quote_ident). Everything else still counts as not covering: deleted_at IS NULL, IS NOT NULL combined with other conditions, OR, and other spellings such as NOT (x IS NULL). So the change can only remove false positives. It is still a catalog-only read. The "what pgbot cannot see" section of docs/findings/fk_unindexed.md is updated.

Regression test: TestIntegration_fkPartialIndexCoverage (needs PGBOT_TEST_SUPERUSER_DSN). It checks the planner premise with a generic-plan EXPLAIN (no ANALYZE) on the admin connection. It then runs the real collector as the read-only role and checks the covered tables (single column; quoted multi-column) and the not-covered ones (soft delete; mixed predicate). It fails on the old SQL and passes on PG 14 and PG 18.

Verification

  • gofmt -l . produced no output. go build ./... and go vet ./... passed.
  • go test -race ./... passed.
  • golangci-lint run ./... (v2.14.0) reported 0 issues.
  • The internal/collect and cmd/pgbot integration suites passed against a local PG 18 with pg_stat_statements loaded, as superuser plus pgbot_ro.
  • No JSON Context schema changes (internal/model, schema/*.json untouched).

🤖 Generated with Claude Code

alexshapalov and others added 5 commits November 23, 2026 14:01
store.List builds its result with `var out []ListItem` and returns it nil
when the snapshots table is empty; the resource marshalled that directly,
so a fresh install answered resources/read with the JSON document `null`
while the resource's contract (and a populated store) is an array.
Normalise nil to an empty slice before marshalling. The CLI path
(`pgbot baselines list`) already handled the empty case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…#142)

referencesTable built `(?i)\b<table>\b` with regexp.MustCompile on every
call, and referencedTables calls it samples x tables for every shifted
query, so one analysis compiled thousands of identical regexps
(172 allocs per 8-table pass in the new benchmark).

Replace it with a scan that implements the same semantics directly:
Go's \b is an ASCII [0-9A-Za-z_] boundary and (?i) is Unicode simple case
folding, so a rune-wise SimpleFold comparison plus a neighbouring-byte
boundary check is exactly equivalent. Behaviour is deliberately
unchanged (including that \b is ASCII-only, so non-ASCII names keep their
current matching); the old regexp is kept in the test as an oracle, with
table-driven cases and FuzzReferencesTable pinning equivalence (fuzzed
~620k inputs locally). TestReferencesTable_doesNotAllocate fails on the
old code.

BenchmarkReferencesTable (8 tables, M1 Pro): 42.6us/172 allocs -> 5.4us/0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ering (#129)

unindexed_fks.sql rejected every partial index (`indpred IS NULL`). That is
right in general — a partial index does not contain every child row, and
the RI check must find all of them — but too strict for the one predicate
the RI lookup itself guarantees: the trigger queries
`WHERE $1 OPERATOR(=) fk_col [AND ...]`, the operator is strict, so the
clause implies `fk_col IS NOT NULL` and the planner uses an index with that
predicate even in the generic plan (verified with auto_explain on PG 18:
"Index Scan using c_parent" for the RI query during DELETE on the parent).

Accept a partial index whose predicate, as deparsed by pg_get_expr, is a
conjunction of `<fk column> IS NOT NULL` terms (any subset of the FK's
columns, quoted names via quote_ident). Anything else — soft-delete
predicates, IS NOT NULL mixed with other conditions, OR, other spellings —
stays "not covering", so the change can only drop findings that were
false positives. Still a pure catalog read.

Integration test (PGBOT_TEST_SUPERUSER_DSN) proves the planner premise
and the covered / not-covered cases; it fails on the old SQL. Ran green on
PG 14 and PG 18.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alexshapalov
alexshapalov merged commit 21c8083 into main Nov 23, 2026
21 checks passed
@alexshapalov
alexshapalov deleted the fix/mcp-why-fk branch November 23, 2026 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant