-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[DBMON-5640] Postgres - Compile and reuse regex patterns #21317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files🚀 New features to boost your workflow:
|
from .util import DBExplainError | ||
from .version_utils import V12 | ||
|
||
# Pre-compiled regex patterns for performance optimization |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've always found these comments kinda weird… sure it saves some performance, but only if you explode the Python regex cache to begin with (I think the runtime has one, and I think it can be expanded with a library call)…
If the regex has bad backtracking or something, it being compiled will probably be in the noise…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this comment is bad and slipped in. Let me remove that so it's not misleading. To clarify the changes, it's less that we'll see improvement in base cases but more that we'll avoid potential performance hit pitfalls on hot loop paths.
As you mentioned below Python does have a lru cache for on the fly regex. It has a a cap of 512 patterns. Way higher than we realistically should see within a single Postgres check instance, but since this runs as a plugin alongside potentially many other instances of this or other checks it's not impossible. We expose config options for passing in lists of regex patterns for customer provided queries in a number integrations which leaves this more susceptible to unknown increases in counts.
The main benefit here is we'll be holding onto a hard reference to the compiled regex directly so this is guaranteeing that our hot path regex don't accidentally get evicted and churned
t.extend(self._tags_no_db) | ||
return t | ||
|
||
def _get_compiled_pattern(self, pattern_str): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sort of seems like the same logic Python already uses internally… https://github.com/python/cpython/blob/fbfbfdf33c3cfe00c6319423ac69f5fb4e877034/Lib/re/__init__.py#L321
This is probably an improvement (is it benchmarked?) but the more of this regex stuff I see in the agent the more I wonder if there isn't a better global approach to this. Python include an internal cache for storing compiled regexes, so calling It's still a |
Review from jasonmp85 is dismissed. Related teams and files:
- database-monitoring-agent
- postgres/datadog_checks/postgres/explain_parameterized_queries.py
What does this PR do?
This PR compiles and reuses regex patterns in the Postgres integration to avoid rebuilding the pattern frequently
Motivation
PARAMETERIZED_QUERY_PATTERN
- This is a fairly expensive regex pattern that does lookaheads and lookbacks of capture groups. We run this static regex pattern in a tight loop for each query sampled that is explained vis parameterized query. This should have the most benefit seenReview checklist (to be filled by reviewers)
qa/skip-qa
label 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