AWS Database Blog
Monitor query plans for Amazon Aurora PostgreSQL
As we continue to innovate and add new features on behalf of our customers, you can now maintain and manage query plans in Amazon Aurora PostgreSQL-Compatible Edition. This feature enables you to identify the query plans contributing to the current database load and track the performance statistics of query plans over time.
In this post, we demonstrate how you can monitor query plans to maintain optimal database performance, and discuss some key use cases of the monitoring query plan feature.
Efficient query planning is a crucial part of database performance, so we have added features such as Amazon RDS Performance Insights support for Amazon Aurora PostgreSQL, which provides database monitoring capabilities, and Aurora PostgreSQL query plan management, which provides plan stability and plan adaptability to maintain optimal database performance.
Solution overview
Monitoring query plans is currently available with Amazon Aurora PostgreSQL version 14.10, 15.5, and higher versions. On the supported versions, this feature is disabled by default and is controlled by the parameter aurora_compute_plan_id with a default value of 0 (OFF). You can enable it by setting aurora_compute_plan_id
to 1 (ON) in the parameter group. Additionally, pg_stat_statements must be in shared_preload_libraries
for the plans to be tracked.
Before we explore the new views, it’s important to understand what a plan identifier is. Separate from a query identifier, a plan identifier is made up of the sql_hash
and a hash of the plan. This provides unique insights into how a query is run.
You can use the following functions to access and monitor query plans:
- aurora_stat_activity – Shows information related to the current activity of a process, including a plan identifier of the running query
- aurora_stat_plans – Returns query plans and other important statistics for the query identifier
For a comprehensive understanding of these functions and their usage, refer to Aurora PostgreSQL functions reference.
You can monitor the query plans using the different parameters in a DB parameter group. For more information, see Parameter reference for Aurora PostgreSQL query execution plans.
Prerequisites
To follow along with this post, complete the following prerequisites:
- Create an Aurora PostgreSQL cluster if you don’t already have one. For instructions, refer to Create an Aurora PostgreSQL DB cluster.
- Create an Amazon Elastic Compute Cloud (Amazon EC2) instance to install the PostgreSQL client to access the Aurora PostgreSQL instance. For instructions, refer to Create your EC2 resources and launch your EC2 instance. Or you can set up connectivity between your RDS database and EC2 compute instance in one click.
- On Amazon Linux 2023, use the following command to download the psql command line tool to run all the SQL statements:
Monitor query plans
Let’s dive deeper into the process of monitoring query plans with Aurora PostgreSQL. The following is a comprehensive, step-by-step guide to help you understand it better:
- Make sure
pg_stat_statements
is inshared_preload_libraries
for the plans to be tracked. To verify thatpg_stat_statements
is initialized, run the following command:If
pg_stat_statements
is inshared_preload_libraries
, the command will return the following: - Check that the
aurora_compute_plan_id
parameter setting is set toON
, which isOFF
the default. You can see it in your cluster parameter group:The result should display on in the output:
- Verify that
aurora_stat_plans.with_costs
andaurora_stat_plans.with_timing
parameters are set toON
with the following query: - Turn on the required
aurora_stat_plans.with*
parameters in your cluster parameter group. - Use pgbench to seed some sample data. For this post, initialize
pgbench
with a scale factor 10. You can use following command to createpgbench
tables and load the data: - Run a TCP-B test with
pgbench
for 120 seconds in 64 parallel sessions: - During the
pgbench
run, checkaurora_stat_activity
in another session using the following query:It gives the following result:
You can see the session count of each query and
plan_id
assigned to the queries. This information is helpful to find the query plans for currently running queries. - Use the
aurora_stat_plans
function for the sameplan_id
to check the explain plan:It will show the query plan with the number of times the query was run, total runtime of the query for all runs, and minimum and maximum runtime of the query.
Use case 1: Integration with QPM auto capture
In most scenarios, the Query Plan Management (QPM) feature is set up for automatic plan capture, which captures plans for all statements run more than one time. However, it’s also possible to manually specify a particular set of statements for plan capture. This requires setting capture_plan_baselines
to off
in the cluster parameter group, and setting it to manual in the session where the plans will be manually captured in. If QPM identifies a different plan due to changes, this plan must be approved for use. After approval, the new plan is automatically incorporated into aurora_stat_plans
. To test this use case, complete the following steps:
- Open the custom DB cluster parameter group and set the
rds.enable_plan_management
parameter to1
. - Create the
apg_plan_mgmt
extension for your DB instance. Refer to Turning on Aurora PostgreSQL query plan management for more information. - Create a simple table and insert some data:
- Check the query plan and verify that it goes to Seq Scan:
- Verify the
apg_plan_mgmt.dba_plans
view to check for any approved plans: - Set the following parameters to capture and use the plans:
- Run the query and verify the plan is captured:
- Check
aurora_stat_plans
if the plan is picked up: - Create an index to speed up the query:
- Run the query again and verify if the new plan is captured and in
Unapproved
state: - Check that
aurora_stat_plans
is still using theApproved
plan: - Approve the new plan and reject the old plan:
- Check if
aurora_stat_plans
picked up the new approved plan:
Use case 2: Integration with QPM manual capture
Sometimes, the query optimizer doesn’t generate the best query plan. Although it’s possible to use query hints in your application code to work around this, it’s often discouraged due to maintenance issues. Instead, you can use the pg_hint_plan
extension in PostgreSQL can be used. This saves directives like “scan method,” “join method,” and “join order” in the QPM, allowing the optimizer to enforce the desired plan without hints in the application code.
In this section, we demonstrate how to use the aurora_stat_activity
and aurora_stat_plans
views effectively. We explore how to identify the plans of current queries and cross-reference them with the QPM to find potentially optimized plans.
- Create the following tables and insert data:
- Create the
pgbench_join_simple_query.sql
file using the following command: - Run the following
pgbench
command: - Check the
aurora_stat_plans
andaurora_stat_activity
views for query runs and plans: - Check the approved plan in
dba_plans
for this query; it should match withaurora_stat_plans
andaurora_stat_activity
: - Check the query plan and use
pg_hint_plan
to use hints to generate a better plan: - Enable the
pg_hint_plan.enable_hint
parameter using the following query and check the plan using the hints: - Use
EXPLAIN
for the query with the hints you want to use. In the following example, use theNestLoop (a, b)
hint, which is a directive for the optimizer to use a nested loop to join table a and table b:The output shows that there is another plan that is cost-effective.
- Verify that plan
1283399430
was captured and note the status of the plan: - Approve the new plan and reject the old plan:
- Run the following query and check that
aurora_stat_plans
picked up the plan: - Run
pgbench
again and verify inaurora_stat_plans
if the new plan is picked up:
You can see the calls count increase for the new plan with ID 1283399430
.
Clean up
Complete the following steps after your tests to prevent unnecessary expenses:
Summary
In this post, we showed you how to monitor query plans in Amazon Aurora PostgreSQL with the new query monitoring feature. This feature provides valuable insights and optimizations, promoting improved database performance. It allows users to analyze query plans in real-time, swiftly pinpointing and addressing performance issues. This proactive strategy reduces latency, boosts resource usage, and minimizes downtime, thereby enhancing productivity.
If you have any questions or suggestions, leave a comment.
About the authors
Sameer Malik is a Principal Database Solutions Architect with Amazon Web Services.
Baji Shaik is a Sr. Lead Consultant with AWS Professional Services, Global Competency Centre. His background spans a wide depth and breadth of expertise and experience in SQL and NoSQL database technologies. He is a Database Migration Expert and has developed many successful database solutions addressing challenging business requirements for moving databases from on premises to Amazon RDS and Amazon Aurora PostgreSQL/MySQL. He is an eminent author, having written several books on PostgreSQL. A few of his recent works include “PostgreSQL Configuration,” “Beginning PostgreSQL on the Cloud,” and “PostgreSQL Development Essentials.” Furthermore, he has delivered several conference and workshop sessions.