This PostgreSQL exporter review discusses important metrics, alert rules, and all you need to know in order to monitor PostgreSQL, an open source object-related database system using SQL language.
PostgreSQL is an open-source object-relational database system with a strong reputation for reliability, feature robustness, and performance. It uses the SQL language combined with many features that safely store and scale the most complicated data workloads.
PostgreSQL runs on all major operating systems and is offered as a service by all major cloud providers. It comes with many features aimed at helping developers build applications as well as helping administrators protect data integrity and create fault-tolerant environments. It supports users in managing their data no matter how big or small the dataset.
Since databases are such a critical resource, downtime can cause significant financial and reputation losses, so monitoring is a must. The Postgres exporter is required to monitor and expose Postgres metrics. It queries Postgres, scraps the data, and exposes the metrics to a Kubernetes service endpoint that can further be scrapped by Prometheus to ingest the time series data. For monitoring of Postgres, an external Prometheus exporter is used, which is maintained by the Prometheus Community. On deployment, this exporter scraps sizable metrics from Postgres and helps users get crucial and continuous information about the database which is difficult to extract from PostgreSQL directly.
For this setup, we are using Bitnami PostgreSQL Helm charts to start the Postgres server.
With the latest version of Prometheus (2.33 as of February 2022), these are the ways to set up a Prometheus exporter:
Supported by Prometheus since the beginning
To set up an exporter in native way a Prometheus config needs to be updated to add the target.
A sample configuration:
# scrape_config job
scrape_configs:
- job_name: postgres
scrape_interval: 45s
scrape_timeout: 30s
metrics_path: "/metrics"
static_configs:
- targets:
- <postgres exporter endpoint>
Code language: PHP (php)
This method is applicable for Kubernetes deployment only.
With this, a default scrap config can be added to the prometheus.yaml file and an annotation can be added to the exporter service. With this, Prometheus will automatically start scrapping the data from the services with the mentioned path.
Prometheus.yaml
- job_name: kubernetes-services
scrape_interval: 15s
scrape_timeout: 10s
kubernetes_sd_configs:
- role: service
relabel_configs:
# Example relabel to scrape only endpoints that have
# prometheus.io/scrape: "true" annotation.
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
action: keep
regex: true
# prometheus.io/path: "/scrape/path" annotation.
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
# prometheus.io/port: "80" annotation.
- source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
action: replace
target_label: __address__
regex: (.+)(?::\d+);(\d+)
replacement: $1:$2
Code language: PHP (php)
Exporter service annotations:
annotations:
prometheus.io/path: /metrics
prometheus.io/scrape: "true"
Code language: PHP (php)
Setting up a service monitor
The Prometheus operator supports an automated way of scraping data from the exporters by setting up a service monitor Kubernetes object. For reference, a sample service monitor for PostgreSQL can be found here.
These are the necessary steps:
Step 1
Add/update Prometheus operator’s selectors. By default, the Prometheus operator comes with empty selectors which will select every service monitor available in the cluster for scrapping the data.
To check your Prometheus configuration:
Kubectl get prometheus -n <namespace> -o yaml
Code language: HTML, XML (xml)
A sample output will look like this.
ruleNamespaceSelector: {}
ruleSelector:
matchLabels:
app: kube-prometheus-stack
release: kps
scrapeInterval: 1m
scrapeTimeout: 10s
securityContext:
fsGroup: 2000
runAsGroup: 2000
runAsNonRoot: true
runAsUser: 1000
serviceAccountName: kps-kube-prometheus-stack-prometheus
serviceMonitorNamespaceSelector: {}
serviceMonitorSelector:
matchLabels:
release: kps
Code language: CSS (css)
Here you can see that this Prometheus configuration is selecting all the service monitors with the label release = kps
So with this, if you are modifying the default Prometheus operator configuration for service monitor scrapping, make sure you use the right labels in your service monitor as well.
Step 2
Add a service monitor and make sure it has a matching label and namespace for the Prometheus service monitor selectors (serviceMonitorNamespaceSelector & serviceMonitorSelector).
Sample configuration:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
annotations:
meta.helm.sh/release-name: postgres-exporter
meta.helm.sh/release-namespace: monitor
labels:
app: prometheus-postgres-exporter
app.kubernetes.io/managed-by: Helm
chart: prometheus-postgres-exporter-1.1.0
heritage: Helm
release: kps
name: prometheus-postgres-exporter
namespace: monitor
spec:
endpoints:
- interval: 15s
port: postgres-exporter
selector:
matchLabels:
app: prometheus-postgres-exporter
release: postgres-exporter
As you can see, a matching label on the service monitor release = kps is used that is specified in the Prometheus operator scrapping configuration.
The following ones are handpicked metrics that will provide insights into PostgreSQL.
Prometheus exporter for PostgreSQL server metrics.
CI Tested PostgreSQL versions: 9.4
, 9.5
, 9.6
, 10
, 11
, 12
, 13
, 14
This package is available for Docker:
# Start an example database
docker run --net=host -it --rm -e POSTGRES_PASSWORD=password postgres
# Connect to it
docker run \
--net=host \
-e DATA_SOURCE_NAME="postgresql://postgres:password@localhost:5432/postgres?sslmode=disable" \
quay.io/prometheuscommunity/postgres-exporter
git clone https://github.com/prometheus-community/postgres_exporter.git
cd postgres_exporter
make build
./postgres_exporter <flags>
To build the Docker image:
make promu
promu crossbuild -p linux/amd64 -p linux/armv7 -p linux/amd64 -p linux/ppc64le
make docker
This will build the docker image as prometheuscommunity/postgres_exporter:${branch}
.
help
collector.database
enabled
collector.bgwriter
enabled
web.listen-address
:9187
.web.telemetry-path
/metrics
.disable-default-metrics
queries.yaml
via --extend.query-path
. Default is false
.disable-settings-metrics
pg_settings
. Default is false
.auto-discover-databases
false
.extend.query-path
queries.yaml
dumpmaps
constantLabels
label=value
pairs, separated by commas.version
exclude-databases
include-databases
log.level
debug
, info
, warn
, error
.log.format
logfmt
, json
.web.config.file
The following environment variables configure the exporter:
DATA_SOURCE_NAME
DATA_SOURCE_URI
DATA_SOURCE_NAME
which exclusively accepts the hostnamemy_pg_hostname
ormy_pg_hostname?sslmode=disable
.DATA_SOURCE_URI_FILE
DATA_SOURCE_USER
DATA_SOURCE_URI
, this environment variable is used to specifyDATA_SOURCE_USER_FILE
DATA_SOURCE_PASS
DATA_SOURCE_URI
, this environment variable is used to specifyDATA_SOURCE_PASS_FILE
PG_EXPORTER_WEB_LISTEN_ADDRESS
:9187
.PG_EXPORTER_WEB_TELEMETRY_PATH
/metrics
.PG_EXPORTER_DISABLE_DEFAULT_METRICS
queries.yaml
. Value can be true
or false
. Default is false
.PG_EXPORTER_DISABLE_SETTINGS_METRICS
pg_settings
. Value can be true
or false
. Default is false
.PG_EXPORTER_AUTO_DISCOVER_DATABASES
true
or false
. Default is false
.PG_EXPORTER_EXTEND_QUERY_PATH
queries.yaml
PG_EXPORTER_CONSTANT_LABELS
label=value
pairs, separated by commas.PG_EXPORTER_EXCLUDE_DATABASES
PG_EXPORTER_INCLUDE_DATABASES
PG_EXPORTER_METRIC_PREFIX
pg
Settings set by environment variables starting with PG_
will be overwritten by the corresponding CLI flag if given.
The PostgreSQL server's data source name
must be set via the DATA_SOURCE_NAME
environment variable.
For running it locally on a default Debian/Ubuntu install, this will work (transpose to init script as appropriate):
sudo -u postgres DATA_SOURCE_NAME="user=postgres host=/var/run/postgresql/ sslmode=disable" postgres_exporter
Also, you can set a list of sources to scrape different instances from the one exporter setup. Just define a comma separated string.
sudo -u postgres DATA_SOURCE_NAME="port=5432,port=6432" postgres_exporter
See the github.com/lib/pq module for other ways to format the connection string.
The exporter will attempt to dynamically export additional metrics if they are added in the
future, but they will be marked as "untyped". Additional metric maps can be easily created
from Postgres documentation by copying the tables and using the following Python snippet:
x = """tab separated raw text of a documentation table"""
for l in StringIO(x):
column, ctype, description = l.split('\t')
print """"{0}" : {{ prometheus.CounterValue, prometheus.NewDesc("pg_stat_database_{0}", "{2}", nil, nil) }}, """.format(column.strip(), ctype, description.strip())
Adjust the value of the resultant prometheus value type appropriately. This helps build
rich self-documenting metrics for the exporter.
The -extend.query-path command-line argument specifies a YAML file containing additional queries to run.
Some examples are provided in queries.yaml.
To work with non-officially-supported postgres versions (e.g. 8.2.15),
or variants of postgres (e.g. Greenplum), you can disable the default metrics with the --disable-default-metrics
flag. This removes all built-in metrics, and uses only metrics defined by queries in the queries.yaml
file you supply
(so you must supply one, otherwise the exporter will return nothing but internal statuses and not your database).
To scrape metrics from all databases on a database server, the database DSN's can be dynamically discovered via the--auto-discover-databases
flag. When true, SELECT datname FROM pg_database WHERE datallowconn = true AND datistemplate = false and datname != current_database()
is run for all configured DSN's. From the
result a new set of DSN's is created for which the metrics are scraped.
In addition, the option --exclude-databases
adds the possibily to filter the result from the auto discovery to discard databases you do not need.
If you want to include only subset of databases, you can use option --include-databases
. Exporter still makes request topg_database
table, but do scrape from only if database is in include list.
To be able to collect metrics from pg_stat*
views as non-superuser in PostgreSQL
server versions >= 10 you can grant the pg_monitor
or pg_read_all_stats
built-in roles to the user. If
you need to monitor older PostgreSQL servers, you will have to create functions
and views as a superuser, and assign permissions separately to those.
-- To use IF statements, hence to be able to check if the user exists before
-- attempting creation, we need to switch to procedural SQL (PL/pgSQL)
-- instead of standard SQL.
-- More: https://www.postgresql.org/docs/9.3/plpgsql-overview.html
-- To preserve compatibility with <9.0, DO blocks are not used; instead,
-- a function is created and dropped.
CREATE OR REPLACE FUNCTION __tmp_create_user() returns void as $$
BEGIN
IF NOT EXISTS (
SELECT -- SELECT list can stay empty for this
FROM pg_catalog.pg_user
WHERE usename = 'postgres_exporter') THEN
CREATE USER postgres_exporter;
END IF;
END;
$$ language plpgsql;
SELECT __tmp_create_user();
DROP FUNCTION __tmp_create_user();
ALTER USER postgres_exporter WITH PASSWORD 'password';
ALTER USER postgres_exporter SET SEARCH_PATH TO postgres_exporter,pg_catalog;
-- If deploying as non-superuser (for example in AWS RDS), uncomment the GRANT
-- line below and replace <MASTER_USER> with your root user.
-- GRANT postgres_exporter TO <MASTER_USER>;
GRANT CONNECT ON DATABASE postgres TO postgres_exporter;
Run following command if you use PostgreSQL versions >= 10
GRANT pg_monitor to postgres_exporter;
Run following SQL commands only if you use PostgreSQL versions older than 10.
In PostgreSQL, views run with the permissions of the user that created them so
they can act as security barriers. Functions need to be created to share this
data with the non-superuser. Only creating the views will leave out the most
important bits of data.
CREATE SCHEMA IF NOT EXISTS postgres_exporter;
GRANT USAGE ON SCHEMA postgres_exporter TO postgres_exporter;
CREATE OR REPLACE FUNCTION get_pg_stat_activity() RETURNS SETOF pg_stat_activity AS
$$ SELECT * FROM pg_catalog.pg_stat_activity; $$
LANGUAGE sql
VOLATILE
SECURITY DEFINER;
CREATE OR REPLACE VIEW postgres_exporter.pg_stat_activity
AS
SELECT * from get_pg_stat_activity();
GRANT SELECT ON postgres_exporter.pg_stat_activity TO postgres_exporter;
CREATE OR REPLACE FUNCTION get_pg_stat_replication() RETURNS SETOF pg_stat_replication AS
$$ SELECT * FROM pg_catalog.pg_stat_replication; $$
LANGUAGE sql
VOLATILE
SECURITY DEFINER;
CREATE OR REPLACE VIEW postgres_exporter.pg_stat_replication
AS
SELECT * FROM get_pg_stat_replication();
GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter;
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
CREATE OR REPLACE FUNCTION get_pg_stat_statements() RETURNS SETOF pg_stat_statements AS
$$ SELECT * FROM public.pg_stat_statements; $$
LANGUAGE sql
VOLATILE
SECURITY DEFINER;
CREATE OR REPLACE VIEW postgres_exporter.pg_stat_statements
AS
SELECT * FROM get_pg_stat_statements();
GRANT SELECT ON postgres_exporter.pg_stat_statements TO postgres_exporter;
NOTE
Remember to use postgres
database name in the connection string:
DATA_SOURCE_NAME=postgresql://postgres_exporter:password@localhost:5432/postgres?sslmode=disable
# Run the unit tests
make test
# Start the test database with docker
docker run -p 5432:5432 -e POSTGRES_DB=circle_test -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=test -d postgres
# Run the integration tests
DATA_SOURCE_NAME='postgresql://postgres:test@localhost:5432/circle_test?sslmode=disable' GOOPTS='-v -tags integration' make test
The exporter, alert rule, and dashboard can be deployed in Kubernetes using the Helm chart. The Helm chart used for deployment is taken from the Prometheus community, which can be found here.
If your Postgres server is not up and ready you can start it using Helm:
$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm install my-release bitnami/postgresql
Note that bitnami charts allow you to deploy a Postgres exporter as part of the Helm chart. You can enable it by adding “--set metrics.enabled=true”
helm repo add Prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install my-release prometheus-community/prometheus-postgres-exporter
Some of the common parameters that must be changed in the values file include:
config:
datasource:
# Specify one of both datasource or datasourceSecret
host:
user: postgres
# Only one of password, passwordSecret and pgpassfile can be specified
password:
# Specify passwordSecret if DB password is stored in secret.
passwordSecret: {}
All these parameters can be tuned via the values.yaml file here.
There are multiple ways to scrape the metrics as discussed above. In addition to the native way of setting up Prometheus monitoring, a service monitor can be deployed (if a Prometheus operator is being used) to scrap the data from the Postgres exporter. With this approach, multiple Postgres servers can be scrapped without altering the Prometheus configuration. Every Postgres exporter comes with its own service monitor.
In the above-mentioned chart, a service monitor can be deployed by turning it on from the values.yaml file here.
serviceMonitor:
# When set true then use a ServiceMonitor to configure scraping
enabled: false
# Set the namespace the ServiceMonitor should be deployed
# namespace: monitoring
# Set how frequently Prometheus should scrape
# interval: 30s
# Set path to cloudwatch-exporter telemtery-path
# telemetryPath: /metrics
# Set labels for the ServiceMonitor, use this to define your scrape label for Prometheus Operator
# labels:
# Set timeout for scrape
# timeout: 10s
# Set of labels to transfer from the Kubernetes Service onto the target
# targetLabels: []
# MetricRelabelConfigs to apply to samples before ingestion
# metricRelabelings: []
# Set relabel_configs as per https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
# relabelings: []
Update the annotation section here if you are not using the Prometheus Operator.
service:
annotations:
prometheus.io/path: /metrics
prometheus.io/scrape: "true"
This concludes our discussion of the PostgreSQL exporter! If you have any questions, you can reach our team via support@nexclipper.io. Stay tuned for further exporter reviews and tips coming soon.
After digging into all the valuable metrics, this section explains in detail how we can get critical alerts.
PromQL is a query language for the Prometheus monitoring system. It is designed for building powerful yet simple queries for graphs, alerts, or derived time series (aka recording rules). PromQL is designed from scratch and has zero common grounds with other query languages used in time series databases, such as SQL in TimescaleDB, InfluxQL, or Flux. More details can be found here.
Prometheus comes with a built-in Alert Manager that is responsible for sending alerts (could be email, Slack, or any other supported channel) when any of the trigger conditions is met. Alerting rules allow users to define alerts based on Prometheus query expressions. They are defined based on the available metrics scraped by the exporter. Click here for a good source for community-defined alerts.
A general alert looks as follows:
- alert:(Alert Name)
expr: (Metric exported from exporter) >/</==/<=/=> (Value)
for: (wait for a certain duration between first encountering a new expression output vector element and counting an alert as firing for this element)
labels: (allows specifying a set of additional labels to be attached to the alert)
annotation: (specifies a set of informational labels that can be used to store longer additional information)
Some of the recommended PostgreSQL alerts are:
- alert: PostgresqlDown
expr: pg_up == 0
for: 0m
labels:
severity: critical
annotations:
summary: Postgresql down (instance {{ $labels.instance }})
description: "Postgresql instance is down\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: PostgresqlReplicationLag
expr: pg_replication_lag > 30 and ON(instance) pg_replication_is_replica == 1
for: 0m
labels:
severity: critical
annotations:
summary: Postgresql replication lag (instance {{ $labels.instance }})
description: "PostgreSQL replication lag is going up (> 30s)\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: PostgresqlTooManyConnections
expr: sum by (datname) (pg_stat_activity_count{datname!~"template.*|postgres"}) > pg_settings_max_connections * 0.8
for: 2m
labels:
severity: warning
annotations:
summary: Postgresql too many connections (instance {{ $labels.instance }})
description: "PostgreSQL instance has too many connections (> 80%).\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: PostgresqlHighDbSize
expr: pg_database_size_bytes / (1024 * 1024 * 1024)
> 100 # this value depends on available disk size
for: 0m
labels:
severity: critical
annotations:
summary: Postgresql DB size is more than 100 GB (instance {{ $labels.instance }})
description: "Postgresql DB size is more than 100 GB\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: PostgresqlTXDuration
expr: pg_stat_activity_max_tx_duration{state="active"} > 2
for: 0m
labels:
severity: critical
annotations:
summary: Postgresql active transaction takes more than 2 seconds to complete (instance {{ $labels.instance }})
description: "PostgreSQL Postgresql active transaction takes more than 2 seconds\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
Graphs are easier to understand and more user-friendly than a row of numbers. For this purpose, users can plot their time series data in visualized format using Grafana.
Grafana is an open-source dashboarding tool used for visualizing metrics with the help of customizable and illustrative charts and graphs. It connects very well with Prometheus and makes monitoring easy and informative. Dashboards in Grafana are made up of panels, with each panel running a PromQL query to fetch metrics from Prometheus.
Grafana supports community-driven graphs for most of the widely used software, which can be directly imported to the Grafana Community.
NexClipper uses the PostgreSQL database by the Lucas Estienne dashboard, which is widely accepted and has a lot of useful panels.
What is a Panel?
Panels are the most basic component of a dashboard and can display information in various ways, such as gauge, text, bar chart, graph, and so on. They provide information in a very interactive way. Users can view every panel separately and check the value of metrics within a specific time range.
The values on the panel are queried using PromQL, which is Prometheus Query Language. PromQL is a simple query language used to query metrics within Prometheus. It enables users to query data, aggregate and apply arithmetic functions to the metrics, and then further visualize them on panels.
Here are some examples of panels:
1. Database metrics and settings
2. Database statistics
qwerqwerqwerq
comment test