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

Commit 6f3bd98

Browse files
committed
Extend framework from commit 53be0b1 to report latch waits.
WaitLatch, WaitLatchOrSocket, and WaitEventSetWait now taken an additional wait_event_info parameter; legal values are defined in pgstat.h. This makes it possible to uniquely identify every point in the core code where we are waiting for a latch; extensions can pass WAIT_EXTENSION. Because latches were the major wait primitive not previously covered by this patch, it is now possible to see information in pg_stat_activity on a large number of important wait events not previously addressed, such as ClientRead, ClientWrite, and SyncRep. Unfortunately, many of the wait events added by this patch will fail to appear in pg_stat_activity because they're only used in background processes which don't currently appear in pg_stat_activity. We should fix this either by creating a separate view for such information, or else by deciding to include them in pg_stat_activity after all. Michael Paquier and Robert Haas, reviewed by Alexander Korotkov and Thomas Munro.
1 parent 490ed1e commit 6f3bd98

35 files changed

+584
-83
lines changed

contrib/postgres_fdw/connection.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "access/xact.h"
1818
#include "mb/pg_wchar.h"
1919
#include "miscadmin.h"
20+
#include "pgstat.h"
2021
#include "storage/latch.h"
2122
#include "utils/hsearch.h"
2223
#include "utils/memutils.h"
@@ -496,7 +497,7 @@ pgfdw_get_result(PGconn *conn, const char *query)
496497
wc = WaitLatchOrSocket(MyLatch,
497498
WL_LATCH_SET | WL_SOCKET_READABLE,
498499
PQsocket(conn),
499-
-1L);
500+
-1L, WAIT_EXTENSION);
500501
ResetLatch(MyLatch);
501502

502503
CHECK_FOR_INTERRUPTS();

doc/src/sgml/monitoring.sgml

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,42 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser
679679
buffer in question.
680680
</para>
681681
</listitem>
682+
<listitem>
683+
<para>
684+
<literal>Activity</>: The server process is idle. This is used by
685+
system processes waiting for activity in their main processing loop.
686+
<literal>wait_event</> will identify the specific wait point.
687+
</para>
688+
</listitem>
689+
<listitem>
690+
<para>
691+
<literal>Extension</>: The server process is waiting for activity
692+
in an extension module. This category is useful for modules to
693+
track custom waiting points.
694+
</para>
695+
</listitem>
696+
<listitem>
697+
<para>
698+
<literal>Client</>: The server process is waiting for some activity
699+
on a socket from user applications, and that the server expects
700+
something to happen that is independent from its internal processes.
701+
<literal>wait_event</> will identify the specific wait point.
702+
</para>
703+
</listitem>
704+
<listitem>
705+
<para>
706+
<literal>IPC</>: The server process is waiting for some activity
707+
from another process in the server. <literal>wait_event</> will
708+
identify the specific wait point.
709+
</para>
710+
</listitem>
711+
<listitem>
712+
<para>
713+
<literal>Timeout</>: The server process is waiting for a timeout
714+
to expire. <literal>wait_event</> will identify the specific wait
715+
point.
716+
</para>
717+
</listitem>
682718
</itemizedlist>
683719
</entry>
684720
</row>
@@ -1085,6 +1121,139 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser
10851121
<entry><literal>BufferPin</></entry>
10861122
<entry>Waiting to acquire a pin on a buffer.</entry>
10871123
</row>
1124+
<row>
1125+
<entry morerows="11"><literal>Activity</></entry>
1126+
<entry><literal>ArchiverMain</></entry>
1127+
<entry>Waiting in main loop of the archiver process.</entry>
1128+
</row>
1129+
<row>
1130+
<entry><literal>AutoVacuumMain</></entry>
1131+
<entry>Waiting in main loop of autovacuum launcher process.</entry>
1132+
</row>
1133+
<row>
1134+
<entry><literal>BgWriterHibernate</></entry>
1135+
<entry>Waiting in background writer process, hibernating.</entry>
1136+
</row>
1137+
<row>
1138+
<entry><literal>BgWriterMain</></entry>
1139+
<entry>Waiting in main loop of background writer process background worker.</entry>
1140+
</row>
1141+
<row>
1142+
<entry><literal>CheckpointerMain</></entry>
1143+
<entry>Waiting in main loop of checkpointer process.</entry>
1144+
</row>
1145+
<row>
1146+
<entry><literal>PgStatMain</></entry>
1147+
<entry>Waiting in main loop of the statistics collector process.</entry>
1148+
</row>
1149+
<row>
1150+
<entry><literal>RecoveryWalAll</></entry>
1151+
<entry>Waiting for WAL from any kind of source (local, archive or stream) at recovery.</entry>
1152+
</row>
1153+
<row>
1154+
<entry><literal>RecoveryWalStream</></entry>
1155+
<entry>Waiting for WAL from a stream at recovery.</entry>
1156+
</row>
1157+
<row>
1158+
<entry><literal>SysLoggerMain</></entry>
1159+
<entry>Waiting in main loop of syslogger process.</entry>
1160+
</row>
1161+
<row>
1162+
<entry><literal>WalReceiverMain</></entry>
1163+
<entry>Waiting in main loop of WAL receiver process.</entry>
1164+
</row>
1165+
<row>
1166+
<entry><literal>WalSenderMain</></entry>
1167+
<entry>Waiting in main loop of WAL sender process.</entry>
1168+
</row>
1169+
<row>
1170+
<entry><literal>WalWriterMain</></entry>
1171+
<entry>Waiting in main loop of WAL writer process.</entry>
1172+
</row>
1173+
<row>
1174+
<entry morerows="5"><literal>Client</></entry>
1175+
<entry><literal>ClientRead</></entry>
1176+
<entry>Waiting to read data from the client.</entry>
1177+
</row>
1178+
<row>
1179+
<entry><literal>ClientWrite</></entry>
1180+
<entry>Waiting to write data from the client.</entry>
1181+
</row>
1182+
<row>
1183+
<entry><literal>SSLOpenServer</></entry>
1184+
<entry>Waiting for SSL while attempting connection.</entry>
1185+
</row>
1186+
<row>
1187+
<entry><literal>WalReceiverWaitStart</></entry>
1188+
<entry>Waiting for startup process to send initial data for streaming replication.</entry>
1189+
</row>
1190+
<row>
1191+
<entry><literal>WalSenderWaitForWAL</></entry>
1192+
<entry>Waiting for WAL to be flushed in WAL sender process.</entry>
1193+
</row>
1194+
<row>
1195+
<entry><literal>WalSenderWriteData</></entry>
1196+
<entry>Waiting for any activity when processing replies from WAL receiver in WAL sender process.</entry>
1197+
</row>
1198+
<row>
1199+
<entry><literal>Extension</></entry>
1200+
<entry><literal>Extension</></entry>
1201+
<entry>Waiting in an extension.</entry>
1202+
</row>
1203+
<row>
1204+
<entry morerows="9"><literal>IPC</></entry>
1205+
<entry><literal>BgWorkerShutdown</></entry>
1206+
<entry>Waiting for background worker to shut down.</entry>
1207+
</row>
1208+
<row>
1209+
<entry><literal>BgWorkerStartup</></entry>
1210+
<entry>Waiting for background worker to start up.</entry>
1211+
</row>
1212+
<row>
1213+
<entry><literal>ExecuteGather</></entry>
1214+
<entry>Waiting for activity from child process when executing <literal>Gather</> node.</entry>
1215+
</row>
1216+
<row>
1217+
<entry><literal>MessageQueueInternal</></entry>
1218+
<entry>Waiting for other process to be attached in shared message queue.</entry>
1219+
</row>
1220+
<row>
1221+
<entry><literal>MessageQueuePutMessage</></entry>
1222+
<entry>Waiting to write a protoocol message to a shared message queue.</entry>
1223+
</row>
1224+
<row>
1225+
<entry><literal>MessageQueueReceive</></entry>
1226+
<entry>Waiting to receive bytes from a shared message queue.</entry>
1227+
</row>
1228+
<row>
1229+
<entry><literal>MessageQueueSend</></entry>
1230+
<entry>Waiting to send bytes to a shared message queue.</entry>
1231+
</row>
1232+
<row>
1233+
<entry><literal>ParallelFinish</></entry>
1234+
<entry>Waiting for parallel workers to finish computing.</entry>
1235+
</row>
1236+
<row>
1237+
<entry><literal>SafeSnapshot</></entry>
1238+
<entry>Waiting for a snapshot for a <literal>READ ONLY DEFERRABLE</> transaction.</entry>
1239+
</row>
1240+
<row>
1241+
<entry><literal>SyncRep</></entry>
1242+
<entry>Waiting for confirmation from remote server during synchronous replication.</entry>
1243+
</row>
1244+
<row>
1245+
<entry morerows="2"><literal>Timeout</></entry>
1246+
<entry><literal>BaseBackupThrottle</></entry>
1247+
<entry>Waiting during base backup when throttling activity.</entry>
1248+
</row>
1249+
<row>
1250+
<entry><literal>PgSleep</></entry>
1251+
<entry>Waiting in process that called <function>pg_sleep</>.</entry>
1252+
</row>
1253+
<row>
1254+
<entry><literal>RecoveryApplyDelay</></entry>
1255+
<entry>Waiting to apply WAL at recovery because it is delayed.</entry>
1256+
</row>
10881257
</tbody>
10891258
</tgroup>
10901259
</table>

src/backend/access/transam/parallel.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "libpq/pqmq.h"
2525
#include "miscadmin.h"
2626
#include "optimizer/planmain.h"
27+
#include "pgstat.h"
2728
#include "storage/ipc.h"
2829
#include "storage/sinval.h"
2930
#include "storage/spin.h"
@@ -540,7 +541,8 @@ WaitForParallelWorkersToFinish(ParallelContext *pcxt)
540541
if (!anyone_alive)
541542
break;
542543

543-
WaitLatch(&MyProc->procLatch, WL_LATCH_SET, -1);
544+
WaitLatch(&MyProc->procLatch, WL_LATCH_SET, -1,
545+
WAIT_EVENT_PARALLEL_FINISH);
544546
ResetLatch(&MyProc->procLatch);
545547
}
546548

src/backend/access/transam/xlog.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5827,7 +5827,8 @@ recoveryApplyDelay(XLogReaderState *record)
58275827

58285828
WaitLatch(&XLogCtl->recoveryWakeupLatch,
58295829
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
5830-
secs * 1000L + microsecs / 1000);
5830+
secs * 1000L + microsecs / 1000,
5831+
WAIT_EVENT_RECOVERY_APPLY_DELAY);
58315832
}
58325833
return true;
58335834
}
@@ -11387,7 +11388,7 @@ WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
1138711388

1138811389
WaitLatch(&XLogCtl->recoveryWakeupLatch,
1138911390
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
11390-
wait_time);
11391+
wait_time, WAIT_EVENT_RECOVERY_WAL_STREAM);
1139111392
ResetLatch(&XLogCtl->recoveryWakeupLatch);
1139211393
now = GetCurrentTimestamp();
1139311394
}
@@ -11550,7 +11551,7 @@ WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
1155011551
*/
1155111552
WaitLatch(&XLogCtl->recoveryWakeupLatch,
1155211553
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
11553-
5000L);
11554+
5000L, WAIT_EVENT_RECOVERY_WAL_ALL);
1155411555
ResetLatch(&XLogCtl->recoveryWakeupLatch);
1155511556
break;
1155611557
}

src/backend/executor/nodeGather.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "executor/nodeSubplan.h"
3939
#include "executor/tqueue.h"
4040
#include "miscadmin.h"
41+
#include "pgstat.h"
4142
#include "utils/memutils.h"
4243
#include "utils/rel.h"
4344

@@ -387,7 +388,7 @@ gather_readnext(GatherState *gatherstate)
387388
return NULL;
388389

389390
/* Nothing to do except wait for developments. */
390-
WaitLatch(MyLatch, WL_LATCH_SET, 0);
391+
WaitLatch(MyLatch, WL_LATCH_SET, 0, WAIT_EVENT_EXECUTE_GATHER);
391392
ResetLatch(MyLatch);
392393
nvisited = 0;
393394
}

src/backend/libpq/be-secure-openssl.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
#include "libpq/libpq.h"
6262
#include "miscadmin.h"
63+
#include "pgstat.h"
6364
#include "storage/latch.h"
6465
#include "tcop/tcopprot.h"
6566
#include "utils/memutils.h"
@@ -419,7 +420,8 @@ be_tls_open_server(Port *port)
419420
else
420421
waitfor = WL_SOCKET_WRITEABLE;
421422

422-
WaitLatchOrSocket(MyLatch, waitfor, port->sock, 0);
423+
WaitLatchOrSocket(MyLatch, waitfor, port->sock, 0,
424+
WAIT_EVENT_SSL_OPEN_SERVER);
423425
goto aloop;
424426
case SSL_ERROR_SYSCALL:
425427
if (r < 0)

src/backend/libpq/be-secure.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "libpq/libpq.h"
3535
#include "miscadmin.h"
36+
#include "pgstat.h"
3637
#include "tcop/tcopprot.h"
3738
#include "utils/memutils.h"
3839
#include "storage/ipc.h"
@@ -146,7 +147,8 @@ secure_read(Port *port, void *ptr, size_t len)
146147

147148
ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
148149

149-
WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1);
150+
WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
151+
WAIT_EVENT_CLIENT_READ);
150152

151153
/*
152154
* If the postmaster has died, it's not safe to continue running,
@@ -247,7 +249,8 @@ secure_write(Port *port, void *ptr, size_t len)
247249

248250
ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
249251

250-
WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1);
252+
WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
253+
WAIT_EVENT_CLIENT_WRITE);
251254

252255
/* See comments in secure_read. */
253256
if (event.events & WL_POSTMASTER_DEATH)

src/backend/libpq/pqmq.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "libpq/pqformat.h"
1818
#include "libpq/pqmq.h"
1919
#include "miscadmin.h"
20+
#include "pgstat.h"
2021
#include "tcop/tcopprot.h"
2122
#include "utils/builtins.h"
2223

@@ -171,7 +172,8 @@ mq_putmessage(char msgtype, const char *s, size_t len)
171172
if (result != SHM_MQ_WOULD_BLOCK)
172173
break;
173174

174-
WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0);
175+
WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0,
176+
WAIT_EVENT_MQ_PUT_MESSAGE);
175177
ResetLatch(&MyProc->procLatch);
176178
CHECK_FOR_INTERRUPTS();
177179
}

src/backend/postmaster/autovacuum.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,8 @@ AutoVacLauncherMain(int argc, char *argv[])
598598
*/
599599
rc = WaitLatch(MyLatch,
600600
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
601-
(nap.tv_sec * 1000L) + (nap.tv_usec / 1000L));
601+
(nap.tv_sec * 1000L) + (nap.tv_usec / 1000L),
602+
WAIT_EVENT_AUTOVACUUM_MAIN);
602603

603604
ResetLatch(MyLatch);
604605

src/backend/postmaster/bgworker.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "libpq/pqsignal.h"
1919
#include "postmaster/bgworker_internals.h"
2020
#include "postmaster/postmaster.h"
21+
#include "pgstat.h"
2122
#include "storage/barrier.h"
2223
#include "storage/dsm.h"
2324
#include "storage/ipc.h"
@@ -969,7 +970,8 @@ WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
969970
break;
970971

971972
rc = WaitLatch(MyLatch,
972-
WL_LATCH_SET | WL_POSTMASTER_DEATH, 0);
973+
WL_LATCH_SET | WL_POSTMASTER_DEATH, 0,
974+
WAIT_EVENT_BGWORKER_STARTUP);
973975

974976
if (rc & WL_POSTMASTER_DEATH)
975977
{
@@ -1008,7 +1010,8 @@ WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
10081010
break;
10091011

10101012
rc = WaitLatch(&MyProc->procLatch,
1011-
WL_LATCH_SET | WL_POSTMASTER_DEATH, 0);
1013+
WL_LATCH_SET | WL_POSTMASTER_DEATH, 0,
1014+
WAIT_EVENT_BGWORKER_SHUTDOWN);
10121015

10131016
if (rc & WL_POSTMASTER_DEATH)
10141017
{

src/backend/postmaster/bgwriter.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ BackgroundWriterMain(void)
345345
*/
346346
rc = WaitLatch(MyLatch,
347347
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
348-
BgWriterDelay /* ms */ );
348+
BgWriterDelay /* ms */, WAIT_EVENT_BGWRITER_MAIN);
349349

350350
/*
351351
* If no latch event and BgBufferSync says nothing's happening, extend
@@ -372,7 +372,8 @@ BackgroundWriterMain(void)
372372
/* Sleep ... */
373373
rc = WaitLatch(MyLatch,
374374
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
375-
BgWriterDelay * HIBERNATE_FACTOR);
375+
BgWriterDelay * HIBERNATE_FACTOR,
376+
WAIT_EVENT_BGWRITER_HIBERNATE);
376377
/* Reset the notification request in case we timed out */
377378
StrategyNotifyBgWriter(-1);
378379
}

src/backend/postmaster/checkpointer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,8 @@ CheckpointerMain(void)
556556

557557
rc = WaitLatch(MyLatch,
558558
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
559-
cur_timeout * 1000L /* convert to ms */ );
559+
cur_timeout * 1000L /* convert to ms */,
560+
WAIT_EVENT_CHECKPOINTER_MAIN);
560561

561562
/*
562563
* Emergency bailout if postmaster has died. This is to avoid the

0 commit comments

Comments
 (0)