Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions postgres/changelog.d/21393.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PG: Handle locks without relations like transactionid or virtualxid locks
39 changes: 19 additions & 20 deletions postgres/datadog_checks/postgres/relationsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,7 @@

# The view pg_locks provides access to information about the locks held by active processes within the database server.
LOCK_METRICS = {
'descriptors': [
('mode', 'lock_mode'),
('locktype', 'lock_type'),
('nspname', 'schema'),
('datname', 'db'),
('relname', 'table'),
('granted', 'granted'),
('fastpath', 'fastpath'),
],
'metrics': {'lock_count': ('locks', AgentCheck.gauge)},
'name': 'pg_locks',
'query': """
SELECT mode,
locktype,
Expand All @@ -47,17 +38,25 @@
pc.relname,
granted,
fastpath,
count(*) AS {metrics_columns}
count(*)
FROM pg_locks l
JOIN pg_database pd ON (l.database = pd.oid)
JOIN pg_class pc ON (l.relation = pc.oid)
LEFT JOIN pg_database pd ON (l.database = pd.oid)
LEFT JOIN pg_class pc ON (l.relation = pc.oid)
LEFT JOIN pg_namespace pn ON (pn.oid = pc.relnamespace)
WHERE {relations}
WHERE (pc IS NULL OR ({relations} AND pc.relname NOT LIKE 'pg^_%%' ESCAPE '^'))
AND l.mode IS NOT NULL
AND pc.relname NOT LIKE 'pg^_%%' ESCAPE '^'
GROUP BY pd.datname, pc.relname, pn.nspname, locktype, mode, granted, fastpath""",
'relation': True,
'name': 'lock_metrics',
GROUP BY pd.datname, pc.relname, pn.nspname, locktype, mode, granted, fastpath
""",
'columns': [
{'name': 'lock_mode', 'type': 'tag'},
{'name': 'lock_type', 'type': 'tag'},
{'name': 'schema', 'type': 'tag_not_null'},
{'name': 'db', 'type': 'tag_not_null'},
{'name': 'table', 'type': 'tag_not_null'},
{'name': 'granted', 'type': 'tag'},
{'name': 'fastpath', 'type': 'tag'},
{'name': 'locks', 'type': 'gauge'},
],
}


Expand Down Expand Up @@ -414,8 +413,8 @@
'name': 'index_bloat_metrics',
}

RELATION_METRICS = [LOCK_METRICS, STATIO_METRICS]
DYNAMIC_RELATION_QUERIES = [QUERY_PG_CLASS, QUERY_PG_CLASS_SIZE, IDX_METRICS]
RELATION_METRICS = [STATIO_METRICS]
DYNAMIC_RELATION_QUERIES = [QUERY_PG_CLASS, QUERY_PG_CLASS_SIZE, IDX_METRICS, LOCK_METRICS]


class RelationsManager(object):
Expand Down
64 changes: 60 additions & 4 deletions postgres/tests/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,30 +411,86 @@ def test_vacuum_age(aggregator, integration_check, pg_instance):
[{"relation_regex": "perso.*", "relkind": ["r"]}],
1,
"persons",
None,
[
"db:datadog_test",
"lock_mode:AccessExclusiveLock",
"lock_type:relation",
"granted:True",
"fastpath:False",
"table:persons",
"schema:public",
],
id="test with matching relkind should return 1",
),
pytest.param(
[{"relation_regex": "perso.*", "relkind": ["i"]}],
0,
"persons",
None,
[
"db:datadog_test",
"lock_mode:AccessExclusiveLock",
"lock_type:relation",
"granted:True",
"fastpath:False",
"table:persons",
"schema:public",
],
id="test without matching relkind should return 0",
),
pytest.param(
["pgtable"],
1,
"pgtable",
None,
[
"db:datadog_test",
"lock_mode:AccessExclusiveLock",
"lock_type:relation",
"granted:True",
"fastpath:False",
"table:pgtable",
"schema:public",
],
id="pgtable should be included in lock metrics",
),
pytest.param(
["pg_newtable"],
0,
"pg_newtable",
None,
[
"db:datadog_test",
"lock_mode:AccessExclusiveLock",
"lock_type:relation",
"granted:True",
"fastpath:False",
"table:pg_newtable",
"schema:public",
],
id="pg_newtable should be excluded from query since it starts with `pg_`",
),
pytest.param(
['persons'],
1,
'persons',
[
"lock_mode:ExclusiveLock",
"lock_type:transactionid",
"granted:True",
"fastpath:False",
],
id="transactionid lock should be visible",
),
pytest.param(
['persons'],
1,
'persons',
[
"lock_mode:ExclusiveLock",
"lock_type:virtualxid",
"granted:True",
"fastpath:True",
],
id="virtualxid lock should be visible",
),
],
)
def test_locks_metrics(aggregator, integration_check, pg_instance, relations, lock_count, lock_table_name, tags):
Expand Down
Loading