防止事务 ID 回绕
理解 PostgreSQL 的 32 位事务 ID 如何发生回绕,了解为什么积极执行清理可以防止回绕,以及如何监控并避免可怕的回绕停机
防止事务 ID 回绕 是 CoddyKit 上的免费 PostgreSQL Performance & Query Optimization 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 PostgreSQL Performance & Query Optimization 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 PostgreSQL Performance & Query Optimization 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Transaction ID Wraparound?
PostgreSQL labels every row version with the transaction ID (XID) that created it. XIDs are 32-bit, so there are only about 4 billion of them. They are compared in a circular fashion, and if old rows are not frozen, the comparison can break.
Why It is Dangerous
If XIDs wrap before old rows are frozen, recent rows could appear to be in the future and become invisible. To protect your data, PostgreSQL will refuse new writes before that happens.
Freezing Rows
VACUUM marks very old, still-visible rows as frozen, meaning they are visible to all transactions forever. Frozen rows no longer depend on their original XID, so they are safe from wraparound.
The vacuum_freeze_min_age Setting
This controls how old a row's XID must be before VACUUM freezes it. Lower values freeze sooner; higher values defer work but increase wraparound risk.
SHOW vacuum_freeze_min_age;Autovacuum to the Rescue
When a table's oldest XID exceeds autovacuum_freeze_max_age, autovacuum triggers an anti-wraparound vacuum automatically, even if the table is otherwise idle.
SHOW autovacuum_freeze_max_age;Monitoring Database Age
Check how close each database is to wraparound by reading the age of its oldest unfrozen XID.
SELECT datname, age(datfrozenxid)
FROM pg_database
ORDER BY age(datfrozenxid) DESC;Monitoring Per-Table Age
Drill down to find the specific tables driving the age up. The one with the highest age is the next anti-wraparound target.
SELECT relname, age(relfrozenxid)
FROM pg_class
WHERE relkind = 'r'
ORDER BY age(relfrozenxid) DESC
LIMIT 10;The Warning Signs
The server log warns as you approach the limit:
- database must be vacuumed within N transactions
- Eventually the database goes read-only to protect itself
Never ignore these messages.
Manual Freeze
If a table is far behind, run a vacuum that freezes everything immediately rather than waiting for autovacuum.
VACUUM (FREEZE, VERBOSE) big_table;Best Practices
To stay safe:
- Keep autovacuum enabled and well-tuned
- Avoid extremely long-running transactions that pin the oldest XID
- Monitor
age(datfrozenxid)with alerts - Investigate any table that resists freezing
Estimating Time Until Trouble
You can roughly gauge headroom by comparing the oldest XID age against the ~2 billion safe limit. If a database is consistently climbing toward it, investigate what blocks freezing before alerts fire.
SELECT datname,
2000000000 - age(datfrozenxid) AS xids_left
FROM pg_database
ORDER BY xids_left ASC;Quick Check
Test your wraparound knowledge.
Recap
You learned wraparound prevention:
- 32-bit XIDs can wrap after ~4 billion transactions
- VACUUM freezes old rows to make them permanently visible
- Autovacuum runs anti-wraparound vacuums automatically
- Monitor
age(datfrozenxid)at database and table level - Avoid long transactions and heed the log warnings
用 AI 导师学习 SQL — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 22
- 课程
- 88
常见问题解答
「防止事务 ID 回绕」课时是免费的吗?
是的 — 「防止事务 ID 回绕」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 PostgreSQL Performance & Query Optimization 课程的其余内容,请升级到 CoddyKit PRO。 PostgreSQL Performance & Query Optimization 课程共包含 4 节课。
「防止事务 ID 回绕」这节课中我会学到什么?
理解 PostgreSQL 的 32 位事务 ID 如何发生回绕,了解为什么积极执行清理可以防止回绕,以及如何监控并避免可怕的回绕停机 你通过在浏览器中直接运行的动手代码来练习 PostgreSQL Performance & Query Optimization,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 PostgreSQL Performance & Query Optimization 需要有经验吗?
无需任何先前经验。CoddyKit 上的 PostgreSQL Performance & Query Optimization 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「防止事务 ID 回绕」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 PostgreSQL Performance & Query Optimization 课中编写并运行代码吗?
能。每节 PostgreSQL Performance & Query Optimization 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 了解 MVCC 与 VACUUM
- 自动清理配置与调优
- 事务隔离级别的影响
- 防止事务 ID 回绕