Skip to content

Commit eb1a43c

Browse files
authored
Only query aurora_version() if aurora postgres extensions are available (#20310)
* Check for aws extensions before querying aurora_version * Create 20310.fixed * Minor query tuning * fix linting error
1 parent 6ab6283 commit eb1a43c

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

postgres/changelog.d/20310.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Check for aws extensions before querying aurora_version()

postgres/datadog_checks/postgres/version_utils.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class VersionUtils(object):
2828
def __init__(self):
2929
self.log = get_check_logger()
30-
self._seen_aurora_exception = False
30+
self._is_aurora = None
3131

3232
@staticmethod
3333
def get_raw_version(db):
@@ -38,21 +38,29 @@ def get_raw_version(db):
3838
return raw_version
3939

4040
def is_aurora(self, db):
41-
if self._seen_aurora_exception:
42-
return False
41+
if self._is_aurora is not None:
42+
return self._is_aurora
4343
with db as conn:
4444
with conn.cursor(cursor_factory=CommenterCursor) as cursor:
45-
# This query will pollute PG logs in non aurora versions,
46-
# but is the only reliable way to detect aurora
47-
try:
48-
cursor.execute('select AURORA_VERSION();')
49-
return True
50-
except Exception as e:
51-
self.log.debug(
52-
"Captured exception %s while determining if the DB is aurora. Assuming is not", str(e)
53-
)
54-
self._seen_aurora_exception = True
55-
return False
45+
cursor.execute(
46+
"SELECT 1 FROM pg_available_extension_versions "
47+
"WHERE name ILIKE '%aurora%' OR comment ILIKE '%aurora%' "
48+
"LIMIT 1;"
49+
)
50+
if cursor.fetchone():
51+
# This query will pollute PG logs in non aurora versions,
52+
# but is the only reliable way to detect aurora.
53+
# Since we found aurora extensions, this should exist.
54+
try:
55+
cursor.execute('select AURORA_VERSION();')
56+
self._is_aurora = True
57+
return self._is_aurora
58+
except Exception as e:
59+
self.log.debug(
60+
"Captured exception %s while determining if the DB is aurora. Assuming is not", str(e)
61+
)
62+
self._is_aurora = False
63+
return self._is_aurora
5664

5765
@staticmethod
5866
def parse_version(raw_version):

0 commit comments

Comments
 (0)