diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index 97c23c9..2bc3e3a 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -713,6 +713,7 @@ CREATE VIEW pg_stat_bgwriter AS pg_stat_get_buf_fsync_backend() AS buffers_backend_fsync, pg_stat_get_buf_alloc() AS buffers_alloc, pg_stat_get_buf_clocksweep_backend() AS buffers_backend_clocksweep, + pg_stat_get_buf_touched_freelist() AS buffers_touched_freelist, pg_stat_get_bgwriter_stat_reset_time() AS stats_reset; CREATE VIEW pg_user_mappings AS diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 7475e5a..6493f19 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -5022,6 +5022,7 @@ pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len) globalStats.buf_fsync_backend += msg->m_buf_fsync_backend; globalStats.buf_alloc += msg->m_buf_alloc; globalStats.buf_backend_clocksweep += msg->m_buf_backend_clocksweep; + globalStats.buf_touched_freelist += msg->m_buf_touched_freelist; } /* ---------- diff --git a/src/backend/storage/buffer/freelist.c b/src/backend/storage/buffer/freelist.c index e1d8445..fc21865 100644 --- a/src/backend/storage/buffer/freelist.c +++ b/src/backend/storage/buffer/freelist.c @@ -15,6 +15,7 @@ */ #include "postgres.h" +#include "pgstat.h" #include "storage/buf_internals.h" #include "storage/bufmgr.h" @@ -49,6 +50,8 @@ typedef struct /* Buffers not statistied from freelist since last reset */ uint32 numBufferBackendClocksweep; + uint32 numBufferTouchedFreelist; + /* * protects freelist and related variables (firstFreeBuffer, * lastFreeBuffer, numBufferAllocs, numBufferBackendClocksweep, @@ -344,6 +347,9 @@ StrategyGetFreelistAccessInfo(uint32 *num_buf_to_free, uint32 *num_buf_alloc, *num_buf_backend_clocksweep = StrategyControl->numBufferBackendClocksweep; StrategyControl->numBufferBackendClocksweep = 0; + BgWriterStats.m_buf_touched_freelist = StrategyControl->numBufferTouchedFreelist; + StrategyControl->numBufferTouchedFreelist = 0; + SpinLockRelease(&StrategyControl->freelist_lck); return; @@ -476,6 +482,7 @@ StrategyInitialize(bool init) StrategyControl->completePasses = 0; StrategyControl->numBufferAllocs = 0; StrategyControl->numBufferBackendClocksweep = 0; + StrategyControl->numBufferTouchedFreelist = 0; /* No pending notification */ StrategyControl->bgwriterLatch = NULL; @@ -667,6 +674,9 @@ GetBufferFromFreelist(BufferAccessStrategy strategy) SpinLockAcquire(&StrategyControl->freelist_lck); + /* Buffer selected from freelist is already in use */ + StrategyControl->numBufferTouchedFreelist++; + if (StrategyControl->firstFreeBuffer >= 0) { buf = &BufferDescriptors[StrategyControl->firstFreeBuffer]; diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index 00d815f..06a9a6c 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -101,6 +101,7 @@ extern Datum pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS); extern Datum pg_stat_get_buf_fsync_backend(PG_FUNCTION_ARGS); extern Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS); extern Datum pg_stat_get_buf_clocksweep_backend(PG_FUNCTION_ARGS); +extern Datum pg_stat_get_buf_touched_freelist(PG_FUNCTION_ARGS); extern Datum pg_stat_get_xact_numscans(PG_FUNCTION_ARGS); extern Datum pg_stat_get_xact_tuples_returned(PG_FUNCTION_ARGS); @@ -1503,6 +1504,12 @@ pg_stat_get_buf_clocksweep_backend(PG_FUNCTION_ARGS) } Datum +pg_stat_get_buf_touched_freelist(PG_FUNCTION_ARGS) +{ + PG_RETURN_INT64(pgstat_fetch_global()->buf_touched_freelist); +} + +Datum pg_stat_get_xact_numscans(PG_FUNCTION_ARGS) { Oid relid = PG_GETARG_OID(0); diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index b7efb3d..194e888 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -2785,6 +2785,8 @@ DATA(insert OID = 2859 ( pg_stat_get_buf_alloc PGNSP PGUID 12 1 0 0 0 f f f f DESCR("statistics: number of buffer allocations"); DATA(insert OID = 3218 ( pg_stat_get_buf_clocksweep_backend PGNSP PGUID 12 1 0 0 0 f f f f t f s 0 0 20 "" _null_ _null_ _null_ _null_ pg_stat_get_buf_clocksweep_backend _null_ _null_ _null_ )); DESCR("statistics: number of buffer allocations not satisfied from freelsit"); +DATA(insert OID = 3156 ( pg_stat_get_buf_touched_freelist PGNSP PGUID 12 1 0 0 0 f f f f t f s 0 0 20 "" _null_ _null_ _null_ _null_ pg_stat_get_buf_touched_freelist _null_ _null_ _null_ )); +DESCR("statistics: number of buffers in freelist that are touched"); DATA(insert OID = 2978 ( pg_stat_get_function_calls PGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0 20 "26" _null_ _null_ _null_ _null_ pg_stat_get_function_calls _null_ _null_ _null_ )); DESCR("statistics: number of function calls"); diff --git a/src/include/pgstat.h b/src/include/pgstat.h index 51a2023..c1c5c73 100644 --- a/src/include/pgstat.h +++ b/src/include/pgstat.h @@ -398,6 +398,7 @@ typedef struct PgStat_MsgBgWriter PgStat_Counter m_buf_fsync_backend; PgStat_Counter m_buf_alloc; PgStat_Counter m_buf_backend_clocksweep; + PgStat_Counter m_buf_touched_freelist; PgStat_Counter m_checkpoint_write_time; /* times in milliseconds */ PgStat_Counter m_checkpoint_sync_time; } PgStat_MsgBgWriter; @@ -672,6 +673,7 @@ typedef struct PgStat_GlobalStats PgStat_Counter buf_fsync_backend; PgStat_Counter buf_alloc; PgStat_Counter buf_backend_clocksweep; + PgStat_Counter buf_touched_freelist; TimestampTz stat_reset_timestamp; } PgStat_GlobalStats;