2 * $Header: /cvsroot/pgsql/contrib/pgstattuple/pgstattuple.c,v 1.7 2002/08/23 08:19:49 ishii Exp $
4 * Copyright (c) 2001,2002 Tatsuo Ishii
6 * Permission to use, copy, modify, and distribute this software and
7 * its documentation for any purpose, without fee, and without a
8 * written agreement is hereby granted, provided that the above
9 * copyright notice and this paragraph and the following two
10 * paragraphs appear in all copies.
12 * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
13 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
14 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
15 * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
16 * OF THE POSSIBILITY OF SUCH DAMAGE.
18 * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
21 * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
22 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
28 #include "access/heapam.h"
29 #include "access/transam.h"
30 #include "catalog/namespace.h"
32 #include "utils/builtins.h"
35 PG_FUNCTION_INFO_V1(pgstattuple);
37 extern Datum pgstattuple(PG_FUNCTION_ARGS);
41 * returns live/dead tuples info
43 * C FUNCTION definition
44 * pgstattuple(TEXT) returns setof pgstattuple_view
45 * see pgstattuple.sql for pgstattuple_view
49 #define DUMMY_TUPLE "pgstattuple_view"
54 pgstattuple(PG_FUNCTION_ARGS)
62 BlockNumber block = 0; /* next block to count free space in */
67 uint64 dead_tuple_len = 0;
68 uint64 tuple_count = 0;
69 uint64 dead_tuple_count = 0;
71 double dead_tuple_percent;
72 uint64 free_space = 0; /* free/reusable space in bytes */
73 double free_percent; /* free/reusable space in % */
75 FuncCallContext *funcctx;
80 AttInMetadata *attinmeta;
86 /* stuff done only on the first call of the function */
87 if(SRF_IS_FIRSTCALL())
89 /* create a function context for cross-call persistence */
90 funcctx = SRF_FIRSTCALL_INIT();
92 /* total number of tuples to be returned */
93 funcctx->max_calls = 1;
96 * Build a tuple description for a pgstattupe_view tuple
98 tupdesc = RelationNameGetTupleDesc(DUMMY_TUPLE);
100 /* allocate a slot for a tuple with this tupdesc */
101 slot = TupleDescGetSlot(tupdesc);
103 /* assign slot to function context */
104 funcctx->slot = slot;
107 * Generate attribute metadata needed later to produce tuples from raw
110 attinmeta = TupleDescGetAttInMetadata(tupdesc);
111 funcctx->attinmeta = attinmeta;
114 /* stuff done on every call of the function */
115 funcctx = SRF_PERCALL_SETUP();
116 call_cntr = funcctx->call_cntr;
117 max_calls = funcctx->max_calls;
118 slot = funcctx->slot;
119 attinmeta = funcctx->attinmeta;
122 if (call_cntr >= max_calls)
124 SRF_RETURN_DONE(funcctx);
128 relname = PG_GETARG_TEXT_P(0);
129 relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname,"pgstattuple"));
130 rel = heap_openrv(relrv, AccessShareLock);
132 nblocks = RelationGetNumberOfBlocks(rel);
133 scan = heap_beginscan(rel, SnapshotAny, 0, NULL);
135 /* scan the relation */
136 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
138 if (HeapTupleSatisfiesNow(tuple->t_data))
140 tuple_len += tuple->t_len;
145 dead_tuple_len += tuple->t_len;
150 * To avoid physically reading the table twice, try to do the
151 * free-space scan in parallel with the heap scan. However,
152 * heap_getnext may find no tuples on a given page, so we cannot
153 * simply examine the pages returned by the heap scan.
155 tupblock = BlockIdGetBlockNumber(&tuple->t_self.ip_blkid);
157 while (block <= tupblock)
159 buffer = ReadBuffer(rel, block);
160 free_space += PageGetFreeSpace((Page) BufferGetPage(buffer));
161 ReleaseBuffer(buffer);
167 while (block < nblocks)
169 buffer = ReadBuffer(rel, block);
170 free_space += PageGetFreeSpace((Page) BufferGetPage(buffer));
171 ReleaseBuffer(buffer);
175 heap_close(rel, AccessShareLock);
177 table_len = (uint64)nblocks *BLCKSZ;
182 dead_tuple_percent = 0.0;
187 tuple_percent = (double) tuple_len *100.0 / table_len;
188 dead_tuple_percent = (double) dead_tuple_len *100.0 / table_len;
189 free_percent = (double) free_space *100.0 / table_len;
193 * Prepare a values array for storage in our slot.
194 * This should be an array of C strings which will
195 * be processed later by the appropriate "in" functions.
197 values = (char **) palloc(NCOLUMNS * sizeof(char *));
198 for (i=0;i<NCOLUMNS;i++)
200 values[i] = (char *) palloc(NCHARS * sizeof(char));
203 snprintf(values[i++], NCHARS, "%lld", table_len);
204 snprintf(values[i++], NCHARS, "%lld", tuple_count);
205 snprintf(values[i++], NCHARS, "%lld", tuple_len);
206 snprintf(values[i++], NCHARS, "%.2f", tuple_percent);
207 snprintf(values[i++], NCHARS, "%lld", dead_tuple_count);
208 snprintf(values[i++], NCHARS, "%lld", dead_tuple_len);
209 snprintf(values[i++], NCHARS, "%.2f", dead_tuple_percent);
210 snprintf(values[i++], NCHARS, "%lld", free_space);
211 snprintf(values[i++], NCHARS, "%.2f", free_percent);
214 tuple = BuildTupleFromCStrings(attinmeta, values);
216 /* make the tuple into a datum */
217 result = TupleGetDatum(slot, tuple);
220 for (i=0;i<NCOLUMNS;i++)
226 SRF_RETURN_NEXT(funcctx, result);