Skip to content

Obfuscate EXTRACT field keywords#98

Merged
joelmarcotte merged 1 commit into
mainfrom
joel.marcotte/SDBM-2597
May 5, 2026
Merged

Obfuscate EXTRACT field keywords#98
joelmarcotte merged 1 commit into
mainfrom
joel.marcotte/SDBM-2597

Conversation

@joelmarcotte

@joelmarcotte joelmarcotte commented May 5, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Customer hit duplicate DBM query signatures because EXTRACT(epoch FROM ...) was obfuscated differently depending on capture path: pg_stat_statements normalizes to EXTRACT($1 FROM ...) (replaced with ?), while pg_stat_activity preserved epoch as an unquoted identifier.
  • The obfuscator now tracks EXTRACT( context and rewrites the field argument to ? when it matches a known PostgreSQL field name (epoch, year, month, dow, isodow, microseconds, timezone_hour, etc.), so both code paths converge on EXTRACT(? FROM x).
  • Conservative match: only known field keywords are replaced. Bare epoch outside an EXTRACT call (e.g. a column reference) and unrecognized field names are left alone, so user identifiers aren't accidentally rewritten.

Test plan

  • go test ./... passes (existing + new)
  • New unit cases in obfuscator_test.go cover the standalone Obfuscate path: positive cases (lowercase, uppercase, mixed-case fields), positional parameter regression, and negative cases (epoch as a column, unknown field name)
  • New JSON fixtures in testdata/postgresql/select/ cover ObfuscateAndNormalize:
    • extract-epoch-field.json — primary regression test for the customer issue
    • extract-various-fields.jsonyear/month/dow/microseconds/timezone_hour in one query
    • extract-positional-parameter.json — pg_stat_statements form
    • extract-quoted-string-field.jsonEXTRACT('epoch' FROM x) (string literal alternative)
    • date-trunc-string-literal.json, date-part-string-literal.json — already obfuscated, locked in
    • extract-field-name-as-column.json — confirms epoch/year outside EXTRACT remain untouched
  • BenchmarkObfuscator* shows no measurable regression (the change adds two string compares per non-whitespace token)

🤖 Generated with Claude Code

The pg_stat_statements form `EXTRACT($1 FROM x)` and the pg_stat_activity
form `EXTRACT(epoch FROM x)` previously produced different obfuscated
signatures: the positional parameter was replaced with `?` while the
unquoted field keyword was preserved as an identifier. This split one
logical query into two DBM signatures.

Track EXTRACT( context in the obfuscator and replace the field argument
with `?` when it matches a known PostgreSQL field name (epoch, year,
month, dow, etc.). Bare `epoch` outside an EXTRACT call and unrecognized
field names are left untouched.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@joelmarcotte joelmarcotte requested a review from a team as a code owner May 5, 2026 14:56
@joelmarcotte joelmarcotte changed the title [SDBM-2597] Obfuscate EXTRACT field keywords Obfuscate EXTRACT field keywords May 5, 2026
@joelmarcotte joelmarcotte merged commit 990b4c3 into main May 5, 2026
6 of 7 checks passed
@joelmarcotte joelmarcotte deleted the joel.marcotte/SDBM-2597 branch May 5, 2026 15:52
joelmarcotte added a commit that referenced this pull request May 5, 2026
Adds entries for the three bug fixes (#89, #96, #98) and the perf
follow-up (#99) shipped since v0.2.1.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants