OSDN Git Service

Adapt for SRF(Set Returning Function).
authorTatsuo Ishii <ishii@postgresql.org>
Fri, 23 Aug 2002 08:19:49 +0000 (08:19 +0000)
committerTatsuo Ishii <ishii@postgresql.org>
Fri, 23 Aug 2002 08:19:49 +0000 (08:19 +0000)
contrib/pgstattuple/README.pgstattuple
contrib/pgstattuple/README.pgstattuple.euc_jp
contrib/pgstattuple/pgstattuple.c
contrib/pgstattuple/pgstattuple.sql.in

index 7237b9a..804c37c 100644 (file)
@@ -1,27 +1,36 @@
-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
 
@@ -31,12 +40,15 @@ NOTICE:  physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32
 
 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
 
index 5142e9b..00a4bcb 100644 (file)
@@ -1,34 +1,39 @@
-$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¤Î¥¤¥ó¥¹¥È¡¼¥ë
 
@@ -47,12 +52,14 @@ NOTICE:  physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32
 
    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 ¤ò¼Â¹ÔÃæ¤Ë³ºÅö¥Æ¡¼¥Ö¥ë¤Ë¹¹¿·¤äºï½ü¤¬È¯À¸¤¹¤ë¤È¡¤Àµ¤·¤¯
@@ -69,6 +76,10 @@ NOTICE:  physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32
 
 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
index 498f820..a357f6f 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $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
@@ -28,6 +28,7 @@
 #include "access/heapam.h"
 #include "access/transam.h"
 #include "catalog/namespace.h"
+#include "funcapi.h"
 #include "utils/builtins.h"
 
 
@@ -37,16 +38,22 @@ extern Datum pgstattuple(PG_FUNCTION_ARGS);
 
 /* ----------
  * 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;
@@ -55,7 +62,7 @@ pgstattuple(PG_FUNCTION_ARGS)
        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;
@@ -65,13 +72,67 @@ pgstattuple(PG_FUNCTION_ARGS)
        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))
@@ -113,7 +174,7 @@ pgstattuple(PG_FUNCTION_ARGS)
 
        heap_close(rel, AccessShareLock);
 
-       table_len = (double) nblocks *BLCKSZ;
+       table_len = (uint64)nblocks *BLCKSZ;
 
        if (nblocks == 0)
        {
@@ -128,28 +189,39 @@ pgstattuple(PG_FUNCTION_ARGS)
                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);
 }
index b49a134..7c661a8 100644 (file)
@@ -1,4 +1,16 @@
-DROP FUNCTION pgstattuple(text);
-CREATE FUNCTION pgstattuple(text) RETURNS float8
-AS 'MODULE_PATHNAME', 'pgstattuple'
-LANGUAGE 'c' WITH (isstrict);
+DROP VIEW pgstattuple_view CASCADE;
+CREATE VIEW pgstattuple_view AS
+  SELECT
+    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);