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

Commit 309b2cf

Browse files
committed
Have GetCurrentTransactionStopTimestamp() set xactStopTimestamp if unset
Previously GetCurrentTransactionStopTimestamp() computed a new timestamp whenever xactStopTimestamp was unset and xactStopTimestamp was only set when a commit or abort record was written. An upcoming patch will add additional calls to GetCurrentTransactionStopTimestamp() from pgstats. To avoid computing timestamps multiple times, set xactStopTimestamp in GetCurrentTransactionStopTimestamp() if not already set. Author: Dave Page <dpage@pgadmin.org> Reviewed-by: Andres Freund <andres@anarazel.de> Reviewed-by: Vik Fearing <vik@postgresfriends.org> Discussion: https://postgr.es/m/20220906155325.an3xesq5o3fq36gt%40awork3.anarazel.de
1 parent db1b931 commit 309b2cf

File tree

1 file changed

+21
-21
lines changed
  • src/backend/access/transam

1 file changed

+21
-21
lines changed

src/backend/access/transam/xact.c

+21-21
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,10 @@ static bool currentCommandIdUsed;
263263
/*
264264
* xactStartTimestamp is the value of transaction_timestamp().
265265
* stmtStartTimestamp is the value of statement_timestamp().
266-
* xactStopTimestamp is the time at which we log a commit or abort WAL record.
266+
* xactStopTimestamp is the time at which we log a commit / abort WAL record,
267+
* or if that was skipped, the time of the first subsequent
268+
* GetCurrentTransactionStopTimestamp() call.
269+
*
267270
* These do not change as we enter and exit subtransactions, so we don't
268271
* keep them inside the TransactionState stack.
269272
*/
@@ -865,15 +868,24 @@ GetCurrentStatementStartTimestamp(void)
865868
/*
866869
* GetCurrentTransactionStopTimestamp
867870
*
868-
* We return current time if the transaction stop time hasn't been set
869-
* (which can happen if we decide we don't need to log an XLOG record).
871+
* If the transaction stop time hasn't already been set, which can happen if
872+
* we decided we don't need to log an XLOG record, set xactStopTimestamp.
870873
*/
871874
TimestampTz
872875
GetCurrentTransactionStopTimestamp(void)
873876
{
874-
if (xactStopTimestamp != 0)
875-
return xactStopTimestamp;
876-
return GetCurrentTimestamp();
877+
TransactionState s PG_USED_FOR_ASSERTS_ONLY = CurrentTransactionState;
878+
879+
/* should only be called after commit / abort processing */
880+
Assert(s->state == TRANS_DEFAULT ||
881+
s->state == TRANS_COMMIT ||
882+
s->state == TRANS_ABORT ||
883+
s->state == TRANS_PREPARE);
884+
885+
if (xactStopTimestamp == 0)
886+
xactStopTimestamp = GetCurrentTimestamp();
887+
888+
return xactStopTimestamp;
877889
}
878890

879891
/*
@@ -891,15 +903,6 @@ SetCurrentStatementStartTimestamp(void)
891903
Assert(stmtStartTimestamp != 0);
892904
}
893905

894-
/*
895-
* SetCurrentTransactionStopTimestamp
896-
*/
897-
static inline void
898-
SetCurrentTransactionStopTimestamp(void)
899-
{
900-
xactStopTimestamp = GetCurrentTimestamp();
901-
}
902-
903906
/*
904907
* GetCurrentTransactionNestLevel
905908
*
@@ -1396,9 +1399,7 @@ RecordTransactionCommit(void)
13961399
START_CRIT_SECTION();
13971400
MyProc->delayChkptFlags |= DELAY_CHKPT_START;
13981401

1399-
SetCurrentTransactionStopTimestamp();
1400-
1401-
XactLogCommitRecord(xactStopTimestamp,
1402+
XactLogCommitRecord(GetCurrentTransactionStopTimestamp(),
14021403
nchildren, children, nrels, rels,
14031404
ndroppedstats, droppedstats,
14041405
nmsgs, invalMessages,
@@ -1422,7 +1423,7 @@ RecordTransactionCommit(void)
14221423
*/
14231424

14241425
if (!replorigin || replorigin_session_origin_timestamp == 0)
1425-
replorigin_session_origin_timestamp = xactStopTimestamp;
1426+
replorigin_session_origin_timestamp = GetCurrentTransactionStopTimestamp();
14261427

14271428
TransactionTreeSetCommitTsData(xid, nchildren, children,
14281429
replorigin_session_origin_timestamp,
@@ -1754,8 +1755,7 @@ RecordTransactionAbort(bool isSubXact)
17541755
xact_time = GetCurrentTimestamp();
17551756
else
17561757
{
1757-
SetCurrentTransactionStopTimestamp();
1758-
xact_time = xactStopTimestamp;
1758+
xact_time = GetCurrentTransactionStopTimestamp();
17591759
}
17601760

17611761
XactLogAbortRecord(xact_time,

0 commit comments

Comments
 (0)