|
61 | 61 | } |
62 | 62 |
|
63 | 63 |
|
64 | | -# The pg_stat_all_indexes view will contain one row for each index in the current database, |
65 | | -# showing statistics about accesses to that specific index. |
66 | | -# The pg_stat_user_indexes view contain the same information, but filtered to only show user indexes. |
| 64 | +# This is similaar to pg_stat_user_indexes view |
67 | 65 | IDX_METRICS = { |
68 | | - 'descriptors': [('relname', 'table'), ('schemaname', 'schema'), ('indexrelname', 'index')], |
69 | | - 'metrics': { |
70 | | - 'idx_scan': ('index_scans', AgentCheck.rate), |
71 | | - 'idx_tup_read': ('index_rows_read', AgentCheck.rate), |
72 | | - 'idx_tup_fetch': ('index_rows_fetched', AgentCheck.rate), |
73 | | - 'pg_relation_size(indexrelid) as index_size': ('individual_index_size', AgentCheck.gauge), |
74 | | - }, |
| 66 | + 'name': 'pg_index', |
75 | 67 | 'query': """ |
76 | | -SELECT relname, |
77 | | - schemaname, |
78 | | - indexrelname, |
79 | | - {metrics_columns} |
80 | | - FROM pg_stat_user_indexes |
81 | | - WHERE {relations}""", |
82 | | - 'relation': True, |
83 | | - 'name': 'idx_metrics', |
| 68 | +SELECT |
| 69 | + current_database(), |
| 70 | + schemaname, |
| 71 | + relname, |
| 72 | + indexrelname, |
| 73 | + is_valid, |
| 74 | + idx_scan, |
| 75 | + idx_tup_read, |
| 76 | + idx_tup_fetch, |
| 77 | + index_size |
| 78 | +FROM (SELECT |
| 79 | + N.nspname AS schemaname, |
| 80 | + C.relname AS relname, |
| 81 | + I.relname AS indexrelname, |
| 82 | + X.indisvalid::text AS is_valid, |
| 83 | + pg_stat_get_numscans(I.oid) AS idx_scan, |
| 84 | + pg_stat_get_tuples_returned(I.oid) AS idx_tup_read, |
| 85 | + pg_stat_get_tuples_fetched(I.oid) AS idx_tup_fetch, |
| 86 | + pg_relation_size(indexrelid) as index_size |
| 87 | + FROM pg_class C JOIN |
| 88 | + pg_index X ON C.oid = X.indrelid JOIN |
| 89 | + pg_class I ON I.oid = X.indexrelid |
| 90 | + LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) |
| 91 | + AND C.relkind IN ('r', 'm') |
| 92 | + AND N.nspname NOT IN ('pg_catalog', 'information_schema') |
| 93 | +) s WHERE {relations} |
| 94 | + """.strip(), |
| 95 | + 'columns': [ |
| 96 | + {'name': 'db', 'type': 'tag'}, |
| 97 | + {'name': 'schema', 'type': 'tag'}, |
| 98 | + {'name': 'table', 'type': 'tag'}, |
| 99 | + {'name': 'index', 'type': 'tag'}, |
| 100 | + {'name': 'valid', 'type': 'tag'}, |
| 101 | + {'name': 'index_scans', 'type': 'rate'}, |
| 102 | + {'name': 'index_rows_read', 'type': 'rate'}, |
| 103 | + {'name': 'index_rows_fetched', 'type': 'rate'}, |
| 104 | + {'name': 'individual_index_size', 'type': 'gauge'}, |
| 105 | + ], |
84 | 106 | } |
85 | 107 |
|
86 | 108 |
|
|
386 | 408 | 'name': 'index_bloat_metrics', |
387 | 409 | } |
388 | 410 |
|
389 | | -RELATION_METRICS = [LOCK_METRICS, IDX_METRICS, STATIO_METRICS] |
390 | | -DYNAMIC_RELATION_QUERIES = [QUERY_PG_CLASS, QUERY_PG_CLASS_SIZE] |
| 411 | +RELATION_METRICS = [LOCK_METRICS, STATIO_METRICS] |
| 412 | +DYNAMIC_RELATION_QUERIES = [QUERY_PG_CLASS, QUERY_PG_CLASS_SIZE, IDX_METRICS] |
391 | 413 |
|
392 | 414 |
|
393 | 415 | class RelationsManager(object): |
|
0 commit comments