Advanced PostgreSQL: Indexing, Partitioning, Replication · レッスン

膨張の診断とVACUUM戦略

MVCCがテーブルやインデックスの膨張を引き起こす仕組み、膨張の測定方法、そして高い性能を維持するためのautovacuumの調整方法を理解します。

レッスン 4/413 ステップ

「膨張の診断とVACUUM戦略」はCoddyKit上の無料Advanced PostgreSQL: Indexing, Partitioning, Replicationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
11
レッスン
44

よくある質問

「膨張の診断とVACUUM戦略」レッスンは無料ですか?

はい。「膨張の診断とVACUUM戦略」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Advanced PostgreSQL: Indexing, Partitioning, Replicationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Advanced PostgreSQL: Indexing, Partitioning, Replicationコースには全4レッスンが含まれています。

「膨張の診断とVACUUM戦略」で何を学びますか?

MVCCがテーブルやインデックスの膨張を引き起こす仕組み、膨張の測定方法、そして高い性能を維持するためのautovacuumの調整方法を理解します。 ブラウザで直接実行するハンズオンコードでAdvanced PostgreSQL: Indexing, Partitioning, Replicationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Advanced PostgreSQL: Indexing, Partitioning, Replicationを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのAdvanced PostgreSQL: Indexing, Partitioning, Replicationは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「膨張の診断とVACUUM戦略」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このAdvanced PostgreSQL: Indexing, Partitioning, Replicationレッスンでコードを書いて実行できますか?

はい。すべてのAdvanced PostgreSQL: Indexing, Partitioning, Replicationレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 総合的なパフォーマンスチューニング
  2. 高度な監視とアラート
  3. PostgreSQLの今後の動向
  4. 膨張の診断とVACUUM戦略
← Advanced PostgreSQL: Indexing, Partitioning, Replicationに戻る