Add the usage count statistics to the information available in

contrib/pgbuffercache.

Greg Smith
This commit is contained in:
Bruce Momjian 2007-04-07 16:09:14 +00:00
parent 5695f38f74
commit b0194ab110
3 changed files with 23 additions and 14 deletions

View File

@ -40,7 +40,7 @@ Notes
reldatabase | pg_database.oid | Database for the relation. reldatabase | pg_database.oid | Database for the relation.
relblocknumber | | Offset of the page in the relation. relblocknumber | | Offset of the page in the relation.
isdirty | | Is the page dirty? isdirty | | Is the page dirty?
usagecount | | Page LRU count
There is one row for each buffer in the shared cache. Unused buffers are There is one row for each buffer in the shared cache. Unused buffers are
shown with all fields null except bufferid. shown with all fields null except bufferid.
@ -61,19 +61,21 @@ Sample output
regression=# \d pg_buffercache; regression=# \d pg_buffercache;
View "public.pg_buffercache" View "public.pg_buffercache"
Column | Type | Modifiers Column | Type | Modifiers
----------------+---------+----------- ----------------+----------+-----------
bufferid | integer | bufferid | integer |
relfilenode | oid | relfilenode | oid |
reltablespace | oid | reltablespace | oid |
reldatabase | oid | reldatabase | oid |
relblocknumber | bigint | relblocknumber | bigint |
isdirty | boolean | isdirty | boolean |
usagecount | smallint |
View definition: View definition:
SELECT p.bufferid, p.relfilenode, p.reltablespace, p.reldatabase, SELECT p.bufferid, p.relfilenode, p.reltablespace, p.reldatabase,
p.relblocknumber, p.isdirty p.relblocknumber, p.isdirty, p.usagecount
FROM pg_buffercache_pages() p(bufferid integer, relfilenode oid, FROM pg_buffercache_pages() p(bufferid integer, relfilenode oid,
reltablespace oid, reldatabase oid, relblocknumber bigint, reltablespace oid, reldatabase oid, relblocknumber bigint,
isdirty boolean); isdirty boolean, usagecount smallint);
regression=# SELECT c.relname, count(*) AS buffers regression=# SELECT c.relname, count(*) AS buffers
FROM pg_class c INNER JOIN pg_buffercache b FROM pg_class c INNER JOIN pg_buffercache b

View File

@ -12,7 +12,7 @@ LANGUAGE C;
CREATE VIEW pg_buffercache AS CREATE VIEW pg_buffercache AS
SELECT P.* FROM pg_buffercache_pages() AS P SELECT P.* FROM pg_buffercache_pages() AS P
(bufferid integer, relfilenode oid, reltablespace oid, reldatabase oid, (bufferid integer, relfilenode oid, reltablespace oid, reldatabase oid,
relblocknumber int8, isdirty bool); relblocknumber int8, isdirty bool, usagecount int2);
-- Don't want these to be available at public. -- Don't want these to be available at public.
REVOKE ALL ON FUNCTION pg_buffercache_pages() FROM PUBLIC; REVOKE ALL ON FUNCTION pg_buffercache_pages() FROM PUBLIC;

View File

@ -3,7 +3,7 @@
* pg_buffercache_pages.c * pg_buffercache_pages.c
* display some contents of the buffer cache * display some contents of the buffer cache
* *
* $PostgreSQL: pgsql/contrib/pg_buffercache/pg_buffercache_pages.c,v 1.11 2006/10/22 17:49:21 tgl Exp $ * $PostgreSQL: pgsql/contrib/pg_buffercache/pg_buffercache_pages.c,v 1.12 2007/04/07 16:09:14 momjian Exp $
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include "postgres.h" #include "postgres.h"
@ -16,7 +16,7 @@
#include "utils/relcache.h" #include "utils/relcache.h"
#define NUM_BUFFERCACHE_PAGES_ELEM 6 #define NUM_BUFFERCACHE_PAGES_ELEM 7
PG_MODULE_MAGIC; PG_MODULE_MAGIC;
@ -35,6 +35,7 @@ typedef struct
BlockNumber blocknum; BlockNumber blocknum;
bool isvalid; bool isvalid;
bool isdirty; bool isdirty;
uint16 usagecount;
} BufferCachePagesRec; } BufferCachePagesRec;
@ -91,6 +92,8 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
INT8OID, -1, 0); INT8OID, -1, 0);
TupleDescInitEntry(tupledesc, (AttrNumber) 6, "isdirty", TupleDescInitEntry(tupledesc, (AttrNumber) 6, "isdirty",
BOOLOID, -1, 0); BOOLOID, -1, 0);
TupleDescInitEntry(tupledesc, (AttrNumber) 7, "usage_count",
INT2OID, -1, 0);
fctx->tupdesc = BlessTupleDesc(tupledesc); fctx->tupdesc = BlessTupleDesc(tupledesc);
@ -126,6 +129,7 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
fctx->record[i].reltablespace = bufHdr->tag.rnode.spcNode; fctx->record[i].reltablespace = bufHdr->tag.rnode.spcNode;
fctx->record[i].reldatabase = bufHdr->tag.rnode.dbNode; fctx->record[i].reldatabase = bufHdr->tag.rnode.dbNode;
fctx->record[i].blocknum = bufHdr->tag.blockNum; fctx->record[i].blocknum = bufHdr->tag.blockNum;
fctx->record[i].usagecount = bufHdr->usage_count;
if (bufHdr->flags & BM_DIRTY) if (bufHdr->flags & BM_DIRTY)
fctx->record[i].isdirty = true; fctx->record[i].isdirty = true;
@ -172,6 +176,7 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
nulls[3] = true; nulls[3] = true;
nulls[4] = true; nulls[4] = true;
nulls[5] = true; nulls[5] = true;
nulls[6] = true;
} }
else else
{ {
@ -185,6 +190,8 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
nulls[4] = false; nulls[4] = false;
values[5] = BoolGetDatum(fctx->record[i].isdirty); values[5] = BoolGetDatum(fctx->record[i].isdirty);
nulls[5] = false; nulls[5] = false;
values[6] = Int16GetDatum(fctx->record[i].usagecount);
nulls[6] = false;
} }
/* Build and return the tuple. */ /* Build and return the tuple. */