Skip to content

Commit e8bdcb1

Browse files
bdrouvotAWSCommitfest Bot
authored andcommitted
Adding the pg_stat_backend_transaction view
This view displays one row per server process, showing transaction statistics related to the current activity of that process. It currently displays the pid, the number of XIDs generated, the number of commits, the number of rollbacks and the time at which these statistics were last reset. It's built on top of a new function (pg_stat_get_backend_transactions()). The idea is the same as pg_stat_activity and pg_stat_get_activity(). Adding documentation and tests. XXX: Bump catversion
1 parent 1f715f4 commit e8bdcb1

File tree

7 files changed

+223
-0
lines changed

7 files changed

+223
-0
lines changed

doc/src/sgml/monitoring.sgml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,20 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser
320320
</entry>
321321
</row>
322322

323+
<row>
324+
<entry>
325+
<structname>pg_stat_backend_transaction</structname>
326+
<indexterm><primary>pg_stat_backend_transaction</primary></indexterm>
327+
</entry>
328+
<entry>
329+
One row per server process, showing statistics related to
330+
the current activity of that process, such as number of commits and
331+
rollbacks.
332+
See <link linkend="monitoring-pg-stat-backend-transaction-view">
333+
<structname>pg_stat_backend_transaction</structname></link> for details.
334+
</entry>
335+
</row>
336+
323337
<row>
324338
<entry><structname>pg_stat_replication</structname><indexterm><primary>pg_stat_replication</primary></indexterm></entry>
325339
<entry>One row per WAL sender process, showing statistics about
@@ -1172,6 +1186,91 @@ description | Waiting for a newly initialized WAL file to reach durable storage
11721186
</note>
11731187
</sect2>
11741188

1189+
<sect2 id="monitoring-pg-stat-backend-transaction-view">
1190+
<title><structname>pg_stat_backend_transaction</structname></title>
1191+
1192+
<indexterm>
1193+
<primary>pg_stat_backend_transaction</primary>
1194+
</indexterm>
1195+
1196+
<para>
1197+
The <structname>pg_stat_backend_transaction</structname> view will have one row
1198+
per server process, showing statistics related to
1199+
the current activity of that process.
1200+
</para>
1201+
1202+
<table id="pg-stat-backend-transaction-view" xreflabel="pg_stat_backend_transaction">
1203+
<title><structname>pg_stat_backend_transaction</structname> View</title>
1204+
<tgroup cols="1">
1205+
<thead>
1206+
<row>
1207+
<entry role="catalog_table_entry"><para role="column_definition">
1208+
Column Type
1209+
</para>
1210+
<para>
1211+
Description
1212+
</para></entry>
1213+
</row>
1214+
</thead>
1215+
1216+
<tbody>
1217+
<row>
1218+
<entry role="catalog_table_entry"><para role="column_definition">
1219+
<structfield>pid</structfield> <type>integer</type>
1220+
</para>
1221+
<para>
1222+
Process ID of this backend
1223+
</para></entry>
1224+
</row>
1225+
1226+
<row>
1227+
<entry role="catalog_table_entry"><para role="column_definition">
1228+
<structfield>xid_count</structfield> <type>bigint</type>
1229+
</para>
1230+
<para>
1231+
The number of XID that have been generated by the backend. It does not take
1232+
into account virtual transaction ID on purpose.
1233+
</para></entry>
1234+
</row>
1235+
1236+
<row>
1237+
<entry role="catalog_table_entry"><para role="column_definition">
1238+
<structfield>xact_commit</structfield> <type>bigint</type>
1239+
</para>
1240+
<para>
1241+
The number of transactions that have been committed.
1242+
</para></entry>
1243+
</row>
1244+
1245+
<row>
1246+
<entry role="catalog_table_entry"><para role="column_definition">
1247+
<structfield>xact_rollback</structfield> <type>bigint</type>
1248+
</para>
1249+
<para>
1250+
The number of transactions that have been rolled back.
1251+
</para></entry>
1252+
</row>
1253+
1254+
<row>
1255+
<entry role="catalog_table_entry"><para role="column_definition">
1256+
<structfield>stats_reset</structfield> <type>timestamp with time zone</type>
1257+
</para>
1258+
<para>
1259+
Time at which these statistics were last reset
1260+
</para></entry>
1261+
</row>
1262+
</tbody>
1263+
</tgroup>
1264+
</table>
1265+
1266+
<note>
1267+
<para>
1268+
The view does not return statistics for the checkpointer,
1269+
the background writer, the startup process and the autovacuum launcher.
1270+
</para>
1271+
</note>
1272+
</sect2>
1273+
11751274
<sect2 id="monitoring-pg-stat-replication-view">
11761275
<title><structname>pg_stat_replication</structname></title>
11771276

@@ -4990,6 +5089,22 @@ description | Waiting for a newly initialized WAL file to reach durable storage
49905089
</para></entry>
49915090
</row>
49925091

5092+
<row>
5093+
<entry role="func_table_entry"><para role="func_signature">
5094+
<indexterm>
5095+
<primary>pg_stat_get_backend_transactions</primary>
5096+
</indexterm>
5097+
<function>pg_stat_get_backend_transactions</function> ( <type>integer</type> )
5098+
<returnvalue>setof record</returnvalue>
5099+
</para>
5100+
<para>
5101+
Returns a record of transaction statistics about the backend with the
5102+
specified process ID, or one record for each active backend in the system
5103+
if <literal>NULL</literal> is specified. The fields returned are a
5104+
subset of those in the <structname>pg_stat_backend_transaction</structname> view.
5105+
</para></entry>
5106+
</row>
5107+
49935108
<row>
49945109
<entry id="pg-stat-get-backend-wal" role="func_table_entry"><para role="func_signature">
49955110
<indexterm>

src/backend/catalog/system_views.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,15 @@ CREATE VIEW pg_stat_activity AS
925925
LEFT JOIN pg_database AS D ON (S.datid = D.oid)
926926
LEFT JOIN pg_authid AS U ON (S.usesysid = U.oid);
927927

928+
CREATE VIEW pg_stat_backend_transaction AS
929+
SELECT
930+
S.pid,
931+
S.xid_count,
932+
S.xact_commit,
933+
S.xact_rollback,
934+
S.stats_reset
935+
FROM pg_stat_get_backend_transactions(NULL) AS S;
936+
928937
CREATE VIEW pg_stat_replication AS
929938
SELECT
930939
S.pid,

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,63 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
706706
return (Datum) 0;
707707
}
708708

709+
/*
710+
* Returns statistics of PG backends.
711+
*/
712+
Datum
713+
pg_stat_get_backend_transactions(PG_FUNCTION_ARGS)
714+
{
715+
#define PG_STAT_GET_BACKEND_STATS_COLS 5
716+
int num_backends = pgstat_fetch_stat_numbackends();
717+
int curr_backend;
718+
int pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
719+
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
720+
721+
InitMaterializedSRF(fcinfo, 0);
722+
723+
/* 1-based index */
724+
for (curr_backend = 1; curr_backend <= num_backends; curr_backend++)
725+
{
726+
/* for each row */
727+
Datum values[PG_STAT_GET_BACKEND_STATS_COLS] = {0};
728+
bool nulls[PG_STAT_GET_BACKEND_STATS_COLS] = {0};
729+
LocalPgBackendStatus *local_beentry;
730+
PgBackendStatus *beentry;
731+
PgStat_Backend *backend_stats;
732+
733+
/* Get the next one in the list */
734+
local_beentry = pgstat_get_local_beentry_by_index(curr_backend);
735+
beentry = &local_beentry->backendStatus;
736+
737+
/* If looking for specific PID, ignore all the others */
738+
if (pid != -1 && beentry->st_procpid != pid)
739+
continue;
740+
741+
backend_stats = pgstat_fetch_stat_backend_by_pid(beentry->st_procpid, NULL);
742+
743+
values[0] = Int32GetDatum(beentry->st_procpid);
744+
745+
if (!backend_stats)
746+
continue;
747+
748+
values[1] = Int64GetDatum(backend_stats->xid_count);
749+
values[2] = Int64GetDatum(backend_stats->xact_commit);
750+
values[3] = Int64GetDatum(backend_stats->xact_rollback);
751+
752+
if (backend_stats->stat_reset_timestamp != 0)
753+
values[4] = TimestampTzGetDatum(backend_stats->stat_reset_timestamp);
754+
else
755+
nulls[4] = true;
756+
757+
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
758+
759+
/* If only a single backend was requested, and we found it, break. */
760+
if (pid != -1)
761+
break;
762+
}
763+
764+
return (Datum) 0;
765+
}
709766

710767
Datum
711768
pg_backend_pid(PG_FUNCTION_ARGS)

src/include/catalog/pg_proc.dat

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5657,6 +5657,15 @@
56575657
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,o,o}',
56585658
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,ssl_client_dn,ssl_client_serial,ssl_issuer_dn,gss_auth,gss_princ,gss_enc,gss_delegation,leader_pid,query_id}',
56595659
prosrc => 'pg_stat_get_activity' },
5660+
{ oid => '9555',
5661+
descr => 'statistics: statistics about currently active backends',
5662+
proname => 'pg_stat_get_backend_transactions', prorows => '100', proisstrict => 'f',
5663+
proretset => 't', provolatile => 's', proparallel => 'r',
5664+
prorettype => 'record', proargtypes => 'int4',
5665+
proallargtypes => '{int4,int4,int8,int8,int8,timestamptz}',
5666+
proargmodes => '{i,o,o,o,o,o}',
5667+
proargnames => '{pid,pid,xid_count,xact_commit,xact_rollback,stats_reset}',
5668+
prosrc => 'pg_stat_get_backend_transactions' },
56605669
{ oid => '6318', descr => 'describe wait events',
56615670
proname => 'pg_get_wait_events', procost => '10', prorows => '250',
56625671
proretset => 't', provolatile => 'v', prorettype => 'record',

src/test/regress/expected/rules.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,12 @@ pg_stat_archiver| SELECT archived_count,
18571857
last_failed_time,
18581858
stats_reset
18591859
FROM pg_stat_get_archiver() s(archived_count, last_archived_wal, last_archived_time, failed_count, last_failed_wal, last_failed_time, stats_reset);
1860+
pg_stat_backend_transaction| SELECT pid,
1861+
xid_count,
1862+
xact_commit,
1863+
xact_rollback,
1864+
stats_reset
1865+
FROM pg_stat_get_backend_transactions(NULL::integer) s(pid, xid_count, xact_commit, xact_rollback, stats_reset);
18601866
pg_stat_bgwriter| SELECT pg_stat_get_bgwriter_buf_written_clean() AS buffers_clean,
18611867
pg_stat_get_bgwriter_maxwritten_clean() AS maxwritten_clean,
18621868
pg_stat_get_buf_alloc() AS buffers_alloc,

src/test/regress/expected/stats.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,28 @@ INSERT INTO trunc_stats_test1 DEFAULT VALUES;
135135
INSERT INTO trunc_stats_test1 DEFAULT VALUES;
136136
UPDATE trunc_stats_test1 SET id = id + 10 WHERE id IN (1, 2);
137137
DELETE FROM trunc_stats_test1 WHERE id = 3;
138+
-- in passing, check that backend's commit is incrementing
139+
SELECT xact_commit AS xact_commit_before
140+
FROM pg_stat_backend_transaction WHERE pid = pg_backend_pid() \gset
138141
BEGIN;
139142
UPDATE trunc_stats_test1 SET id = id + 100;
140143
TRUNCATE trunc_stats_test1;
141144
INSERT INTO trunc_stats_test1 DEFAULT VALUES;
142145
COMMIT;
146+
SELECT pg_stat_force_next_flush();
147+
pg_stat_force_next_flush
148+
--------------------------
149+
150+
(1 row)
151+
152+
SELECT xact_commit AS xact_commit_after
153+
FROM pg_stat_backend_transaction WHERE pid = pg_backend_pid() \gset
154+
SELECT :xact_commit_after > :xact_commit_before;
155+
?column?
156+
----------
157+
t
158+
(1 row)
159+
143160
-- use a savepoint: 1 insert, 1 live
144161
BEGIN;
145162
INSERT INTO trunc_stats_test2 DEFAULT VALUES;

src/test/regress/sql/stats.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,22 @@ INSERT INTO trunc_stats_test1 DEFAULT VALUES;
5858
UPDATE trunc_stats_test1 SET id = id + 10 WHERE id IN (1, 2);
5959
DELETE FROM trunc_stats_test1 WHERE id = 3;
6060

61+
-- in passing, check that backend's commit is incrementing
62+
SELECT xact_commit AS xact_commit_before
63+
FROM pg_stat_backend_transaction WHERE pid = pg_backend_pid() \gset
64+
6165
BEGIN;
6266
UPDATE trunc_stats_test1 SET id = id + 100;
6367
TRUNCATE trunc_stats_test1;
6468
INSERT INTO trunc_stats_test1 DEFAULT VALUES;
6569
COMMIT;
6670

71+
SELECT pg_stat_force_next_flush();
72+
SELECT xact_commit AS xact_commit_after
73+
FROM pg_stat_backend_transaction WHERE pid = pg_backend_pid() \gset
74+
75+
SELECT :xact_commit_after > :xact_commit_before;
76+
6777
-- use a savepoint: 1 insert, 1 live
6878
BEGIN;
6979
INSERT INTO trunc_stats_test2 DEFAULT VALUES;

0 commit comments

Comments
 (0)