Skip to content

How to create alerts based on Postgres slow queries?

0

I have my Aurora Postgres cluster configured to log slow queries (log_min_duration_statement: 1000). How can I setup a Cloudwatch alert to notify me when a query's average speed exceeds this?

Or do I need to use the slow logging at all? I see that RDS Performance Insights provides a "Top SQL" tab which includes an "Avg latency (ms)/call" measurement. I presume that this is the data I want to monitor. Can I setup a Cloudwatch alert based on that?

2 Answers
0
Accepted Answer

Hi, to achieve your goal, you have to create a metric filter from the CloudWatch log events corresponding to the slow queries. You will be then able to extract the value corresponding to the duration of your query from the log event message. This duration will be added to your cloudwatch metric tracking those events. You can then fire a CloudWatch alarm when the average is above your target.

In you case, you may want to in fact distinguish your different queries in different metrics to have more granular tracking.

Links:

  1. Amazon RDS : turn on query logging for PostgreSQL https://repost.aws/knowledge-center/rds-postgresql-query-logging
  2. Creating metrics from log events using filters: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/MonitoringLogData.html
  3. Filter and pattern syntax to extract value from message: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html#extract-log-event-values
  4. https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html

Best,

Didier

EXPERT
answered 3 years ago
  • Hello, i have the same question for postgresql, and i did all the workaround, but I can't capture the queries longer than 1000ms.

    This is my not working filter [timestamp, connection_id, user, dbname, pid, log_type, duration>=1000 ms]

    But when runing with * the filter works perfectly, so, I guess something is wrong with my filter

    This one works, but with all queries [timestamp, connection_id, user, dbname, pid, log_type, duration=* ms]

-1
EXPERT
answered 3 years ago
  • None of these notifications are related to query metrics.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.