Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 495864a

Browse files
committed
Refactor code of pg_stat_get_wal() building result tuple
This commit adds to pgstatfuncs.c a new routine called pg_stat_wal_build_tuple(), helper routine for pg_stat_get_wal(). This is in charge of filling one tuple based on the contents of PgStat_WalStats retrieved from pgstats. This refactoring will be used by an upcoming patch introducing backend-level WAL statistics, simplifying the main patch. Note that it is not possible for stats_reset to be NULL in pg_stat_wal; backend statistics need to be able to handle this case. Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Discussion: https://postgr.es/m/Z3zqc4o09dM/Ezyz@ip-10-97-1-34.eu-west-3.compute.internal
1 parent 62ec3e1 commit 495864a

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/backend/utils/adt/pgstatfuncs.c

+30-14
Original file line numberDiff line numberDiff line change
@@ -1632,21 +1632,23 @@ pg_stat_get_backend_io(PG_FUNCTION_ARGS)
16321632
}
16331633

16341634
/*
1635-
* Returns statistics of WAL activity
1635+
* pg_stat_wal_build_tuple
1636+
*
1637+
* Helper routine for pg_stat_get_wal() returning one tuple based on the
1638+
* contents of wal_counters.
16361639
*/
1637-
Datum
1638-
pg_stat_get_wal(PG_FUNCTION_ARGS)
1640+
static Datum
1641+
pg_stat_wal_build_tuple(PgStat_WalCounters wal_counters,
1642+
TimestampTz stat_reset_timestamp)
16391643
{
1640-
#define PG_STAT_GET_WAL_COLS 5
1644+
#define PG_STAT_WAL_COLS 5
16411645
TupleDesc tupdesc;
1642-
Datum values[PG_STAT_GET_WAL_COLS] = {0};
1643-
bool nulls[PG_STAT_GET_WAL_COLS] = {0};
1646+
Datum values[PG_STAT_WAL_COLS] = {0};
1647+
bool nulls[PG_STAT_WAL_COLS] = {0};
16441648
char buf[256];
1645-
PgStat_WalStats *wal_stats;
1646-
PgStat_WalCounters wal_counters;
16471649

16481650
/* Initialise attributes information in the tuple descriptor */
1649-
tupdesc = CreateTemplateTupleDesc(PG_STAT_GET_WAL_COLS);
1651+
tupdesc = CreateTemplateTupleDesc(PG_STAT_WAL_COLS);
16501652
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "wal_records",
16511653
INT8OID, -1, 0);
16521654
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "wal_fpi",
@@ -1660,10 +1662,6 @@ pg_stat_get_wal(PG_FUNCTION_ARGS)
16601662

16611663
BlessTupleDesc(tupdesc);
16621664

1663-
/* Get statistics about WAL activity */
1664-
wal_stats = pgstat_fetch_stat_wal();
1665-
wal_counters = wal_stats->wal_counters;
1666-
16671665
/* Fill values and NULLs */
16681666
values[0] = Int64GetDatum(wal_counters.wal_records);
16691667
values[1] = Int64GetDatum(wal_counters.wal_fpi);
@@ -1677,12 +1675,30 @@ pg_stat_get_wal(PG_FUNCTION_ARGS)
16771675

16781676
values[3] = Int64GetDatum(wal_counters.wal_buffers_full);
16791677

1680-
values[4] = TimestampTzGetDatum(wal_stats->stat_reset_timestamp);
1678+
if (stat_reset_timestamp != 0)
1679+
values[4] = TimestampTzGetDatum(stat_reset_timestamp);
1680+
else
1681+
nulls[4] = true;
16811682

16821683
/* Returns the record as Datum */
16831684
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
16841685
}
16851686

1687+
/*
1688+
* Returns statistics of WAL activity
1689+
*/
1690+
Datum
1691+
pg_stat_get_wal(PG_FUNCTION_ARGS)
1692+
{
1693+
PgStat_WalStats *wal_stats;
1694+
1695+
/* Get statistics about WAL activity */
1696+
wal_stats = pgstat_fetch_stat_wal();
1697+
1698+
return (pg_stat_wal_build_tuple(wal_stats->wal_counters,
1699+
wal_stats->stat_reset_timestamp));
1700+
}
1701+
16861702
/*
16871703
* Returns statistics of SLRU caches.
16881704
*/

0 commit comments

Comments
 (0)