From ce8ca88e1ee91f431dfa61bb81b324c1ca864df2 Mon Sep 17 00:00:00 2001 From: Seth Samuel Date: Wed, 19 Aug 2026 15:12:15 -0400 Subject: [PATCH 1/3] Respect dbstrict in schema collection --- postgres/datadog_checks/postgres/schemas.py | 28 ++++++++++++--------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/postgres/datadog_checks/postgres/schemas.py b/postgres/datadog_checks/postgres/schemas.py index 4dec050418ab0..70e65a3feb49e 100644 --- a/postgres/datadog_checks/postgres/schemas.py +++ b/postgres/datadog_checks/postgres/schemas.py @@ -208,18 +208,22 @@ def _get_databases(self): query = DATABASE_INFORMATION_QUERY params: list[str] = [] - query += regex_exclude_clauses("datname", self._config.exclude_databases) - params.extend(self._config.exclude_databases) - - query += regex_include_clause("datname", self._config.include_databases) - params.extend(self._config.include_databases) - - # Autodiscovery trumps exclude and include - autodiscovery_databases = self._check.autodiscovery.get_items() if self._check.autodiscovery else [] - if autodiscovery_databases: - in_clause = ", ".join(["%s"] * len(autodiscovery_databases)) - query += f" AND datname IN ({in_clause})" - params.extend(autodiscovery_databases) + if self._check._config.dbstrict and not self._check.autodiscovery: + query += f" AND datname = %s" + params.extend([self._check._config.dbname]) + else: + query += regex_exclude_clauses("datname", self._config.exclude_databases) + params.extend(self._config.exclude_databases) + + query += regex_include_clause("datname", self._config.include_databases) + params.extend(self._config.include_databases) + + # Autodiscovery trumps exclude and include + autodiscovery_databases = self._check.autodiscovery.get_items() if self._check.autodiscovery else [] + if autodiscovery_databases: + in_clause = ", ".join(["%s"] * len(autodiscovery_databases)) + query += f" AND datname IN ({in_clause})" + params.extend(autodiscovery_databases) with self._check._get_main_db() as conn: with conn.cursor(row_factory=dict_row) as cursor: From 628f3d71c6315b8d920e7595d1965117b84c8f43 Mon Sep 17 00:00:00 2001 From: Seth Samuel Date: Wed, 19 Aug 2026 15:13:39 -0400 Subject: [PATCH 2/3] Changelog --- postgres/changelog.d/24923.fixed | 1 + postgres/datadog_checks/postgres/schemas.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 postgres/changelog.d/24923.fixed diff --git a/postgres/changelog.d/24923.fixed b/postgres/changelog.d/24923.fixed new file mode 100644 index 0000000000000..ca676df2405c4 --- /dev/null +++ b/postgres/changelog.d/24923.fixed @@ -0,0 +1 @@ +Respect dbstrict in schema collection diff --git a/postgres/datadog_checks/postgres/schemas.py b/postgres/datadog_checks/postgres/schemas.py index 70e65a3feb49e..d73e77d3ea839 100644 --- a/postgres/datadog_checks/postgres/schemas.py +++ b/postgres/datadog_checks/postgres/schemas.py @@ -210,7 +210,7 @@ def _get_databases(self): if self._check._config.dbstrict and not self._check.autodiscovery: query += f" AND datname = %s" - params.extend([self._check._config.dbname]) + params.append(self._check._config.dbname) else: query += regex_exclude_clauses("datname", self._config.exclude_databases) params.extend(self._config.exclude_databases) From 43199da3fd0d6c262b8c40e43f52af2f95c0673d Mon Sep 17 00:00:00 2001 From: Seth Samuel Date: Thu, 20 Aug 2026 14:46:37 -0400 Subject: [PATCH 3/3] Fix --- postgres/datadog_checks/postgres/schemas.py | 4 ++-- postgres/tests/test_schemas.py | 24 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/postgres/datadog_checks/postgres/schemas.py b/postgres/datadog_checks/postgres/schemas.py index d73e77d3ea839..f61d6fc9892c2 100644 --- a/postgres/datadog_checks/postgres/schemas.py +++ b/postgres/datadog_checks/postgres/schemas.py @@ -208,8 +208,8 @@ def _get_databases(self): query = DATABASE_INFORMATION_QUERY params: list[str] = [] - if self._check._config.dbstrict and not self._check.autodiscovery: - query += f" AND datname = %s" + if self._check._config.dbstrict and not self._check.autodiscovery: + query += " AND datname = %s" params.append(self._check._config.dbname) else: query += regex_exclude_clauses("datname", self._config.exclude_databases) diff --git a/postgres/tests/test_schemas.py b/postgres/tests/test_schemas.py index 16b5c47abec9f..110700b4f533b 100644 --- a/postgres/tests/test_schemas.py +++ b/postgres/tests/test_schemas.py @@ -2,6 +2,8 @@ # All rights reserved # Licensed under a 3-clause BSD style license (see LICENSE) +from unittest import mock + import pytest from datadog_checks.postgres.schemas import PostgresSchemaCollector @@ -82,6 +84,28 @@ def test_databases_include_and_exclude_filter(dbm_instance, integration_check): assert 'datadog_test' not in database_names +def test_databases_dbstrict(dbm_instance, integration_check): + dbm_instance['dbstrict'] = True + check = integration_check(dbm_instance) + collector = PostgresSchemaCollector(check) + + databases = collector._get_databases() + database_names = [database['name'] for database in databases] + assert database_names == [dbm_instance['dbname']] + + +def test_databases_dbstrict_with_autodiscovery(dbm_instance, integration_check): + dbm_instance['dbstrict'] = True + dbm_instance['database_autodiscovery'] = {'enabled': True, 'include': ['^dogs_[0-9]$']} + check = integration_check(dbm_instance) + collector = PostgresSchemaCollector(check) + + with mock.patch.object(check.autodiscovery, 'get_items', return_value=['dogs_3']): + databases = collector._get_databases() + database_names = [database['name'] for database in databases] + assert database_names == ['dogs_3'] + + def test_get_cursor(dbm_instance, integration_check): check = integration_check(dbm_instance) check.version = POSTGRES_VERSION