diff --git a/src/backend/storage/buffer/freelist.c b/src/backend/storage/buffer/freelist.c index 073b69d..fa567b4 100644 --- a/src/backend/storage/buffer/freelist.c +++ b/src/backend/storage/buffer/freelist.c @@ -521,3 +521,36 @@ StrategyRejectBuffer(BufferAccessStrategy strategy, volatile BufferDesc *buf) return true; } + +/* + * BuffersUsed -- return a high water mark of buffer pages allocated + * + * This number is not a true count of buffers currently in use, because + * it's possible that some of those buffers allocated and used have been + * added to the free buffer list since then. The size of the free list is + * not considered here, but since few operations actually add things to it + * anyway that's unlikely to be a large source of error. + */ +int64 +BuffersUsed(void) +{ + int used; + int passes; + int victim; + int allocs; + LWLockAcquire(BufFreelistLock, LW_EXCLUSIVE); + + /* Once you've made one pass over the buffer chain, you have at + some point used all of them */ + if (StrategyControl->completePasses == 0) + used=StrategyControl->nextVictimBuffer; + else + used=NBuffers; + LWLockRelease(BufFreelistLock); + /* Only needed for debugging */ + passes=StrategyControl->completePasses; + victim=StrategyControl->nextVictimBuffer; + elog(DEBUG1,"Buffers used=%d passes=%d victim=%d",used,passes,victim); + return (int64) used; +} + diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index a4e0252..29cff01 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -21,6 +21,7 @@ #include "commands/dbcommands.h" #include "commands/tablespace.h" #include "miscadmin.h" +#include "storage/buf_internals.h" #include "storage/fd.h" #include "utils/acl.h" #include "utils/builtins.h" @@ -28,7 +29,6 @@ #include "utils/relmapper.h" #include "utils/syscache.h" - /* Return physical size of directory contents, or 0 if dir doesn't exist */ static int64 db_dir_size(const char *path) @@ -621,3 +621,22 @@ pg_relation_filepath(PG_FUNCTION_ARGS) PG_RETURN_TEXT_P(cstring_to_text(path)); } + +/* + * Find out the size of the used space in the buffer cache + * + * See pg_buffers_used for notes on the limitations of the + * technique used. + */ +Datum +pg_buffers_used(PG_FUNCTION_ARGS) +{ + int used; + int64 size; + + used=BuffersUsed(); + size=used * BLCKSZ; + elog(DEBUG1, "Used buffer cache bytes: %lld",size); + + PG_RETURN_INT64(size); +} diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index f2751a4..660f46e 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -3322,7 +3322,8 @@ DATA(insert OID = 3820 ( pg_last_xlog_receive_location PGNSP PGUID 12 1 0 0 f f DESCR("current xlog flush location"); DATA(insert OID = 3821 ( pg_last_xlog_replay_location PGNSP PGUID 12 1 0 0 f f f t f v 0 0 25 "" _null_ _null_ _null_ _null_ pg_last_xlog_replay_location _null_ _null_ _null_ )); DESCR("last xlog replay location"); - +DATA(insert OID = 3822 ( pg_buffers_used PGNSP PGUID 12 1 0 0 f f f t f v 0 0 20 "" _null_ _null_ _null_ _null_ pg_buffers_used _null_ _null_ _null_ )); +DESCR("bytes of shared_buffers cache used"); DATA(insert OID = 2621 ( pg_reload_conf PGNSP PGUID 12 1 0 0 f f f t f v 0 0 16 "" _null_ _null_ _null_ _null_ pg_reload_conf _null_ _null_ _null_ )); DESCR("reload configuration files"); DATA(insert OID = 2622 ( pg_rotate_logfile PGNSP PGUID 12 1 0 0 f f f t f v 0 0 16 "" _null_ _null_ _null_ _null_ pg_rotate_logfile _null_ _null_ _null_ )); diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h index 4e894ef..c3f5520 100644 --- a/src/include/storage/buf_internals.h +++ b/src/include/storage/buf_internals.h @@ -188,6 +188,7 @@ extern bool StrategyRejectBuffer(BufferAccessStrategy strategy, extern int StrategySyncStart(uint32 *complete_passes, uint32 *num_buf_alloc); extern Size StrategyShmemSize(void); extern void StrategyInitialize(bool init); +extern int64 BuffersUsed(void); /* buf_table.c */ extern Size BufTableShmemSize(int size); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index e8f38a6..8899a2e 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -437,6 +437,7 @@ extern Datum pg_table_size(PG_FUNCTION_ARGS); extern Datum pg_indexes_size(PG_FUNCTION_ARGS); extern Datum pg_relation_filenode(PG_FUNCTION_ARGS); extern Datum pg_relation_filepath(PG_FUNCTION_ARGS); +extern Datum pg_buffers_used(PG_FUNCTION_ARGS); /* genfile.c */ extern Datum pg_stat_file(PG_FUNCTION_ARGS);