mirror of
https://github.com/postgres/postgres.git
synced 2025-08-24 09:27:52 +03:00
Add an EXPLAIN (BUFFERS) option to show buffer-usage statistics.
This patch also removes buffer-usage statistics from the track_counts output, since this (or the global server statistics) is deemed to be a better interface to this information. Itagaki Takahiro, reviewed by Euler Taveira de Oliveira.
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/storage/buffer/buf_init.c,v 1.82 2009/01/01 17:23:47 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/storage/buffer/buf_init.c,v 1.83 2009/12/15 04:57:47 rhaas Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -22,16 +22,6 @@ BufferDesc *BufferDescriptors;
|
||||
char *BufferBlocks;
|
||||
int32 *PrivateRefCount;
|
||||
|
||||
/* statistics counters */
|
||||
long int ReadBufferCount;
|
||||
long int ReadLocalBufferCount;
|
||||
long int BufferHitCount;
|
||||
long int LocalBufferHitCount;
|
||||
long int BufferFlushCount;
|
||||
long int LocalBufferFlushCount;
|
||||
long int BufFileReadCount;
|
||||
long int BufFileWriteCount;
|
||||
|
||||
|
||||
/*
|
||||
* Data Structures:
|
||||
|
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/storage/buffer/bufmgr.c,v 1.252 2009/06/11 14:49:01 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/storage/buffer/bufmgr.c,v 1.253 2009/12/15 04:57:47 rhaas Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -34,6 +34,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "catalog/catalog.h"
|
||||
#include "executor/instrument.h"
|
||||
#include "miscadmin.h"
|
||||
#include "pg_trace.h"
|
||||
#include "pgstat.h"
|
||||
@@ -300,22 +301,23 @@ ReadBuffer_common(SMgrRelation smgr, bool isLocalBuf, ForkNumber forkNum,
|
||||
|
||||
if (isLocalBuf)
|
||||
{
|
||||
ReadLocalBufferCount++;
|
||||
bufHdr = LocalBufferAlloc(smgr, forkNum, blockNum, &found);
|
||||
if (found)
|
||||
LocalBufferHitCount++;
|
||||
pgBufferUsage.local_blks_hit++;
|
||||
else
|
||||
pgBufferUsage.local_blks_read++;
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadBufferCount++;
|
||||
|
||||
/*
|
||||
* lookup the buffer. IO_IN_PROGRESS is set if the requested block is
|
||||
* not currently in memory.
|
||||
*/
|
||||
bufHdr = BufferAlloc(smgr, forkNum, blockNum, strategy, &found);
|
||||
if (found)
|
||||
BufferHitCount++;
|
||||
pgBufferUsage.shared_blks_hit++;
|
||||
else
|
||||
pgBufferUsage.shared_blks_read++;
|
||||
}
|
||||
|
||||
/* At this point we do NOT hold any locks. */
|
||||
@@ -1610,54 +1612,6 @@ SyncOneBuffer(int buf_id, bool skip_recently_used)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Return a palloc'd string containing buffer usage statistics.
|
||||
*/
|
||||
char *
|
||||
ShowBufferUsage(void)
|
||||
{
|
||||
StringInfoData str;
|
||||
float hitrate;
|
||||
float localhitrate;
|
||||
|
||||
initStringInfo(&str);
|
||||
|
||||
if (ReadBufferCount == 0)
|
||||
hitrate = 0.0;
|
||||
else
|
||||
hitrate = (float) BufferHitCount *100.0 / ReadBufferCount;
|
||||
|
||||
if (ReadLocalBufferCount == 0)
|
||||
localhitrate = 0.0;
|
||||
else
|
||||
localhitrate = (float) LocalBufferHitCount *100.0 / ReadLocalBufferCount;
|
||||
|
||||
appendStringInfo(&str,
|
||||
"!\tShared blocks: %10ld read, %10ld written, buffer hit rate = %.2f%%\n",
|
||||
ReadBufferCount - BufferHitCount, BufferFlushCount, hitrate);
|
||||
appendStringInfo(&str,
|
||||
"!\tLocal blocks: %10ld read, %10ld written, buffer hit rate = %.2f%%\n",
|
||||
ReadLocalBufferCount - LocalBufferHitCount, LocalBufferFlushCount, localhitrate);
|
||||
appendStringInfo(&str,
|
||||
"!\tDirect blocks: %10ld read, %10ld written\n",
|
||||
BufFileReadCount, BufFileWriteCount);
|
||||
|
||||
return str.data;
|
||||
}
|
||||
|
||||
void
|
||||
ResetBufferUsage(void)
|
||||
{
|
||||
BufferHitCount = 0;
|
||||
ReadBufferCount = 0;
|
||||
BufferFlushCount = 0;
|
||||
LocalBufferHitCount = 0;
|
||||
ReadLocalBufferCount = 0;
|
||||
LocalBufferFlushCount = 0;
|
||||
BufFileReadCount = 0;
|
||||
BufFileWriteCount = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* AtEOXact_Buffers - clean up at end of transaction.
|
||||
*
|
||||
@@ -1916,7 +1870,7 @@ FlushBuffer(volatile BufferDesc *buf, SMgrRelation reln)
|
||||
(char *) BufHdrGetBlock(buf),
|
||||
false);
|
||||
|
||||
BufferFlushCount++;
|
||||
pgBufferUsage.shared_blks_written++;
|
||||
|
||||
/*
|
||||
* Mark the buffer as clean (unless BM_JUST_DIRTIED has become set) and
|
||||
|
@@ -9,13 +9,14 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.87 2009/06/11 14:49:01 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.88 2009/12/15 04:57:47 rhaas Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#include "postgres.h"
|
||||
|
||||
#include "catalog/catalog.h"
|
||||
#include "executor/instrument.h"
|
||||
#include "storage/buf_internals.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/smgr.h"
|
||||
@@ -209,7 +210,7 @@ LocalBufferAlloc(SMgrRelation smgr, ForkNumber forkNum, BlockNumber blockNum,
|
||||
/* Mark not-dirty now in case we error out below */
|
||||
bufHdr->flags &= ~BM_DIRTY;
|
||||
|
||||
LocalBufferFlushCount++;
|
||||
pgBufferUsage.local_blks_written++;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -7,7 +7,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/storage/file/buffile.c,v 1.34 2009/06/11 14:49:01 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/storage/file/buffile.c,v 1.35 2009/12/15 04:57:47 rhaas Exp $
|
||||
*
|
||||
* NOTES:
|
||||
*
|
||||
@@ -34,6 +34,7 @@
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
#include "executor/instrument.h"
|
||||
#include "storage/fd.h"
|
||||
#include "storage/buffile.h"
|
||||
#include "storage/buf_internals.h"
|
||||
@@ -240,7 +241,7 @@ BufFileLoadBuffer(BufFile *file)
|
||||
file->offsets[file->curFile] += file->nbytes;
|
||||
/* we choose not to advance curOffset here */
|
||||
|
||||
BufFileReadCount++;
|
||||
pgBufferUsage.temp_blks_read++;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -304,7 +305,7 @@ BufFileDumpBuffer(BufFile *file)
|
||||
file->curOffset += bytestowrite;
|
||||
wpos += bytestowrite;
|
||||
|
||||
BufFileWriteCount++;
|
||||
pgBufferUsage.temp_blks_written++;
|
||||
}
|
||||
file->dirty = false;
|
||||
|
||||
|
Reference in New Issue
Block a user