![]() |
VOOZH | about |
Database Monitoring provides deep visibility into your Postgres databases by exposing query metrics, query samples, explain plans, database states, failovers, and events.
The Agent collects telemetry directly from the database by logging in as a read-only user. Do the following setup to enable Database Monitoring with your Postgres database:
127.0.0.1 or the socket. The Agent should not connect to the database through a proxy, load balancer, or connection pooler such as pgbouncer. If the Agent connects to different hosts while it is running (as in the case of failover, load balancing, and so on), the Agent calculates the difference in statistics between two hosts, producing inaccurate metrics.Enable Resource Collection in the Resource Collection section of your Amazon Web Services integration tile.
Configure the following parameters in the DB parameter group and then restart the server for the settings to take effect. For more information about these parameters, see the Postgres documentation.
Required parameters
| Parameter | Value | Description |
|---|---|---|
shared_preload_libraries | pg_stat_statements | Required for postgresql.queries.* metrics. Enables collection of query metrics using the pg_stat_statements extension. |
track_activity_query_size | 4096 | Required for collection of larger queries. Increases the size of SQL text in pg_stat_activity. If left at the default value then queries longer than 1024 characters will not be collected. |
Optional parameters
| Parameter | Value | Description |
|---|---|---|
pg_stat_statements.track | ALL | Enables tracking of statements within stored procedures and functions. |
pg_stat_statements.max | 10000 | Increases the number of normalized queries tracked in pg_stat_statements. Recommended for high-volume databases that see many different types of queries from many different clients. |
pg_stat_statements.track_utility | off | Disables utility commands like PREPARE and EXPLAIN. Setting this value to off means only queries like SELECT, UPDATE, and DELETE are tracked. |
track_io_timing | on | Enables collection of block read and write times for queries. |
auto_explain (optional)By default, the agent only gathers EXPLAIN plans for a sampling of in-flight queries. These plans are of a more general nature, especially when application code uses prepared statements.
To collect full EXPLAIN ANALYZE plans taken from all queries, you need to use auto_explain, a first-party extension bundled with PostgreSQL available in all major providers. Logging collection is a prerequisite to auto_explain collection, so enable it before continuing.
auto_explain produces log lines that may contain sensitive application data, similar to raw values in non-obfuscated SQL. Use the dbm_parameterized_queries_read permission to control access to the resulting plans. To restrict visibility of the log lines themselves—which are visible to all users in your Datadog organization by default—also configure RBAC for Logs. Datadog recommends using both permissions to protect sensitive information effectively.auto_explain settings. The log format must be json, but other settings can vary depending on your application. This example logs an EXPLAIN ANALYZE plan for all queries over one second, including buffer information but omitting timing (which can have overhead).| Parameter | Value | Description |
|---|---|---|
shared_preload_libraries | pg_stat_statements,auto_explain | Enables automatic EXPLAIN ANALYZE |
auto_explain.log_format | json | Generates machine-readable plans |
auto_explain.log_min_duration | 1000 | Logs plans when queries exceed one second |
auto_explain.log_analyze | on | Use the ANALYZE form of EXPLAIN |
auto_explain.log_buffers | on | Include buffer use in plans |
auto_explain.log_timing | off | Do not include timing (high overhead) |
auto_explain.log_triggers | on | Include plans for trigger statement |
auto_explain.log_verbose | on | Use verbose plan type |
auto_explain.log_nested_statements | on | Include nested statements |
auto_explain.sample_rate | 1 | Explain all queries over duration |
Change the log_line_prefix to enable richer event correlation. For more information, see the RDS DB parameter groups documentation. auto_explain ingestion requires this be set to %m:%r:%u@%d:[%p]:%l:%e:%s:%v:%x:%c:%q%a.
To ensure your RDS instances are forwarding logs to CloudWatch and Datadog, follow the instructions for Amazon RDS Log Collection.
The Datadog Agent requires read-only access to the database server to collect statistics and queries.
Run the following SQL commands on the primary database server (the writer) in the cluster if Postgres is replicated. The Agent can collect telemetry from all databases on the server regardless of which database it connects to. Use the default postgres database unless you need the Agent to run custom queries against data unique to a different database.
Connect to your chosen database as a superuser (or another user with sufficient permissions). For example, to connect to the postgres database using psql:
psql -h mydb.example.com -d postgres -U postgres
Create the datadog user:
CREATEUSERdatadogWITHpassword'<PASSWORD>';Note: IAM authentication is also supported. See the guide on how to configure this for your RDS instance.
Give the datadog user permission to relevant tables:
ALTERROLEdatadogINHERIT;Create the following schema in every database:
CREATESCHEMAdatadog;GRANTUSAGEONSCHEMAdatadogTOdatadog;GRANTUSAGEONSCHEMApublicTOdatadog;GRANTpg_monitorTOdatadog;CREATEEXTENSIONIFNOTEXISTSpg_stat_statementsschemapublic;Create the following schema in every database:
CREATESCHEMAdatadog;GRANTUSAGEONSCHEMAdatadogTOdatadog;GRANTUSAGEONSCHEMApublicTOdatadog;GRANTpg_monitorTOdatadog;CREATEEXTENSIONIFNOTEXISTSpg_stat_statementsschemapublic;Create the following schema in every database:
CREATESCHEMAdatadog;GRANTUSAGEONSCHEMAdatadogTOdatadog;GRANTUSAGEONSCHEMApublicTOdatadog;GRANTSELECTONpg_stat_databaseTOdatadog;CREATEEXTENSIONIFNOTEXISTSpg_stat_statements;Create functions in every database to enable the Agent to read the full contents of pg_stat_activity and pg_stat_statements:
CREATEORREPLACEFUNCTIONdatadog.pg_stat_activity()RETURNSSETOFpg_stat_activityAS$$SELECT*FROMpg_catalog.pg_stat_activity;$$LANGUAGEsqlSECURITYDEFINER;CREATEORREPLACEFUNCTIONdatadog.pg_stat_statements()RETURNSSETOFpg_stat_statementsAS$$SELECT*FROMpg_stat_statements;$$LANGUAGEsqlSECURITYDEFINER;SELECT permission on those tables to the datadog user. Example: grant SELECT on <TABLE_NAME> to datadog;. See PostgreSQL custom metric collection for more information.Create the following function in every database to enable the Agent to collect explain plans:
CREATEORREPLACEFUNCTIONdatadog.explain_statement(l_queryTEXT,OUTexplainJSON)RETURNSSETOFJSONAS$$DECLAREcursREFCURSOR;planJSON;BEGINSETTRANSACTIONREADONLY;OPENcursFOREXECUTEpg_catalog.concat('EXPLAIN (FORMAT JSON) ',l_query);FETCHcursINTOplan;CLOSEcurs;RETURNQUERYSELECTplan;END;$$LANGUAGE'plpgsql'RETURNSNULLONNULLINPUTSECURITYDEFINER;Create the following function in every database to enable the Agent to collect column-level table statistics from pg_stats:
CREATEORREPLACEFUNCTIONdatadog.column_statistics()RETURNSTABLE(schemanamename,tablenamename,attnamename,n_distinctreal,avg_widthinteger,null_fracreal,inheritedboolean,correlationreal,most_common_freqsreal[])AS$$SELECTschemaname,tablename,attname,n_distinct,avg_width,null_frac,inherited,correlation,most_common_freqsFROMpg_catalog.pg_statsWHEREschemanameNOTIN('pg_catalog','information_schema');$$LANGUAGEsqlSECURITYDEFINERSETsearch_path=pg_catalog,pg_temp;After the function exists, enable collection in your Postgres instance config:
instances:- dbm:true...collect_column_statistics:enabled:trueFor tuning options, see Advanced Configuration.
Store your password using secret management software such as Vault. You can then reference this password as ENC[<SECRET_NAME>] in your Agent configuration files: for example, ENC[datadog_user_database_password]. See Secrets Management for more information.
The examples on this page use datadog_user_database_password to refer to the name of the secret where your password is stored. It is possible to reference your password in plain text, but this is not recommended.
To verify the permissions are correct, run the following commands to confirm the Agent user is able to connect to the database and read the core tables:
psql -h localhost -U datadog postgres -A \
-c "select * from pg_stat_database limit 1;" \
&& echo -e "\e[0;32mPostgres connection - OK\e[0m" \
|| echo -e "\e[0;31mCannot connect to Postgres\e[0m"
psql -h localhost -U datadog postgres -A \
-c "select * from pg_stat_activity limit 1;" \
&& echo -e "\e[0;32mPostgres pg_stat_activity read OK\e[0m" \
|| echo -e "\e[0;31mCannot read from pg_stat_activity\e[0m"
psql -h localhost -U datadog postgres -A \
-c "select * from pg_stat_statements limit 1;" \
&& echo -e "\e[0;32mPostgres pg_stat_statements read OK\e[0m" \
|| echo -e "\e[0;31mCannot read from pg_stat_statements\e[0m"
psql -h localhost -U datadog postgres -A \
-c "select * from pg_stat_database limit 1;" \
&& echo -e "\e[0;32mPostgres connection - OK\e[0m" \
|| echo -e "\e[0;31mCannot connect to Postgres\e[0m"
psql -h localhost -U datadog postgres -A \
-c "select * from datadog.pg_stat_activity() limit 1;" \
&& echo -e "\e[0;32mPostgres pg_stat_activity read OK\e[0m" \
|| echo -e "\e[0;31mCannot read from pg_stat_activity\e[0m"
psql -h localhost -U datadog postgres -A \
-c "select * from datadog.pg_stat_statements() limit 1;" \
&& echo -e "\e[0;32mPostgres pg_stat_statements read OK\e[0m" \
|| echo -e "\e[0;31mCannot read from pg_stat_statements\e[0m"
When it prompts for a password, use the password you entered when you created the datadog user.
To monitor RDS hosts, install the Datadog Agent in your infrastructure and configure it to connect to each instance endpoint remotely. The Agent does not need to run on the database, it only needs to connect to it. For additional Agent installation methods not mentioned here, see the Agent installation instructions.
To configure collecting Database Monitoring metrics for an Agent running on a host, for example when you provision a small EC2 instance for the Agent to collect from an RDS database:
Edit the postgres.d/conf.yaml file to point to your host / port and set the masters to monitor. See the sample postgres.d/conf.yaml for all available configuration options.
init_config:instances:- dbm:truehost:'<AWS_INSTANCE_ENDPOINT>'port:5432username:datadogpassword:'ENC[datadog_user_database_password]'aws:instance_endpoint:'<AWS_INSTANCE_ENDPOINT>'region:'<REGION>'tags:- "dbinstanceidentifier:<DB_INSTANCE_NAME>"## Required for Postgres 9.6: Uncomment these lines to use the functions created in the setup# pg_stat_statements_view: datadog.pg_stat_statements()# pg_stat_activity_view: datadog.pg_stat_activity()## Optional: Connect to a different database if needed for `custom_queries`# dbname: '<DB_NAME>'For Agent versions ≤ 7.49, add the following setting to the instance config where host and port are specified:
ssl:allowIf you want to authenticate with IAM, specify the region and instance_endpoint parameters, and set managed_authentication.enabled to true.
Note: only enable managed_authentication if you want to use IAM authentication. IAM authentication takes precedence over the password field.
init_config:instances:- dbm:truehost:'<AWS_INSTANCE_ENDPOINT>'port:5432username:datadogaws:instance_endpoint:'<AWS_INSTANCE_ENDPOINT>'region:'<REGION>'managed_authentication:enabled:truetags:- "dbinstanceidentifier:<DB_INSTANCE_NAME>"## Required for Postgres 9.6: Uncomment these lines to use the functions created in the setup# pg_stat_statements_view: datadog.pg_stat_statements()# pg_stat_activity_view: datadog.pg_stat_activity()## Optional: Connect to a different database if needed for `custom_queries`# dbname: '<DB_NAME>'For information on configuring IAM authentication on your RDS instance, see Connecting with Managed Authentication.
To configure an integration for an Agent running in a Docker container such as in ECS or Fargate, you have a couple of methods available, all of which are covered in detail in the Docker Configuration Documentation.
The examples below show how to use Docker Labels and Autodiscovery Templates to configure the Postgres integration.
Note: The Agent must have read permission on the Docker socket for Autodiscovery of labels to work.
Run the following command from your command line to start the Agent. Replace the placeholder values with those for your account and environment.
export DD_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
export DD_AGENT_VERSION=<AGENT_VERSION>
docker run -e "DD_API_KEY=${DD_API_KEY}" \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
-l com.datadoghq.ad.checks='{"postgres": {
"init_config": {},
"instances": [{
"dbm": true,
"host": "<AWS_INSTANCE_ENDPOINT>",
"port": 5432,
"username": "datadog",
"password": "<UNIQUEPASSWORD>",
"aws": {
"instance_endpoint": "<AWS_INSTANCE_ENDPOINT>",
"region": "<REGION>"
},
"tags": ["dbinstanceidentifier:<DB_INSTANCE_NAME>"]
}]
}}' \
registry.datadoghq.com/agent:${DD_AGENT_VERSION}
For Postgres 9.6, add the following settings to the instance config where host and port are specified:
"pg_stat_statements_view": "datadog.pg_stat_statements()","pg_stat_activity_view": "datadog.pg_stat_activity()"You can also specify labels in a Dockerfile, allowing you to build and deploy a custom Agent without modifying your infrastructure configuration:
FROMregistry.datadoghq.com/agent:<AGENT_VERSION>LABEL "com.datadoghq.ad.check_names"='["postgres"]'LABEL "com.datadoghq.ad.init_configs"='[{}]'LABEL "com.datadoghq.ad.instances"='[{"dbm": true, "host": "<AWS_INSTANCE_ENDPOINT>", "port": 5432,"username": "datadog","password": "ENC[datadog_user_database_password]","aws": {"instance_endpoint": "<AWS_INSTANCE_ENDPOINT>", "region": "<REGION>"}, "tags": ["dbinstanceidentifier:<DB_INSTANCE_NAME>"]}]'For Postgres 9.6, add the following settings to the instance config where host and port are specified:
"pg_stat_statements_view": "datadog.pg_stat_statements()","pg_stat_activity_view": "datadog.pg_stat_activity()"To avoid exposing the datadog user’s password in plain text, use the Agent’s secret management package and declare the password using the ENC[] syntax. Alternatively, see the Autodiscovery template variables documentation to provide the password as an environment variable.
If you’re running a Kubernetes cluster, use the Datadog Cluster Agent to enable Database Monitoring.
Note: Make sure cluster checks are enabled for your Datadog Cluster Agent before proceeding.
Below are step-by-step instructions for configuring the Postgres integration using different Datadog Cluster Agent deployment methods.
Using the Operator instructions in Kubernetes and Integrations as a reference, follow the steps below to set up the Postgres integration:
Create or update the datadog-agent.yaml file with the following configuration:
apiVersion:datadoghq.com/v2alpha1kind:DatadogAgentmetadata:name:datadogspec:global:clusterName:<CLUSTER_NAME>site:<DD_SITE>credentials:apiSecret:secretName:datadog-agent-secretkeyName:api-keyfeatures:clusterChecks:enabled:trueoverride:nodeAgent:image:name:agenttag:<AGENT_VERSION>clusterAgent:extraConfd:configDataMap:postgres.yaml:|- cluster_check: true
init_config:
instances:
- host: <AWS_INSTANCE_ENDPOINT>
port: 5432
username: datadog
password: 'ENC[datadog_user_database_password]'
dbm: true
aws:
instance_endpoint: <AWS_INSTANCE_ENDPOINT>
region: <REGION>
tags:
- "dbinstanceidentifier:<DB_INSTANCE_NAME>"Note: For Postgres 9.6, add the following lines to the instance config where host and port are specified:
pg_stat_statements_view:datadog.pg_stat_statements()pg_stat_activity_view:datadog.pg_stat_activity()Apply the changes to the Datadog Operator using the following command:
kubectl apply -f datadog-agent.yaml
Using the Helm instructions in Kubernetes and Integrations as a reference, follow the steps below to set up the Postgres integration:
Update your datadog-values.yaml file (used in the Cluster Agent installation instructions) with the following configuration:
datadog:clusterChecks:enabled:trueclusterChecksRunner:enabled:trueclusterAgent:enabled:trueconfd:postgres.yaml:|- cluster_check: true
init_config:
instances:
- dbm: true
host: <AWS_INSTANCE_ENDPOINT>
port: 5432
username: datadog
password: 'ENC[datadog_user_database_password]'
aws:
instance_endpoint: <AWS_INSTANCE_ENDPOINT>
region: <REGION>
tags:
- "dbinstanceidentifier:<DB_INSTANCE_NAME>"Note: For Postgres 9.6, add the following lines to the instance config where host and port are specified:
pg_stat_statements_view:datadog.pg_stat_statements()pg_stat_activity_view:datadog.pg_stat_activity()Deploy the Agent with the above configuration file using the following command:
helm install datadog-agent -f datadog-values.yaml datadog/datadog
--set targetSystem=windows to the helm install command.To configure a cluster check with a mounted configuration file, mount the configuration file in the Cluster Agent container at the path: /conf.d/postgres.yaml:
cluster_check:true# Make sure to include this flaginit_config:instances:- dbm:truehost:'<AWS_INSTANCE_ENDPOINT>'port:5432username:datadogpassword:'ENC[datadog_user_database_password]'aws:instance_endpoint:<AWS_INSTANCE_ENDPOINT>region:<REGION>tags:- "dbinstanceidentifier:<DB_INSTANCE_NAME>"## Required: For Postgres 9.6, uncomment these lines to use the functions created in the setup# pg_stat_statements_view: datadog.pg_stat_statements()# pg_stat_activity_view: datadog.pg_stat_activity()Instead of mounting a file, you can declare the instance configuration as a Kubernetes service. To configure this check for an Agent running on Kubernetes, create a service using the following syntax:
apiVersion:v1kind:Servicemetadata:name:postgreslabels:tags.datadoghq.com/env:'<ENV>'tags.datadoghq.com/service:'<SERVICE>'annotations:ad.datadoghq.com/service.checks:| {
"postgres": {
"init_config": <INIT_CONFIG>,
"instances": [
{
"dbm": true,
"host": "<AWS_INSTANCE_ENDPOINT>",
"port": 5432,
"username": "datadog",
"password": "ENC[datadog_user_database_password]",
"aws": {
"instance_endpoint": "<AWS_INSTANCE_ENDPOINT>",
"region": "<REGION>"
},
"tags": [
"dbinstanceidentifier:<DB_INSTANCE_NAME>"
]
}
]
}
}spec:ports:- port:5432protocol:TCPtargetPort:5432name:postgresFor more information, see Autodiscovery Annotations.
If you’re using Postgres 9.6, add the following to the instance configuration:
"pg_stat_statements_view": "datadog.pg_stat_statements()",
"pg_stat_activity_view": "datadog.pg_stat_activity()"
The Cluster Agent automatically registers this configuration and begins running the Postgres check.
To avoid exposing the datadog user’s password in plain text, use the Agent’s secret management package and declare the password using the ENC[] syntax.
Run the Agent’s status subcommand and look for postgres under the Checks section. Or visit the Databases page to get started!
It is common to configure a single Agent host to connect to multiple remote database instances (see Agent installation architectures for DBM). To connect to multiple hosts, create an entry for each host in the Postgres integration config.
init_config:instances:- dbm:truehost:example-service-primary.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'tags:- 'env:prod'- 'team:team-discovery'- 'service:example-service'- dbm:truehost:example-service–replica-1.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'tags:- 'env:prod'- 'team:team-discovery'- 'service:example-service'- dbm:truehost:example-service–replica-2.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'tags:- 'env:prod'- 'team:team-discovery'- 'service:example-service'[...]Use the database_autodiscovery option to permit the Agent to discover all databases on your host to monitor. You can specify include or exclude fields to narrow the scope of databases discovered. See the sample postgres.d/conf.yaml for more details.
init_config:instances:- dbm:truehost:example-service-primary.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'database_autodiscovery:enabled:true# Optionally, set the include field to specify# a set of databases you are interested in discoveringinclude:- mydb.*- example.*tags:- 'env:prod'- 'team:team-discovery'- 'service:example-service'To collect custom metrics, use the custom_queries option. See the sample postgres.d/conf.yaml for more details.
init_config:instances:- dbm:truehost:localhostport:5432username:datadogpassword:'ENC[datadog_user_database_password]'custom_queries:- metric_prefix:employeequery:SELECT age, salary, hours_worked, name FROM hr.employees;columns:- name:custom.employee_agetype:gauge- name:custom.employee_salarytype:gauge- name:custom.employee_hourstype:count- name:nametype:tagtags:- 'table:employees'In order to collect relation metrics (such as postgresql.seq_scans, postgresql.dead_rows, postgresql.index_rows_read, and postgresql.table_size), the Agent must be configured to connect to each database (by default, the Agent only connects to the postgres database).
Specify a single “DBM” instance to collect DBM telemetry from all databases. Use the database_autodiscovery option to avoid specifying each database name.
init_config:instances:# This instance is the "DBM" instance. It will connect to the# all logical databases, and send DBM telemetry from all databases- dbm:truehost:example-service-primary.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'database_autodiscovery:enabled:trueexclude:- ^users$- ^inventory$relations:- relation_regex:.*# This instance only collects data from the `users` database# and collects relation metrics from tables prefixed by "2022_"- host:example-service-primary.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'dbname:usersdbstrict:truerelations:- relation_regex:2022_.*relkind:- r- i# This instance only collects data from the `inventory` database# and collects relation metrics only from the specified tables- host:example-service-primary.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'dbname:inventorydbstrict:truerelations:- relation_name:products- relation_name:external_seller_productsTo enable this feature, use the collect_schemas option. You must also configure the Agent to connect to each logical database.
Use the database_autodiscovery option to avoid specifying each logical database. See the sample postgres.d/conf.yaml for more details.
init_config:# This instance only collects data from the `users` database# and collects relation metrics only from the specified tablesinstances:- dbm:truehost:example-service-primary.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'dbname:usersdbstrict:truecollect_schemas:enabled:truerelations:- products- external_seller_products# This instance detects every logical database automatically# and collects relation metrics from every table- dbm:truehost:example-service–replica-1.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'database_autodiscovery:enabled:truecollect_schemas:enabled:truerelations:- relation_regex:.*If the Agent must connect through a proxy such as the Cloud SQL Auth proxy, all telemetry is tagged with the hostname of the proxy rather than the database instance. Use the reported_hostname option to set a custom override of the hostname detected by the Agent.
init_config:instances:- dbm:truehost:localhostport:5000username:datadogpassword:'ENC[datadog_user_database_password]'reported_hostname:example-service-primary- dbm:truehost:localhostport:5001username:datadogpassword:'ENC[datadog_user_database_password]'reported_hostname:example-service-replica-1To see infrastructure metrics from AWS, such as CPU, alongside the database telemetry in DBM, install the RDS integration (optional).
If you have installed and configured the integrations and Agent as described and it is not working as expected, see Troubleshooting.
Additional helpful documentation, links, and articles:
| |