PostgreSQL monitoring integration
Preview
We're still working on query-level monitoring feature of this integration, but we'd love for you to try it out!
This feature is currently provided as part of a preview pursuant to our pre-release policies.
The New Relic PostgreSQL on-host integration receives and sends inventory metrics from your PostgreSQL instance to the New Relic platform, where you can aggregate and visualize key performance metrics. Data from instances, databases, and clusters helps you find the source of problems.
Important
For best results, regularly update the integration package and the infrastructure agent.
PostgreSQL users and permissions
If you also want to obtain table and index-related metrics (for example, table size and index size), the PostgreSQL role used by the integration (new_relic
) also needs SELECT
permissions on the tables from which it will gather metrics from. For example, to allow the integration to collect metrics from all the tables and indexes present in the database (in the public schema
), use the following:
GRANT SELECT ON ALL TABLES IN SCHEMA public TO new_relic;
If you also want to obtain query-level metrics from the PostgreSQL custom query config file, the PostgreSQL role used by the integration (new_relic
) needs to be added to the (pg_read_all_stats
) role. This is due to the user leveraging the (pg_stat_statements
) extension.
GRANT pg_read_all_stats TO new_relic;
Enabling the pg_stat_statements
extension may require you to manually create it from a query prompt:
CREATE EXTENSION pg_stat_statements;
The postgresql-config.yml
file
The postgresql-config.yml
file is the integration's YAML-format configuration where you can place required login credentials and configure how data is collected. Which options you change depend on your setup and preference. The configuration file has common settings applicable to all integrations, such as interval
, timeout
, inventory_source
.
Specific settings related to PostgreSQL are defined using the env section of the postgresql-config.yml
file. These settings control the connection to your PostgreSQL instance as well as other security settings and features.
See these more complex config examples.
JSON array: Interpreted as a list of database names from which to collect all relevant metrics, including any tables and indexes belonging to that database.
For example:
collection_list: '["postgres"]'JSON object: only entities specified in the object will be collected, no automatic discovery will be performed. The levels of JSON are
database name -> schema name -> table name -> index name
.For example:
collection_list: '{"postgres":{"public":{"pg_table1":["pg_index1","pg_index2"],"pg_table2":[]}}}'ALL
: collect metrics for all databases, schemas, tables, and indexes discovered.For example:
collection_list: 'ALL'integrations:- name: nri-postgresqlenv:USERNAME: postgresPASSWORD: passHOSTNAME: psql-sample.localnetPORT: 6432DATABASE: postgresCOLLECT_DB_LOCK_METRICS: falseCOLLECTION_LIST: '{"postgres":{"public":{"pg_table1":["pg_index1","pg_index2"],"pg_table2":[]}}}'# Enable query performance monitoring by setting its value to true to obtain query monitoring metrics.ENABLE_QUERY_MONITORING: 'true'# Set the threshold for query response time (in ms) to retrieve individual query performance metrics. Defaults to 500 ms.# QUERY_MONITORING_RESPONSE_TIME_THRESHOLD: '500ms'# The number of records for each query performance metrics - Defaults to 20# QUERY_MONITORING_COUNT_THRESHOLD: '20'TIMEOUT: 10interval: 15slabels:env: productionrole: postgresqlinventory_source: config/postgresql
Azure/AWS SSL Enabled Options: Azure Flexible managed database offerings require SSL to connect. AWS RDS/Aurora may require SSL if your MySQL version is 5.7+ and require_secure_transport is set to ON in your Aurora Parameter Group. To accomodate the SSL requirement, these settings in the postgresql-config.yml need to be set to
true
.For example:
ENABLE_SSL: "true"TRUST_SERVER_CERTIFICATE: "true"Azure/AWS SSL Disabled Options: In addition to the settings above, the following SSL settings should be commented out or removed from the config. This is due to the config trusting the server certificate above.
For example:
# SSL_ROOT_CERT_LOCATION: /etc/newrelic-infra/root_cert.crt# SSL_CERT_LOCATION: /etc/newrelic-infra/postgresql.crt# SSL_KEY_LOCATION: /etc/newrelic-infra/postgresql.keySummary: Once these settings are in place, the complete Azure/AWS config file should look like the one below. Note: the infra agent and Postgresql integration should be installed on a host with network access to the database instances.
For example:
integrations:- name: nri-postgresqlenv:USERNAME: newrelicPASSWORD: passwordHOSTNAME: AWS-or-Azure-instance-namePORT: 5432DATABASE: postgresCOLLECT_DB_LOCK_METRICS: falseCOLLECTION_LIST: 'ALL'# Enable query performance monitoring by setting its value to true to obtain query monitoring metrics.ENABLE_QUERY_MONITORING: 'true'# Set the threshold for query response time (in ms) to retrieve individual query performance metrics. Defaults to 500 ms.# QUERY_MONITORING_RESPONSE_TIME_THRESHOLD: '500ms'# The number of records for each query performance metrics - Defaults to 20# QUERY_MONITORING_COUNT_THRESHOLD: '20'ENABLE_SSL: "true"TRUST_SERVER_CERTIFICATE: "true"# SSL_ROOT_CERT_LOCATION: /etc/newrelic-infra/root_cert.crt# SSL_CERT_LOCATION: /etc/newrelic-infra/postgresql.crt# SSL_KEY_LOCATION: /etc/newrelic-infra/postgresql.keyTIMEOUT: 10interval: 15slabels:env: productionrole: postgresqlinventory_source: config/postgresql
integrations: - name: nri-postgresql env: USERNAME: postgres PASSWORD: pass HOSTNAME: psql-sample.localnet PORT: 6432 DATABASE: postgres
COLLECT_DB_LOCK_METRICS: false COLLECTION_LIST: '["postgres"]' # Enable query performance monitoring by setting its value to true to obtain query monitoring metrics. ENABLE_QUERY_MONITORING: 'true' # Set the threshold for query response time (in ms) to retrieve individual query performance metrics. Defaults to 500 ms. # QUERY_MONITORING_RESPONSE_TIME_THRESHOLD: '500ms' # The number of records for each query performance metrics - Defaults to 20 # QUERY_MONITORING_COUNT_THRESHOLD: '20' ENABLE_SSL: true TRUST_SERVER_CERTIFICATE: false SSL_ROOT_CERT_LOCATION: /etc/newrelic-infra/root_cert.crt SSL_CERT_LOCATION: /etc/newrelic-infra/postgresql.crt SSL_KEY_LOCATION: /etc/newrelic-infra/postgresql.key TIMEOUT: 10 interval: 15s labels: env: production role: postgresql inventory_source: config/postgresql
integrations: - name: nri-postgresql env: USERNAME: postgres PASSWORD: pass HOSTNAME: psql-sample.localnet PORT: 6432 DATABASE: postgres
COLLECT_DB_LOCK_METRICS: false COLLECTION_LIST: ALL # Enable query performance monitoring by setting its value to true to obtain query monitoring metrics. ENABLE_QUERY_MONITORING: 'true' # Set the threshold for query response time (in ms) to retrieve individual query performance metrics. Defaults to 500 ms. # QUERY_MONITORING_RESPONSE_TIME_THRESHOLD: '500ms' # The number of records for each query performance metrics - Defaults to 20 # QUERY_MONITORING_COUNT_THRESHOLD: '20' CUSTOM_METRICS_QUERY: >- select 'rows_inserted' as "metric_name", 'delta' as "metric_type", sd.tup_inserted as "metric_value", sd.datid as "database_id" from pg_stat_database sd; TIMEOUT: 10 interval: 15s labels: env: production role: postgresql inventory_source: config/postgresql
An additional YAML configuration file with one or more custom SQL can be defined and the integration will need the path to the file in the CUSTOM_METRICS_CONFIG parameter.
postgresql-config.yml
integrations:- name: nri-postgresqlenv:USERNAME: postgresPASSWORD: passHOSTNAME: psql-sample.localnetPORT: 6432DATABASE: postgresCOLLECT_DB_LOCK_METRICS: falseCOLLECTION_LIST: ALL# Enable query performance monitoring by setting its value to true to obtain query monitoring metrics.ENABLE_QUERY_MONITORING: 'true'# Set the threshold for query response time (in ms) to retrieve individual query performance metrics. Defaults to 500 ms.# QUERY_MONITORING_RESPONSE_TIME_THRESHOLD: '500ms'# The number of records for each query performance metrics - Defaults to 20# QUERY_MONITORING_COUNT_THRESHOLD: '20'CUSTOM_METRICS_CONFIG: "path/to/postgresql-custom-query.yml"TIMEOUT: 10interval: 15slabels:env: productionrole: postgresqlinventory_source: config/postgresqlpostgresql-custom-query.yml
---queries:# Metric names are set to the column names in the query results- query: >-SELECTBG.checkpoints_timed AS scheduled_checkpoints_performed,BG.checkpoints_req AS requested_checkpoints_performed,BG.buffers_checkpoint AS buffers_written_during_checkpoint,BG.buffers_clean AS buffers_written_by_background_writer,BG.maxwritten_clean AS background_writer_stops,BG.buffers_backend AS buffers_written_by_backend,BG.buffers_alloc AS buffers_allocatedFROM pg_stat_bgwriter BG;# database defaults to the auth database in the main configdatabase: new_frontier_config_dev# If not set explicitly here, metric type will default to# 'gauge' for numbers and 'attribute' for stringsmetric_types:buffers_allocated: rate# If unset, sample_name defaults to PostgresqlCustomSamplesample_name: MyCustomSample
For more about the general structure of on-host integration configuration, see On-host integration configuration overview.
Metrics collected by the integration
The PostgreSQL integration collects the following metrics. Some metric names are prefixed with a category indicator and a period, such as db.
or index.
.
PostgreSQLDatabaseSample attributes | Description |
---|---|
| Number of backends currently connected to this database. |
| The maximum number of concurrent connections to the database server. |
| Committed transactions per second. |
| Transactions rolled back per second. |
| Number of disk blocks read in this database per second. |
| Number of times disk blocks were found already in the buffer cache, so that a read was not necessary. This only includes hits in the PostgreSQL buffer cache, not the operating system's file system cache. |
| Rows returned by queries per second. |
| Rows fetched by queries per second. |
| Rows inserted per second. |
| Rows updated per second. |
| Rows deleted per second. |
| Number of queries in this database that have been canceled due to dropped tablespaces. |
| Number of queries in this database that have been canceled due to lock timeouts. |
| Number of queries in this database that have been canceled due to old snapshots. |
| Number of queries in this database that have been canceled due to pinned buffers. |
| Number of queries in this database that have been canceled due to deadlocks. |
| Number of temporary files created by queries in this database. All temporary files are counted, regardless of why the temporary file was created (for example, sorting or hashing), and regardless of the |
| Total amount of data written to temporary files by queries in this database. All temporary files are counted, regardless of why the temporary file was created, and regardless of the |
| Number of deadlocks detected in this database. |
| Time spent reading data file blocks by backends in this database, in milliseconds. |
| Time spent writing data file blocks by backends in this database, in milliseconds. |
PostgreSQLIndexSample attributes | Description |
---|---|
| The size of an index. |
| The number of index entries returned by scans on this index. |
| The number of index entries fetched by scans on this index. |
PostgreSQLInstanceSample attributes | Description |
---|---|
| Number of scheduled checkpoints that have been performed. |
| Number of requested checkpoints that have been performed. |
| Number of buffers written during checkpoints. |
| Number of buffers written by the background writer. |
| Number of times the background writer stopped a cleaning scan because it had written too many buffers. |
| Number of buffers written directly by a backend. |
| Number of buffers allocated. |
| Number of times a backend had to execute its own |
| Total amount of time that has been spent in the portion of checkpoint processing where files are written to disk, in milliseconds. |
| Total amount of time that has been spent in the portion of checkpoint processing where files are synchronized to disk, in milliseconds. |
PostgreSQLTableSample attributes | Description |
---|---|
| The total disk space used by the table, including indexes and TOAST data. |
| The total disk space used by indexes attached to the specified table. |
| Number of live rows. |
| Number of dead rows. |
| The number of disk blocks read from all indexes on this table. |
| The number of buffer hits in all indexes on this table. |
| The number of disk blocks read from this table's TOAST table index. |
| The number of buffer hits in this table's TOAST table index. |
| Time of last vacuum on table. |
| Time of last automatic vacuum on table. |
| Time of last analyze on table. |
| Time of last automatic analyze on table. |
| Number of sequential scans initiated on this table per second. |
| Number of live rows fetched by sequential scans per second. |
| Number of index scans initiated on this table. |
| Number of live rows fetched by index scans. |
| Rows inserted per second. |
| Rows updated per second. |
| Rows deleted per second. |
| Size of bloat in bytes. |
| Size of disk spaced used by the main fork of the table. |
| Fraction of table data size that is bloat. |
PgBouncerSample attributes | Description |
---|---|
| The transaction rate. |
| The query rate. |
| The total network traffic received. |
| The total network traffic sent. |
| Time spent by |
| Time spent by |
| The average number of transactions per second in last stat period. |
| The average transaction duration. |
| The average number of queries per second in last stat period. |
| The client network traffic received. |
| The client network traffic sent. |
| The average query duration. |
| Client connections linked to server connection and able to process queries. |
| Client connections waiting on a server connection. |
| Client connections that have not forwarded query cancellations to the server yet. |
| Client connections that have forwarded query cancellations to the server and are waiting for the server response. |
| Server connections that are currently forwarding a cancel request. |
| Servers that normally could become idle but are waiting to do so until all in-flight cancel requests have completed that were sent to cancel a query on this server. |
| Server connections linked to a client connection. |
| Server connections idle and ready for a client query. |
| Server connections idle more than |
| Server connections currently running either |
| Server connections currently in the process of logging in. |
| Age of oldest unserved client connection. |
Metrics collected by the query monitoring
The PostgreSQL query monitoring integration collects the following metrics
PostgresSlowQueries attributes | Description |
---|---|
| The unique identifier for each query. |
| The SQL statement that was run. |
| The name of the database where the query is run. |
| The schema involved in the query. |
| The number of times the query is run. |
| The average time to run in milliseconds. |
| The average number of disk reads performed by the query. |
| The average number of disk writes performed by the query. |
| The type of SQL statement (e.g., SELECT, INSERT). |
| The timestamp that indicates when the data is collected. |
PostgresWaitEvents attributes | Description |
---|---|
| The name of the wait event encountered. |
| The category of the wait event. |
| The total time spent on the wait event, in milliseconds. |
| The timestamp when the data was collected. |
| The unique identifier for each query. |
| The SQL statement that was run. |
| The name of the database where the query ran. |
PostgresBlockingSessions attributes | Description |
---|---|
| The Process ID of the blocked query. |
| The SQL statement run in the blocked process. |
| The unique identifier for the blocked query. |
| The time at which the blocked query is started. |
| The name of the database in which blocking occurs. |
| The SQL statement causing the block. |
| The process ID of the query causing the block. |
| The unique identifier of the query causing the block. |
| The time at which blocking query was executed. |
PostgresIndividualQueries attributes | Description |
---|---|
| The unique identifier for a query. |
| The SQL statement that was run. |
| The name of the database where the query was run. |
| The CPU time used by the query, in milliseconds. |
| The execution time in milliseconds. |
| The query identifier for the plan. |
PostgresExecutionPlanMetrics attributes | Description |
---|---|
| The type of operation performed at a node in the plan. |
| Indicates if the node supports parallel operation. |
| Indicates if the node can perform asynchronous operations. |
| The direction of scanning such as, forward or backward. |
| The name of the index used in the plan, if applicable. |
| The name of the table or relation involved in the operation. |
| The alias used in the query for the relation. |
| The estimated cost to start the node operation. |
| The estimated total cost to complete the node operation. |
| The estimated number of rows this plan node returns. |
| The estimated average width of rows this node output in bytes. |
| The number of rows removed from this node by filters. |
| The name of the database where the query runs. |
| The unique identifier for the particular query operation. |
| The identifier for this specific plan. |
| The depth or level of the node in the plan hierarchy. |