0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · 课时

诊断膨胀与清理策略

了解 MVCC 如何造成表和索引膨胀、如何测量膨胀程度,以及如何调节自动清理以保持高性能。

诊断膨胀与清理策略 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced PostgreSQL: Indexing, Partitioning, Replication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

MVCC and Dead Tuples

PostgreSQL uses MVCC: updates and deletes leave behind old row versions called dead tuples. Until they are cleaned up, they occupy space and slow scans. This wasted space is bloat.

What VACUUM Does

VACUUM reclaims dead tuples for reuse and updates visibility information. It usually does not return space to the OS; VACUUM FULL does but rewrites the whole table and takes a strong lock.

Measuring Bloat

Inspect dead tuple counts per table from the statistics view.

SELECT relname, n_live_tup, n_dead_tup,
       last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC;

Autovacuum Basics

Autovacuum runs in the background, triggering when dead tuples exceed a threshold based on table size and the scale factor setting.

-- trigger ~ threshold + scale_factor * n_live_tup
autovacuum_vacuum_scale_factor = 0.2

Tuning Hot Tables

For large, frequently updated tables, the default 20% scale factor is too lazy. Lower it per table so vacuum runs more often on less garbage.

ALTER TABLE orders SET (
  autovacuum_vacuum_scale_factor = 0.02);

Vacuum Throttling

Autovacuum throttles itself with cost limits to avoid I/O storms. On modern hardware you can raise autovacuum_vacuum_cost_limit so vacuum finishes faster.

autovacuum_vacuum_cost_limit = 2000

Transaction ID Wraparound

VACUUM also prevents transaction ID wraparound, a catastrophic condition. Aggressive anti-wraparound vacuums are non-negotiable and cannot be skipped.

SELECT datname, age(datfrozenxid)
FROM pg_database ORDER BY 2 DESC;

Index Bloat

Indexes bloat too. REINDEX CONCURRENTLY rebuilds an index without blocking writes, restoring its compactness.

REINDEX INDEX CONCURRENTLY orders_pkey;

HOT Updates

Heap-Only Tuple updates avoid index churn when no indexed column changes. Leaving some free space via a lower fillfactor helps HOT updates and reduces bloat.

ALTER TABLE orders SET (fillfactor = 90);

VACUUM vs ANALYZE

VACUUM reclaims space; ANALYZE refreshes the planner statistics. Autovacuum does both, but after big bulk loads run ANALYZE manually for fresh plans.

ANALYZE orders;

A Monitoring Habit

Alert on rising n_dead_tup, growing table size with stable row counts, and high age(datfrozenxid). These early signals let you tune before queries slow down.

Quick Check

A large, hot table keeps growing despite stable row counts. What is the likely cause and fix?

Recap

You learned to diagnose bloat from MVCC dead tuples, measure it with pg_stat_user_tables, tune autovacuum per table, guard against ID wraparound, and use REINDEX CONCURRENTLY and fillfactor to keep performance high.

常见问题解答

「诊断膨胀与清理策略」课时是免费的吗?

是的 — 「诊断膨胀与清理策略」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程的其余内容,请升级到 CoddyKit PRO。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。

「诊断膨胀与清理策略」这节课中我会学到什么?

了解 MVCC 如何造成表和索引膨胀、如何测量膨胀程度,以及如何调节自动清理以保持高性能。 你通过在浏览器中直接运行的动手代码来练习 Advanced PostgreSQL: Indexing, Partitioning, Replication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Advanced PostgreSQL: Indexing, Partitioning, Replication 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「诊断膨胀与清理策略」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课中编写并运行代码吗?

能。每节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 全面性能调优
  2. 高级监控与告警
  3. PostgreSQL 的未来趋势
  4. 诊断膨胀与清理策略
← 返回 Advanced PostgreSQL: Indexing, Partitioning, Replication