Code cleanup for pg_buffercache, from Mark Kirkwood.
This commit is contained in:
parent
c2c0b14086
commit
bd95c74985
@ -3,12 +3,14 @@
|
|||||||
* 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.10 2006/10/19 18:32:46 tgl Exp $
|
* $PostgreSQL: pgsql/contrib/pg_buffercache/pg_buffercache_pages.c,v 1.11 2006/10/22 17:49:21 tgl Exp $
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
#include "funcapi.h"
|
|
||||||
|
#include "access/heapam.h"
|
||||||
#include "catalog/pg_type.h"
|
#include "catalog/pg_type.h"
|
||||||
|
#include "funcapi.h"
|
||||||
#include "storage/buf_internals.h"
|
#include "storage/buf_internals.h"
|
||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "utils/relcache.h"
|
#include "utils/relcache.h"
|
||||||
@ -26,7 +28,6 @@ Datum pg_buffercache_pages(PG_FUNCTION_ARGS);
|
|||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
|
|
||||||
uint32 bufferid;
|
uint32 bufferid;
|
||||||
Oid relfilenode;
|
Oid relfilenode;
|
||||||
Oid reltablespace;
|
Oid reltablespace;
|
||||||
@ -34,7 +35,6 @@ typedef struct
|
|||||||
BlockNumber blocknum;
|
BlockNumber blocknum;
|
||||||
bool isvalid;
|
bool isvalid;
|
||||||
bool isdirty;
|
bool isdirty;
|
||||||
|
|
||||||
} BufferCachePagesRec;
|
} BufferCachePagesRec;
|
||||||
|
|
||||||
|
|
||||||
@ -43,11 +43,8 @@ typedef struct
|
|||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
|
TupleDesc tupdesc;
|
||||||
AttInMetadata *attinmeta;
|
|
||||||
BufferCachePagesRec *record;
|
BufferCachePagesRec *record;
|
||||||
char *values[NUM_BUFFERCACHE_PAGES_ELEM];
|
|
||||||
|
|
||||||
} BufferCachePagesContext;
|
} BufferCachePagesContext;
|
||||||
|
|
||||||
|
|
||||||
@ -56,10 +53,10 @@ typedef struct
|
|||||||
* relation node/tablespace/database/blocknum and dirty indicator.
|
* relation node/tablespace/database/blocknum and dirty indicator.
|
||||||
*/
|
*/
|
||||||
PG_FUNCTION_INFO_V1(pg_buffercache_pages);
|
PG_FUNCTION_INFO_V1(pg_buffercache_pages);
|
||||||
|
|
||||||
Datum
|
Datum
|
||||||
pg_buffercache_pages(PG_FUNCTION_ARGS)
|
pg_buffercache_pages(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
|
|
||||||
FuncCallContext *funcctx;
|
FuncCallContext *funcctx;
|
||||||
Datum result;
|
Datum result;
|
||||||
MemoryContext oldcontext;
|
MemoryContext oldcontext;
|
||||||
@ -77,7 +74,10 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
|
|||||||
/* Switch context when allocating stuff to be used in later calls */
|
/* Switch context when allocating stuff to be used in later calls */
|
||||||
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
||||||
|
|
||||||
/* Construct a tuple to return. */
|
/* Create a user function context for cross-call persistence */
|
||||||
|
fctx = (BufferCachePagesContext *) palloc(sizeof(BufferCachePagesContext));
|
||||||
|
|
||||||
|
/* Construct a tuple descriptor for the result rows. */
|
||||||
tupledesc = CreateTemplateTupleDesc(NUM_BUFFERCACHE_PAGES_ELEM, false);
|
tupledesc = CreateTemplateTupleDesc(NUM_BUFFERCACHE_PAGES_ELEM, false);
|
||||||
TupleDescInitEntry(tupledesc, (AttrNumber) 1, "bufferid",
|
TupleDescInitEntry(tupledesc, (AttrNumber) 1, "bufferid",
|
||||||
INT4OID, -1, 0);
|
INT4OID, -1, 0);
|
||||||
@ -92,27 +92,14 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
|
|||||||
TupleDescInitEntry(tupledesc, (AttrNumber) 6, "isdirty",
|
TupleDescInitEntry(tupledesc, (AttrNumber) 6, "isdirty",
|
||||||
BOOLOID, -1, 0);
|
BOOLOID, -1, 0);
|
||||||
|
|
||||||
/* Generate attribute metadata needed later to produce tuples */
|
fctx->tupdesc = BlessTupleDesc(tupledesc);
|
||||||
funcctx->attinmeta = TupleDescGetAttInMetadata(tupledesc);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Create a function context for cross-call persistence and initialize
|
|
||||||
* the buffer counters.
|
|
||||||
*/
|
|
||||||
fctx = (BufferCachePagesContext *) palloc(sizeof(BufferCachePagesContext));
|
|
||||||
funcctx->max_calls = NBuffers;
|
|
||||||
funcctx->user_fctx = fctx;
|
|
||||||
|
|
||||||
/* Allocate NBuffers worth of BufferCachePagesRec records. */
|
/* Allocate NBuffers worth of BufferCachePagesRec records. */
|
||||||
fctx->record = (BufferCachePagesRec *) palloc(sizeof(BufferCachePagesRec) * NBuffers);
|
fctx->record = (BufferCachePagesRec *) palloc(sizeof(BufferCachePagesRec) * NBuffers);
|
||||||
|
|
||||||
/* allocate the strings for tuple formation */
|
/* Set max calls and remember the user function context. */
|
||||||
fctx->values[0] = (char *) palloc(3 * sizeof(uint32) + 1);
|
funcctx->max_calls = NBuffers;
|
||||||
fctx->values[1] = (char *) palloc(3 * sizeof(uint32) + 1);
|
funcctx->user_fctx = fctx;
|
||||||
fctx->values[2] = (char *) palloc(3 * sizeof(uint32) + 1);
|
|
||||||
fctx->values[3] = (char *) palloc(3 * sizeof(uint32) + 1);
|
|
||||||
fctx->values[4] = (char *) palloc(3 * sizeof(uint32) + 1);
|
|
||||||
fctx->values[5] = (char *) palloc(2);
|
|
||||||
|
|
||||||
/* Return to original context when allocating transient memory */
|
/* Return to original context when allocating transient memory */
|
||||||
MemoryContextSwitchTo(oldcontext);
|
MemoryContextSwitchTo(oldcontext);
|
||||||
@ -167,19 +154,11 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
|
|||||||
if (funcctx->call_cntr < funcctx->max_calls)
|
if (funcctx->call_cntr < funcctx->max_calls)
|
||||||
{
|
{
|
||||||
uint32 i = funcctx->call_cntr;
|
uint32 i = funcctx->call_cntr;
|
||||||
char *values[NUM_BUFFERCACHE_PAGES_ELEM];
|
Datum values[NUM_BUFFERCACHE_PAGES_ELEM];
|
||||||
int j;
|
bool nulls[NUM_BUFFERCACHE_PAGES_ELEM];
|
||||||
|
|
||||||
/*
|
|
||||||
* Use a temporary values array, initially pointing to fctx->values,
|
|
||||||
* so it can be reassigned w/o losing the storage for subsequent
|
|
||||||
* calls.
|
|
||||||
*/
|
|
||||||
for (j = 0; j < NUM_BUFFERCACHE_PAGES_ELEM; j++)
|
|
||||||
{
|
|
||||||
values[j] = fctx->values[j];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
values[0] = Int32GetDatum(fctx->record[i].bufferid);
|
||||||
|
nulls[0] = false;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set all fields except the bufferid to null if the buffer is unused
|
* Set all fields except the bufferid to null if the buffer is unused
|
||||||
@ -188,43 +167,32 @@ pg_buffercache_pages(PG_FUNCTION_ARGS)
|
|||||||
if (fctx->record[i].blocknum == InvalidBlockNumber ||
|
if (fctx->record[i].blocknum == InvalidBlockNumber ||
|
||||||
fctx->record[i].isvalid == false)
|
fctx->record[i].isvalid == false)
|
||||||
{
|
{
|
||||||
|
nulls[1] = true;
|
||||||
sprintf(values[0], "%u", fctx->record[i].bufferid);
|
nulls[2] = true;
|
||||||
values[1] = NULL;
|
nulls[3] = true;
|
||||||
values[2] = NULL;
|
nulls[4] = true;
|
||||||
values[3] = NULL;
|
nulls[5] = true;
|
||||||
values[4] = NULL;
|
|
||||||
values[5] = NULL;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
values[1] = ObjectIdGetDatum(fctx->record[i].relfilenode);
|
||||||
sprintf(values[0], "%u", fctx->record[i].bufferid);
|
nulls[1] = false;
|
||||||
sprintf(values[1], "%u", fctx->record[i].relfilenode);
|
values[2] = ObjectIdGetDatum(fctx->record[i].reltablespace);
|
||||||
sprintf(values[2], "%u", fctx->record[i].reltablespace);
|
nulls[2] = false;
|
||||||
sprintf(values[3], "%u", fctx->record[i].reldatabase);
|
values[3] = ObjectIdGetDatum(fctx->record[i].reldatabase);
|
||||||
sprintf(values[4], "%u", fctx->record[i].blocknum);
|
nulls[3] = false;
|
||||||
if (fctx->record[i].isdirty)
|
values[4] = Int64GetDatum((int64) fctx->record[i].blocknum);
|
||||||
{
|
nulls[4] = false;
|
||||||
strcpy(values[5], "t");
|
values[5] = BoolGetDatum(fctx->record[i].isdirty);
|
||||||
|
nulls[5] = false;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
strcpy(values[5], "f");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Build and return the tuple. */
|
/* Build and return the tuple. */
|
||||||
tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
|
tuple = heap_form_tuple(fctx->tupdesc, values, nulls);
|
||||||
result = HeapTupleGetDatum(tuple);
|
result = HeapTupleGetDatum(tuple);
|
||||||
|
|
||||||
|
|
||||||
SRF_RETURN_NEXT(funcctx, result);
|
SRF_RETURN_NEXT(funcctx, result);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
SRF_RETURN_DONE(funcctx);
|
SRF_RETURN_DONE(funcctx);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user