Turn TransactionIdRetreat/Advance into inline functions
Hi!
This patch is inspired by [0]/messages/by-id/5b558da8-99fb-0a99-83dd-f72f05388517@enterprisedb.com and many others.
I've notice recent activity to convert macros into inline functions. We
should make TransactionIdRetreat/Advance functions
Instead of a macro, should we?
I also think about NormalTransactionIdPrecedes and
NormalTransactionIdFollows, but maybe, they should be addressed
separately: the comment says that "this is a macro for speed".
Any thoughts?
[0]: /messages/by-id/5b558da8-99fb-0a99-83dd-f72f05388517@enterprisedb.com
/messages/by-id/5b558da8-99fb-0a99-83dd-f72f05388517@enterprisedb.com
--
Best regards,
Maxim Orlov.
Attachments:
v1-0001-Convert-macros-to-static-inline-functions-transam.patchapplication/octet-stream; name=v1-0001-Convert-macros-to-static-inline-functions-transam.patchDownload
From 044b2ed516a466264282f5fe3a456e9f08934106 Mon Sep 17 00:00:00 2001
From: Maxim Orlov <orlovmg@gmail.com>
Date: Mon, 10 Oct 2022 17:11:52 +0300
Subject: [PATCH v1] Convert macros to static inline functions (transam.h)
---
src/backend/access/heap/heapam.c | 2 +-
src/backend/access/transam/subtrans.c | 2 +-
src/backend/access/transam/varsup.c | 2 +-
src/backend/access/transam/xlog.c | 4 ++--
src/backend/replication/logical/snapbuild.c | 4 ++--
src/backend/storage/ipc/procarray.c | 20 ++++++++--------
src/include/access/transam.h | 26 +++++++++++++--------
7 files changed, 33 insertions(+), 27 deletions(-)
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index bd4d85041d3..94f0f2dd917 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -8944,7 +8944,7 @@ heap_xlog_freeze_page(XLogReaderState *record)
RelFileLocator rlocator;
TransactionId latestRemovedXid = cutoff_xid;
- TransactionIdRetreat(latestRemovedXid);
+ TransactionIdRetreat(&latestRemovedXid);
XLogRecGetBlockTag(record, 0, &rlocator, NULL, NULL);
ResolveRecoveryConflictWithSnapshot(latestRemovedXid, rlocator);
diff --git a/src/backend/access/transam/subtrans.c b/src/backend/access/transam/subtrans.c
index 66d35481552..cb479d41679 100644
--- a/src/backend/access/transam/subtrans.c
+++ b/src/backend/access/transam/subtrans.c
@@ -347,7 +347,7 @@ TruncateSUBTRANS(TransactionId oldestXact)
* a page and oldestXact == next XID. In that case, if we didn't subtract
* one, we'd trigger SimpleLruTruncate's wraparound detection.
*/
- TransactionIdRetreat(oldestXact);
+ TransactionIdRetreat(&oldestXact);
cutoffPage = TransactionIdToPage(oldestXact);
SimpleLruTruncate(SubTransCtl, cutoffPage);
diff --git a/src/backend/access/transam/varsup.c b/src/backend/access/transam/varsup.c
index 849a7ce9d6d..44313167171 100644
--- a/src/backend/access/transam/varsup.c
+++ b/src/backend/access/transam/varsup.c
@@ -300,7 +300,7 @@ AdvanceNextFullTransactionIdPastXid(TransactionId xid)
* because the span of active xids cannot exceed one epoch at any given
* point in the WAL stream.
*/
- TransactionIdAdvance(xid);
+ TransactionIdAdvance(&xid);
epoch = EpochFromFullTransactionId(ShmemVariableCache->nextXid);
if (unlikely(xid < next_xid))
++epoch;
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 27085b15a83..6835b9a24f8 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -5448,7 +5448,7 @@ StartupXLOG(void)
running.nextXid = XidFromFullTransactionId(checkPoint.nextXid);
running.oldestRunningXid = oldestActiveXID;
latestCompletedXid = XidFromFullTransactionId(checkPoint.nextXid);
- TransactionIdRetreat(latestCompletedXid);
+ TransactionIdRetreat(&latestCompletedXid);
Assert(TransactionIdIsNormal(latestCompletedXid));
running.latestCompletedXid = latestCompletedXid;
running.xids = xids;
@@ -7820,7 +7820,7 @@ xlog_redo(XLogReaderState *record)
running.nextXid = XidFromFullTransactionId(checkPoint.nextXid);
running.oldestRunningXid = oldestActiveXID;
latestCompletedXid = XidFromFullTransactionId(checkPoint.nextXid);
- TransactionIdRetreat(latestCompletedXid);
+ TransactionIdRetreat(&latestCompletedXid);
Assert(TransactionIdIsNormal(latestCompletedXid));
running.latestCompletedXid = latestCompletedXid;
running.xids = xids;
diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c
index 54499c06fe9..a0f376689f6 100644
--- a/src/backend/replication/logical/snapbuild.c
+++ b/src/backend/replication/logical/snapbuild.c
@@ -634,7 +634,7 @@ SnapBuildInitialSnapshot(SnapBuild *builder)
newxip[newxcnt++] = xid;
}
- TransactionIdAdvance(xid);
+ TransactionIdAdvance(&xid);
}
/* adjust remaining snapshot fields as needed */
@@ -1126,7 +1126,7 @@ SnapBuildCommitTxn(SnapBuild *builder, XLogRecPtr lsn, TransactionId xid,
TransactionIdFollowsOrEquals(xmax, builder->xmax)))
{
builder->xmax = xmax;
- TransactionIdAdvance(builder->xmax);
+ TransactionIdAdvance(&builder->xmax);
}
/* if there's any reason to build a historic snapshot, do so now */
diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c
index 207c4b27fdf..3cddcaafb4e 100644
--- a/src/backend/storage/ipc/procarray.c
+++ b/src/backend/storage/ipc/procarray.c
@@ -1023,7 +1023,7 @@ ProcArrayInitRecovery(TransactionId initializedUptoXID)
* ProcArrayApplyRecoveryInfo().
*/
latestObservedXid = initializedUptoXID;
- TransactionIdRetreat(latestObservedXid);
+ TransactionIdRetreat(&latestObservedXid);
}
/*
@@ -1216,13 +1216,13 @@ ProcArrayApplyRecoveryInfo(RunningTransactions running)
* haven't gone through RecordKnownAssignedTransactionId().
*/
Assert(TransactionIdIsNormal(latestObservedXid));
- TransactionIdAdvance(latestObservedXid);
+ TransactionIdAdvance(&latestObservedXid);
while (TransactionIdPrecedes(latestObservedXid, running->nextXid))
{
ExtendSUBTRANS(latestObservedXid);
- TransactionIdAdvance(latestObservedXid);
+ TransactionIdAdvance(&latestObservedXid);
}
- TransactionIdRetreat(latestObservedXid); /* = running->nextXid - 1 */
+ TransactionIdRetreat(&latestObservedXid); /* = running->nextXid - 1 */
/* ----------
* Now we've got the running xids we need to set the global values that
@@ -1733,7 +1733,7 @@ ComputeXidHorizons(ComputeXidHorizonsResult *h)
initial = XidFromFullTransactionId(h->latest_completed);
Assert(TransactionIdIsValid(initial));
- TransactionIdAdvance(initial);
+ TransactionIdAdvance(&initial);
h->oldest_considered_running = initial;
h->shared_oldest_nonremovable = initial;
@@ -2274,7 +2274,7 @@ GetSnapshotData(Snapshot snapshot)
/* xmax is always latestCompletedXid + 1 */
xmax = XidFromFullTransactionId(latest_completed);
- TransactionIdAdvance(xmax);
+ TransactionIdAdvance(&xmax);
Assert(TransactionIdIsNormal(xmax));
/* initialize xmin calculation with xmax */
@@ -4400,7 +4400,7 @@ RecordKnownAssignedTransactionIds(TransactionId xid)
next_expected_xid = latestObservedXid;
while (TransactionIdPrecedes(next_expected_xid, xid))
{
- TransactionIdAdvance(next_expected_xid);
+ TransactionIdAdvance(&next_expected_xid);
ExtendSUBTRANS(next_expected_xid);
}
Assert(next_expected_xid == xid);
@@ -4419,7 +4419,7 @@ RecordKnownAssignedTransactionIds(TransactionId xid)
* Add (latestObservedXid, xid] onto the KnownAssignedXids array.
*/
next_expected_xid = latestObservedXid;
- TransactionIdAdvance(next_expected_xid);
+ TransactionIdAdvance(&next_expected_xid);
KnownAssignedXidsAdd(next_expected_xid, xid, false);
/*
@@ -4685,7 +4685,7 @@ KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
while (TransactionIdPrecedes(next_xid, to_xid))
{
nxids++;
- TransactionIdAdvance(next_xid);
+ TransactionIdAdvance(&next_xid);
}
}
@@ -4741,7 +4741,7 @@ KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
{
KnownAssignedXids[head] = next_xid;
KnownAssignedXidsValid[head] = true;
- TransactionIdAdvance(next_xid);
+ TransactionIdAdvance(&next_xid);
head++;
}
diff --git a/src/include/access/transam.h b/src/include/access/transam.h
index 775471d2a7d..5352d22a9bb 100644
--- a/src/include/access/transam.h
+++ b/src/include/access/transam.h
@@ -88,12 +88,14 @@ FullTransactionIdFromU64(uint64 value)
}
/* advance a transaction ID variable, handling wraparound correctly */
-#define TransactionIdAdvance(dest) \
- do { \
- (dest)++; \
- if ((dest) < FirstNormalTransactionId) \
- (dest) = FirstNormalTransactionId; \
- } while(0)
+static inline void
+TransactionIdAdvance(TransactionId *dest)
+{
+ (*dest)++;
+
+ if (*dest < FirstNormalTransactionId)
+ *dest = FirstNormalTransactionId;
+}
/*
* Retreat a FullTransactionId variable, stepping over xids that would appear
@@ -138,10 +140,14 @@ FullTransactionIdAdvance(FullTransactionId *dest)
}
/* back up a transaction ID variable, handling wraparound correctly */
-#define TransactionIdRetreat(dest) \
- do { \
- (dest)--; \
- } while ((dest) < FirstNormalTransactionId)
+static inline void
+TransactionIdRetreat(TransactionId *dest)
+{
+ (*dest)--;
+
+ while (*dest < FirstNormalTransactionId)
+ (*dest)--;
+}
/* compare two XIDs already known to be normal; this is a macro for speed */
#define NormalTransactionIdPrecedes(id1, id2) \
--
2.37.0 (Apple Git-136)
Maxim Orlov <orlovmg@gmail.com> writes:
I've notice recent activity to convert macros into inline functions. We
should make TransactionIdRetreat/Advance functions
Instead of a macro, should we?
-1. Having to touch all the call sites like this outweighs
any claimed advantage: it makes them uglier and it will greatly
complicate any back-patching we might have to do in those areas.
regards, tom lane
-1. Having to touch all the call sites like this outweighs
any claimed advantage: it makes them uglier and it will greatly
complicate any back-patching we might have to do in those areas.regards, tom lane
Ok, got it. But what if we change the semantics of these calls to
xid = TransactionIdAdvance(xid) ?
--
Best regards,
Maxim Orlov.
Maxim Orlov <orlovmg@gmail.com> writes:
-1. Having to touch all the call sites like this outweighs
any claimed advantage: it makes them uglier and it will greatly
complicate any back-patching we might have to do in those areas.
Ok, got it. But what if we change the semantics of these calls to
xid = TransactionIdAdvance(xid) ?
Uh ... you'd still have to touch all the call sites.
regards, tom lane