Repository navigation
fix: MCP baselines empty store, why regexp perf, fk partial index - #170
Merged
Merged
Conversation
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>
# Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #157
Fixes #142
Fixes #129
One commit per issue.
#157:
pgbot://baselinesread asnullon an empty storestore.List()returns a nil slice when there are no snapshots, and the MCP resource marshalled that slice directly, so the body wasnull. 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:
referencesTablecompiled a regexp on every callThe regexp is replaced with an allocation-free scan that keeps the regexp's exact semantics. Go's
\bis an ASCII[0-9A-Za-z_]boundary and(?i)is Unicode simple folding, so the scan compares runes by theirSimpleFoldorbit 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.FuzzReferencesTablechecks equivalence and ran about 620k inputs locally with no mismatch.TestReferencesTable_doesNotAllocatefails 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_unindexedignored partial indexesA 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 impliesfk_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_exprpredicate is a conjunction of<fk column> IS NOT NULLterms (any subset of the FK columns, matched withquote_ident). Everything else still counts as not covering:deleted_at IS NULL, IS NOT NULL combined with other conditions, OR, and other spellings such asNOT (x IS NULL). So the change can only remove false positives. It is still a catalog-only read. The "what pgbot cannot see" section ofdocs/findings/fk_unindexed.mdis updated.Regression test:
TestIntegration_fkPartialIndexCoverage(needsPGBOT_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 ./...andgo vet ./...passed.go test -race ./...passed.golangci-lint run ./...(v2.14.0) reported 0 issues.internal/collectandcmd/pgbotintegration suites passed against a local PG 18 with pg_stat_statements loaded, as superuser pluspgbot_ro.internal/model,schema/*.jsonuntouched).🤖 Generated with Claude Code