Skip to content

Commit e620ee3

Browse files
Optimize commit_siblings in two ways to improve group commit.
First, avoid scanning the whole ProcArray once we know there are at least commit_siblings active; second, skip the check altogether if commit_siblings = 0. Greg Smith
1 parent 5a031a5 commit e620ee3

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

doc/src/sgml/config.sgml

+12-5
Original file line numberDiff line numberDiff line change
@@ -1683,17 +1683,24 @@ SET ENABLE_SEQSCAN TO OFF;
16831683
</indexterm>
16841684
<listitem>
16851685
<para>
1686-
Time delay between writing a commit record to the WAL buffer
1687-
and flushing the buffer out to disk, in microseconds. A
1688-
nonzero delay can allow multiple transactions to be committed
1689-
with only one <function>fsync()</function> system call, if
1686+
When the commit data for a transaction is flushed to disk, any
1687+
additional commits ready at that time are also flushed out.
1688+
<varname>commit_delay</varname> adds a time delay, set in
1689+
microseconds, before writing some commit records to the WAL
1690+
buffer and flushing the buffer out to disks. A nonzero delay
1691+
can allow more transactions to be committed with only one call
1692+
to the active <varname>wal_sync_method</varname>, if
16901693
system load is high enough that additional transactions become
16911694
ready to commit within the given interval. But the delay is
16921695
just wasted if no other transactions become ready to
16931696
commit. Therefore, the delay is only performed if at least
16941697
<varname>commit_siblings</varname> other transactions are
16951698
active at the instant that a server process has written its
1696-
commit record. The default is zero (no delay).
1699+
commit record. The default is zero (no delay). Since
1700+
all pending commit data flushes are written at every flush
1701+
regardless of this setting, it is rare that adding delay to
1702+
that by increasing this parameter will actually improve commit
1703+
performance.
16971704
</para>
16981705
</listitem>
16991706
</varlistentry>

src/backend/access/transam/xact.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ RecordTransactionCommit(void)
10521052
* fewer than CommitSiblings other backends with active transactions.
10531053
*/
10541054
if (CommitDelay > 0 && enableFsync &&
1055-
CountActiveBackends() >= CommitSiblings)
1055+
MinimumActiveBackends(CommitSiblings))
10561056
pg_usleep(CommitDelay);
10571057

10581058
XLogFlush(XactLastRecEnd);

src/backend/storage/ipc/procarray.c

+12-5
Original file line numberDiff line numberDiff line change
@@ -1886,20 +1886,25 @@ CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode)
18861886
}
18871887

18881888
/*
1889-
* CountActiveBackends --- count backends (other than myself) that are in
1890-
* active transactions. This is used as a heuristic to decide if
1889+
* MinimumActiveBackends --- count backends (other than myself) that are
1890+
* in active transactions. Return true if the count exceeds the
1891+
* minimum threshold passed. This is used as a heuristic to decide if
18911892
* a pre-XLOG-flush delay is worthwhile during commit.
18921893
*
18931894
* Do not count backends that are blocked waiting for locks, since they are
18941895
* not going to get to run until someone else commits.
18951896
*/
1896-
int
1897-
CountActiveBackends(void)
1897+
bool
1898+
MinimumActiveBackends(int min)
18981899
{
18991900
ProcArrayStruct *arrayP = procArray;
19001901
int count = 0;
19011902
int index;
19021903

1904+
/* Quick short-circuit if no minimum is specified */
1905+
if (min == 0)
1906+
return true;
1907+
19031908
/*
19041909
* Note: for speed, we don't acquire ProcArrayLock. This is a little bit
19051910
* bogus, but since we are only testing fields for zero or nonzero, it
@@ -1932,9 +1937,11 @@ CountActiveBackends(void)
19321937
if (proc->waitLock != NULL)
19331938
continue; /* do not count if blocked on a lock */
19341939
count++;
1940+
if (count >= min)
1941+
break;
19351942
}
19361943

1937-
return count;
1944+
return count >= min;
19381945
}
19391946

19401947
/*

src/backend/utils/misc/guc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1816,7 +1816,7 @@ static struct config_int ConfigureNamesInt[] =
18161816
NULL
18171817
},
18181818
&CommitSiblings,
1819-
5, 1, 1000, NULL, NULL
1819+
5, 0, 1000, NULL, NULL
18201820
},
18211821

18221822
{

src/include/storage/procarray.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ extern VirtualTransactionId *GetCurrentVirtualXIDs(TransactionId limitXmin,
6060
extern VirtualTransactionId *GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid);
6161
extern pid_t CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode);
6262

63-
extern int CountActiveBackends(void);
63+
extern bool MinimumActiveBackends(int min);
6464
extern int CountDBBackends(Oid databaseid);
6565
extern void CancelDBBackends(Oid databaseid, ProcSignalReason sigmode, bool conflictPending);
6666
extern int CountUserBackends(Oid roleid);

0 commit comments

Comments
 (0)