Skip to content

Commit f55e0cc

Browse files
committed
Session WAL generation statistic
1 parent 8f1de94 commit f55e0cc

File tree

6 files changed

+35
-1
lines changed

6 files changed

+35
-1
lines changed

src/backend/access/transam/xlog.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,9 @@ CopyXLogRecordToWAL(int write_len, bool isLogSwitch, XLogRecData *rdata,
14861486
currpos = GetXLogBuffer(CurrPos);
14871487
freespace = INSERT_FREESPACE(CurrPos);
14881488

1489+
/* Accumulate amount of data written to WAL for pg_xact_activity */
1490+
MyProc->walWritten += write_len;
1491+
14891492
/*
14901493
* there should be enough space for at least the first field (xl_tot_len)
14911494
* on this page.

src/backend/catalog/system_views.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,9 @@ CREATE VIEW pg_stat_activity AS
758758
LEFT JOIN pg_database AS D ON (S.datid = D.oid)
759759
LEFT JOIN pg_authid AS U ON (S.usesysid = U.oid);
760760

761+
CREATE VIEW pg_stat_wal_activity AS
762+
SELECT a.*,pg_stat_get_wal_activity(a.pid) as wal_written FROM pg_stat_activity a;
763+
761764
CREATE VIEW pg_stat_replication AS
762765
SELECT
763766
S.pid,

src/backend/storage/lmgr/proc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ InitProcess(void)
390390
MyPgXact->xid = InvalidTransactionId;
391391
MyPgXact->xmin = InvalidTransactionId;
392392
MyProc->pid = MyProcPid;
393+
MyProc->walWritten = 0;
394+
393395
/* backendId, databaseId and roleId will be filled in later */
394396
MyProc->backendId = InvalidBackendId;
395397
MyProc->databaseId = InvalidOid;

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ pg_stat_get_progress_info(PG_FUNCTION_ARGS)
546546
Datum
547547
pg_stat_get_activity(PG_FUNCTION_ARGS)
548548
{
549-
#define PG_STAT_GET_ACTIVITY_COLS 29
549+
550+
#define PG_STAT_GET_ACTIVITY_COLS 29
550551
int num_backends = pgstat_fetch_stat_numbackends();
551552
int curr_backend;
552553
int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
@@ -918,6 +919,22 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
918919
}
919920

920921

922+
Datum
923+
pg_stat_get_wal_activity(PG_FUNCTION_ARGS)
924+
{
925+
int32 pid = PG_GETARG_INT32(0);
926+
PGPROC* proc = BackendPidGetProc(pid);
927+
if (proc == NULL)
928+
{
929+
proc = AuxiliaryPidGetProc(pid);
930+
}
931+
if (proc == NULL)
932+
PG_RETURN_NULL();
933+
else
934+
PG_RETURN_INT64(proc->walWritten);
935+
}
936+
937+
921938
Datum
922939
pg_backend_pid(PG_FUNCTION_ARGS)
923940
{

src/include/catalog/pg_proc.dat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5152,6 +5152,10 @@
51525152
proargmodes => '{i,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}',
51535153
proargnames => '{pid,datid,pid,usesysid,application_name,state,query,wait_event_type,wait_event,xact_start,query_start,backend_start,state_change,client_addr,client_hostname,client_port,backend_xid,backend_xmin,backend_type,ssl,sslversion,sslcipher,sslbits,sslcompression,ssl_client_dn,ssl_client_serial,ssl_issuer_dn,gss_auth,gss_princ,gss_enc}',
51545154
prosrc => 'pg_stat_get_activity' },
5155+
{ oid => '2121', descr => 'statistics: WAL activity',
5156+
proname => 'pg_stat_get_wal_activity', provolatile => 's', proisstrict => 't',
5157+
proparallel => 'r', prorettype => 'int8', proargtypes => 'int4',
5158+
prosrc => 'pg_stat_get_wal_activity' },
51555159
{ oid => '3318',
51565160
descr => 'statistics: information about progress of backends running maintenance command',
51575161
proname => 'pg_stat_get_progress_info', prorows => '100', proretset => 't',

src/include/storage/proc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ struct PGPROC
203203
PGPROC *lockGroupLeader; /* lock group leader, if I'm a member */
204204
dlist_head lockGroupMembers; /* list of members, if I'm a leader */
205205
dlist_node lockGroupLink; /* my member link, if I'm a member */
206+
207+
/*
208+
* Amount of data written to the WAL by this process
209+
*/
210+
uint64 walWritten;
206211
};
207212

208213
/* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */

0 commit comments

Comments
 (0)