[DBMON-6664] Optimize Postgres query metrics#23823
Draft
eric-weaver wants to merge 7 commits into
Draft
Conversation
🎉 All green!🧪 All tests passed 🎯 Code Coverage (details) 🔗 Commit SHA: f2b31c7 | Docs | Datadog PR Page | Give us feedback! |
Contributor
Validation ReportAll 21 validations passed. Show details
|
Codecov Report❌ Patch coverage is Additional details and impacted files🚀 New features to boost your workflow:
|
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.
What does this PR do?
Adds a new
PostgresStatementMetricsV2collection pipeline for Postgres query metrics that replaces the naive full-table scan with a change-detection and cached-obfuscation approach. Enabled via the hidden config flagquery_metrics.use_v2: true.How the V2 algorithm works:
pg_stat_statements(false)for integer counters only (no query text pulled from disk).DeltaDetectordiffs against the previous snapshot to find rows wherecallsincremented.queryids,ObfuscationLookupchecks a two-tier LRU cache (queryid -> query_signature -> ObfuscationResult). On cache miss, fetch raw text from PG, obfuscate via FFI, cache, and discard the raw text.(query_signature, datname, rolname)and emit.Key design properties:
pg_stat_statements(false)skips text from disk -- reduces PG-side I/O and wire bytes.queryids sharing aquery_signatureshare oneObfuscationResult-- deduplicates cache entries.pg_stat_statements.max.New files:
delta_detector.pyobfuscation_lookup.pyqueryid -> query_signature -> ObfuscationResultstatements_v2.pyTest coverage:
test_statements_v2.py) coveringDeltaDetectorandObfuscationLookupin isolationtest_statements_v2_integration.py) mirroring key V1 integration tests: end-to-end collection, cold start, duplicate merging, error handling, pgss dealloc, WAL metrics, and internal telemetryOutput contract is identical to V1 -- same payload structure, same
dbm-metricsanddbm-samplesevent formats, same metric names. V1 code (statements.py) is untouched.Benchmark Results
Benchmarks were run on a
local-devPostgres stack with 4 agent variants (V1, V1-incremental, V2, V3) across Low Churn, High Churn, Cold Start, and Eviction Pressure workloads. V3 was an intermediate prototype whose logic is now collapsed into V2.Full 4-Way Comparison (blended 60-minute averages)
Key Improvements (V2 vs V1)
The primary driver is eliminating redundant obfuscation: V1 obfuscates every row every 10-second interval (5-10k FFI calls), while V2 only obfuscates on cache miss (typically < 100 per interval in steady state).
Motivation
The existing V1 statement metrics collection pulls the full
pg_stat_statementstable (including query text) every 10 seconds, obfuscates every row through a Python-to-Go FFI bridge, then discards ~95% of rows that haven't changed. This wastes PostgreSQL I/O, network bandwidth, CPU on redundant obfuscation, and memory on transient string allocations. The experimentalincremental_query_metricsflag partially addressed this but was never fully productionized.V2 was designed from scratch to be efficient by default: detect what changed first (integers only, no text), then resolve only the queries that need it. The architecture also lays groundwork for future batch FFI obfuscation support.
Review checklist (to be filled by reviewers)
qa/skip-qalabel if the PR doesn't need to be tested during QA.backport/<branch-name>label to the PR and it will automatically open a backport PR once this one is merged