Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf83b599

Browse files
committed
Make walsender more responsive.
Per testing by Andres Freund, this improves replication performanceand reduces replication latency and latency jitter. I was a bitconcerned about moving more work into XLogInsert, but testing seemsto show that it's not a problem in practice.Along the way, improve comments for WaitLatchOrSocket.Andres Freund. Review and stylistic cleanup by me.
1 parent9ad45c1 commitf83b599

File tree

7 files changed

+59
-36
lines changed

7 files changed

+59
-36
lines changed

‎src/backend/access/transam/twophase.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,13 +1042,6 @@ EndPrepare(GlobalTransaction gxact)
10421042

10431043
/* If we crash now, we have prepared: WAL replay will fix things */
10441044

1045-
/*
1046-
* Wake up all walsenders to send WAL up to the PREPARE record immediately
1047-
* if replication is enabled
1048-
*/
1049-
if (max_wal_senders>0)
1050-
WalSndWakeup();
1051-
10521045
/* write correct CRC and close file */
10531046
if ((write(fd,&statefile_crc,sizeof(pg_crc32)))!=sizeof(pg_crc32))
10541047
{
@@ -2045,13 +2038,6 @@ RecordTransactionCommitPrepared(TransactionId xid,
20452038
/* Flush XLOG to disk */
20462039
XLogFlush(recptr);
20472040

2048-
/*
2049-
* Wake up all walsenders to send WAL up to the COMMIT PREPARED record
2050-
* immediately if replication is enabled
2051-
*/
2052-
if (max_wal_senders>0)
2053-
WalSndWakeup();
2054-
20552041
/* Mark the transaction committed in pg_clog */
20562042
TransactionIdCommitTree(xid,nchildren,children);
20572043

@@ -2132,13 +2118,6 @@ RecordTransactionAbortPrepared(TransactionId xid,
21322118
/* Always flush, since we're about to remove the 2PC state file */
21332119
XLogFlush(recptr);
21342120

2135-
/*
2136-
* Wake up all walsenders to send WAL up to the ABORT PREPARED record
2137-
* immediately if replication is enabled
2138-
*/
2139-
if (max_wal_senders>0)
2140-
WalSndWakeup();
2141-
21422121
/*
21432122
* Mark the transaction aborted in clog. This is not absolutely necessary
21442123
* but we may as well do it while we are here.

‎src/backend/access/transam/xact.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,13 +1141,6 @@ RecordTransactionCommit(void)
11411141

11421142
XLogFlush(XactLastRecEnd);
11431143

1144-
/*
1145-
* Wake up all walsenders to send WAL up to the COMMIT record
1146-
* immediately if replication is enabled
1147-
*/
1148-
if (max_wal_senders>0)
1149-
WalSndWakeup();
1150-
11511144
/*
11521145
* Now we may update the CLOG, if we wrote a COMMIT record above
11531146
*/

‎src/backend/access/transam/xlog.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,8 @@ begin:;
10251025

10261026
END_CRIT_SECTION();
10271027

1028+
/* wakeup the WalSnd now that we released the WALWriteLock */
1029+
WalSndWakeupProcessRequests();
10281030
returnRecPtr;
10291031
}
10301032

@@ -1208,6 +1210,9 @@ begin:;
12081210

12091211
END_CRIT_SECTION();
12101212

1213+
/* wakeup the WalSnd now that we outside contented locks */
1214+
WalSndWakeupProcessRequests();
1215+
12111216
returnRecPtr;
12121217
}
12131218

@@ -1792,6 +1797,10 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible, bool xlog_switch)
17921797
if (finishing_seg|| (xlog_switch&&last_iteration))
17931798
{
17941799
issue_xlog_fsync(openLogFile,openLogSegNo);
1800+
1801+
/* signal that we need to wakeup WalSnd later */
1802+
WalSndWakeupRequest();
1803+
17951804
LogwrtResult.Flush=LogwrtResult.Write;/* end of page */
17961805

17971806
if (XLogArchivingActive())
@@ -1854,7 +1863,11 @@ XLogWrite(XLogwrtRqst WriteRqst, bool flexible, bool xlog_switch)
18541863
openLogFile=XLogFileOpen(openLogSegNo);
18551864
openLogOff=0;
18561865
}
1866+
18571867
issue_xlog_fsync(openLogFile,openLogSegNo);
1868+
1869+
/* signal that we need to wakeup WalSnd later */
1870+
WalSndWakeupRequest();
18581871
}
18591872
LogwrtResult.Flush=LogwrtResult.Write;
18601873
}
@@ -2120,6 +2133,9 @@ XLogFlush(XLogRecPtr record)
21202133

21212134
END_CRIT_SECTION();
21222135

2136+
/* wakeup the WalSnd now that we released the WALWriteLock */
2137+
WalSndWakeupProcessRequests();
2138+
21232139
/*
21242140
* If we still haven't flushed to the request point then we have a
21252141
* problem; most likely, the requested flush point is past end of XLOG.
@@ -2245,13 +2261,8 @@ XLogBackgroundFlush(void)
22452261

22462262
END_CRIT_SECTION();
22472263

2248-
/*
2249-
* If we wrote something then we have something to send to standbys also,
2250-
* otherwise the replication delay become around 7s with just async
2251-
* commit.
2252-
*/
2253-
if (wrote_something)
2254-
WalSndWakeup();
2264+
/* wakeup the WalSnd now that we released the WALWriteLock */
2265+
WalSndWakeupProcessRequests();
22552266

22562267
returnwrote_something;
22572268
}

‎src/backend/port/unix_latch.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,9 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
418418
* NB: when calling this in a signal handler, be sure to save and restore
419419
* errno around it. (That's standard practice in most signal handlers, of
420420
* course, but we used to omit it in handlers that only set a flag.)
421+
*
422+
* NB: this function is called from critical sections and signal handlers so
423+
* throwing an error is not a good idea.
421424
*/
422425
void
423426
SetLatch(volatileLatch*latch)

‎src/backend/port/win32_latch.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
247247
returnresult;
248248
}
249249

250+
/*
251+
* The comments above the unix implementation (unix_latch.c) of this function
252+
* apply here as well.
253+
*/
250254
void
251255
SetLatch(volatileLatch*latch)
252256
{

‎src/backend/replication/walsender.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ boolam_cascading_walsender = false;/* Am I cascading WAL to
8181
intmax_wal_senders=0;/* the maximum number of concurrent walsenders */
8282
intreplication_timeout=60*1000;/* maximum time to send one
8383
* WAL data message */
84+
/*
85+
* State for WalSndWakeupRequest
86+
*/
87+
boolwake_wal_senders= false;
8488

8589
/*
8690
* These variables are used similarly to openLogFile/Id/Seg/Off,
@@ -1395,7 +1399,12 @@ WalSndShmemInit(void)
13951399
}
13961400
}
13971401

1398-
/* Wake up all walsenders */
1402+
/*
1403+
* Wake up all walsenders
1404+
*
1405+
* This will be called inside critical sections, so throwing an error is not
1406+
* adviseable.
1407+
*/
13991408
void
14001409
WalSndWakeup(void)
14011410
{

‎src/include/replication/walsender.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern bool am_walsender;
2121
externboolam_cascading_walsender;
2222
externvolatilesig_atomic_twalsender_shutdown_requested;
2323
externvolatilesig_atomic_twalsender_ready_to_stop;
24+
externboolwake_wal_senders;
2425

2526
/* user-settable parameters */
2627
externintmax_wal_senders;
@@ -35,4 +36,27 @@ extern void WalSndRqstFileReload(void);
3536

3637
externDatumpg_stat_get_wal_senders(PG_FUNCTION_ARGS);
3738

39+
/*
40+
* Remember that we want to wakeup walsenders later
41+
*
42+
* This is separated from doing the actual wakeup because the writeout is done
43+
* while holding contended locks.
44+
*/
45+
#defineWalSndWakeupRequest() \
46+
do { wake_wal_senders = true; } while (0)
47+
48+
/*
49+
* wakeup walsenders if there is work to be done
50+
*/
51+
#defineWalSndWakeupProcessRequests()\
52+
do\
53+
{\
54+
if (wake_wal_senders)\
55+
{\
56+
wake_wal_senders = false;\
57+
if (max_wal_senders > 0)\
58+
WalSndWakeup();\
59+
}\
60+
} while (0)
61+
3862
#endif/* _WALSENDER_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp