Skip to content

Commit af4b1a0

Browse files
Refactor GetOldestXmin() to use flags
Replace ignoreVacuum parameter with more flexible flags. Author: Eiji Seki Review: Haribabu Kommi
1 parent 49bff53 commit af4b1a0

File tree

10 files changed

+44
-23
lines changed

10 files changed

+44
-23
lines changed

contrib/pg_visibility/pg_visibility.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ collect_corrupt_items(Oid relid, bool all_visible, bool all_frozen)
557557
if (all_visible)
558558
{
559559
/* Don't pass rel; that will fail in recovery. */
560-
OldestXmin = GetOldestXmin(NULL, true);
560+
OldestXmin = GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM);
561561
}
562562

563563
rel = relation_open(relid, AccessShareLock);
@@ -674,7 +674,7 @@ collect_corrupt_items(Oid relid, bool all_visible, bool all_frozen)
674674
* a buffer lock. And this shouldn't happen often, so it's
675675
* worth being careful so as to avoid false positives.
676676
*/
677-
RecomputedOldestXmin = GetOldestXmin(NULL, true);
677+
RecomputedOldestXmin = GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM);
678678

679679
if (!TransactionIdPrecedes(OldestXmin, RecomputedOldestXmin))
680680
record_corrupt_item(items, &tuple.t_self);

contrib/pgstattuple/pgstatapprox.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ statapprox_heap(Relation rel, output_type *stat)
7070
TransactionId OldestXmin;
7171
uint64 misc_count = 0;
7272

73-
OldestXmin = GetOldestXmin(rel, true);
73+
OldestXmin = GetOldestXmin(rel, PROCARRAY_FLAGS_VACUUM);
7474
bstrategy = GetAccessStrategy(BAS_BULKREAD);
7575

7676
nblocks = RelationGetNumberOfBlocks(rel);

src/backend/access/transam/xlog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8895,7 +8895,7 @@ CreateCheckPoint(int flags)
88958895
* StartupSUBTRANS hasn't been called yet.
88968896
*/
88978897
if (!RecoveryInProgress())
8898-
TruncateSUBTRANS(GetOldestXmin(NULL, false));
8898+
TruncateSUBTRANS(GetOldestXmin(NULL, PROCARRAY_FLAGS_DEFAULT));
88998899

89008900
/* Real work is done, but log and update stats before releasing lock. */
89018901
LogCheckpointEnd(false);
@@ -9258,7 +9258,7 @@ CreateRestartPoint(int flags)
92589258
* this because StartupSUBTRANS hasn't been called yet.
92599259
*/
92609260
if (EnableHotStandby)
9261-
TruncateSUBTRANS(GetOldestXmin(NULL, false));
9261+
TruncateSUBTRANS(GetOldestXmin(NULL, PROCARRAY_FLAGS_DEFAULT));
92629262

92639263
/* Real work is done, but log and update before releasing lock. */
92649264
LogCheckpointEnd(true);

src/backend/catalog/index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2270,7 +2270,7 @@ IndexBuildHeapRangeScan(Relation heapRelation,
22702270
{
22712271
snapshot = SnapshotAny;
22722272
/* okay to ignore lazy VACUUMs here */
2273-
OldestXmin = GetOldestXmin(heapRelation, true);
2273+
OldestXmin = GetOldestXmin(heapRelation, PROCARRAY_FLAGS_VACUUM);
22742274
}
22752275

22762276
scan = heap_beginscan_strat(heapRelation, /* relation */

src/backend/commands/analyze.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ acquire_sample_rows(Relation onerel, int elevel,
10001000
totalblocks = RelationGetNumberOfBlocks(onerel);
10011001

10021002
/* Need a cutoff xmin for HeapTupleSatisfiesVacuum */
1003-
OldestXmin = GetOldestXmin(onerel, true);
1003+
OldestXmin = GetOldestXmin(onerel, PROCARRAY_FLAGS_VACUUM);
10041004

10051005
/* Prepare for sampling block numbers */
10061006
BlockSampler_Init(&bs, totalblocks, targrows, random());

src/backend/commands/vacuum.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ vacuum_set_xid_limits(Relation rel,
527527
* always an independent transaction.
528528
*/
529529
*oldestXmin =
530-
TransactionIdLimitedForOldSnapshots(GetOldestXmin(rel, true), rel);
530+
TransactionIdLimitedForOldSnapshots(GetOldestXmin(rel, PROCARRAY_FLAGS_VACUUM), rel);
531531

532532
Assert(TransactionIdIsNormal(*oldestXmin));
533533

@@ -939,7 +939,7 @@ vac_update_datfrozenxid(void)
939939
* committed pg_class entries for new tables; see AddNewRelationTuple().
940940
* So we cannot produce a wrong minimum by starting with this.
941941
*/
942-
newFrozenXid = GetOldestXmin(NULL, true);
942+
newFrozenXid = GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM);
943943

944944
/*
945945
* Similarly, initialize the MultiXact "min" with the value that would be

src/backend/replication/walreceiver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ XLogWalRcvSendHSFeedback(bool immed)
12211221
* everything else has been checked.
12221222
*/
12231223
if (hot_standby_feedback)
1224-
xmin = GetOldestXmin(NULL, false);
1224+
xmin = GetOldestXmin(NULL, PROCARRAY_FLAGS_DEFAULT);
12251225
else
12261226
xmin = InvalidTransactionId;
12271227

src/backend/storage/ipc/procarray.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,8 +1260,9 @@ TransactionIdIsActive(TransactionId xid)
12601260
* If rel is NULL or a shared relation, all backends are considered, otherwise
12611261
* only backends running in this database are considered.
12621262
*
1263-
* If ignoreVacuum is TRUE then backends with the PROC_IN_VACUUM flag set are
1264-
* ignored.
1263+
* The flags are used to ignore the backends in calculation when any of the
1264+
* corresponding flags is set. Typically, if you want to ignore ones with
1265+
* PROC_IN_VACUUM flag, you can use PROCARRAY_FLAGS_VACUUM.
12651266
*
12661267
* This is used by VACUUM to decide which deleted tuples must be preserved in
12671268
* the passed in table. For shared relations backends in all databases must be
@@ -1302,7 +1303,7 @@ TransactionIdIsActive(TransactionId xid)
13021303
* GetOldestXmin() move backwards, with no consequences for data integrity.
13031304
*/
13041305
TransactionId
1305-
GetOldestXmin(Relation rel, bool ignoreVacuum)
1306+
GetOldestXmin(Relation rel, int flags)
13061307
{
13071308
ProcArrayStruct *arrayP = procArray;
13081309
TransactionId result;
@@ -1340,14 +1341,7 @@ GetOldestXmin(Relation rel, bool ignoreVacuum)
13401341
volatile PGPROC *proc = &allProcs[pgprocno];
13411342
volatile PGXACT *pgxact = &allPgXact[pgprocno];
13421343

1343-
/*
1344-
* Backend is doing logical decoding which manages xmin separately,
1345-
* check below.
1346-
*/
1347-
if (pgxact->vacuumFlags & PROC_IN_LOGICAL_DECODING)
1348-
continue;
1349-
1350-
if (ignoreVacuum && (pgxact->vacuumFlags & PROC_IN_VACUUM))
1344+
if (pgxact->vacuumFlags & flags)
13511345
continue;
13521346

13531347
if (allDbs ||

src/include/storage/proc.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ struct XidCache
3939
TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS];
4040
};
4141

42-
/* Flags for PGXACT->vacuumFlags */
42+
/*
43+
* Flags for PGXACT->vacuumFlags
44+
*
45+
* Note: If you modify these flags, you need to modify PROCARRAY_XXX flags
46+
* in src/include/storage/procarray.h.
47+
*/
4348
#define PROC_IS_AUTOVACUUM 0x01 /* is it an autovac worker? */
4449
#define PROC_IN_VACUUM 0x02 /* currently running lazy vacuum */
4550
#define PROC_IN_ANALYZE 0x04 /* currently running analyze */

src/include/storage/procarray.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@
2020
#include "utils/snapshot.h"
2121

2222

23+
/*
24+
* These are to implement PROCARRAY_FLAGS_XXX
25+
*
26+
* Note: These flags are cloned from PROC_XXX flags in src/include/storage/proc.h
27+
* to avoid forcing to include proc.h when including procarray.h. So if you modify
28+
* PROC_XXX flags, you need to modify these flags.
29+
*/
30+
#define PROCARRAY_VACUUM_FLAG 0x02 /* currently running lazy vacuum */
31+
#define PROCARRAY_ANALYZE_FLAG 0x04 /* currently running analyze */
32+
#define PROCARRAY_LOGICAL_DECODING_FLAG 0x10 /* currently doing logical
33+
* decoding outside xact */
34+
35+
/* Use the following flags as an input "flags" to GetOldestXmin function */
36+
/* Consider all backends except for logical decoding ones which manage xmin separately */
37+
#define PROCARRAY_FLAGS_DEFAULT PROCARRAY_LOGICAL_DECODING_FLAG
38+
/* Ignore vacuum backends */
39+
#define PROCARRAY_FLAGS_VACUUM PROCARRAY_FLAGS_DEFAULT | PROCARRAY_VACUUM_FLAG
40+
/* Ignore analyze backends */
41+
#define PROCARRAY_FLAGS_ANALYZE PROCARRAY_FLAGS_DEFAULT | PROCARRAY_ANALYZE_FLAG
42+
/* Ignore both vacuum and analyze backends */
43+
#define PROCARRAY_FLAGS_VACUUM_ANALYZE PROCARRAY_FLAGS_DEFAULT | PROCARRAY_VACUUM_FLAG | PROCARRAY_ANALYZE_FLAG
44+
2345
extern Size ProcArrayShmemSize(void);
2446
extern void CreateSharedProcArray(void);
2547
extern void ProcArrayAdd(PGPROC *proc);
@@ -53,7 +75,7 @@ extern RunningTransactions GetRunningTransactionData(void);
5375

5476
extern bool TransactionIdIsInProgress(TransactionId xid);
5577
extern bool TransactionIdIsActive(TransactionId xid);
56-
extern TransactionId GetOldestXmin(Relation rel, bool ignoreVacuum);
78+
extern TransactionId GetOldestXmin(Relation rel, int flags);
5779
extern TransactionId GetOldestActiveTransactionId(void);
5880
extern TransactionId GetOldestSafeDecodingTransactionId(void);
5981

0 commit comments

Comments
 (0)