]> granicus.if.org Git - postgresql/commitdiff
Refactor GetOldestXmin() to use flags
authorSimon Riggs <simon@2ndQuadrant.com>
Wed, 22 Mar 2017 16:51:01 +0000 (16:51 +0000)
committerSimon Riggs <simon@2ndQuadrant.com>
Wed, 22 Mar 2017 16:51:01 +0000 (16:51 +0000)
Replace ignoreVacuum parameter with more flexible flags.

Author: Eiji Seki
Review: Haribabu Kommi

contrib/pg_visibility/pg_visibility.c
contrib/pgstattuple/pgstatapprox.c
src/backend/access/transam/xlog.c
src/backend/catalog/index.c
src/backend/commands/analyze.c
src/backend/commands/vacuum.c
src/backend/replication/walreceiver.c
src/backend/storage/ipc/procarray.c
src/include/storage/proc.h
src/include/storage/procarray.h

index d0f7618f6222604c26649157b628d242788b5ee2..ee3936e09a9ae4046e8e153b0f166e60a176b410 100644 (file)
@@ -557,7 +557,7 @@ collect_corrupt_items(Oid relid, bool all_visible, bool all_frozen)
        if (all_visible)
        {
                /* Don't pass rel; that will fail in recovery. */
-               OldestXmin = GetOldestXmin(NULL, true);
+               OldestXmin = GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM);
        }
 
        rel = relation_open(relid, AccessShareLock);
@@ -674,7 +674,7 @@ collect_corrupt_items(Oid relid, bool all_visible, bool all_frozen)
                                 * a buffer lock. And this shouldn't happen often, so it's
                                 * worth being careful so as to avoid false positives.
                                 */
-                               RecomputedOldestXmin = GetOldestXmin(NULL, true);
+                               RecomputedOldestXmin = GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM);
 
                                if (!TransactionIdPrecedes(OldestXmin, RecomputedOldestXmin))
                                        record_corrupt_item(items, &tuple.t_self);
index 8db1e203733f4c65ca286b0cb39347fa5a0487cd..46c167a96a5e67d6bb39140dbff42126b80fbe2d 100644 (file)
@@ -70,7 +70,7 @@ statapprox_heap(Relation rel, output_type *stat)
        TransactionId OldestXmin;
        uint64          misc_count = 0;
 
-       OldestXmin = GetOldestXmin(rel, true);
+       OldestXmin = GetOldestXmin(rel, PROCARRAY_FLAGS_VACUUM);
        bstrategy = GetAccessStrategy(BAS_BULKREAD);
 
        nblocks = RelationGetNumberOfBlocks(rel);
index 9480377611d3b49ab1b11be2c3a346ab34597884..ff4cf3a810b6c8cc9fd982a11338147f2f8ff883 100644 (file)
@@ -8895,7 +8895,7 @@ CreateCheckPoint(int flags)
         * StartupSUBTRANS hasn't been called yet.
         */
        if (!RecoveryInProgress())
-               TruncateSUBTRANS(GetOldestXmin(NULL, false));
+               TruncateSUBTRANS(GetOldestXmin(NULL, PROCARRAY_FLAGS_DEFAULT));
 
        /* Real work is done, but log and update stats before releasing lock. */
        LogCheckpointEnd(false);
@@ -9258,7 +9258,7 @@ CreateRestartPoint(int flags)
         * this because StartupSUBTRANS hasn't been called yet.
         */
        if (EnableHotStandby)
-               TruncateSUBTRANS(GetOldestXmin(NULL, false));
+               TruncateSUBTRANS(GetOldestXmin(NULL, PROCARRAY_FLAGS_DEFAULT));
 
        /* Real work is done, but log and update before releasing lock. */
        LogCheckpointEnd(true);
index 8d42a347ea555b50e6c234b2c5b9eb65e217ebf4..7924c30369ffadb1ff358862f612712e863ceeda 100644 (file)
@@ -2270,7 +2270,7 @@ IndexBuildHeapRangeScan(Relation heapRelation,
        {
                snapshot = SnapshotAny;
                /* okay to ignore lazy VACUUMs here */
-               OldestXmin = GetOldestXmin(heapRelation, true);
+               OldestXmin = GetOldestXmin(heapRelation, PROCARRAY_FLAGS_VACUUM);
        }
 
        scan = heap_beginscan_strat(heapRelation,       /* relation */
index b91df986c54e821e30c56ecf667c9fd05d3704ab..055338fdff2c623ba56a02c6c020a973bc203918 100644 (file)
@@ -1000,7 +1000,7 @@ acquire_sample_rows(Relation onerel, int elevel,
        totalblocks = RelationGetNumberOfBlocks(onerel);
 
        /* Need a cutoff xmin for HeapTupleSatisfiesVacuum */
-       OldestXmin = GetOldestXmin(onerel, true);
+       OldestXmin = GetOldestXmin(onerel, PROCARRAY_FLAGS_VACUUM);
 
        /* Prepare for sampling block numbers */
        BlockSampler_Init(&bs, totalblocks, targrows, random());
index ff633faca6418fcddbdcd98277bb3a032223fbd7..d8ae3e1e43344ab29ee221a97c5ded68840ece45 100644 (file)
@@ -527,7 +527,7 @@ vacuum_set_xid_limits(Relation rel,
         * always an independent transaction.
         */
        *oldestXmin =
-               TransactionIdLimitedForOldSnapshots(GetOldestXmin(rel, true), rel);
+               TransactionIdLimitedForOldSnapshots(GetOldestXmin(rel, PROCARRAY_FLAGS_VACUUM), rel);
 
        Assert(TransactionIdIsNormal(*oldestXmin));
 
@@ -939,7 +939,7 @@ vac_update_datfrozenxid(void)
         * committed pg_class entries for new tables; see AddNewRelationTuple().
         * So we cannot produce a wrong minimum by starting with this.
         */
-       newFrozenXid = GetOldestXmin(NULL, true);
+       newFrozenXid = GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM);
 
        /*
         * Similarly, initialize the MultiXact "min" with the value that would be
index 18d9d7e4ec2251eed9e69a47f72a8986d1020660..31c567b37ef4f9584ec72b00b8012227a141050e 100644 (file)
@@ -1221,7 +1221,7 @@ XLogWalRcvSendHSFeedback(bool immed)
         * everything else has been checked.
         */
        if (hot_standby_feedback)
-               xmin = GetOldestXmin(NULL, false);
+               xmin = GetOldestXmin(NULL, PROCARRAY_FLAGS_DEFAULT);
        else
                xmin = InvalidTransactionId;
 
index 0f8f435faffb413f5300f04da69c47113674df4c..3e562d3a545135f432b24adf3b38f5908177462f 100644 (file)
@@ -1260,8 +1260,9 @@ TransactionIdIsActive(TransactionId xid)
  * If rel is NULL or a shared relation, all backends are considered, otherwise
  * only backends running in this database are considered.
  *
- * If ignoreVacuum is TRUE then backends with the PROC_IN_VACUUM flag set are
- * ignored.
+ * The flags are used to ignore the backends in calculation when any of the
+ * corresponding flags is set. Typically, if you want to ignore ones with
+ * PROC_IN_VACUUM flag, you can use PROCARRAY_FLAGS_VACUUM.
  *
  * This is used by VACUUM to decide which deleted tuples must be preserved in
  * the passed in table. For shared relations backends in all databases must be
@@ -1302,7 +1303,7 @@ TransactionIdIsActive(TransactionId xid)
  * GetOldestXmin() move backwards, with no consequences for data integrity.
  */
 TransactionId
-GetOldestXmin(Relation rel, bool ignoreVacuum)
+GetOldestXmin(Relation rel, int flags)
 {
        ProcArrayStruct *arrayP = procArray;
        TransactionId result;
@@ -1340,14 +1341,7 @@ GetOldestXmin(Relation rel, bool ignoreVacuum)
                volatile PGPROC *proc = &allProcs[pgprocno];
                volatile PGXACT *pgxact = &allPgXact[pgprocno];
 
-               /*
-                * Backend is doing logical decoding which manages xmin separately,
-                * check below.
-                */
-               if (pgxact->vacuumFlags & PROC_IN_LOGICAL_DECODING)
-                       continue;
-
-               if (ignoreVacuum && (pgxact->vacuumFlags & PROC_IN_VACUUM))
+               if (pgxact->vacuumFlags & flags)
                        continue;
 
                if (allDbs ||
index 5f38fa6b4f1f14f41bb5cde599d74b6e093d714b..945dd1d592f27a4be8fcddf61e5f0d3e23123bfa 100644 (file)
@@ -39,7 +39,12 @@ struct XidCache
        TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS];
 };
 
-/* Flags for PGXACT->vacuumFlags */
+/*
+ * Flags for PGXACT->vacuumFlags
+ *
+ * Note: If you modify these flags, you need to modify PROCARRAY_XXX flags
+ * in src/include/storage/procarray.h.
+ */
 #define                PROC_IS_AUTOVACUUM      0x01    /* is it an autovac worker? */
 #define                PROC_IN_VACUUM          0x02    /* currently running lazy vacuum */
 #define                PROC_IN_ANALYZE         0x04    /* currently running analyze */
index 9d5a13eb3b2aacde6380346d9903843a568a891e..c8e1ae517c98e6c04ca6be5efabe4f0c250c5d94 100644 (file)
 #include "utils/snapshot.h"
 
 
+/*
+ * These are to implement PROCARRAY_FLAGS_XXX
+ *
+ * Note: These flags are cloned from PROC_XXX flags in src/include/storage/proc.h
+ * to avoid forcing to include proc.h when including procarray.h. So if you modify
+ * PROC_XXX flags, you need to modify these flags.
+ */
+#define                PROCARRAY_VACUUM_FLAG                   0x02    /* currently running lazy vacuum */
+#define                PROCARRAY_ANALYZE_FLAG                  0x04    /* currently running analyze */
+#define                PROCARRAY_LOGICAL_DECODING_FLAG 0x10    /* currently doing logical
+                                                                                                        * decoding outside xact */
+
+/* Use the following flags as an input "flags" to GetOldestXmin function */
+/* Consider all backends except for logical decoding ones which manage xmin separately */
+#define                PROCARRAY_FLAGS_DEFAULT                 PROCARRAY_LOGICAL_DECODING_FLAG
+/* Ignore vacuum backends */
+#define                PROCARRAY_FLAGS_VACUUM                  PROCARRAY_FLAGS_DEFAULT | PROCARRAY_VACUUM_FLAG
+/* Ignore analyze backends */
+#define                PROCARRAY_FLAGS_ANALYZE                 PROCARRAY_FLAGS_DEFAULT | PROCARRAY_ANALYZE_FLAG
+/* Ignore both vacuum and analyze backends */
+#define                PROCARRAY_FLAGS_VACUUM_ANALYZE  PROCARRAY_FLAGS_DEFAULT | PROCARRAY_VACUUM_FLAG | PROCARRAY_ANALYZE_FLAG
+
 extern Size ProcArrayShmemSize(void);
 extern void CreateSharedProcArray(void);
 extern void ProcArrayAdd(PGPROC *proc);
@@ -53,7 +75,7 @@ extern RunningTransactions GetRunningTransactionData(void);
 
 extern bool TransactionIdIsInProgress(TransactionId xid);
 extern bool TransactionIdIsActive(TransactionId xid);
-extern TransactionId GetOldestXmin(Relation rel, bool ignoreVacuum);
+extern TransactionId GetOldestXmin(Relation rel, int flags);
 extern TransactionId GetOldestActiveTransactionId(void);
 extern TransactionId GetOldestSafeDecodingTransactionId(void);