-pgstattuple README 2001/10/01 Tatsuo Ishii
+pgstattuple README 2002/08/22 Tatsuo Ishii
1. What is pgstattuple?
- pgstattuple returns the percentage of the "dead" tuples of a
- table. This will help users to judge if vacuum is needed.
-
- In addition, pgstattuple prints more detailed information using
- NOTICE.
-
-test=# select pgstattuple('tellers');
-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%
- pgstattuple
--------------
- 18.75
-(1 row)
-
- Above example shows tellers table includes 18.75% dead tuples.
-
- physical length physical size of the table in MB
- live tuples information on the live tuples
- dead tuples information on the dead tuples
- free/reusable space available space
- overhead overhead space
+ pgstattuple returns the table length, percentage of the "dead"
+ tuples of a table and other info. This may help users to determine
+ whether vacuum is necessary or not. Here is an example session:
+
+test=# \x
+Expanded display is on.
+test=# select * from pgstattuple('pg_proc');
+-[ RECORD 1 ]------+-------
+table_len | 458752
+tuple_count | 1470
+tuple_len | 438896
+tuple_percent | 95.67
+dead_tuple_count | 11
+dead_tuple_len | 3157
+dead_tuple_percent | 0.69
+free_space | 8932
+free_percent | 1.95
+
+
+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
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
- of the "dead" tuples of a table.
+ The argument is the table name. Note that pgstattuple never
+ returns more than 1 tuple.
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¤È¤Ï
pgstattuple¤Ï¡¤UPDATE¤äDELETE¤Çºî¤é¤ì¤¿¥Æ¡¼¥Ö¥ë¤Î¥´¥ßÎΰè¤ÎÂ礤µ¤ò¡¤
¥Æ¡¼¥Ö¥ë¼«ÂΤÎʪÍýŪ¤ÊÂ礤µ¤ËÂФ¹¤ë¥Ñ¡¼¥»¥ó¥Æ¡¼¥¸¤ÇÊֵѤ·¤Þ¤¹¡¥¤Ä
¤Þ¤ê¡¤ÊÖµÑÃͤ¬Â礤±¤ì¤Ð¡¤¤½¤ì¤À¤±¥´¥ß¤â¿¤¤¤Î¤Ç¡¤vacuum¤ò¤«¤±¤ëɬ
- Íפ¬¤¢¤ë¤È¤¤¤¦È½ÃǤνõ¤±¤Ë¤Ê¤ë¤ï¤±¤Ç¤¹¡¥
-
- ¤³¤ì¤À¤±¤Ç¤Ï¾ðÊóÎ̤¬¾¯¤Ê¤¤¤Î¤Ç¡¤NOTICE¥á¥Ã¥»¡¼¥¸¤Ç¤¤¤í¤ó¤Ê¾ðÊó¤ò¤Ä
- ¤¤¤Ç¤Ë½ÐÎϤ·¤Þ¤¹¡¥
-
-test=# select pgstattuple('tellers');
-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%
- pgstattuple
--------------
- 18.75
-(1 row)
-
- ¤³¤Î¼Â¹ÔÎã¤Ç¤Ï¡¤19%¤Û¤É¤¬¥´¥ß¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤³¤È¤¬»Ç¤¨¤Þ¤¹¡¥NOTICE¥á¥Ã
- ¥»¡¼¥¸¤Î¸«Êý¤â½ñ¤¤¤Æ¤ª¤¤Þ¤¹¡¥
-
- physical length ¥Æ¡¼¥Ö¥ë¤ÎʪÍýŪ¤Ê¥µ¥¤¥º¤òMBñ°Ì¤Çɽ¼¨
- live tuples ¥´¥ß¤Ç¤Ï¤Ê¤¤¥¿¥×¥ë¤Ë´Ø¤¹¤ë¾ðÊó¡¥¥¿¥×¥ë¿ô¡¤³Æ
- ¥¿¥×¥ë¤¬Àê¤á¤ëÎΰè¤Î¹ç·×¡¤¥Æ¡¼¥Ö¥ëÁ´ÂΤËÂФ¹¤ë
- ÈæΨ¤òɽ¼¨¤·¤Þ¤¹¡¥
- dead tuples ¥´¥ß¤Ë¤Ê¤Ã¤¿¥¿¥×¥ë¤Ë´Ø¤¹¤ë¾ðÊó¡¥
- free/reusable space ÍøÍѲÄǽ¤Ê̤»ÈÍÑÎΰè¤äºÆÍøÍѲÄǽÎΰè
- overhead ´ÉÍý¤Î¤¿¤á¤ÎÎΰ褬¥Æ¡¼¥Ö¥ëÁ´ÂΤËÀê¤á¤ëÈæΨ
+ Íפ¬¤¢¤ë¤È¤¤¤¦È½ÃǤνõ¤±¤Ë¤Ê¤ë¤ï¤±¤Ç¤¹¡¥¤³¤ì°Ê³°¤Ë¤â¤¤¤í¤¤¤í¤Ê¾ðÊó
+ ¤¬ÊÖ¤ê¤Þ¤¹¡¥
+
+test=# \x
+Expanded display is on.
+test=# select * from pgstattuple('pg_proc');
+-[ RECORD 1 ]------+-------
+table_len | 458752
+tuple_count | 1470
+tuple_len | 438896
+tuple_percent | 95.67
+dead_tuple_count | 11
+dead_tuple_len | 3157
+dead_tuple_percent | 0.69
+free_space | 8932
+free_percent | 1.95
+
+³Æ¹àÌܤÎÀâÌÀ¤Ç¤¹¡¥
+
+table_len -- ¥Æ¡¼¥Ö¥ë¤ÎʪÍýŪ¤ÊÂ礤µ(¥Ð¥¤¥È)
+tuple_count -- ¥¿¥×¥ë¿ô
+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¤Î¥¤¥ó¥¹¥È¡¼¥ë
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 ¤ò¼Â¹ÔÃæ¤Ë³ºÅö¥Æ¡¼¥Ö¥ë¤Ë¹¹¿·¤äºï½ü¤¬È¯À¸¤¹¤ë¤È¡¤Àµ¤·¤¯
5. ²þÄûÍúÎò
+ 2002/08/23
+
+ SRF(Set Returning Function)¤ò»È¤Ã¤Æ7.3ÍѤ˽ñ¤´¹¤¨¡¥
+
2001/12/20 Tom Lane¤Ë¤è¤ë½¤Àµ
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
* its documentation for any purpose, without fee, and without a
#include "access/heapam.h"
#include "access/transam.h"
#include "catalog/namespace.h"
+#include "funcapi.h"
#include "utils/builtins.h"
/* ----------
* pgstattuple:
- * returns the percentage of dead tuples
+ * returns live/dead tuples info
*
* 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
pgstattuple(PG_FUNCTION_ARGS)
{
- text *relname = PG_GETARG_TEXT_P(0);
+ text *relname;
RangeVar *relrv;
Relation rel;
HeapScanDesc scan;
BlockNumber block = 0; /* next block to count free space in */
BlockNumber tupblock;
Buffer buffer;
- double table_len;
+ uint64 table_len;
uint64 tuple_len = 0;
uint64 dead_tuple_len = 0;
uint64 tuple_count = 0;
uint64 free_space = 0; /* free/reusable space in bytes */
double free_percent; /* free/reusable space in % */
- relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname,
- "pgstattuple"));
+ FuncCallContext *funcctx;
+ 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);
nblocks = RelationGetNumberOfBlocks(rel);
scan = heap_beginscan(rel, SnapshotAny, 0, NULL);
+ /* scan the relation */
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
{
if (HeapTupleSatisfiesNow(tuple->t_data))
heap_close(rel, AccessShareLock);
- table_len = (double) nblocks *BLCKSZ;
+ table_len = (uint64)nblocks *BLCKSZ;
if (nblocks == 0)
{
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%%",
-
- table_len / (1024 * 1024), /* physical length in MB */
-
- (double) tuple_count, /* number of live tuples */
- (double) tuple_len / (1024 * 1024), /* live tuples in MB */
- tuple_percent, /* live tuples in % */
-
- (double) dead_tuple_count, /* number of dead tuples */
- (double) dead_tuple_len / (1024 * 1024), /* dead tuples in MB */
- dead_tuple_percent, /* dead tuples in % */
-
- (double) free_space / (1024 * 1024), /* free/available space in
- * MB */
-
- free_percent, /* free/available space in % */
-
- /* overhead in % */
- (nblocks == 0) ? 0.0 : 100.0
- - tuple_percent
- - dead_tuple_percent
- - free_percent);
-
- PG_RETURN_FLOAT8(dead_tuple_percent);
+ /*
+ * Prepare a values array for storage in our slot.
+ * This should be an array of C strings which will
+ * be processed later by the appropriate "in" functions.
+ */
+ values = (char **) palloc(NCOLUMNS * sizeof(char *));
+ for (i=0;i<NCOLUMNS;i++)
+ {
+ values[i] = (char *) palloc(NCHARS * sizeof(char));
+ }
+ i = 0;
+ snprintf(values[i++], NCHARS, "%lld", table_len);
+ snprintf(values[i++], NCHARS, "%lld", tuple_count);
+ snprintf(values[i++], NCHARS, "%lld", tuple_len);
+ snprintf(values[i++], NCHARS, "%.2f", tuple_percent);
+ snprintf(values[i++], NCHARS, "%lld", dead_tuple_count);
+ snprintf(values[i++], NCHARS, "%lld", dead_tuple_len);
+ snprintf(values[i++], NCHARS, "%.2f", dead_tuple_percent);
+ snprintf(values[i++], NCHARS, "%lld", free_space);
+ snprintf(values[i++], NCHARS, "%.2f", free_percent);
+
+ /* build a tuple */
+ tuple = BuildTupleFromCStrings(attinmeta, values);
+
+ /* 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);
}