@@ -248,6 +248,7 @@ static void LagTrackerWrite(XLogRecPtr lsn, TimestampTz local_flush_time);
248248static TimeOffset LagTrackerRead (int head , XLogRecPtr lsn , TimestampTz now );
249249static bool TransactionIdInRecentPast (TransactionId xid , uint32 epoch );
250250
251+ static void UpdateSpillStats (LogicalDecodingContext * ctx );
251252static void XLogRead (WALSegmentContext * segcxt , char * buf , XLogRecPtr startptr , Size count );
252253
253254
@@ -1261,7 +1262,8 @@ WalSndWriteData(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid,
12611262/*
12621263 * LogicalDecodingContext 'update_progress' callback.
12631264 *
1264- * Write the current position to the lag tracker (see XLogSendPhysical).
1265+ * Write the current position to the lag tracker (see XLogSendPhysical),
1266+ * and update the spill statistics.
12651267 */
12661268static void
12671269WalSndUpdateProgress (LogicalDecodingContext * ctx , XLogRecPtr lsn , TransactionId xid )
@@ -1280,6 +1282,11 @@ WalSndUpdateProgress(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId
12801282
12811283 LagTrackerWrite (lsn , now );
12821284 sendTime = now ;
1285+
1286+ /*
1287+ * Update statistics about transactions that spilled to disk.
1288+ */
1289+ UpdateSpillStats (ctx );
12831290}
12841291
12851292/*
@@ -2318,6 +2325,9 @@ InitWalSenderSlot(void)
23182325 walsnd -> state = WALSNDSTATE_STARTUP ;
23192326 walsnd -> latch = & MyProc -> procLatch ;
23202327 walsnd -> replyTime = 0 ;
2328+ walsnd -> spillTxns = 0 ;
2329+ walsnd -> spillCount = 0 ;
2330+ walsnd -> spillBytes = 0 ;
23212331 SpinLockRelease (& walsnd -> mutex );
23222332 /* don't need the lock anymore */
23232333 MyWalSnd = (WalSnd * ) walsnd ;
@@ -3219,7 +3229,7 @@ offset_to_interval(TimeOffset offset)
32193229Datum
32203230pg_stat_get_wal_senders (PG_FUNCTION_ARGS )
32213231{
3222- #define PG_STAT_GET_WAL_SENDERS_COLS 12
3232+ #define PG_STAT_GET_WAL_SENDERS_COLS 15
32233233 ReturnSetInfo * rsinfo = (ReturnSetInfo * ) fcinfo -> resultinfo ;
32243234 TupleDesc tupdesc ;
32253235 Tuplestorestate * tupstore ;
@@ -3274,6 +3284,9 @@ pg_stat_get_wal_senders(PG_FUNCTION_ARGS)
32743284 int pid ;
32753285 WalSndState state ;
32763286 TimestampTz replyTime ;
3287+ int64 spillTxns ;
3288+ int64 spillCount ;
3289+ int64 spillBytes ;
32773290 Datum values [PG_STAT_GET_WAL_SENDERS_COLS ];
32783291 bool nulls [PG_STAT_GET_WAL_SENDERS_COLS ];
32793292
@@ -3294,6 +3307,9 @@ pg_stat_get_wal_senders(PG_FUNCTION_ARGS)
32943307 applyLag = walsnd -> applyLag ;
32953308 priority = walsnd -> sync_standby_priority ;
32963309 replyTime = walsnd -> replyTime ;
3310+ spillTxns = walsnd -> spillTxns ;
3311+ spillCount = walsnd -> spillCount ;
3312+ spillBytes = walsnd -> spillBytes ;
32973313 SpinLockRelease (& walsnd -> mutex );
32983314
32993315 memset (nulls , 0 , sizeof (nulls ));
@@ -3375,6 +3391,11 @@ pg_stat_get_wal_senders(PG_FUNCTION_ARGS)
33753391 nulls [11 ] = true;
33763392 else
33773393 values [11 ] = TimestampTzGetDatum (replyTime );
3394+
3395+ /* spill to disk */
3396+ values [12 ] = Int64GetDatum (spillTxns );
3397+ values [13 ] = Int64GetDatum (spillCount );
3398+ values [14 ] = Int64GetDatum (spillBytes );
33783399 }
33793400
33803401 tuplestore_putvalues (tupstore , tupdesc , values , nulls );
@@ -3611,3 +3632,20 @@ LagTrackerRead(int head, XLogRecPtr lsn, TimestampTz now)
36113632 Assert (time != 0 );
36123633 return now - time ;
36133634}
3635+
3636+ static void
3637+ UpdateSpillStats (LogicalDecodingContext * ctx )
3638+ {
3639+ ReorderBuffer * rb = ctx -> reorder ;
3640+
3641+ SpinLockAcquire (& MyWalSnd -> mutex );
3642+
3643+ MyWalSnd -> spillTxns = rb -> spillTxns ;
3644+ MyWalSnd -> spillCount = rb -> spillCount ;
3645+ MyWalSnd -> spillBytes = rb -> spillBytes ;
3646+
3647+ elog (DEBUG2 , "UpdateSpillStats: updating stats %p %ld %ld %ld" ,
3648+ rb , rb -> spillTxns , rb -> spillCount , rb -> spillBytes );
3649+
3650+ SpinLockRelease (& MyWalSnd -> mutex );
3651+ }
0 commit comments