Force VACUUM to recalculate oldestXmin even when we haven't changed our
authorTom Lane
Tue, 1 Sep 2009 04:46:49 +0000 (04:46 +0000)
committerTom Lane
Tue, 1 Sep 2009 04:46:49 +0000 (04:46 +0000)
own database's datfrozenxid, if the current value is old enough to be
forcing autovacuums or warning messages.  This ensures that a bogus
value is replaced as soon as possible.  Per a comment from Heikki.

src/backend/access/transam/varsup.c
src/backend/commands/vacuum.c
src/include/access/transam.h

index e44cf0d45057215fda3d7e68eeecd1c1a0cfa5fc..425bc3f403a2a947ec6721e8c1282c5bd5ee1f14 100644 (file)
@@ -6,7 +6,7 @@
  * Copyright (c) 2000-2009, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/access/transam/varsup.c,v 1.85 2009/08/31 02:23:21 tgl Exp $
+ *   $PostgreSQL: pgsql/src/backend/access/transam/varsup.c,v 1.86 2009/09/01 04:46:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -372,32 +372,41 @@ SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
 
 
 /*
- * TransactionIdLimitIsValid -- is the shared XID wrap-limit data sane?
+ * ForceTransactionIdLimitUpdate -- does the XID wrap-limit data need updating?
  *
  * We primarily check whether oldestXidDB is valid.  The cases we have in
  * mind are that that database was dropped, or the field was reset to zero
  * by pg_resetxlog.  In either case we should force recalculation of the
- * wrap limit.  In future we might add some more sanity checks here.
+ * wrap limit.  Also do it if oldestXid is old enough to be forcing
+ * autovacuums or other actions; this ensures we update our state as soon
+ * as possible once extra overhead is being incurred.
  */
 bool
-TransactionIdLimitIsValid(void)
+ForceTransactionIdLimitUpdate(void)
 {
+   TransactionId nextXid;
+   TransactionId xidVacLimit;
    TransactionId oldestXid;
    Oid         oldestXidDB;
 
    /* Locking is probably not really necessary, but let's be careful */
    LWLockAcquire(XidGenLock, LW_SHARED);
+   nextXid = ShmemVariableCache->nextXid;
+   xidVacLimit = ShmemVariableCache->xidVacLimit;
    oldestXid = ShmemVariableCache->oldestXid;
    oldestXidDB = ShmemVariableCache->oldestXidDB;
    LWLockRelease(XidGenLock);
 
    if (!TransactionIdIsNormal(oldestXid))
-       return false;           /* shouldn't happen, but just in case */
+       return true;            /* shouldn't happen, but just in case */
+   if (TransactionIdFollowsOrEquals(nextXid, xidVacLimit) &&
+       TransactionIdIsValid(xidVacLimit))
+       return true;            /* past VacLimit, don't delay updating */
    if (!SearchSysCacheExists(DATABASEOID,
                              ObjectIdGetDatum(oldestXidDB),
                              0, 0, 0))
-       return false;           /* could happen, per comment above */
-   return true;
+       return true;            /* could happen, per comments above */
+   return false;
 }
 
 
index 260eab82d1bda7f61fad60cc777ff35385e335f7..51c44c80dd5ee84b2aded2c05d3205083b34f8f3 100644 (file)
@@ -13,7 +13,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.392 2009/09/01 02:54:51 alvherre Exp $
+ *   $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.393 2009/09/01 04:46:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -893,9 +893,10 @@ vac_update_datfrozenxid(void)
 
    /*
     * If we were able to advance datfrozenxid, see if we can truncate pg_clog.
-    * Also do it if the shared XID-wrap-limit info is stale.
+    * Also do it if the shared XID-wrap-limit info is stale, since this
+    * action will update that too.
     */
-   if (dirty || !TransactionIdLimitIsValid())
+   if (dirty || ForceTransactionIdLimitUpdate())
        vac_truncate_clog(newFrozenXid);
 }
 
index 87609e6f81cf8bf177e4b968cebc46c1e3eb1b5c..6f3370dcd2c4a632ed32094ae2220fce34fe1e7a 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/transam.h,v 1.69 2009/08/31 02:23:23 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/transam.h,v 1.70 2009/09/01 04:46:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -156,7 +156,7 @@ extern TransactionId GetNewTransactionId(bool isSubXact);
 extern TransactionId ReadNewTransactionId(void);
 extern void SetTransactionIdLimit(TransactionId oldest_datfrozenxid,
                      Oid oldest_datoid);
-extern bool TransactionIdLimitIsValid(void);
+extern bool ForceTransactionIdLimitUpdate(void);
 extern Oid GetNewObjectId(void);
 
 #endif   /* TRAMSAM_H */