postgres-tuning
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePostgreSQL Tuning
PostgreSQL 性能调优
Overview
概述
Optimizes PostgreSQL 17/18+ performance across I/O, query execution, indexing, and maintenance. Covers the native AIO subsystem introduced in PostgreSQL 18 for throughput gains on modern storage, forensic query plan analysis with EXPLAIN BUFFERS (auto-included in PG18), B-tree skip scans for composite indexes, native UUIDv7 generation, and autovacuum tuning for high-churn tables.
When to use: Diagnosing slow queries, configuring async I/O, tuning shared_buffers and work_mem, optimizing indexes for write-heavy workloads, managing table bloat, pgvector HNSW tuning.
When NOT to use: Schema design (use a data modeling tool), application-level caching strategy, database selection decisions, ORM query generation.
Key monitoring views:
- — identifies slow query patterns by cumulative execution time
pg_stat_statements - — granular I/O analysis by backend type, object, and context (PG16+)
pg_stat_io - — checkpoint frequency and timing (PG17+; previously in
pg_stat_checkpointer)pg_stat_bgwriter - — dead tuple counts for bloat detection and autovacuum monitoring
pg_stat_user_tables - — buffer cache hit ratios per table
pg_statio_user_tables - — in-progress AIO operations (PG18+)
pg_aios
针对I/O、查询执行、索引和维护环节优化PostgreSQL 17/18+的性能。涵盖PostgreSQL 18中引入的原生AIO子系统(可提升现代存储的吞吐量)、使用EXPLAIN BUFFERS进行查询计划深度分析(PG18中自动包含该功能)、复合索引的B-tree跳扫、原生UUIDv7生成,以及针对高变动率表的autovacuum调优。
适用场景: 诊断慢查询、配置异步I/O、调优shared_buffers和work_mem、优化写密集型工作负载的索引、管理表膨胀、pgvector HNSW调优。
不适用场景: schema设计(请使用数据建模工具)、应用级缓存策略、数据库选型决策、ORM查询生成。
核心监控视图:
- — 按累计执行时间识别慢查询模式
pg_stat_statements - — 按后端类型、对象和上下文进行细粒度I/O分析(PG16+)
pg_stat_io - — 检查点频率与计时(PG17+;原属于
pg_stat_checkpointer)pg_stat_bgwriter - — 用于膨胀检测和autovacuum监控的死元组计数
pg_stat_user_tables - — 各表的缓冲区缓存命中率
pg_statio_user_tables - — 进行中的AIO操作(PG18+)
pg_aios
Quick Reference
快速参考
| Pattern | Configuration / Query | Key Points |
|---|---|---|
| Async I/O | | PG18 default is |
| I/O concurrency | | |
| Forensic EXPLAIN | | PG18 auto-includes BUFFERS with ANALYZE; target Shared Hit > 95% |
| UUIDv7 primary keys | | PG18 built-in; time-ordered, monotonic within a session; RFC 9562 compliant |
| B-tree skip scan | Composite index on | PG18 skips leading column; works best with low-cardinality prefix and equality on trailing columns |
| Aggressive autovacuum | | Triggers at 1% row change instead of default 20% |
| Shared buffers | Start at 25% of RAM | Do not exceed 40% without benchmarking |
| work_mem tuning | | Prevents sort spills to disk; allocated per operator, not per query |
| BRIN index | | 100x smaller than B-tree for physically ordered time-series data |
| HNSW vector index | | Tune |
| GIN index | | JSONB containment, full-text search, array operators; slower writes |
| Checkpoint tuning | | Spread writes over 90% of timeout window to avoid I/O storms |
| WAL compression | | Available since PG15; reduces WAL I/O 50-70% for write-heavy workloads |
| Bloat detection | | Reindex concurrently if bloat > 30% |
| I/O monitoring | | Watch |
| Checkpoint monitoring | | PG17+ moved checkpoint stats out of |
| 模式 | 配置/查询语句 | 关键要点 |
|---|---|---|
| 异步I/O | | PG18默认值为 |
| I/O并发度 | | |
| 深度查询计划分析 | | PG18中使用ANALYZE时自动包含BUFFERS信息;目标Shared Hit需>95% |
| UUIDv7主键 | | PG18内置功能;按时间排序,会话内保持单调;符合RFC 9562标准 |
| B-tree跳扫 | 基于 | PG18支持跳过前导列;在前缀列基数低且后续列使用等值条件时效果最佳 |
| 激进式自动清理 | | 当行变更率达1%时触发,替代默认的20% |
| 共享缓冲区 | 初始设置为内存的25% | 未经基准测试不要超过40% |
| work_mem调优 | 按会话设置 | 避免排序操作溢出到磁盘;该内存按运算符分配,而非按查询分配 |
| BRIN索引 | | 对于物理有序的时序数据,比B-tree索引小100倍 |
| HNSW向量索引 | | 调优 |
| GIN索引 | | 支持JSONB包含查询、全文搜索、数组操作;会降低写入速度 |
| 检查点调优 | | 将写入操作分散到超时窗口的90%时间内,避免I/O风暴 |
| WAL压缩 | | 从PG15开始可用;对于写密集型工作负载可减少50-70%的WAL I/O |
| 膨胀检测 | | 当膨胀率>30%时,执行并发重索引操作 |
| I/O监控 | | 关注 |
| 检查点监控 | | PG17+中检查点统计信息从 |
Key Version Changes
版本核心变更
PostgreSQL 18:
- Native async I/O via parameter (reads only; writes remain synchronous)
io_method - Built-in function with monotonic ordering within a session (RFC 9562)
uuidv7() - alias for
uuidv4()andgen_random_uuid()for UUIDv7uuid_extract_timestamp() - B-tree skip scan for composite indexes (equality on trailing columns, low-cardinality prefix)
- EXPLAIN ANALYZE auto-includes buffer statistics without specifying BUFFERS
- gains byte-level columns (
pg_stat_io,read_bytes,write_bytes);extend_bytesremovedop_bytes - default changed from 1 to 16
effective_io_concurrency - AIO monitoring via system view for in-progress I/O operations
pg_aios
PostgreSQL 17:
- Checkpoint statistics moved from to
pg_stat_bgwriterpg_stat_checkpointer - Column renames: to
checkpoints_timed,num_timedtocheckpoints_reqnum_requested - and
buffers_backendremoved frombuffers_backend_fsync(now inpg_stat_bgwriter)pg_stat_io
PostgreSQL 15:
- expanded from boolean to support
wal_compression,pglz, andlz4algorithmszstd
PostgreSQL 18:
- 通过参数支持原生异步I/O(仅覆盖读操作;写操作仍为同步)
io_method - 内置函数,会话内保持单调排序(符合RFC 9562)
uuidv7() - 为添加
gen_random_uuid()别名,新增uuidv4()用于UUIDv7uuid_extract_timestamp() - 复合索引支持B-tree跳扫(后续列使用等值条件,前缀列基数低)
- EXPLAIN ANALYZE自动包含缓冲区统计信息,无需手动指定BUFFERS
- 新增字节级列(
pg_stat_io、read_bytes、write_bytes);移除extend_bytesop_bytes - 默认值从1改为16
effective_io_concurrency - 新增系统视图用于监控进行中的AIO操作
pg_aios
PostgreSQL 17:
- 检查点统计信息从迁移至
pg_stat_bgwriterpg_stat_checkpointer - 列名变更:改为
checkpoints_timed,num_timed改为checkpoints_reqnum_requested - 中移除
pg_stat_bgwriter和buffers_backend(现位于buffers_backend_fsync)pg_stat_io
PostgreSQL 15:
- 从布尔类型扩展为支持
wal_compression、pglz和lz4算法zstd
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Using | PG18 provides built-in |
Using | The correct PG18 parameter is |
Querying | Checkpoint stats moved to |
| Using SELECT * in high-frequency queries | Select only needed columns to reduce I/O and improve cache hit ratios |
| Ignoring sequential scans on tables over 10k rows | Add targeted indexes on columns used in WHERE, ORDER BY, and JOIN clauses |
| Setting shared_buffers above 40% of RAM without testing | Start at 25% and benchmark; excessive allocation causes OS page cache contention |
| Leaving autovacuum at default settings for high-churn tables | Tune |
| Over-indexing columns rarely used in queries | Every extra index slows UPDATE/INSERT and prevents HOT (Heap Only Tuple) updates |
| Expecting B-tree skip scan to work with range predicates | PG18 skip scan only works with equality operators on trailing columns |
| Ignoring "External Merge Disk" in query plans | Increase work_mem for specific sessions; it indicates sort spills to disk |
Setting | PostgreSQL must be built with |
| Assuming PG18 AIO accelerates writes | AIO in PG18 only covers reads (seq scans, bitmap heap scans, VACUUM); writes remain synchronous |
| 错误操作 | 正确做法 |
|---|---|
使用 | PG18提供内置 |
将 | PG18中正确的参数是 |
在PG17+版本中查询 | PG17中检查点统计信息已迁移至 |
| 在高频查询中使用SELECT * | 仅选择所需列,以减少I/O并提升缓存命中率 |
| 忽略超过1万行的表上的顺序扫描 | 为WHERE、ORDER BY和JOIN子句中使用的列添加针对性索引 |
| 未经测试将shared_buffers设置为超过内存的40% | 初始设置为25%并进行基准测试;分配过多会导致操作系统页缓存竞争 |
| 高变动率表保留autovacuum默认设置 | 对于频繁执行UPDATE/DELETE的表,将 |
| 为很少在查询中使用的列过度索引 | 额外的索引会减慢UPDATE/INSERT操作,且会阻止HOT(仅堆元组)更新 |
| 期望B-tree跳扫支持范围谓词 | PG18的跳扫仅支持后续列使用等值运算符 |
| 忽略查询计划中的"External Merge Disk" | 为特定会话提高work_mem;该标识表示排序操作已溢出到磁盘 |
未验证编译标志就设置 | PostgreSQL必须使用 |
| 认为PG18的AIO可加速写操作 | PG18中的AIO仅覆盖读操作(顺序扫描、位图堆扫描、VACUUM);写操作仍为同步 |
Tuning Workflow
调优流程
- Identify slow queries from (sort by
pg_stat_statements)total_exec_time - Analyze execution plans with
EXPLAIN (ANALYZE, BUFFERS, SETTINGS) - Check buffer hit ratios via (target > 99%)
pg_statio_user_tables - Monitor I/O patterns via (watch evictions and disk reads)
pg_stat_io - Optimize with targeted indexes, work_mem adjustments, or query rewrites
- Verify improvements by re-running EXPLAIN and comparing costs
- Maintain with aggressive autovacuum settings for high-churn tables
- 识别:从中找出慢查询(按
pg_stat_statements排序)total_exec_time - 分析:使用分析执行计划
EXPLAIN (ANALYZE, BUFFERS, SETTINGS) - 检查:通过查看缓冲区命中率(目标>99%)
pg_statio_user_tables - 监控:通过监控I/O模式(关注缓存驱逐和磁盘读操作)
pg_stat_io - 优化:采用针对性索引、调整work_mem或重写查询
- 验证:重新运行EXPLAIN并对比成本,确认优化效果
- 维护:为高变动率表配置激进的autovacuum设置
Delegation
任务分工
- Discover slow queries and I/O bottlenecks: Use agent to analyze pg_stat_statements, pg_stat_io, and slow query logs
Explore - Execute query plan analysis and index optimization: Use agent to run EXPLAIN ANALYZE, create indexes, and verify performance improvements
Task - Design database scaling and partitioning strategy: Use agent to architect sharding, partitioning, and replication topology
Plan
- 发现慢查询与I/O瓶颈:使用Explore工具分析pg_stat_statements、pg_stat_io和慢查询日志
- 执行查询计划分析与索引优化:使用Task工具运行EXPLAIN ANALYZE、创建索引并验证性能提升
- 设计数据库扩容与分区策略:使用Plan工具设计分片、分区和复制拓扑
References
参考资料
- Async I/O configuration and storage tuning
- Query plan analysis and operator forensics
- Indexing strategies and bloat management
- 异步I/O配置与存储调优
- 查询计划分析与算子深度解析
- 索引策略与膨胀管理