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
34 changes: 34 additions & 0 deletions ansible/files/queries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
pg_table_size:
query: |
SELECT
n.nspname AS schema,
c.relname AS table,
c.reltuples AS row_estimate,
pg_total_relation_size(c.oid) AS total_bytes,
pg_relation_size(c.oid) AS table_bytes,
pg_indexes_size(c.oid) AS index_bytes,
COALESCE(pg_total_relation_size(NULLIF(c.reltoastrelid, 0)), 0) AS toast_bytes
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The schema filter excludes only pg_catalog and information_schema, which means this query will also export metrics for system schemas like pg_toast (and potentially pg_temp_*). That adds noisy/high-cardinality series (including toast tables themselves) and can increase scrape cost. Consider explicitly excluding pg_toast and temporary schemas (or alternatively whitelisting the intended application schemas).

Suggested change
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
AND n.nspname <> 'pg_toast'
AND n.nspname NOT LIKE 'pg_toast_%'
AND n.nspname NOT LIKE 'pg_temp_%'

Copilot uses AI. Check for mistakes.
metrics:
- schema:
usage: LABEL
- table:
usage: LABEL
- row_estimate:
usage: GAUGE
description: "Estimated row count from pg_class.reltuples"
- total_bytes:
usage: GAUGE
description: "Total size including indexes and toast"
- table_bytes:
usage: GAUGE
description: "Table data size"
- index_bytes:
usage: GAUGE
description: "Total index size"
- toast_bytes:
usage: GAUGE
description: "Toast size"
9 changes: 9 additions & 0 deletions ansible/tasks/internal/postgres-exporter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
extra_opts: [--strip-components=1]
become: yes

- name: deploy queries.yml for custom metrics
copy:
src: files/queries.yml
dest: /opt/postgres_exporter/queries.yml
owner: root
group: root
mode: '0644'
Comment on lines +42 to +44
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queries.yml is installed into a directory owned by postgres:postgres, but the file itself is owned by root:root. It’s readable so it will work, but this ownership mismatch can be surprising and makes it harder to manage the file without privilege escalation. Consider aligning ownership/group with the service user (and restricting permissions if desired).

Suggested change
owner: root
group: root
mode: '0644'
owner: postgres
group: postgres
mode: '0640'

Copilot uses AI. Check for mistakes.
become: yes

- name: exporter create a service
template:
src: files/postgres_exporter.service.j2
Expand Down
Loading