@@ -179,6 +179,7 @@ WalSndHandshake(void)
179
179
{
180
180
int firstchar ;
181
181
182
+ WalSndSetState (WALSNDSTATE_STARTUP );
182
183
set_ps_display ("idle" , false);
183
184
184
185
/* Wait for a command to arrive */
@@ -482,6 +483,9 @@ WalSndLoop(void)
482
483
if (!XLogSend (output_message ,& caughtup ))
483
484
break ;
484
485
}
486
+
487
+ /* Update our state to indicate if we're behind or not */
488
+ WalSndSetState (caughtup ?WALSNDSTATE_STREAMING :WALSNDSTATE_CATCHUP );
485
489
}
486
490
487
491
/*
@@ -533,6 +537,7 @@ InitWalSnd(void)
533
537
*/
534
538
walsnd -> pid = MyProcPid ;
535
539
MemSet (& walsnd -> sentPtr ,0 ,sizeof (XLogRecPtr ));
540
+ walsnd -> state = WALSNDSTATE_STARTUP ;
536
541
SpinLockRelease (& walsnd -> mutex );
537
542
/* don't need the lock anymore */
538
543
OwnLatch ((Latch * )& walsnd -> latch );
@@ -960,14 +965,53 @@ WalSndWakeup(void)
960
965
SetLatch (& WalSndCtl -> walsnds [i ].latch );
961
966
}
962
967
968
+ /* Set state for current walsender (only called in walsender) */
969
+ void
970
+ WalSndSetState (WalSndState state )
971
+ {
972
+ /* use volatile pointer to prevent code rearrangement */
973
+ volatile WalSnd * walsnd = MyWalSnd ;
974
+
975
+ Assert (am_walsender );
976
+
977
+ if (walsnd -> state == state )
978
+ return ;
979
+
980
+ SpinLockAcquire (& walsnd -> mutex );
981
+ walsnd -> state = state ;
982
+ SpinLockRelease (& walsnd -> mutex );
983
+ }
984
+
985
+ /*
986
+ * Return a string constant representing the state. This is used
987
+ * in system views, and should *not* be translated.
988
+ */
989
+ static const char *
990
+ WalSndGetStateString (WalSndState state )
991
+ {
992
+ switch (state )
993
+ {
994
+ case WALSNDSTATE_STARTUP :
995
+ return "STARTUP" ;
996
+ case WALSNDSTATE_BACKUP :
997
+ return "BACKUP" ;
998
+ case WALSNDSTATE_CATCHUP :
999
+ return "CATCHUP" ;
1000
+ case WALSNDSTATE_STREAMING :
1001
+ return "STREAMING" ;
1002
+ }
1003
+ return "UNKNOWN" ;
1004
+ }
1005
+
1006
+
963
1007
/*
964
1008
* Returns activity of walsenders, including pids and xlog locations sent to
965
1009
* standby servers.
966
1010
*/
967
1011
Datum
968
1012
pg_stat_get_wal_senders (PG_FUNCTION_ARGS )
969
1013
{
970
- #define PG_STAT_GET_WAL_SENDERS_COLS 2
1014
+ #define PG_STAT_GET_WAL_SENDERS_COLS 3
971
1015
ReturnSetInfo * rsinfo = (ReturnSetInfo * )fcinfo -> resultinfo ;
972
1016
TupleDesc tupdesc ;
973
1017
Tuplestorestate * tupstore ;
@@ -1021,7 +1065,8 @@ pg_stat_get_wal_senders(PG_FUNCTION_ARGS)
1021
1065
1022
1066
memset (nulls ,0 ,sizeof (nulls ));
1023
1067
values [0 ]= Int32GetDatum (walsnd -> pid );
1024
- values [1 ]= CStringGetTextDatum (sent_location );
1068
+ values [1 ]= CStringGetTextDatum (WalSndGetStateString (walsnd -> state ));
1069
+ values [2 ]= CStringGetTextDatum (sent_location );
1025
1070
1026
1071
tuplestore_putvalues (tupstore ,tupdesc ,values ,nulls );
1027
1072
}