

# 监控 Aurora PostgreSQL 的查询执行计划和峰值内存
<a name="AuroraPostgreSQL.Monitoring.Query.Plans"></a>

您可以监控 Aurora PostgreSQL 数据库实例中的查询执行计划，以检测导致当前数据库负载的执行计划，并使用 `aurora_compute_plan_id` 参数跟踪执行计划随时间推移的性能统计数据。每当执行查询时，都会为查询使用的执行计划分配一个标识符，同一计划的后续执行将使用相同的标识符。

在 Aurora PostgreSQL 14.10、15.5 及更高版本中，数据库参数组中 `aurora_compute_plan_id` 默认为 `OFF`。要分配计划标识符，请在参数组中将 `aurora_compute_plan_id` 设置为 `ON`。

此计划标识符用于多个不同用途的实用程序中。

可以监控数据库实例中的查询峰值内存使用量，来检测导致以下版本中数据库内存使用量高的查询：
+ 16.3 及所有更高版本
+ 15.7 及更高版本
+ 14.12 及更高版本

每当查询运行时，都会跟踪查询使用的峰值内存。查询通常会运行多次；可以查看每个查询跨所有运行的平均、最小和最大内存使用量值。

**Topics**
+ [使用 Aurora 函数访问查询执行计划和峰值内存](#AuroraPostgreSQL.Monitoring.Query.Plans.Functions)
+ [Aurora PostgreSQL 查询执行计划的参数参考](#AuroraPostgreSQL.Monitoring.Query.Plans.Parameters)

## 使用 Aurora 函数访问查询执行计划和峰值内存
<a name="AuroraPostgreSQL.Monitoring.Query.Plans.Functions"></a>

使用 `aurora_compute_plan_id`，您可以使用以下函数访问执行计划：
+ aurora\$1stat\$1activity
+ aurora\$1stat\$1plans

查询峰值内存不包括在查询处理开始之前分配的内存。在每个查询的计划和执行阶段，单独跟踪和报告峰值内存使用量。

![\[\]](http://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/images/aurora-apg-peakmemory.png)


可以使用以下函数访问查询峰值内存统计数据：
+ aurora\$1stat\$1statements
+ aurora\$1stat\$1plans

有关这些函数的更多信息，请参阅[Aurora PostgreSQL 函数参考‏‏‏‏‏‏](Appendix.AuroraPostgreSQL.Functions.md)。

## Aurora PostgreSQL 查询执行计划的参数参考
<a name="AuroraPostgreSQL.Monitoring.Query.Plans.Parameters"></a>

您可以使用数据库参数组中的以下参数监控查询执行计划。

**Topics**
+ [aurora\$1compute\$1plan\$1id](#aurora.compute_plan_id)
+ [aurora\$1stat\$1plans.minutes\$1until\$1recapture](#aurora.minutes_until_recapture)
+ [aurora\$1stat\$1plans.calls\$1until\$1recapture](#aurora.calls_until_recapture)
+ [aurora\$1stat\$1plans.with\$1costs](#aurora.with_costs)
+ [aurora\$1stat\$1plans.with\$1analyze](#aurora.with_analyze)
+ [aurora\$1stat\$1plans.with\$1timing](#aurora.with_timing)
+ [aurora\$1stat\$1plans.with\$1buffers](#aurora.with_buffers)
+ [aurora\$1stat\$1plans.with\$1wal](#aurora.with_wal)
+ [aurora\$1stat\$1plans.with\$1triggers](#aurora.with_triggers)

**注意**  
`aurora_stat_plans.with_*` 参数的配置仅对新捕获的计划生效。

### aurora\$1compute\$1plan\$1id
<a name="aurora.compute_plan_id"></a>

`aurora_compute_plan_id` 是一个配置参数，用于控制在查询执行期间是否分配计划标识符。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Monitoring.Query.Plans.html)

### aurora\$1stat\$1plans.minutes\$1until\$1recapture
<a name="aurora.minutes_until_recapture"></a>

重新捕获计划之前要经过的分钟数。默认值为 0，表示将禁用重新捕获计划。如果超过了 `aurora_stat_plans.calls_until_recapture` 阈值，仍然可以重新捕获该计划。


| 默认值 | 允许值 | 说明 | 
| --- | --- | --- | 
| 0 | 0-1073741823 | 设置重新捕获计划之前要经过的分钟数。 | 

### aurora\$1stat\$1plans.calls\$1until\$1recapture
<a name="aurora.calls_until_recapture"></a>

重新捕获计划之前调用该计划的次数。默认值为 0，表示在多次调用后将禁用重新捕获计划。如果超过了 `aurora_stat_plans.minutes_until_recapture` 阈值，仍然可以重新捕获该计划。


| 默认值 | 允许值 | 说明 | 
| --- | --- | --- | 
| 0 | 0-1073741823 | 设置重新捕获计划之前的调用次数。 | 

### aurora\$1stat\$1plans.with\$1costs
<a name="aurora.with_costs"></a>

捕获 EXPLAIN 计划时包含估计成本。允许的值是 `on` 和 `off`。默认值为 `on`。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Monitoring.Query.Plans.html)

### aurora\$1stat\$1plans.with\$1analyze
<a name="aurora.with_analyze"></a>

使用 ANALYZE 控制 EXPLAIN 计划。此模式仅在第一次捕获计划时使用。允许的值是 `on` 和 `off`。默认值为 `off`。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Monitoring.Query.Plans.html)

### aurora\$1stat\$1plans.with\$1timing
<a name="aurora.with_timing"></a>

使用 ANALYZE 时，将在解释中捕获计划时间。默认值为 `on`。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Monitoring.Query.Plans.html)

### aurora\$1stat\$1plans.with\$1buffers
<a name="aurora.with_buffers"></a>

使用 ANALYZE 时，将在解释中捕获计划缓冲区的使用情况。默认值为 `off`。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Monitoring.Query.Plans.html)

### aurora\$1stat\$1plans.with\$1wal
<a name="aurora.with_wal"></a>

使用 ANALYZE 时，将在解释中捕获计划 wal 的使用情况。默认值为 `off`。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Monitoring.Query.Plans.html)

### aurora\$1stat\$1plans.with\$1triggers
<a name="aurora.with_triggers"></a>

使用 `ANALYZE` 时，将在解释中捕获计划触发器执行统计信息。默认值为 `off`。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Monitoring.Query.Plans.html)