Skip to content

Commit b593bed

Browse files
authored
Make statement_timeout local for Postgres schemas (DataDog#23954)
* Make statement_timeout local for Postgres schemas * Changelog * Disable autocommit for schemas * Manually set autocommit * Manually commit * Try/finally * with transaction * Disable in tests
1 parent 8b5777e commit b593bed

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

postgres/changelog.d/23954.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make statement_timeout local for Postgres schemas collection

postgres/datadog_checks/postgres/schemas.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,12 @@ def _get_databases(self):
230230
def _get_cursor(self, database_name):
231231
with self._check.db_pool.get_connection(database_name) as conn:
232232
with conn.cursor(row_factory=dict_row) as cursor:
233-
query, params = self.get_rows_query()
234-
cursor.execute(f"SET statement_timeout = '{self._config.max_query_duration}s';")
235-
cursor.execute(query, params)
236-
yield cursor
233+
# Explicitly wrap these queries in a transaction so we can set the statement timeout locally
234+
with conn.transaction():
235+
query, params = self.get_rows_query()
236+
cursor.execute(f"SET LOCAL statement_timeout = '{self._config.max_query_duration}s';")
237+
cursor.execute(query, params)
238+
yield cursor
237239

238240
def _get_schemas_query(self):
239241
query = SCHEMA_QUERY

postgres/tests/test_statements.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,7 @@ def test_pg_stat_statements_max_warning(
19601960
def test_pg_stat_statements_dealloc(aggregator, integration_check, dbm_instance_replica2):
19611961
dbm_instance_replica2['query_samples'] = {'enabled': False}
19621962
dbm_instance_replica2['query_activity'] = {'enabled': False}
1963+
dbm_instance_replica2['collect_schemas'] = {'enabled': False}
19631964
with _get_superconn(dbm_instance_replica2) as superconn:
19641965
with superconn.cursor() as cur:
19651966
cur.execute("select pg_stat_statements_reset();")

0 commit comments

Comments
 (0)