diff --git a/postgres/changelog.d/20310.fixed b/postgres/changelog.d/20310.fixed new file mode 100644 index 0000000000000..cb3688a96b8f9 --- /dev/null +++ b/postgres/changelog.d/20310.fixed @@ -0,0 +1 @@ +Check for aws extensions before querying aurora_version() diff --git a/postgres/datadog_checks/postgres/version_utils.py b/postgres/datadog_checks/postgres/version_utils.py index 89b27e3e3d735..349f866054b4f 100644 --- a/postgres/datadog_checks/postgres/version_utils.py +++ b/postgres/datadog_checks/postgres/version_utils.py @@ -27,7 +27,7 @@ class VersionUtils(object): def __init__(self): self.log = get_check_logger() - self._seen_aurora_exception = False + self._is_aurora = None @staticmethod def get_raw_version(db): @@ -38,21 +38,29 @@ def get_raw_version(db): return raw_version def is_aurora(self, db): - if self._seen_aurora_exception: - return False + if self._is_aurora is not None: + return self._is_aurora with db as conn: with conn.cursor(cursor_factory=CommenterCursor) as cursor: - # This query will pollute PG logs in non aurora versions, - # but is the only reliable way to detect aurora - try: - cursor.execute('select AURORA_VERSION();') - return True - except Exception as e: - self.log.debug( - "Captured exception %s while determining if the DB is aurora. Assuming is not", str(e) - ) - self._seen_aurora_exception = True - return False + cursor.execute( + "SELECT 1 FROM pg_available_extension_versions " + "WHERE name ILIKE '%aurora%' OR comment ILIKE '%aurora%' " + "LIMIT 1;" + ) + if cursor.fetchone(): + # This query will pollute PG logs in non aurora versions, + # but is the only reliable way to detect aurora. + # Since we found aurora extensions, this should exist. + try: + cursor.execute('select AURORA_VERSION();') + self._is_aurora = True + return self._is_aurora + except Exception as e: + self.log.debug( + "Captured exception %s while determining if the DB is aurora. Assuming is not", str(e) + ) + self._is_aurora = False + return self._is_aurora @staticmethod def parse_version(raw_version):