Skip to content

Commit 42b4b0b

Browse files
Avoid SnapshotResetXmin() during AtEOXact_Snapshot()
For normal commits and aborts we already reset PgXact->xmin Avoiding touching highly contented shmem improves concurrent performance. Simon Riggs Discussion: CANP8+jJdXE9b+b9F8CQT-LuxxO0PBCB-SZFfMVAdp+akqo4zfg@mail.gmail.com
1 parent 8398c83 commit 42b4b0b

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/backend/access/transam/xact.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -2137,7 +2137,7 @@ CommitTransaction(void)
21372137
AtEOXact_ComboCid();
21382138
AtEOXact_HashTables(true);
21392139
AtEOXact_PgStat(true);
2140-
AtEOXact_Snapshot(true);
2140+
AtEOXact_Snapshot(true, false);
21412141
AtCommit_ApplyLauncher();
21422142
pgstat_report_xact_timestamp(0);
21432143

@@ -2409,7 +2409,7 @@ PrepareTransaction(void)
24092409
AtEOXact_ComboCid();
24102410
AtEOXact_HashTables(true);
24112411
/* don't call AtEOXact_PgStat here; we fixed pgstat state above */
2412-
AtEOXact_Snapshot(true);
2412+
AtEOXact_Snapshot(true, true);
24132413
pgstat_report_xact_timestamp(0);
24142414

24152415
CurrentResourceOwner = NULL;
@@ -2640,7 +2640,7 @@ CleanupTransaction(void)
26402640
* do abort cleanup processing
26412641
*/
26422642
AtCleanup_Portals(); /* now safe to release portal memory */
2643-
AtEOXact_Snapshot(false); /* and release the transaction's snapshots */
2643+
AtEOXact_Snapshot(false, false); /* and release the transaction's snapshots */
26442644

26452645
CurrentResourceOwner = NULL; /* and resource owner */
26462646
if (TopTransactionResourceOwner)

src/backend/utils/time/snapmgr.c

+18-3
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,12 @@ xmin_cmp(const pairingheap_node *a, const pairingheap_node *b, void *arg)
954954
*
955955
* If there are no more snapshots, we can reset our PGXACT->xmin to InvalidXid.
956956
* Note we can do this without locking because we assume that storing an Xid
957-
* is atomic.
957+
* is atomic. We do this because it will allow multi-statement transactions to
958+
* reset their xmin and prevent us from holding back removal of dead rows;
959+
* this has little purpose when we are dealing with very fast statements in
960+
* read committed mode since the xmin will advance quickly anyway. It has no
961+
* use at all when we are running single statement transactions since the xmin
962+
* is reset as part of end of transaction anyway.
958963
*
959964
* Even if there are some remaining snapshots, we may be able to advance our
960965
* PGXACT->xmin to some degree. This typically happens when a portal is
@@ -1051,7 +1056,7 @@ AtSubAbort_Snapshot(int level)
10511056
* Snapshot manager's cleanup function for end of transaction
10521057
*/
10531058
void
1054-
AtEOXact_Snapshot(bool isCommit)
1059+
AtEOXact_Snapshot(bool isCommit, bool isPrepare)
10551060
{
10561061
/*
10571062
* In transaction-snapshot mode we must release our privately-managed
@@ -1136,7 +1141,17 @@ AtEOXact_Snapshot(bool isCommit)
11361141

11371142
FirstSnapshotSet = false;
11381143

1139-
SnapshotResetXmin();
1144+
/*
1145+
* During normal commit and abort processing, we call
1146+
* ProcArrayEndTransaction() or ProcArrayClearTransaction() to
1147+
* reset the PgXact->xmin. That call happens prior to the call to
1148+
* AtEOXact_Snapshot(), so we need not touch xmin here at all,
1149+
* accept when we are preparing a transaction.
1150+
*/
1151+
if (isPrepare)
1152+
SnapshotResetXmin();
1153+
1154+
Assert(MyPgXact->xmin == 0);
11401155
}
11411156

11421157

src/include/utils/snapmgr.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ extern void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner);
8585

8686
extern void AtSubCommit_Snapshot(int level);
8787
extern void AtSubAbort_Snapshot(int level);
88-
extern void AtEOXact_Snapshot(bool isCommit);
88+
extern void AtEOXact_Snapshot(bool isCommit, bool isPrepare);
8989

9090
extern void ImportSnapshot(const char *idstr);
9191
extern bool XactHasExportedSnapshots(void);

0 commit comments

Comments
 (0)