Skip to content

Commit 6afe657

Browse files
authored
Fix missing table owner and omit empty tables (#22134)
* Fix missing table owner and omit empty tables * Changelog * Lint * Fix toast_table * Add snapshot tests * Changelog * Normalize schemas before compare * Comment * Normalize before using for list sort * Sort keys
1 parent b84bbe4 commit 6afe657

24 files changed

+26447
-22
lines changed

postgres/changelog.d/22134.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix missing table owner and toast table fields in Postgres schema collection.
2+
Omit empty tables from collection.
3+
Fix the missing description in Postgres schema databases.
4+
Add snapshot tests for Postgres schema collection.

postgres/datadog_checks/postgres/schemas.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ class DatabaseObject(TypedDict):
3939
SELECT c.oid AS table_id,
4040
c.relnamespace AS schema_id,
4141
c.relname AS table_name,
42-
c.relhasindex AS has_indexes,
43-
c.relowner :: regrole AS owner,
44-
( CASE
45-
WHEN c.relkind = 'p' THEN TRUE
46-
ELSE FALSE
47-
END ) AS has_partitions,
42+
c.relowner :: regrole :: text AS table_owner,
4843
t.relname AS toast_table
4944
FROM pg_class c
5045
left join pg_class t
@@ -57,8 +52,7 @@ class DatabaseObject(TypedDict):
5752
SELECT c.oid AS table_id,
5853
c.relnamespace AS schema_id,
5954
c.relname AS table_name,
60-
c.relhasindex AS has_indexes,
61-
c.relowner :: regrole AS owner,
55+
c.relowner :: regrole :: text AS table_owner,
6256
t.relname AS toast_table
6357
FROM pg_class c
6458
left join pg_class t
@@ -176,10 +170,8 @@ class PostgresDatabaseObject(DatabaseObject):
176170
datname AS NAME,
177171
pg_encoding_to_char(encoding) AS encoding,
178172
rolname AS owner,
179-
description
173+
shobj_description(db.oid, 'pg_database') AS description
180174
FROM pg_catalog.pg_database db
181-
LEFT JOIN pg_catalog.pg_description dc
182-
ON dc.objoid = db.oid
183175
JOIN pg_roles a
184176
ON datdba = a.oid
185177
WHERE datname NOT LIKE 'template%'
@@ -328,7 +320,7 @@ def get_rows_query(self):
328320
),
329321
schema_tables AS (
330322
SELECT schemas.schema_id, schemas.schema_name, schemas.schema_owner,
331-
tables.table_id, tables.table_name
323+
tables.table_id, tables.table_name, tables.table_owner, tables.toast_table
332324
FROM schemas
333325
LEFT JOIN tables ON schemas.schema_id = tables.schema_id
334326
ORDER BY schemas.schema_name, tables.table_name
@@ -346,7 +338,7 @@ def get_rows_query(self):
346338
{partitions_ctes}
347339
348340
SELECT schema_tables.schema_id, schema_tables.schema_name, schema_tables.schema_owner,
349-
schema_tables.table_id, schema_tables.table_name,
341+
schema_tables.table_id, schema_tables.table_name, schema_tables.table_owner, schema_tables.toast_table,
350342
array_agg(row_to_json(columns.*)) FILTER (WHERE columns.name IS NOT NULL) as columns,
351343
array_agg(row_to_json(indexes.*)) FILTER (WHERE indexes.name IS NOT NULL) as indexes,
352344
array_agg(row_to_json(constraints.*)) FILTER (WHERE constraints.name IS NOT NULL)
@@ -358,7 +350,7 @@ def get_rows_query(self):
358350
LEFT JOIN constraints ON schema_tables.table_id = constraints.table_id
359351
{partition_joins}
360352
GROUP BY schema_tables.schema_id, schema_tables.schema_name, schema_tables.schema_owner,
361-
schema_tables.table_id, schema_tables.table_name
353+
schema_tables.table_id, schema_tables.table_name, schema_tables.table_owner, schema_tables.toast_table
362354
;
363355
"""
364356

@@ -386,7 +378,7 @@ def _map_row(self, database: DatabaseInfo, cursor_row) -> DatabaseObject:
386378
for k, v in {
387379
"id": str(cursor_row.get("table_id")),
388380
"name": cursor_row.get("table_name"),
389-
"owner": cursor_row.get("owner"),
381+
"owner": cursor_row.get("table_owner"),
390382
# The query can create duplicates of the joined tables
391383
"columns": list({v and v['name']: v for v in cursor_row.get("columns") or []}.values())[
392384
: self._config.max_columns
@@ -401,7 +393,9 @@ def _map_row(self, database: DatabaseInfo, cursor_row) -> DatabaseObject:
401393
}.items()
402394
if v is not None
403395
}
404-
],
396+
]
397+
if cursor_row.get("table_name")
398+
else [],
405399
}.items()
406400
if v is not None
407401
}

postgres/tests/compose/resources/01_postgres.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ GRANT SELECT ON pg_stat_database TO datadog;
1313
GRANT SELECT ON pg_stat_database TO datadog_no_catalog;
1414
GRANT SELECT ON ALL tables IN SCHEMA pg_catalog to datadog;
1515
CREATE DATABASE datadog_test;
16+
COMMENT ON DATABASE datadog_test IS 'Datadog test database';
1617
GRANT ALL PRIVILEGES ON DATABASE datadog_test TO datadog;
1718
CREATE DATABASE dogs;
1819
GRANT USAGE on SCHEMA public to bob;

0 commit comments

Comments
 (0)