Skip to content

Commit 6ad7a20

Browse files
bdrouvotAWSCommitfest Bot
authored andcommitted
Adding per backend commit and rollback counters
It relies on the existing per backend statistics that has been added in 9aea73f. The new pending counters are updated when the database ones are flushed (to reduce the overhead of incrementing new counters).
1 parent 8f29467 commit 6ad7a20

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

src/backend/utils/activity/pgstat_backend.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* reported within critical sections so we use static memory in order to avoid
3838
* memory allocation.
3939
*/
40-
static PgStat_BackendPending PendingBackendStats;
40+
PgStat_BackendPending PendingBackendStats;
4141
static bool backend_has_iostats = false;
4242

4343
/*
@@ -48,6 +48,11 @@ static bool backend_has_iostats = false;
4848
*/
4949
static WalUsage prevBackendWalUsage;
5050

51+
/*
52+
* For backend commit and rollback statistics.
53+
*/
54+
bool backend_has_xactstats = false;
55+
5156
/*
5257
* Utility routines to report I/O stats for backends, kept here to avoid
5358
* exposing PendingBackendStats to the outside world.
@@ -261,6 +266,34 @@ pgstat_flush_backend_entry_wal(PgStat_EntryRef *entry_ref)
261266
prevBackendWalUsage = pgWalUsage;
262267
}
263268

269+
/*
270+
* Flush out locally pending backend transaction statistics. Locking is managed
271+
* by the caller.
272+
*/
273+
static void
274+
pgstat_flush_backend_entry_xact(PgStat_EntryRef *entry_ref)
275+
{
276+
PgStatShared_Backend *shbackendent;
277+
278+
/*
279+
* This function can be called even if nothing at all has happened for
280+
* transaction statistics. In this case, avoid unnecessarily modifying
281+
* the stats entry.
282+
*/
283+
if (!backend_has_xactstats)
284+
return;
285+
286+
shbackendent = (PgStatShared_Backend *) entry_ref->shared_stats;
287+
288+
shbackendent->stats.xact_commit += PendingBackendStats.pending_xact_commit;
289+
shbackendent->stats.xact_rollback += PendingBackendStats.pending_xact_rollback;
290+
291+
PendingBackendStats.pending_xact_commit = 0;
292+
PendingBackendStats.pending_xact_rollback = 0;
293+
294+
backend_has_xactstats = false;
295+
}
296+
264297
/*
265298
* Flush out locally pending backend statistics
266299
*
@@ -285,6 +318,10 @@ pgstat_flush_backend(bool nowait, bits32 flags)
285318
pgstat_backend_wal_have_pending())
286319
has_pending_data = true;
287320

321+
/* Some transaction data pending? */
322+
if ((flags & PGSTAT_BACKEND_FLUSH_XACT) && backend_has_xactstats)
323+
has_pending_data = true;
324+
288325
if (!has_pending_data)
289326
return false;
290327

@@ -300,6 +337,9 @@ pgstat_flush_backend(bool nowait, bits32 flags)
300337
if (flags & PGSTAT_BACKEND_FLUSH_WAL)
301338
pgstat_flush_backend_entry_wal(entry_ref);
302339

340+
if (flags & PGSTAT_BACKEND_FLUSH_XACT)
341+
pgstat_flush_backend_entry_xact(entry_ref);
342+
303343
pgstat_unlock_entry(entry_ref);
304344

305345
return false;

src/backend/utils/activity/pgstat_database.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,13 @@ pgstat_update_dbstats(TimestampTz ts)
342342
dbentry->blk_read_time += pgStatBlockReadTime;
343343
dbentry->blk_write_time += pgStatBlockWriteTime;
344344

345+
/* Do the same for backend stats */
346+
PendingBackendStats.pending_xact_commit += pgStatXactCommit;
347+
PendingBackendStats.pending_xact_rollback += pgStatXactRollback;
348+
349+
backend_has_xactstats = true;
350+
pgstat_report_fixed = true;
351+
345352
if (pgstat_should_report_connstat())
346353
{
347354
long secs;

src/include/pgstat.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ typedef struct PgStat_Backend
496496
TimestampTz stat_reset_timestamp;
497497
PgStat_BktypeIO io_stats;
498498
PgStat_WalCounters wal_counters;
499+
PgStat_Counter xact_commit;
500+
PgStat_Counter xact_rollback;
499501
} PgStat_Backend;
500502

501503
/* ---------
@@ -508,6 +510,12 @@ typedef struct PgStat_BackendPending
508510
* Backend statistics store the same amount of IO data as PGSTAT_KIND_IO.
509511
*/
510512
PgStat_PendingIO pending_io;
513+
514+
/*
515+
* Transaction statistics pending flush.
516+
*/
517+
PgStat_Counter pending_xact_commit;
518+
PgStat_Counter pending_xact_rollback;
511519
} PgStat_BackendPending;
512520

513521
/*
@@ -807,6 +815,13 @@ extern PGDLLIMPORT int pgstat_track_functions;
807815
extern PGDLLIMPORT int pgstat_fetch_consistency;
808816

809817

818+
/*
819+
* Variables in pgstat_backend.c
820+
*/
821+
822+
extern PGDLLIMPORT PgStat_BackendPending PendingBackendStats;
823+
extern PGDLLIMPORT bool backend_has_xactstats;
824+
810825
/*
811826
* Variables in pgstat_bgwriter.c
812827
*/

src/include/utils/pgstat_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,8 @@ extern void pgstat_archiver_snapshot_cb(void);
647647
/* flags for pgstat_flush_backend() */
648648
#define PGSTAT_BACKEND_FLUSH_IO (1 << 0) /* Flush I/O statistics */
649649
#define PGSTAT_BACKEND_FLUSH_WAL (1 << 1) /* Flush WAL statistics */
650-
#define PGSTAT_BACKEND_FLUSH_ALL (PGSTAT_BACKEND_FLUSH_IO | PGSTAT_BACKEND_FLUSH_WAL)
650+
#define PGSTAT_BACKEND_FLUSH_XACT (1 << 2) /* Flush xact statistics */
651+
#define PGSTAT_BACKEND_FLUSH_ALL (PGSTAT_BACKEND_FLUSH_IO | PGSTAT_BACKEND_FLUSH_WAL | PGSTAT_BACKEND_FLUSH_XACT)
651652

652653
extern bool pgstat_flush_backend(bool nowait, bits32 flags);
653654
extern bool pgstat_backend_flush_cb(bool nowait);

0 commit comments

Comments
 (0)