]> granicus.if.org Git - postgresql/blob - contrib/pgstattuple/pgstattuple.c
Adapt for SRF(Set Returning Function).
[postgresql] / contrib / pgstattuple / pgstattuple.c
1 /*
2  * $Header: /cvsroot/pgsql/contrib/pgstattuple/pgstattuple.c,v 1.7 2002/08/23 08:19:49 ishii Exp $
3  *
4  * Copyright (c) 2001,2002  Tatsuo Ishii
5  *
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.
11  *
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.
17  *
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.
23  */
24
25 #include "postgres.h"
26
27 #include "fmgr.h"
28 #include "access/heapam.h"
29 #include "access/transam.h"
30 #include "catalog/namespace.h"
31 #include "funcapi.h"
32 #include "utils/builtins.h"
33
34
35 PG_FUNCTION_INFO_V1(pgstattuple);
36
37 extern Datum pgstattuple(PG_FUNCTION_ARGS);
38
39 /* ----------
40  * pgstattuple:
41  * returns live/dead tuples info
42  *
43  * C FUNCTION definition
44  * pgstattuple(TEXT) returns setof pgstattuple_view
45  * see pgstattuple.sql for pgstattuple_view
46  * ----------
47  */
48
49 #define DUMMY_TUPLE "pgstattuple_view"
50 #define NCOLUMNS 9
51 #define NCHARS 32
52
53 Datum
54 pgstattuple(PG_FUNCTION_ARGS)
55 {
56         text       *relname;
57         RangeVar   *relrv;
58         Relation        rel;
59         HeapScanDesc scan;
60         HeapTuple       tuple;
61         BlockNumber nblocks;
62         BlockNumber block = 0;          /* next block to count free space in */
63         BlockNumber tupblock;
64         Buffer          buffer;
65         uint64          table_len;
66         uint64          tuple_len = 0;
67         uint64          dead_tuple_len = 0;
68         uint64          tuple_count = 0;
69         uint64          dead_tuple_count = 0;
70         double          tuple_percent;
71         double          dead_tuple_percent;
72         uint64          free_space = 0; /* free/reusable space in bytes */
73         double          free_percent;   /* free/reusable space in % */
74
75         FuncCallContext    *funcctx;
76         int                                     call_cntr;
77         int                                     max_calls;
78         TupleDesc                       tupdesc;
79         TupleTableSlot     *slot;
80         AttInMetadata      *attinmeta;
81
82         char **values;
83         int i;
84         Datum           result;
85
86         /* stuff done only on the first call of the function */
87         if(SRF_IS_FIRSTCALL())
88         {
89                 /* create a function context for cross-call persistence */
90                 funcctx = SRF_FIRSTCALL_INIT();
91     
92                 /* total number of tuples to be returned */
93                 funcctx->max_calls = 1;
94     
95                 /*
96                  * Build a tuple description for a pgstattupe_view tuple
97                  */
98                 tupdesc = RelationNameGetTupleDesc(DUMMY_TUPLE);
99     
100                 /* allocate a slot for a tuple with this tupdesc */
101                 slot = TupleDescGetSlot(tupdesc);
102     
103                 /* assign slot to function context */
104                 funcctx->slot = slot;
105     
106                 /*
107                  * Generate attribute metadata needed later to produce tuples from raw
108                  * C strings
109                  */
110                 attinmeta = TupleDescGetAttInMetadata(tupdesc);
111                 funcctx->attinmeta = attinmeta;
112         }
113
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;
120
121         /* Are we done? */
122         if (call_cntr >= max_calls)
123         {
124                 SRF_RETURN_DONE(funcctx);
125         }
126
127         /* open relation */
128         relname = PG_GETARG_TEXT_P(0);
129         relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname,"pgstattuple"));
130         rel = heap_openrv(relrv, AccessShareLock);
131
132         nblocks = RelationGetNumberOfBlocks(rel);
133         scan = heap_beginscan(rel, SnapshotAny, 0, NULL);
134
135         /* scan the relation */
136         while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
137         {
138                 if (HeapTupleSatisfiesNow(tuple->t_data))
139                 {
140                         tuple_len += tuple->t_len;
141                         tuple_count++;
142                 }
143                 else
144                 {
145                         dead_tuple_len += tuple->t_len;
146                         dead_tuple_count++;
147                 }
148
149                 /*
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.
154                  */
155                 tupblock = BlockIdGetBlockNumber(&tuple->t_self.ip_blkid);
156
157                 while (block <= tupblock)
158                 {
159                         buffer = ReadBuffer(rel, block);
160                         free_space += PageGetFreeSpace((Page) BufferGetPage(buffer));
161                         ReleaseBuffer(buffer);
162                         block++;
163                 }
164         }
165         heap_endscan(scan);
166
167         while (block < nblocks)
168         {
169                 buffer = ReadBuffer(rel, block);
170                 free_space += PageGetFreeSpace((Page) BufferGetPage(buffer));
171                 ReleaseBuffer(buffer);
172                 block++;
173         }
174
175         heap_close(rel, AccessShareLock);
176
177         table_len = (uint64)nblocks *BLCKSZ;
178
179         if (nblocks == 0)
180         {
181                 tuple_percent = 0.0;
182                 dead_tuple_percent = 0.0;
183                 free_percent = 0.0;
184         }
185         else
186         {
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;
190         }
191
192         /*
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.
196          */
197         values = (char **) palloc(NCOLUMNS * sizeof(char *));
198         for (i=0;i<NCOLUMNS;i++)
199         {
200                 values[i] = (char *) palloc(NCHARS * sizeof(char));
201         }
202         i = 0;
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);
212     
213         /* build a tuple */
214         tuple = BuildTupleFromCStrings(attinmeta, values);
215     
216         /* make the tuple into a datum */
217         result = TupleGetDatum(slot, tuple);
218     
219         /* Clean up */
220         for (i=0;i<NCOLUMNS;i++)
221         {
222                 pfree(values[i]);
223         }
224         pfree(values);
225     
226         SRF_RETURN_NEXT(funcctx, result);
227 }