Skip to content
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

Bug fix: fix postgres activity inflated query durations #11765

Merged
merged 1 commit into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions postgres/datadog_checks/postgres/statement_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@
]

PG_BLOCKING_PIDS_FUNC = ",pg_blocking_pids(pid) as blocking_pids"
CURRENT_TIME_FUNC = "clock_timestamp() as now,"

PG_STAT_ACTIVITY_QUERY = re.sub(
r'\s+',
' ',
"""
SELECT {pg_stat_activity_cols} {pg_blocking_func} FROM {pg_stat_activity_view}
SELECT {current_time_func} {pg_stat_activity_cols} {pg_blocking_func} FROM {pg_stat_activity_view}
WHERE coalesce(TRIM(query), '') != ''
AND query_start IS NOT NULL
{extra_filters}
Expand Down Expand Up @@ -236,12 +237,17 @@ def _get_active_connections(self):
def _get_new_pg_stat_activity(self, available_activity_columns):
start_time = time.time()
extra_filters, params = self._get_extra_filters_and_params(filter_stale_idle_conn=True)
report_activity = self._report_activity_event()
cur_time_func = ""
blocking_func = ""
# minimum version for pg_blocking_pids function is v9.6
# only call pg_blocking_pids as often as we collect activity snapshots
if self._check.version >= V9_6 and self._report_activity_event():
if self._check.version >= V9_6 and report_activity:
blocking_func = PG_BLOCKING_PIDS_FUNC
if report_activity:
cur_time_func = CURRENT_TIME_FUNC
query = PG_STAT_ACTIVITY_QUERY.format(
current_time_func=cur_time_func,
pg_stat_activity_cols=', '.join(available_activity_columns),
pg_blocking_func=blocking_func,
pg_stat_activity_view=self._config.pg_stat_activity_view,
Expand Down
6 changes: 5 additions & 1 deletion postgres/tests/test_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import mock
import psycopg2
import pytest
from dateutil import parser
from semver import VersionInfo
from six import string_types

Expand Down Expand Up @@ -644,7 +645,7 @@ def obfuscate_sql(query, options=None):
'query_signature': '9382c42e92099c04',
'statement': "BEGIN TRANSACTION; SELECT city FROM persons WHERE city = 'hello';",
},
["xact_start", "query_start", "pid", "client_port", "client_addr", "backend_type", "blocking_pids"],
["now", "xact_start", "query_start", "pid", "client_port", "client_addr", "backend_type", "blocking_pids"],
{
'usename': 'bob',
'state': 'active',
Expand Down Expand Up @@ -735,6 +736,9 @@ def wait(conn):
for val in expected_keys:
assert val in bobs_query

# assert that the current timestamp is being collected as an ISO timestamp with TZ info
assert parser.isoparse(bobs_query['now']).tzinfo, "current timestamp not formatted correctly"

if 'blocking_pids' in expected_keys:
# if we are collecting pg blocking information, then
# blocking_bob's pid should show up in bob's activity
Expand Down