From d6540d1cb42ede398e2940010ee5fcfd8a617639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20No=C3=ABl?= Date: Mon, 9 Feb 2026 16:09:45 -0500 Subject: [PATCH] Add pg_stat_statements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Philippe Noël --- charts/cluster/templates/_bootstrap.tpl | 1 + charts/cluster/templates/cluster.yaml | 1 + .../monitoring-pg-stat-statements.yaml | 52 +++++++++++++++++++ ..._default_configuration_cluster-assert.yaml | 1 + .../01-non_default_configuration_cluster.yaml | 1 + 5 files changed, 56 insertions(+) create mode 100644 charts/cluster/templates/monitoring-pg-stat-statements.yaml diff --git a/charts/cluster/templates/_bootstrap.tpl b/charts/cluster/templates/_bootstrap.tpl index 32a05c98d5..6ef02e00be 100644 --- a/charts/cluster/templates/_bootstrap.tpl +++ b/charts/cluster/templates/_bootstrap.tpl @@ -20,6 +20,7 @@ bootstrap: {{- else if eq .Values.type "timescaledb" }} - CREATE EXTENSION IF NOT EXISTS timescaledb; {{- end }} + - CREATE EXTENSION IF NOT EXISTS pg_stat_statements; {{- with .Values.cluster.initdb }} {{- range .postInitApplicationSQL }} {{- printf "- %s" . | nindent 6 }} diff --git a/charts/cluster/templates/cluster.yaml b/charts/cluster/templates/cluster.yaml index 4a17479773..0365cba206 100644 --- a/charts/cluster/templates/cluster.yaml +++ b/charts/cluster/templates/cluster.yaml @@ -73,6 +73,7 @@ spec: {{- if eq .Values.type "timescaledb" }} - timescaledb {{- end }} + - pg_stat_statements {{- with .Values.cluster.postgresql.shared_preload_libraries }} {{- toYaml . | nindent 6 }} {{- end }} diff --git a/charts/cluster/templates/monitoring-pg-stat-statements.yaml b/charts/cluster/templates/monitoring-pg-stat-statements.yaml new file mode 100644 index 0000000000..61648aeb2b --- /dev/null +++ b/charts/cluster/templates/monitoring-pg-stat-statements.yaml @@ -0,0 +1,52 @@ +{{- if .Values.cluster.monitoring.instrumentation.pgStatStatements }} +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "cluster.fullname" . }}-monitoring-pg-stat-statements + namespace: {{ include "cluster.namespace" . }} + labels: + cnpg.io/reload: "" + {{- include "cluster.labels" . | nindent 4 }} +data: + custom-queries: | + pg_stat_statements: + query: | + SELECT d.datname + , sum(s.calls) AS calls + , sum(s.total_exec_time) / 1000 AS total_exec_time_seconds + , sum(s.rows) AS rows + , sum(s.shared_blks_hit) AS shared_blks_hit + , sum(s.shared_blks_read) AS shared_blks_read + , sum(s.blk_read_time) / 1000 AS blk_read_time_seconds + , sum(s.blk_write_time) / 1000 AS blk_write_time_seconds + FROM pg_stat_statements s + JOIN pg_database d ON s.dbid = d.oid + GROUP BY d.datname; + target_databases: ["*"] + predicate_query: "SELECT 1 FROM pg_extension WHERE extname = 'pg_stat_statements';" + metrics: + - datname: + description: Name of the database + usage: LABEL + - calls: + description: Total number of query executions + usage: COUNTER + - total_exec_time_seconds: + description: Total execution time in seconds + usage: COUNTER + - rows: + description: Total number of rows returned or affected + usage: COUNTER + - shared_blks_hit: + description: Total number of shared block cache hits + usage: COUNTER + - shared_blks_read: + description: Total number of shared blocks read from disk + usage: COUNTER + - blk_read_time_seconds: + description: Total block read time in seconds + usage: COUNTER + - blk_write_time_seconds: + description: Total block write time in seconds + usage: COUNTER +{{- end }} diff --git a/charts/cluster/test/postgresql-cluster-configuration/01-non_default_configuration_cluster-assert.yaml b/charts/cluster/test/postgresql-cluster-configuration/01-non_default_configuration_cluster-assert.yaml index 47439b88dd..149cd61215 100644 --- a/charts/cluster/test/postgresql-cluster-configuration/01-non_default_configuration_cluster-assert.yaml +++ b/charts/cluster/test/postgresql-cluster-configuration/01-non_default_configuration_cluster-assert.yaml @@ -29,6 +29,7 @@ spec: pg_ident: - mymap /^(.*)@mydomain\.com$ \1 shared_preload_libraries: + - pg_stat_statements - pgaudit synchronous: method: any diff --git a/charts/cluster/test/postgresql-cluster-configuration/01-non_default_configuration_cluster.yaml b/charts/cluster/test/postgresql-cluster-configuration/01-non_default_configuration_cluster.yaml index 16a048c17c..6b78eba989 100644 --- a/charts/cluster/test/postgresql-cluster-configuration/01-non_default_configuration_cluster.yaml +++ b/charts/cluster/test/postgresql-cluster-configuration/01-non_default_configuration_cluster.yaml @@ -104,6 +104,7 @@ cluster: pg_ident: - mymap /^(.*)@mydomain\.com$ \1 shared_preload_libraries: + - pg_stat_statements - pgaudit synchronous: method: any