RDS PostgreSQL从9.6升级至11后查询高IOPS与CPU占用异常排查
RDS PostgreSQL 9.6升级11版本后查询性能劣化问题
问题背景
- 环境变更:RDS托管PostgreSQL集群从9.6版本升级至11版本
- 变更前后业务数据集完全一致
- 变更后相关业务查询出现读IOPS、CPU占用飙升的性能劣化,初期无法定位根因
- 核心疑问:该类性能问题是否可能由索引损坏导致?
问题查询与旧索引执行计划
目标查询SQL
EXPLAIN (ANALYZE TRUE, VERBOSE TRUE, COSTS TRUE, BUFFERS TRUE, TIMING TRUE) SELECT consumertr0_.ref_id AS col_0_0_ FROM consumer_transactions consumertr0_ WHERE (consumertr0_.remaining_amount IS NOT NULL) AND (consumertr0_.expiry_time IS NOT NULL) AND consumertr0_.expiry_time > '2020-12-15T00:00:00' AND consumertr0_.expiry_time < NOW() AND consumertr0_.remaining_amount > 0 ORDER BY consumertr0_.expiry_time ASC LIMIT 20000;
旧索引下的实际执行结果
Limit (cost=0.57..80391.67 rows=20000 width=24) (actual time=191716.213..192077.229 rows=20000 loops=1) Output: ref_id, expiry_time Buffers: shared hit=9481343 read=1566736 I/O Timings: read=97.486 -> Index Scan using consumer_transactions_expiry_time_remaining_amount on public.consumer_transactions consumertr0_ (cost=0.57..1109723.40 rows=276081 width=24) (actual time=191716.211..192075.241 rows=20000 loops=1) Output: ref_id, expiry_time Index Cond: ((consumertr0_.expiry_time > '2020-12-15 00:00:00'::timestamp without time zone) AND (consumertr0_.expiry_time < now())) Buffers: shared hit=9481343 read=1566736 I/O Timings: read=97.486 Planning Time: 1.525 ms Execution Time: 192078.720 ms (11 rows)
关联对象定义与统计信息
慢查询使用的旧索引定义
"consumer_transactions_expiry_time_remaining_amount" btree (expiry_time, remaining_amount) WHERE expiry_time IS NOT NULL AND remaining_amount IS NOT NULL AND remaining_amount > 0::numeric
目标表的统计信息更新记录
relname | last_analyze | last_autoanalyze consumer_transactions | 2021-01-24 22:00:03.144379+00 |
历史表现:升级前相同数据规模下,该查询仅需极低IO即可快速完成,未留存9.6版本下的执行计划。
临时验证方案与效果
- 操作:创建与旧索引定义规则完全一致、仅名称不同的新索引
consumer_transactions_expiry_time_remaining_amount2 - 效果:替换索引后查询性能恢复正常,后续待根因定位完成后将删除旧索引
新索引下的实际执行结果
EXPLAIN (ANALYZE TRUE, VERBOSE TRUE, COSTS TRUE, BUFFERS TRUE, TIMING TRUE) SELECT consumertr0_.ref_id AS col_0_0_ FROM consumer_transactions consumertr0_ WHERE (consumertr0_.remaining_amount IS NOT NULL) AND (consumertr0_.expiry_time IS NOT NULL) AND consumertr0_.expiry_time > '2019-07-01T00:00:00' AND consumertr0_.expiry_time < NOW() AND consumertr0_.remaining_amount > 0 ORDER BY consumertr0_.expiry_time ASC LIMIT 20000; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Limit (cost=0.57..73592.06 rows=20000 width=24) (actual time=0.048..18.307 rows=20000 loops=1) Output: ref_id, expiry_time Buffers: shared hit=11140 -> Index Scan using consumer_transactions_expiry_time_remaining_amount2 on public.consumer_transactions consumertr0_ (cost=0.57..22273478.26 rows=6053275 width=24) (actual time=0.047..16.119 rows=20000 loops=1) Output: ref_id, expiry_time Index Cond: ((consumertr0_.expiry_time > '2019-07-01 00:00:00'::timestamp without time zone) AND (consumertr0_.expiry_time < now())) Buffers: shared hit=11140 Planning Time: 1.160 ms Execution Time: 19.600 ms (9 rows)
观测到的异常点
- 旧索引执行计划中,索引扫描阶段的实际时间直接从191716.211ms开始统计,无法解释该时间点之前的耗时消耗去向
- 索引膨胀统计数据:
current_database | schemaname | tblname | idxname | real_size | extra_size | extra_ratio | fillfactor | bloat_size | bloat_ratio | is_na ------------------+------------+-----------------------+-----------------------------------------------------+-------------+-------------+-------------------+------------+-------------+---------------------+------- proddb | public | consumer_transactions | consumer_transactions_expiry_time_remaining_amount | 4748820480 | 3698360320 | 77.8795563145819 | 90 | 3583516672 | 75.4611947765185 | f proddb | public | consumer_transactions | consumer_transactions_expiry_time_remaining_amount2 | 1755013120 | 704552960 | 40.1451676896866 | 90 | 589709312 | 33.6014190024973 | f
内容的提问来源于Stack Exchange,提问作者Monika Yadav
相关产品推荐
相关产品推荐

