Skip to content

Commit ae88816

Browse files
mobuchowskiclaude
andauthored
postgres: collect relkind in schema metadata (DataDog#23888)
* postgres: collect relkind (incl views/matviews) in schema metadata - Add c.relkind to PG_TABLES_QUERY_V10_PLUS and PG_TABLES_QUERY_V9 SELECT lists - Expand relkind filter to include views ('v') and materialized views ('m') - Propagate relkind through get_rows_query() schema_tables CTE, final SELECT, and GROUP BY - Add relkind: str to TableObject TypedDict - Add relkind to _map_row() per-table payload - Add CREATE VIEW and CREATE MATERIALIZED VIEW to test database setup SQL - Update test assertions to verify relkind is present and correct - Update schema snapshot fixtures: add relkind to all existing tables, add persons_view and persons_matview entries (fixtures need regeneration in CI with actual OIDs via test_collect_schema_snapshot auto-update) Environment: Datadog workspace Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Maciej Obuchowski <mobuchowski@datadoghq.com> * remove pulling (materialized) views Signed-off-by: Maciej Obuchowski <maciej.obuchowski@datadoghq.com> * changelog entry Signed-off-by: Maciej Obuchowski <maciej.obuchowski@datadoghq.com> * Tighten relkind assertion to match collector query policy The collector query in schemas.py only returns relkind values 'r', 'p', and 'f' (regular tables, partitioned tables, foreign tables). Tighten the test assertion to match, removing 'v' and 'm' which were included when views/matviews were considered but then removed. Environment: Datadog workspace Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Maciej Obuchowski <maciej.obuchowski@datadoghq.com> --------- Signed-off-by: Maciej Obuchowski <mobuchowski@datadoghq.com> Signed-off-by: Maciej Obuchowski <maciej.obuchowski@datadoghq.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 860ccb2 commit ae88816

22 files changed

Lines changed: 868 additions & 431 deletions

postgres/changelog.d/23888.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Collect ``relkind`` for Postgres tables in DBM schema metadata.

postgres/datadog_checks/postgres/schemas.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class DatabaseObject(TypedDict):
4141
c.relnamespace AS schema_id,
4242
c.relname AS table_name,
4343
c.relowner :: regrole :: text AS table_owner,
44+
c.relkind::text AS relkind,
4445
t.relname AS toast_table
4546
FROM pg_class c
4647
left join pg_class t
@@ -54,6 +55,7 @@ class DatabaseObject(TypedDict):
5455
c.relnamespace AS schema_id,
5556
c.relname AS table_name,
5657
c.relowner :: regrole :: text AS table_owner,
58+
c.relkind::text AS relkind,
5759
t.relname AS toast_table
5860
FROM pg_class c
5961
left join pg_class t
@@ -138,6 +140,7 @@ class DatabaseObject(TypedDict):
138140
class TableObject(TypedDict):
139141
id: str
140142
name: str
143+
relkind: str
141144
columns: list
142145
indexes: list
143146
foreign_keys: list
@@ -315,7 +318,7 @@ def get_rows_query(self):
315318
),
316319
schema_tables AS (
317320
SELECT schemas.schema_id, schemas.schema_name, schemas.schema_owner,
318-
tables.table_id, tables.table_name, tables.table_owner, tables.toast_table
321+
tables.table_id, tables.table_name, tables.table_owner, tables.relkind, tables.toast_table
319322
FROM schemas
320323
LEFT JOIN tables ON schemas.schema_id = tables.schema_id
321324
ORDER BY schemas.schema_name, tables.table_name
@@ -333,7 +336,8 @@ def get_rows_query(self):
333336
{partitions_ctes}
334337
335338
SELECT schema_tables.schema_id, schema_tables.schema_name, schema_tables.schema_owner,
336-
schema_tables.table_id, schema_tables.table_name, schema_tables.table_owner, schema_tables.toast_table,
339+
schema_tables.table_id, schema_tables.table_name, schema_tables.table_owner, schema_tables.relkind,
340+
schema_tables.toast_table,
337341
array_agg(row_to_json(columns.*)) FILTER (WHERE columns.name IS NOT NULL) as columns,
338342
array_agg(row_to_json(indexes.*)) FILTER (WHERE indexes.name IS NOT NULL) as indexes,
339343
array_agg(row_to_json(constraints.*)) FILTER (WHERE constraints.name IS NOT NULL)
@@ -345,7 +349,8 @@ def get_rows_query(self):
345349
LEFT JOIN constraints ON schema_tables.table_id = constraints.table_id
346350
{partition_joins}
347351
GROUP BY schema_tables.schema_id, schema_tables.schema_name, schema_tables.schema_owner,
348-
schema_tables.table_id, schema_tables.table_name, schema_tables.table_owner, schema_tables.toast_table
352+
schema_tables.table_id, schema_tables.table_name, schema_tables.table_owner, schema_tables.relkind,
353+
schema_tables.toast_table
349354
;
350355
"""
351356

@@ -382,6 +387,7 @@ def _map_row(self, database: DatabaseInfo, cursor_row) -> DatabaseObject:
382387
"foreign_keys": list(
383388
{v and v['name']: v for v in cursor_row.get("foreign_keys") or []}.values()
384389
),
390+
"relkind": cursor_row.get("relkind"),
385391
"toast_table": cursor_row.get("toast_table"),
386392
"num_partitions": cursor_row.get("num_partitions"),
387393
"partition_key": cursor_row.get("partition_key"),

postgres/tests/fixtures/schema_snapshot_v10_C.json

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
}
5454
],
5555
"indexes": [],
56-
"foreign_keys": []
56+
"foreign_keys": [],
57+
"relkind": "r"
5758
}
5859
]
5960
}
@@ -113,7 +114,8 @@
113114
}
114115
],
115116
"indexes": [],
116-
"foreign_keys": []
117+
"foreign_keys": [],
118+
"relkind": "r"
117119
}
118120
]
119121
}
@@ -167,7 +169,8 @@
167169
}
168170
],
169171
"indexes": [],
170-
"foreign_keys": []
172+
"foreign_keys": [],
173+
"relkind": "f"
171174
}
172175
]
173176
}
@@ -227,7 +230,8 @@
227230
}
228231
],
229232
"indexes": [],
230-
"foreign_keys": []
233+
"foreign_keys": [],
234+
"relkind": "r"
231235
}
232236
]
233237
}
@@ -287,7 +291,8 @@
287291
}
288292
],
289293
"indexes": [],
290-
"foreign_keys": []
294+
"foreign_keys": [],
295+
"relkind": "r"
291296
}
292297
]
293298
}
@@ -362,7 +367,8 @@
362367
}
363368
],
364369
"indexes": [],
365-
"foreign_keys": []
370+
"foreign_keys": [],
371+
"relkind": "r"
366372
}
367373
]
368374
}
@@ -422,7 +428,8 @@
422428
}
423429
],
424430
"indexes": [],
425-
"foreign_keys": []
431+
"foreign_keys": [],
432+
"relkind": "r"
426433
}
427434
]
428435
}
@@ -482,7 +489,8 @@
482489
}
483490
],
484491
"indexes": [],
485-
"foreign_keys": []
492+
"foreign_keys": [],
493+
"relkind": "r"
486494
}
487495
]
488496
}
@@ -542,7 +550,8 @@
542550
}
543551
],
544552
"indexes": [],
545-
"foreign_keys": []
553+
"foreign_keys": [],
554+
"relkind": "r"
546555
}
547556
]
548557
}
@@ -602,7 +611,8 @@
602611
}
603612
],
604613
"indexes": [],
605-
"foreign_keys": []
614+
"foreign_keys": [],
615+
"relkind": "r"
606616
}
607617
]
608618
}
@@ -662,7 +672,8 @@
662672
}
663673
],
664674
"indexes": [],
665-
"foreign_keys": []
675+
"foreign_keys": [],
676+
"relkind": "r"
666677
}
667678
]
668679
}
@@ -722,7 +733,8 @@
722733
}
723734
],
724735
"indexes": [],
725-
"foreign_keys": []
736+
"foreign_keys": [],
737+
"relkind": "r"
726738
}
727739
]
728740
}
@@ -782,7 +794,8 @@
782794
}
783795
],
784796
"indexes": [],
785-
"foreign_keys": []
797+
"foreign_keys": [],
798+
"relkind": "r"
786799
}
787800
]
788801
}
@@ -848,7 +861,8 @@
848861
"definition": "FOREIGN KEY (city) REFERENCES cities(city)",
849862
"table_id": "16641"
850863
}
851-
]
864+
],
865+
"relkind": "r"
852866
}
853867
]
854868
}
@@ -908,7 +922,8 @@
908922
}
909923
],
910924
"indexes": [],
911-
"foreign_keys": []
925+
"foreign_keys": [],
926+
"relkind": "r"
912927
}
913928
]
914929
}
@@ -963,7 +978,8 @@
963978
"is_partial": false
964979
}
965980
],
966-
"foreign_keys": []
981+
"foreign_keys": [],
982+
"relkind": "r"
967983
}
968984
]
969985
}
@@ -1018,7 +1034,8 @@
10181034
"is_partial": false
10191035
}
10201036
],
1021-
"foreign_keys": []
1037+
"foreign_keys": [],
1038+
"relkind": "r"
10221039
}
10231040
]
10241041
}
@@ -1078,7 +1095,8 @@
10781095
}
10791096
],
10801097
"indexes": [],
1081-
"foreign_keys": []
1098+
"foreign_keys": [],
1099+
"relkind": "r"
10821100
}
10831101
]
10841102
}
@@ -1138,7 +1156,8 @@
11381156
}
11391157
],
11401158
"indexes": [],
1141-
"foreign_keys": []
1159+
"foreign_keys": [],
1160+
"relkind": "r"
11421161
}
11431162
]
11441163
}
@@ -1214,7 +1233,8 @@
12141233
"is_partial": false
12151234
}
12161235
],
1217-
"foreign_keys": []
1236+
"foreign_keys": [],
1237+
"relkind": "r"
12181238
}
12191239
]
12201240
}

0 commit comments

Comments
 (0)