Skip to content

Commit

Permalink
Avoid SnapshotResetXmin() during AtEOXact_Snapshot()
Browse files Browse the repository at this point in the history
For normal commits and aborts we already reset PgXact->xmin,
so we can simply avoid running SnapshotResetXmin() twice.

During performance tests by Alexander Korotkov, diagnosis
by Andres Freund showed PgXact array as a bottleneck. After
manual analysis by me of the code paths that touch those
memory locations, I was able to identify extraneous code
in the main transaction commit path.

Avoiding touching highly contented shmem improves concurrent
performance slightly on all workloads, confirmed by tests
run by Ashutosh Sharma and Alexander Korotkov.

Simon Riggs

Discussion: CANP8+jJdXE9b+b9F8CQT-LuxxO0PBCB-SZFfMVAdp+akqo4zfg@mail.gmail.com
  • Loading branch information
simonat2ndQuadrant committed Apr 6, 2017
1 parent fd01983 commit 6bad580
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/backend/access/transam/xact.c
Original file line number Diff line number Diff line change
Expand Up @@ -2137,7 +2137,7 @@ CommitTransaction(void)
AtEOXact_ComboCid();
AtEOXact_HashTables(true);
AtEOXact_PgStat(true);
AtEOXact_Snapshot(true);
AtEOXact_Snapshot(true, false);
AtCommit_ApplyLauncher();
pgstat_report_xact_timestamp(0);

Expand Down Expand Up @@ -2409,7 +2409,7 @@ PrepareTransaction(void)
AtEOXact_ComboCid();
AtEOXact_HashTables(true);
/* don't call AtEOXact_PgStat here; we fixed pgstat state above */
AtEOXact_Snapshot(true);
AtEOXact_Snapshot(true, true);
pgstat_report_xact_timestamp(0);

CurrentResourceOwner = NULL;
Expand Down Expand Up @@ -2640,7 +2640,7 @@ CleanupTransaction(void)
* do abort cleanup processing
*/
AtCleanup_Portals(); /* now safe to release portal memory */
AtEOXact_Snapshot(false); /* and release the transaction's snapshots */
AtEOXact_Snapshot(false, false); /* and release the transaction's snapshots */

CurrentResourceOwner = NULL; /* and resource owner */
if (TopTransactionResourceOwner)
Expand Down
14 changes: 12 additions & 2 deletions src/backend/utils/time/snapmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ AtSubAbort_Snapshot(int level)
* Snapshot manager's cleanup function for end of transaction
*/
void
AtEOXact_Snapshot(bool isCommit)
AtEOXact_Snapshot(bool isCommit, bool isPrepare)
{
/*
* In transaction-snapshot mode we must release our privately-managed
Expand Down Expand Up @@ -1136,7 +1136,17 @@ AtEOXact_Snapshot(bool isCommit)

FirstSnapshotSet = false;

SnapshotResetXmin();
/*
* During normal commit and abort processing, we call
* ProcArrayEndTransaction() or ProcArrayClearTransaction() to
* reset the PgXact->xmin. That call happens prior to the call to
* AtEOXact_Snapshot(), so we need not touch xmin here at all,
* accept when we are preparing a transaction.
*/
if (isPrepare)
SnapshotResetXmin();

Assert(isPrepare || MyPgXact->xmin == 0);
}


Expand Down
2 changes: 1 addition & 1 deletion src/include/utils/snapmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ extern void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner);

extern void AtSubCommit_Snapshot(int level);
extern void AtSubAbort_Snapshot(int level);
extern void AtEOXact_Snapshot(bool isCommit);
extern void AtEOXact_Snapshot(bool isCommit, bool isPrepare);

extern void ImportSnapshot(const char *idstr);
extern bool XactHasExportedSnapshots(void);
Expand Down

0 comments on commit 6bad580

Please sign in to comment.