2 * $Header: /cvsroot/pgsql/contrib/pgstattuple/pgstattuple.c,v 1.3 2001/12/19 20:28:41 tgl Exp $
4 * Copyright (c) 2001 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"
31 PG_FUNCTION_INFO_V1(pgstattuple);
33 extern Datum pgstattuple(PG_FUNCTION_ARGS);
37 * returns the percentage of dead tuples
39 * C FUNCTION definition
40 * pgstattuple(NAME) returns FLOAT8
44 pgstattuple(PG_FUNCTION_ARGS)
46 Name p = PG_GETARG_NAME(0);
52 BlockNumber block = 0; /* next block to count free space in */
57 uint64 dead_tuple_len = 0;
58 uint64 tuple_count = 0;
59 uint64 dead_tuple_count = 0;
61 double dead_tuple_percent;
63 uint64 free_space = 0; /* free/reusable space in bytes */
64 double free_percent; /* free/reusable space in % */
66 rel = heap_openr(NameStr(*p), AccessShareLock);
67 nblocks = RelationGetNumberOfBlocks(rel);
68 scan = heap_beginscan(rel, false, SnapshotAny, 0, NULL);
70 while ((tuple = heap_getnext(scan, 0)))
72 if (HeapTupleSatisfiesNow(tuple->t_data))
74 tuple_len += tuple->t_len;
79 dead_tuple_len += tuple->t_len;
84 * To avoid physically reading the table twice, try to do the
85 * free-space scan in parallel with the heap scan. However,
86 * heap_getnext may find no tuples on a given page, so we cannot
87 * simply examine the pages returned by the heap scan.
89 tupblock = BlockIdGetBlockNumber(&tuple->t_self.ip_blkid);
91 while (block <= tupblock)
93 buffer = ReadBuffer(rel, block);
94 free_space += PageGetFreeSpace((Page) BufferGetPage(buffer));
95 ReleaseBuffer(buffer);
101 while (block < nblocks)
103 buffer = ReadBuffer(rel, block);
104 free_space += PageGetFreeSpace((Page) BufferGetPage(buffer));
105 ReleaseBuffer(buffer);
109 heap_close(rel, AccessShareLock);
111 table_len = (double) nblocks *BLCKSZ;
116 dead_tuple_percent = 0.0;
121 tuple_percent = (double) tuple_len *100.0 / table_len;
122 dead_tuple_percent = (double) dead_tuple_len *100.0 / table_len;
123 free_percent = (double) free_space *100.0 / table_len;
126 elog(NOTICE, "physical length: %.2fMB live tuples: %.0f (%.2fMB, %.2f%%) dead tuples: %.0f (%.2fMB, %.2f%%) free/reusable space: %.2fMB (%.2f%%) overhead: %.2f%%",
128 table_len / (1024 * 1024), /* physical length in MB */
130 (double) tuple_count, /* number of live tuples */
131 (double) tuple_len / (1024 * 1024), /* live tuples in MB */
132 tuple_percent, /* live tuples in % */
134 (double) dead_tuple_count, /* number of dead tuples */
135 (double) dead_tuple_len / (1024 * 1024), /* dead tuples in MB */
136 dead_tuple_percent, /* dead tuples in % */
138 (double) free_space / (1024 * 1024), /* free/available space in
141 free_percent, /* free/available space in % */
144 (nblocks == 0) ? 0.0 : 100.0
149 PG_RETURN_FLOAT8(dead_tuple_percent);