Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0d76b60

Browse files
committed
Various micro-optimizations for GetSnapshopData().
Heikki Linnakangas had the idea of rearranging GetSnapshotData to avoid checking for sub-XIDs when no top-level XID is present. This patch does that plus further a bit of further, related rearrangement. Benchmarking show a significant improvement on unlogged tables at higher concurrency levels, and mostly indifferent result on permanent tables (which are presumably bottlenecked elsewhere). Most of the benefit seems to come from using the new NormalTransactionIdPrecedes() macro rather than the function call TransactionIdPrecedes().
1 parent a4cd6ab commit 0d76b60

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/backend/storage/ipc/procarray.c

+20-17
Original file line numberDiff line numberDiff line change
@@ -1324,30 +1324,33 @@ GetSnapshotData(Snapshot snapshot)
13241324
/* Update globalxmin to be the smallest valid xmin */
13251325
xid = pgxact->xmin; /* fetch just once */
13261326
if (TransactionIdIsNormal(xid) &&
1327-
TransactionIdPrecedes(xid, globalxmin))
1327+
NormalTransactionIdPrecedes(xid, globalxmin))
13281328
globalxmin = xid;
13291329

13301330
/* Fetch xid just once - see GetNewTransactionId */
13311331
xid = pgxact->xid;
13321332

13331333
/*
1334-
* If the transaction has been assigned an xid < xmax we add it to
1335-
* the snapshot, and update xmin if necessary. There's no need to
1336-
* store XIDs >= xmax, since we'll treat them as running anyway.
1337-
* We don't bother to examine their subxids either.
1338-
*
1339-
* We don't include our own XID (if any) in the snapshot, but we
1340-
* must include it into xmin.
1334+
* If the transaction has no XID assigned, we can skip it; it won't
1335+
* have sub-XIDs either. If the XID is >= xmax, we can also skip
1336+
* it; such transactions will be treated as running anyway (and any
1337+
* sub-XIDs will also be >= xmax).
13411338
*/
1342-
if (TransactionIdIsNormal(xid))
1343-
{
1344-
if (TransactionIdFollowsOrEquals(xid, xmax))
1339+
if (!TransactionIdIsNormal(xid)
1340+
|| !NormalTransactionIdPrecedes(xid, xmax))
13451341
continue;
1346-
if (pgxact != MyPgXact)
1347-
snapshot->xip[count++] = xid;
1348-
if (TransactionIdPrecedes(xid, xmin))
1349-
xmin = xid;
1350-
}
1342+
1343+
/*
1344+
* We don't include our own XIDs (if any) in the snapshot, but we
1345+
* must include them in xmin.
1346+
*/
1347+
if (NormalTransactionIdPrecedes(xid, xmin))
1348+
xmin = xid;
1349+
if (pgxact == MyPgXact)
1350+
continue;
1351+
1352+
/* Add XID to snapshot. */
1353+
snapshot->xip[count++] = xid;
13511354

13521355
/*
13531356
* Save subtransaction XIDs if possible (if we've already
@@ -1364,7 +1367,7 @@ GetSnapshotData(Snapshot snapshot)
13641367
*
13651368
* Again, our own XIDs are not included in the snapshot.
13661369
*/
1367-
if (!suboverflowed && pgxact != MyPgXact)
1370+
if (!suboverflowed)
13681371
{
13691372
if (pgxact->overflowed)
13701373
suboverflowed = true;

src/include/access/transam.h

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
(dest)--; \
5959
} while ((dest) < FirstNormalTransactionId)
6060

61+
/* compare two XIDs already known to be normal; this is a macro for speed */
62+
#define NormalTransactionIdPrecedes(id1, id2) \
63+
(AssertMacro(TransactionIdIsNormal(id1) && TransactionIdIsNormal(id2)), \
64+
(int32) ((id1) - (id2)) < 0)
6165

6266
/* ----------
6367
* Object ID (OID) zero is InvalidOid.

0 commit comments

Comments
 (0)