mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-01-12 18:34:36 +08:00
Initialize the minimum frozen Xid in vac_update_datfrozenxid using
GetOldestXmin() instead of RecentGlobalXmin; this is safer because we do not depend on the latter being correctly set elsewhere, and while it is more expensive, this code path is not performance-critical. This is a real risk for autovacuum, because it can execute whole cycles without doing a single vacuum, which would mean that RecentGlobalXmin would stay at its initialization value, FirstNormalTransactionId, causing a bogus value to be inserted in pg_database. This bug could explain some recent reports of failure to truncate pg_clog. At the same time, change the initialization of RecentGlobalXmin to InvalidTransactionId, and ensure that it's set to something else whenever it's going to be used. Using it as FirstNormalTransactionId in HOT page pruning could incur in data loss. InitPostgres takes care of setting it to a valid value, but the extra checks are there to prevent "special" backends from behaving in unusual ways. Per Tom Lane's detailed problem dissection in 29544.1221061979@sss.pgh.pa.us
This commit is contained in:
parent
d22e47dba8
commit
49b505fd6c
@ -13,7 +13,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.342.2.5 2008/02/11 19:14:38 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.342.2.6 2008/09/11 14:01:16 alvherre Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -764,14 +764,12 @@ vac_update_datfrozenxid(void)
|
||||
bool dirty = false;
|
||||
|
||||
/*
|
||||
* Initialize the "min" calculation with RecentGlobalXmin. Any
|
||||
* not-yet-committed pg_class entries for new tables must have
|
||||
* relfrozenxid at least this high, because any other open xact must have
|
||||
* RecentXmin >= its PGPROC.xmin >= our RecentGlobalXmin; see
|
||||
* AddNewRelationTuple(). So we cannot produce a wrong minimum by
|
||||
* starting with this.
|
||||
* Initialize the "min" calculation with GetOldestXmin, which is a
|
||||
* reasonable approximation to the minimum relfrozenxid for not-yet-
|
||||
* committed pg_class entries for new tables; see AddNewRelationTuple().
|
||||
* Se we cannot produce a wrong minimum by starting with this.
|
||||
*/
|
||||
newFrozenXid = RecentGlobalXmin;
|
||||
newFrozenXid = GetOldestXmin(true, true);
|
||||
|
||||
/*
|
||||
* We must seqscan pg_class to find the minimum Xid, because there is no
|
||||
@ -965,18 +963,16 @@ vacuum_rel(Oid relid, VacuumStmt *vacstmt, char expected_relkind)
|
||||
/* Begin a transaction for vacuuming this relation */
|
||||
StartTransactionCommand();
|
||||
|
||||
if (vacstmt->full)
|
||||
{
|
||||
/* functions in indexes may want a snapshot set */
|
||||
ActiveSnapshot = CopySnapshot(GetTransactionSnapshot());
|
||||
}
|
||||
else
|
||||
/*
|
||||
* Functions in indexes may want a snapshot set. Also, setting
|
||||
* a snapshot ensures that RecentGlobalXmin is kept truly recent.
|
||||
*/
|
||||
ActiveSnapshot = CopySnapshot(GetTransactionSnapshot());
|
||||
|
||||
if (!vacstmt->full)
|
||||
{
|
||||
/*
|
||||
* During a lazy VACUUM we do not run any user-supplied functions, and
|
||||
* so it should be safe to not create a transaction snapshot.
|
||||
*
|
||||
* We can furthermore set the inVacuum flag, which lets other
|
||||
* During a lazy VACUUM we can set the inVacuum flag, which lets other
|
||||
* concurrent VACUUMs know that they can ignore this one while
|
||||
* determining their OldestXmin. (The reason we don't set inVacuum
|
||||
* during a full VACUUM is exactly that we may have to run user-
|
||||
|
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.172 2006/11/05 22:42:09 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.172.2.1 2008/09/11 14:01:16 alvherre Exp $
|
||||
*
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
@ -42,6 +42,7 @@
|
||||
#include "utils/guc.h"
|
||||
#include "utils/portal.h"
|
||||
#include "utils/relcache.h"
|
||||
#include "utils/tqual.h"
|
||||
#include "utils/syscache.h"
|
||||
#include "pgstat.h"
|
||||
|
||||
@ -380,10 +381,15 @@ InitPostgres(const char *dbname, const char *username)
|
||||
on_shmem_exit(ShutdownPostgres, 0);
|
||||
|
||||
/*
|
||||
* Start a new transaction here before first access to db
|
||||
* Start a new transaction here before first access to db, and get a
|
||||
* snapshot. We don't have a use for the snapshot itself, but we're
|
||||
* interested in the secondary effect that it sets RecentGlobalXmin.
|
||||
*/
|
||||
if (!bootstrap)
|
||||
{
|
||||
StartTransactionCommand();
|
||||
(void) GetTransactionSnapshot();
|
||||
}
|
||||
|
||||
/*
|
||||
* Now that we have a transaction, we can take locks. Take a writer's
|
||||
|
Loading…
Reference in New Issue
Block a user