19
19
from datadog_checks .base .utils .tracking import tracked_method
20
20
from datadog_checks .postgres .cursor import CommenterCursor , CommenterDictCursor
21
21
22
- from .query_calls_cache import QueryCallsCache
23
22
from .util import DatabaseConfigurationError , payload_pg_version , warning_with_tags
24
23
from .version_utils import V9_4 , V14
25
24
28
27
except ImportError :
29
28
from ..stubs import datadog_agent
30
29
31
- QUERYID_TO_CALLS_QUERY = """
32
- SELECT queryid, calls
33
- FROM {pg_stat_statements_view}
34
- WHERE queryid IS NOT NULL
35
- """
36
-
37
30
STATEMENTS_QUERY = """
38
31
SELECT {cols}
39
32
FROM {pg_stat_statements_view} as pg_stat_statements
40
33
LEFT JOIN pg_roles
41
34
ON pg_stat_statements.userid = pg_roles.oid
42
35
LEFT JOIN pg_database
43
36
ON pg_stat_statements.dbid = pg_database.oid
44
- WHERE query NOT LIKE 'EXPLAIN %% '
45
- AND queryid = ANY('{{ {called_queryids} }}'::bigint[])
37
+ WHERE query != '<insufficient privilege> '
38
+ AND query NOT LIKE 'EXPLAIN %%'
46
39
{filters}
47
40
{extra_clauses}
48
41
"""
@@ -153,7 +146,6 @@ def __init__(self, check, config, shutdown_callback):
153
146
self .tags = None
154
147
self ._state = StatementMetrics ()
155
148
self ._stat_column_cache = []
156
- self ._query_calls_cache = QueryCallsCache ()
157
149
self ._track_io_timing_cache = None
158
150
self ._obfuscate_options = to_native_string (json .dumps (self ._config .obfuscator_options ))
159
151
# full_statement_text_cache: limit the ingestion rate of full statement text events per query_signature
@@ -185,11 +177,7 @@ def _get_pg_stat_statements_columns(self):
185
177
186
178
# Querying over '*' with limit 0 allows fetching only the column names from the cursor without data
187
179
query = STATEMENTS_QUERY .format (
188
- cols = '*' ,
189
- pg_stat_statements_view = self ._config .pg_stat_statements_view ,
190
- extra_clauses = "LIMIT 0" ,
191
- filters = "" ,
192
- called_queryids = "" ,
180
+ cols = '*' , pg_stat_statements_view = self ._config .pg_stat_statements_view , extra_clauses = "LIMIT 0" , filters = ""
193
181
)
194
182
with self ._check ._get_main_db () as conn :
195
183
with conn .cursor (cursor_factory = CommenterCursor ) as cursor :
@@ -198,39 +186,6 @@ def _get_pg_stat_statements_columns(self):
198
186
self ._stat_column_cache = col_names
199
187
return col_names
200
188
201
- def _check_called_queries (self ):
202
- pgss_view_without_query_text = self ._config .pg_stat_statements_view
203
- if pgss_view_without_query_text == "pg_stat_statements" :
204
- # Passing false for the showtext argument leads to a huge performance increase. This
205
- # allows the engine to avoid retrieving the potentially large amount of text data.
206
- # The query count query does not depend on the statement text, so it's safe for this use case.
207
- # For more info: https://www.postgresql.org/docs/current/pgstatstatements.html#PGSTATSTATEMENTS-FUNCS
208
- pgss_view_without_query_text = "pg_stat_statements(false)"
209
-
210
- with self ._check ._get_main_db () as conn :
211
- with conn .cursor (cursor_factory = CommenterCursor ) as cursor :
212
- called_queryids = []
213
- query = QUERYID_TO_CALLS_QUERY .format (pg_stat_statements_view = pgss_view_without_query_text )
214
- rows = self ._execute_query (cursor , query , params = (self ._config .dbname ,))
215
- for row in rows :
216
- queryid = row [0 ]
217
- if queryid is None :
218
- continue
219
-
220
- calls = row [1 ]
221
- calls_changed = self ._query_calls_cache .set_calls (queryid , calls )
222
- if calls_changed :
223
- called_queryids .append (queryid )
224
- self ._query_calls_cache .end_query_call_snapshot ()
225
- self ._check .gauge (
226
- "dd.postgresql.pg_stat_statements.calls_changed" ,
227
- len (called_queryids ),
228
- tags = self .tags ,
229
- hostname = self ._check .resolved_hostname ,
230
- )
231
-
232
- return called_queryids
233
-
234
189
def run_job (self ):
235
190
# do not emit any dd.internal metrics for DBM specific check code
236
191
self .tags = [t for t in self ._tags if not t .startswith ('dd.internal' )]
@@ -267,7 +222,6 @@ def collect_per_statement_metrics(self):
267
222
@tracked_method (agent_check_getter = agent_check_getter , track_result_length = True )
268
223
def _load_pg_stat_statements (self ):
269
224
try :
270
- called_queryids = self ._check_called_queries ()
271
225
available_columns = set (self ._get_pg_stat_statements_columns ())
272
226
missing_columns = PG_STAT_STATEMENTS_REQUIRED_COLUMNS - available_columns
273
227
if len (missing_columns ) > 0 :
@@ -342,7 +296,6 @@ def _load_pg_stat_statements(self):
342
296
pg_stat_statements_view = self ._config .pg_stat_statements_view ,
343
297
filters = filters ,
344
298
extra_clauses = "" ,
345
- called_queryids = ', ' .join ([str (i ) for i in called_queryids ]),
346
299
),
347
300
params = params ,
348
301
)
0 commit comments