Skip to content

Commit 3c89afd

Browse files
feat( cluster): Add pg_stat_statements instrumentation (#805)
Signed-off-by: Philippe Noël <philippemnoel@gmail.com> Signed-off-by: Itay Grudev <itay@verito.digital> Co-authored-by: Itay Grudev <itay@verito.digital>
1 parent 1605a4c commit 3c89afd

7 files changed

Lines changed: 84 additions & 2 deletions

File tree

charts/cluster/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ Kubernetes: `>=1.29.0-0`
174174
| cluster.monitoring.disableDefaultQueries | bool | `false` | Whether the default queries should be injected. Set it to true if you don't want to inject default queries into the cluster. |
175175
| cluster.monitoring.enabled | bool | `false` | Whether to enable monitoring |
176176
| cluster.monitoring.instrumentation.logicalReplication | bool | `true` | Enable logical replication metrics |
177+
| cluster.monitoring.instrumentation.pgStatStatements | bool | `false` | Enable planning and execution statistics for all SQL statements. Increases shared memory usage. |
177178
| cluster.monitoring.podMonitor.enabled | bool | `true` | Whether to enable the PodMonitor |
178179
| cluster.monitoring.podMonitor.labels | object | `{}` | Additional labels to set on the generated PodMonitor resource. Add labels your monitoring stack requires (for example `team-name`). |
179180
| cluster.monitoring.podMonitor.metricRelabelings | list | `[]` | The list of metric relabelings for the PodMonitor. Applied to samples before ingestion. |

charts/cluster/templates/_bootstrap.tpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ bootstrap:
1010
{{- if .Values.cluster.initdb.owner }}
1111
owner: {{ tpl .Values.cluster.initdb.owner . }}
1212
{{- end }}
13-
{{- if or (eq .Values.type "postgis") (eq .Values.type "timescaledb") (not (empty .Values.cluster.initdb.postInitApplicationSQL)) }}
13+
{{- if or (eq .Values.type "postgis") (eq .Values.type "timescaledb") (not (empty .Values.cluster.initdb.postInitApplicationSQL)) .Values.cluster.monitoring.instrumentation.pgStatStatements }}
1414
postInitApplicationSQL:
1515
{{- if eq .Values.type "postgis" }}
1616
- CREATE EXTENSION IF NOT EXISTS postgis;
@@ -20,6 +20,9 @@ bootstrap:
2020
{{- else if eq .Values.type "timescaledb" }}
2121
- CREATE EXTENSION IF NOT EXISTS timescaledb;
2222
{{- end }}
23+
{{- if .Values.cluster.monitoring.instrumentation.pgStatStatements }}
24+
- CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
25+
{{- end }}
2326
{{- with .Values.cluster.initdb }}
2427
{{- range .postInitApplicationSQL }}
2528
{{- printf "- %s" . | nindent 6 }}

charts/cluster/templates/cluster.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@ spec:
6868
{{ end }}
6969
enablePDB: {{ .Values.cluster.enablePDB }}
7070
postgresql:
71-
{{- if or (eq .Values.type "timescaledb") (not (empty .Values.cluster.postgresql.shared_preload_libraries)) }}
71+
{{- if or (eq .Values.type "timescaledb") (not (empty .Values.cluster.postgresql.shared_preload_libraries)) .Values.cluster.monitoring.instrumentation.pgStatStatements }}
7272
shared_preload_libraries:
7373
{{- if eq .Values.type "timescaledb" }}
7474
- timescaledb
7575
{{- end }}
76+
{{- if .Values.cluster.monitoring.instrumentation.pgStatStatements }}
77+
- pg_stat_statements
78+
{{- end }}
7679
{{- with .Values.cluster.postgresql.shared_preload_libraries }}
7780
{{- toYaml . | nindent 6 }}
7881
{{- end }}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{{- if .Values.cluster.monitoring.instrumentation.pgStatStatements }}
2+
apiVersion: v1
3+
kind: ConfigMap
4+
metadata:
5+
name: {{ include "cluster.fullname" . }}-monitoring-pg-stat-statements
6+
namespace: {{ include "cluster.namespace" . }}
7+
labels:
8+
cnpg.io/reload: ""
9+
{{- include "cluster.labels" . | nindent 4 }}
10+
data:
11+
custom-queries: |
12+
pg_stat_statements:
13+
query: |
14+
SELECT d.datname
15+
, sum(s.calls) AS calls
16+
, sum(s.total_exec_time) / 1000 AS total_exec_time_seconds
17+
, sum(s.rows) AS rows
18+
, sum(s.shared_blks_hit) AS shared_blks_hit
19+
, sum(s.shared_blks_read) AS shared_blks_read
20+
, sum(s.blk_read_time) / 1000 AS blk_read_time_seconds
21+
, sum(s.blk_write_time) / 1000 AS blk_write_time_seconds
22+
FROM pg_stat_statements s
23+
JOIN pg_database d ON s.dbid = d.oid
24+
GROUP BY d.datname;
25+
target_databases: ["*"]
26+
predicate_query: "SELECT 1 FROM pg_extension WHERE extname = 'pg_stat_statements';"
27+
metrics:
28+
- datname:
29+
description: Name of the database
30+
usage: LABEL
31+
- calls:
32+
description: Total number of query executions
33+
usage: COUNTER
34+
- total_exec_time_seconds:
35+
description: Total execution time in seconds
36+
usage: COUNTER
37+
- rows:
38+
description: Total number of rows returned or affected
39+
usage: COUNTER
40+
- shared_blks_hit:
41+
description: Total number of shared block cache hits
42+
usage: COUNTER
43+
- shared_blks_read:
44+
description: Total number of shared blocks read from disk
45+
usage: COUNTER
46+
- blk_read_time_seconds:
47+
description: Total block read time in seconds
48+
usage: COUNTER
49+
- blk_write_time_seconds:
50+
description: Total block write time in seconds
51+
usage: COUNTER
52+
{{- end }}

charts/cluster/test/monitoring/01-monitoring_cluster-assert.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ spec:
1919
- name: monitoring-cluster-monitoring
2020
key: custom-queries
2121
enablePodMonitor: false
22+
bootstrap:
23+
initdb:
24+
postInitApplicationSQL:
25+
- CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
26+
postgresql:
27+
shared_preload_libraries:
28+
- pg_stat_statements
2229
---
2330
apiVersion: monitoring.coreos.com/v1
2431
kind: PodMonitor
@@ -124,3 +131,14 @@ data:
124131
- ratio:
125132
description: Cache hit ratio
126133
usage: GAUGE
134+
---
135+
apiVersion: v1
136+
kind: ConfigMap
137+
metadata:
138+
name: monitoring-cluster-monitoring-logical-replication
139+
140+
---
141+
apiVersion: v1
142+
kind: ConfigMap
143+
metadata:
144+
name: monitoring-cluster-monitoring-pg-stat-statements

charts/cluster/test/monitoring/01-monitoring_cluster.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ cluster:
88
monitoring:
99
enabled: true
1010
disableDefaultQueries: true
11+
instrumentation:
12+
logicalReplication: true
13+
pgStatStatements: true
1114
tls:
1215
enabled: true
1316
customQueries:

charts/cluster/values.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ cluster:
327327
instrumentation:
328328
# -- Enable logical replication metrics
329329
logicalReplication: true
330+
# -- Enable planning and execution statistics for all SQL statements. Increases shared memory usage.
331+
pgStatStatements: false
330332
# -- Whether the default queries should be injected.
331333
# Set it to true if you don't want to inject default queries into the cluster.
332334
disableDefaultQueries: false

0 commit comments

Comments
 (0)