Prévenir le bouclage des identifiants de transaction
Comprenez comment les identifiants de transaction PostgreSQL sur 32 bits peuvent revenir à leur valeur initiale, pourquoi un nettoyage agressif l’empêche et comment surveiller et éviter l’arrêt redouté dû à ce bouclage.
Prévenir le bouclage des identifiants de transaction est une leçon PostgreSQL Performance & Query Optimization gratuite sur CoddyKit. Ceci est la leçon 4 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage PostgreSQL Performance & Query Optimization, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours PostgreSQL Performance & Query Optimization comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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
Apprends SQL avec un tuteur IA — gratuit
Écris et exécute du vrai code dans ton navigateur, obtiens de l'aide instantanée d'un tuteur IA disponible 24h/24, et reprends là où tu t'es arrêté sur le web ou dans l'app.
- Cours
- 22
- Leçons
- 88
Questions Fréquemment Posées
La leçon « Prévenir le bouclage des identifiants de transaction » est-elle gratuite ?
Oui — le texte complet de « Prévenir le bouclage des identifiants de transaction » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours PostgreSQL Performance & Query Optimization, passe à CoddyKit PRO. Le cours PostgreSQL Performance & Query Optimization comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Prévenir le bouclage des identifiants de transaction » ?
Comprenez comment les identifiants de transaction PostgreSQL sur 32 bits peuvent revenir à leur valeur initiale, pourquoi un nettoyage agressif l’empêche et comment surveiller et éviter l’arrêt redou… Tu pratiques PostgreSQL Performance & Query Optimization avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer PostgreSQL Performance & Query Optimization ?
Aucune expérience préalable n'est requise. PostgreSQL Performance & Query Optimization sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 4 sur 4.
Combien de temps prend la leçon « Prévenir le bouclage des identifiants de transaction » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon PostgreSQL Performance & Query Optimization ?
Oui. Chaque leçon PostgreSQL Performance & Query Optimization inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Comprendre MVCC et VACUUM
- Configuration et réglage de l’autovacuum
- Impact des niveaux d’isolation des transactions
- Prévenir le bouclage des identifiants de transaction