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

Fix Postgres statements to remove information_schema query #8498

Merged
merged 1 commit into from
Feb 1, 2021
Merged
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
22 changes: 14 additions & 8 deletions postgres/datadog_checks/postgres/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
ON pg_stat_statements.dbid = pg_database.oid
WHERE pg_database.datname = %s
AND query != '<insufficient privilege>'
LIMIT {limit}
"""

DEFAULT_STATEMENTS_LIMIT = 10000

# Required columns for the check to run
PG_STAT_STATEMENTS_REQUIRED_COLUMNS = frozenset({'calls', 'query', 'total_time', 'rows'})

Expand Down Expand Up @@ -83,14 +86,16 @@ def _get_pg_stat_statements_columns(self, db):
version is not a reliable way to determine the available columns on `pg_stat_statements`. The database can
be upgraded without upgrading extensions, even when the extension is included by default.
"""
query = """
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'pg_stat_statements';
"""
columns = self._execute_query(db.cursor(), query)
return [column[0] for column in columns]
# Querying over '*' with limit 0 allows fetching only the column names from the cursor without data
query = STATEMENTS_QUERY.format(
cols='*',
pg_stat_statements_view=self.config.pg_stat_statements_view,
limit=0,
)
cursor = db.cursor()
self._execute_query(cursor, query, params=(self.config.dbname,))
colnames = [desc[0] for desc in cursor.description]
return colnames

def collect_per_statement_metrics(self, db):
try:
Expand Down Expand Up @@ -123,6 +128,7 @@ def _collect_per_statement_metrics(self, db):
STATEMENTS_QUERY.format(
cols=', '.join(query_columns),
pg_stat_statements_view=self.config.pg_stat_statements_view,
limit=DEFAULT_STATEMENTS_LIMIT,
),
params=(self.config.dbname,),
)
Expand Down