| PostgreSQL Reference Manual - Volume 3 - Server Administration Guide by The PostgreSQL Global Development Group Paperback (6"x9"), 204 pages ISBN 0954612043 RRP £13.95 ($24.95) Sales of this book support the PostgreSQL project! Get a printed copy>>> |
9.1.3 Preventing transaction ID wraparound failures
PostgreSQL's MVCC transaction semantics depend on being able to compare transaction ID (XID) numbers: a row version with an insertion XID greater than the current transaction's XID is “in the future” and should not be visible to the current transaction. But since transaction IDs have limited size (32 bits at this writing) a cluster that runs for a long time (more than 4 billion transactions) would suffer transaction ID wraparound: the XID counter wraps around to zero, and all of a sudden transactions that were in the past appear to be in the future--which means their outputs become invisible. In short, catastrophic data loss. (Actually the data is still there, but that's cold comfort if you can't get at it.) To avoid this, it is necessary to vacuum every table in every database at least once every two billion transactions.
The reason that periodic vacuuming solves the problem is that
PostgreSQL distinguishes a special XID
FrozenXID. This XID is always considered older
than every normal XID. Normal XIDs are
compared using modulo-231 arithmetic. This means
that for every normal XID, there are two billion XIDs that are
“older” and two billion that are “newer”; another
way to say it is that the normal XID space is circular with no
endpoint. Therefore, once a row version has been created with a particular
normal XID, the row version will appear to be “in the past” for
the next two billion transactions, no matter which normal XID we are
talking about. If the row version still exists after more than two billion
transactions, it will suddenly appear to be in the future. To
prevent data loss, old row versions must be reassigned the XID
FrozenXID sometime before they reach the
two-billion-transactions-old mark. Once they are assigned this
special XID, they will appear to be “in the past” to all
normal transactions regardless of wraparound issues, and so such
row versions will be good until deleted, no matter how long that is.
This reassignment of old XIDs is handled by VACUUM.
VACUUM's behavior is controlled by the configuration parameter
vacuum_freeze_min_age: any XID older than
vacuum_freeze_min_age transactions is replaced by
FrozenXID. Larger values of vacuum_freeze_min_age
preserve transactional information longer, while smaller values increase
the number of transactions that can elapse before the table must be
vacuumed again.
The maximum time that a table can go unvacuumed is two billion
transactions minus the vacuum_freeze_min_age that was used
when it was last vacuumed.
If it were to go unvacuumed for longer than that,
data loss could result. To ensure that this does not
happen, the autovacuum facility described in
section 9.1.4 The auto-vacuum daemon is invoked on any table
that might contain XIDs older than the age specified by the
configuration parameter
autovacuum_freeze_max_age. (This will happen
even if autovacuum is otherwise disabled.)
This implies that if a table is not otherwise vacuumed,
autovacuum will be invoked on it approximately once every
autovacuum_freeze_max_age minus
vacuum_freeze_min_age transactions.
For tables that are regularly vacuumed for space reclamation purposes,
this is of little importance. However, for static tables
(including tables that receive inserts, but no updates or deletes),
there is no need for vacuuming for space reclamation, and so it can
be useful to try to maximize the interval between forced autovacuums
on very large static tables. Obviously one can do this either by
increasing autovacuum_freeze_max_age or by decreasing
vacuum_freeze_min_age.
The sole disadvantage of increasing autovacuum_freeze_max_age
is that the ‘pg_clog’ subdirectory of the database cluster
will take more space, because it must store the commit status for all
transactions back to the autovacuum_freeze_max_age horizon.
The commit status uses two bits per transaction, so if
autovacuum_freeze_max_age has its maximum allowed value of
a little less than two billion, ‘pg_clog’ can be expected to
grow to about half a gigabyte. If this is trivial compared to your
total database size, setting autovacuum_freeze_max_age to
its maximum allowed value is recommended. Otherwise, set it depending
on what you are willing to allow for ‘pg_clog’ storage.
(The default, 200 million transactions, translates to about 50MB of
‘pg_clog’ storage.)
One disadvantage of decreasing vacuum_freeze_min_age is that
it may cause VACUUM to do useless work: changing a table row's
XID to FrozenXID is a waste of time if the row is modified
soon thereafter (causing it to acquire a new XID). So the setting should
be large enough that rows are not frozen until they are unlikely to change
any more. Another disadvantage of decreasing this setting is
that details about exactly which transaction inserted or modified a
row will be lost sooner. This information sometimes comes in handy,
particularly when trying to analyze what went wrong after a database
failure. For these two reasons, decreasing this setting is not
recommended except for completely static tables.
To track the age of the oldest XIDs in a database,
VACUUM stores XID
statistics in the system tables pg_class and
pg_database. In particular,
the relfrozenxid column of a table's
pg_class row contains the freeze cutoff XID that was used
by the last VACUUM for that table. All normal
XIDs older than this cutoff XID are guaranteed to have been replaced by
FrozenXID within the table. Similarly,
the datfrozenxid column of a database's
pg_database row is a lower bound on the normal XIDs
appearing in that database--it is just the minimum of the
per-table relfrozenxid values within the database.
A convenient way to
examine this information is to execute queries such as
SELECT relname, age(relfrozenxid) FROM pg_class WHERE relkind = 'r'; SELECT datname, age(datfrozenxid) FROM pg_database;
The age column measures the number of transactions from the
cutoff XID to the current transaction's XID. Immediately after a
VACUUM, age(relfrozenxid) should be a little
more than the vacuum_freeze_min_age setting that was used
(more by the number of transactions started since the VACUUM
started). If age(relfrozenxid) exceeds
autovacuum_freeze_max_age, an autovacuum will soon be forced
for the table.
If for some reason autovacuum fails to clear old XIDs from a table, the system will begin to emit warning messages like this when the database's oldest XIDs reach ten million transactions from the wraparound point:
WARNING: database "mydb" must be vacuumed within 177009986 transactions HINT: To avoid a database shutdown, execute a full-database VACUUM in "mydb".
If these warnings are ignored, the system will shut down and refuse to execute any new transactions once there are fewer than 1 million transactions left until wraparound:
ERROR: database is shut down to avoid wraparound data loss in database "mydb" HINT: Stop the postmaster and use a standalone backend to VACUUM in "mydb".
The 1-million-transaction safety margin exists to let the
administrator recover without data loss, by manually executing the
required VACUUM commands. However, since the system will not
execute commands once it has gone into the safety shutdown mode,
the only way to do this is to stop the server and use a single-user
backend to execute VACUUM. The shutdown mode is not enforced
by a single-user backend. See the postgres reference
page for details about using a single-user backend.
| ISBN 0954612043 | PostgreSQL Reference Manual - Volume 3 - Server Administration Guide | See the print edition |