Commit cf7ee638 authored by Tatsuo Ishii's avatar Tatsuo Ishii

Adapt for SRF(Set Returning Function).

parent 6415ffe7
pgstattuple README 2001/10/01 Tatsuo Ishii pgstattuple README 2002/08/22 Tatsuo Ishii
1. What is pgstattuple? 1. What is pgstattuple?
pgstattuple returns the percentage of the "dead" tuples of a pgstattuple returns the table length, percentage of the "dead"
table. This will help users to judge if vacuum is needed. tuples of a table and other info. This may help users to determine
whether vacuum is necessary or not. Here is an example session:
In addition, pgstattuple prints more detailed information using
NOTICE. test=# \x
Expanded display is on.
test=# select pgstattuple('tellers'); test=# select * from pgstattuple('pg_proc');
NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 320 (0.01MB, 18.75%) free/reusable space: 0.01MB (18.06%) overhead: 62.02% -[ RECORD 1 ]------+-------
pgstattuple table_len | 458752
------------- tuple_count | 1470
18.75 tuple_len | 438896
(1 row) tuple_percent | 95.67
dead_tuple_count | 11
Above example shows tellers table includes 18.75% dead tuples. dead_tuple_len | 3157
dead_tuple_percent | 0.69
physical length physical size of the table in MB free_space | 8932
live tuples information on the live tuples free_percent | 1.95
dead tuples information on the dead tuples
free/reusable space available space
overhead overhead space Here are explanations for each column:
table_len -- physical table length in bytes
tuple_count -- number of live tuples
tuple_len -- total tuples length in bytes
tuple_percent -- live tuples in %
dead_tuple_len -- total dead tuples length in bytes
dead_tuple_percent -- dead tuples in %
free_space -- free space in bytes
free_percent -- free space in %
2. Installing pgstattuple 2. Installing pgstattuple
...@@ -31,12 +40,15 @@ NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32 ...@@ -31,12 +40,15 @@ NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32
3. Using pgstattuple 3. Using pgstattuple
pgstattuple can be called as a function: pgstattuple may be called as a SRF (set returning function) and is
defined as follows:
pgstattuple(TEXT) RETURNS FLOAT8 CREATE OR REPLACE FUNCTION pgstattuple(text) RETURNS SETOF pgstattuple_view
AS 'MODULE_PATHNAME', 'pgstattuple'
LANGUAGE 'c' WITH (isstrict);
The argument is the table name. pgstattuple returns the percentage The argument is the table name. Note that pgstattuple never
of the "dead" tuples of a table. returns more than 1 tuple.
4. Notes 4. Notes
......
$Id: README.pgstattuple.euc_jp,v 1.2 2001/12/20 01:56:08 ishii Exp $ $Id: README.pgstattuple.euc_jp,v 1.3 2002/08/23 08:19:49 ishii Exp $
pgstattuple README 2001/10/01 石井達夫 pgstattuple README 2002/08/22 石井達夫
1. pgstattupleとは 1. pgstattupleとは
pgstattupleは,UPDATEやDELETEで作られたテーブルのゴミ領域の大きさを, pgstattupleは,UPDATEやDELETEで作られたテーブルのゴミ領域の大きさを,
テーブル自体の物理的な大きさに対するパーセンテージで返却します.つ テーブル自体の物理的な大きさに対するパーセンテージで返却します.つ
まり,返却値が大きければ,それだけゴミも多いので,vacuumをかける必 まり,返却値が大きければ,それだけゴミも多いので,vacuumをかける必
要があるという判断の助けになるわけです. 要があるという判断の助けになるわけです.これ以外にもいろいろな情報
が返ります.
これだけでは情報量が少ないので,NOTICEメッセージでいろんな情報をつ
いでに出力します. test=# \x
Expanded display is on.
test=# select pgstattuple('tellers'); test=# select * from pgstattuple('pg_proc');
NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 320 (0.01MB, 18.75%) free/reusable space: 0.01MB (18.06%) overhead: 62.02% -[ RECORD 1 ]------+-------
pgstattuple table_len | 458752
------------- tuple_count | 1470
18.75 tuple_len | 438896
(1 row) tuple_percent | 95.67
dead_tuple_count | 11
この実行例では,19%ほどがゴミになっていることが伺えます.NOTICEメッ dead_tuple_len | 3157
セージの見方も書いておきます. dead_tuple_percent | 0.69
free_space | 8932
physical length テーブルの物理的なサイズをMB単位で表示 free_percent | 1.95
live tuples ゴミではないタプルに関する情報.タプル数,各
タプルが占める領域の合計,テーブル全体に対する 各項目の説明です.
比率を表示します.
dead tuples ゴミになったタプルに関する情報. table_len -- テーブルの物理的な大きさ(バイト)
free/reusable space 利用可能な未使用領域や再利用可能領域 tuple_count -- タプル数
overhead 管理のための領域がテーブル全体に占める比率 tuple_len -- タプル長の合計(バイト)
tuple_percent -- タプルの割合.table_lenに対するtuple_lenの比率.
dead_tuple_len -- デッドタプル数
dead_tuple_percent -- デッドタプルの割合.table_lenに対するtuple_lenの比率.
free_space -- 再利用可能な領域(バイト)
free_percent -- 再利用可能な領域.table_lenに対するfree_spaceの比率.
2. pgstattupleのインストール 2. pgstattupleのインストール
...@@ -47,12 +52,14 @@ NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32 ...@@ -47,12 +52,14 @@ NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32
pgstattupleの呼び出し形式は以下です. pgstattupleの呼び出し形式は以下です.
pgstattuple(NAME) RETURNS FLOAT8 CREATE OR REPLACE FUNCTION pgstattuple(text) RETURNS SETOF pgstattuple_view
AS 'MODULE_PATHNAME', 'pgstattuple'
LANGUAGE 'c' WITH (isstrict);
第一引数: テーブル名 第一引数: テーブル名
関数の戻りはUPDATEやDELETEで作られたテーブルのゴミ領域の大きさで, 関数の戻りはpgstattuple_view型です.また,pgstattupleは1行しか行を
テーブルの物理的な大きさに対する割合(パーセント)で返却します 返しません
なお,pgstattupleはテーブルにAccessShareLockしかかけないので, なお,pgstattupleはテーブルにAccessShareLockしかかけないので,
pgstattuple を実行中に該当テーブルに更新や削除が発生すると,正しく pgstattuple を実行中に該当テーブルに更新や削除が発生すると,正しく
...@@ -69,6 +76,10 @@ NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32 ...@@ -69,6 +76,10 @@ NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32
5. 改訂履歴 5. 改訂履歴
2002/08/23
SRF(Set Returning Function)を使って7.3用に書き換え.
2001/12/20 Tom Laneによる修正 2001/12/20 Tom Laneによる修正
Fix pgstattuple to acquire a read lock on the target table. This Fix pgstattuple to acquire a read lock on the target table. This
......
/* /*
* $Header: /cvsroot/pgsql/contrib/pgstattuple/pgstattuple.c,v 1.6 2002/05/20 23:51:40 tgl Exp $ * $Header: /cvsroot/pgsql/contrib/pgstattuple/pgstattuple.c,v 1.7 2002/08/23 08:19:49 ishii Exp $
* *
* Copyright (c) 2001 Tatsuo Ishii * Copyright (c) 2001,2002 Tatsuo Ishii
* *
* Permission to use, copy, modify, and distribute this software and * Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose, without fee, and without a * its documentation for any purpose, without fee, and without a
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/transam.h" #include "access/transam.h"
#include "catalog/namespace.h" #include "catalog/namespace.h"
#include "funcapi.h"
#include "utils/builtins.h" #include "utils/builtins.h"
...@@ -37,16 +38,22 @@ extern Datum pgstattuple(PG_FUNCTION_ARGS); ...@@ -37,16 +38,22 @@ extern Datum pgstattuple(PG_FUNCTION_ARGS);
/* ---------- /* ----------
* pgstattuple: * pgstattuple:
* returns the percentage of dead tuples * returns live/dead tuples info
* *
* C FUNCTION definition * C FUNCTION definition
* pgstattuple(NAME) returns FLOAT8 * pgstattuple(TEXT) returns setof pgstattuple_view
* see pgstattuple.sql for pgstattuple_view
* ---------- * ----------
*/ */
#define DUMMY_TUPLE "pgstattuple_view"
#define NCOLUMNS 9
#define NCHARS 32
Datum Datum
pgstattuple(PG_FUNCTION_ARGS) pgstattuple(PG_FUNCTION_ARGS)
{ {
text *relname = PG_GETARG_TEXT_P(0); text *relname;
RangeVar *relrv; RangeVar *relrv;
Relation rel; Relation rel;
HeapScanDesc scan; HeapScanDesc scan;
...@@ -55,7 +62,7 @@ pgstattuple(PG_FUNCTION_ARGS) ...@@ -55,7 +62,7 @@ pgstattuple(PG_FUNCTION_ARGS)
BlockNumber block = 0; /* next block to count free space in */ BlockNumber block = 0; /* next block to count free space in */
BlockNumber tupblock; BlockNumber tupblock;
Buffer buffer; Buffer buffer;
double table_len; uint64 table_len;
uint64 tuple_len = 0; uint64 tuple_len = 0;
uint64 dead_tuple_len = 0; uint64 dead_tuple_len = 0;
uint64 tuple_count = 0; uint64 tuple_count = 0;
...@@ -65,13 +72,67 @@ pgstattuple(PG_FUNCTION_ARGS) ...@@ -65,13 +72,67 @@ pgstattuple(PG_FUNCTION_ARGS)
uint64 free_space = 0; /* free/reusable space in bytes */ uint64 free_space = 0; /* free/reusable space in bytes */
double free_percent; /* free/reusable space in % */ double free_percent; /* free/reusable space in % */
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname, FuncCallContext *funcctx;
"pgstattuple")); int call_cntr;
int max_calls;
TupleDesc tupdesc;
TupleTableSlot *slot;
AttInMetadata *attinmeta;
char **values;
int i;
Datum result;
/* stuff done only on the first call of the function */
if(SRF_IS_FIRSTCALL())
{
/* create a function context for cross-call persistence */
funcctx = SRF_FIRSTCALL_INIT();
/* total number of tuples to be returned */
funcctx->max_calls = 1;
/*
* Build a tuple description for a pgstattupe_view tuple
*/
tupdesc = RelationNameGetTupleDesc(DUMMY_TUPLE);
/* allocate a slot for a tuple with this tupdesc */
slot = TupleDescGetSlot(tupdesc);
/* assign slot to function context */
funcctx->slot = slot;
/*
* Generate attribute metadata needed later to produce tuples from raw
* C strings
*/
attinmeta = TupleDescGetAttInMetadata(tupdesc);
funcctx->attinmeta = attinmeta;
}
/* stuff done on every call of the function */
funcctx = SRF_PERCALL_SETUP();
call_cntr = funcctx->call_cntr;
max_calls = funcctx->max_calls;
slot = funcctx->slot;
attinmeta = funcctx->attinmeta;
/* Are we done? */
if (call_cntr >= max_calls)
{
SRF_RETURN_DONE(funcctx);
}
/* open relation */
relname = PG_GETARG_TEXT_P(0);
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname,"pgstattuple"));
rel = heap_openrv(relrv, AccessShareLock); rel = heap_openrv(relrv, AccessShareLock);
nblocks = RelationGetNumberOfBlocks(rel); nblocks = RelationGetNumberOfBlocks(rel);
scan = heap_beginscan(rel, SnapshotAny, 0, NULL); scan = heap_beginscan(rel, SnapshotAny, 0, NULL);
/* scan the relation */
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{ {
if (HeapTupleSatisfiesNow(tuple->t_data)) if (HeapTupleSatisfiesNow(tuple->t_data))
...@@ -113,7 +174,7 @@ pgstattuple(PG_FUNCTION_ARGS) ...@@ -113,7 +174,7 @@ pgstattuple(PG_FUNCTION_ARGS)
heap_close(rel, AccessShareLock); heap_close(rel, AccessShareLock);
table_len = (double) nblocks *BLCKSZ; table_len = (uint64)nblocks *BLCKSZ;
if (nblocks == 0) if (nblocks == 0)
{ {
...@@ -128,28 +189,39 @@ pgstattuple(PG_FUNCTION_ARGS) ...@@ -128,28 +189,39 @@ pgstattuple(PG_FUNCTION_ARGS)
free_percent = (double) free_space *100.0 / table_len; free_percent = (double) free_space *100.0 / table_len;
} }
elog(DEBUG3, "physical length: %.2fMB live tuples: %.0f (%.2fMB, %.2f%%) dead tuples: %.0f (%.2fMB, %.2f%%) free/reusable space: %.2fMB (%.2f%%) overhead: %.2f%%", /*
* Prepare a values array for storage in our slot.
table_len / (1024 * 1024), /* physical length in MB */ * This should be an array of C strings which will
* be processed later by the appropriate "in" functions.
(double) tuple_count, /* number of live tuples */ */
(double) tuple_len / (1024 * 1024), /* live tuples in MB */ values = (char **) palloc(NCOLUMNS * sizeof(char *));
tuple_percent, /* live tuples in % */ for (i=0;i<NCOLUMNS;i++)
{
(double) dead_tuple_count, /* number of dead tuples */ values[i] = (char *) palloc(NCHARS * sizeof(char));
(double) dead_tuple_len / (1024 * 1024), /* dead tuples in MB */ }
dead_tuple_percent, /* dead tuples in % */ i = 0;
snprintf(values[i++], NCHARS, "%lld", table_len);
(double) free_space / (1024 * 1024), /* free/available space in snprintf(values[i++], NCHARS, "%lld", tuple_count);
* MB */ snprintf(values[i++], NCHARS, "%lld", tuple_len);
snprintf(values[i++], NCHARS, "%.2f", tuple_percent);
free_percent, /* free/available space in % */ snprintf(values[i++], NCHARS, "%lld", dead_tuple_count);
snprintf(values[i++], NCHARS, "%lld", dead_tuple_len);
/* overhead in % */ snprintf(values[i++], NCHARS, "%.2f", dead_tuple_percent);
(nblocks == 0) ? 0.0 : 100.0 snprintf(values[i++], NCHARS, "%lld", free_space);
- tuple_percent snprintf(values[i++], NCHARS, "%.2f", free_percent);
- dead_tuple_percent
- free_percent); /* build a tuple */
tuple = BuildTupleFromCStrings(attinmeta, values);
PG_RETURN_FLOAT8(dead_tuple_percent);
/* make the tuple into a datum */
result = TupleGetDatum(slot, tuple);
/* Clean up */
for (i=0;i<NCOLUMNS;i++)
{
pfree(values[i]);
}
pfree(values);
SRF_RETURN_NEXT(funcctx, result);
} }
DROP FUNCTION pgstattuple(text); DROP VIEW pgstattuple_view CASCADE;
CREATE FUNCTION pgstattuple(text) RETURNS float8 CREATE VIEW pgstattuple_view AS
AS 'MODULE_PATHNAME', 'pgstattuple' SELECT
LANGUAGE 'c' WITH (isstrict); 0::BIGINT AS table_len, -- physical table length in bytes
0::BIGINT AS tuple_count, -- number of live tuples
0::BIGINT AS tuple_len, -- total tuples length in bytes
0.0::FLOAT AS tuple_percent, -- live tuples in %
0::BIGINT AS dead_tuple_count, -- number of dead tuples
0::BIGINT AS dead_tuple_len, -- total dead tuples length in bytes
0.0::FLOAT AS dead_tuple_percent, -- dead tuples in %
0::BIGINT AS free_space, -- free space in bytes
0.0::FLOAT AS free_percent; -- free space in %
CREATE OR REPLACE FUNCTION pgstattuple(text) RETURNS SETOF pgstattuple_view
AS 'MODULE_PATHNAME', 'pgstattuple'
LANGUAGE 'c' WITH (isstrict);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment