7 * Copyright (c) 2006 Satoshi Nagayasu <nagayasus@nttdata.co.jp>
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose, without fee, and without a
11 * written agreement is hereby granted, provided that the above
12 * copyright notice and this paragraph and the following two
13 * paragraphs appear in all copies.
15 * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
16 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
17 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
18 * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
19 * OF THE POSSIBILITY OF SUCH DAMAGE.
21 * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
24 * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
25 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
30 #include "access/heapam.h"
31 #include "access/nbtree.h"
32 #include "catalog/namespace.h"
34 #include "miscadmin.h"
35 #include "storage/bufmgr.h"
36 #include "utils/builtins.h"
39 extern Datum
pgstatindex(PG_FUNCTION_ARGS
);
40 extern Datum
pg_relpages(PG_FUNCTION_ARGS
);
42 PG_FUNCTION_INFO_V1(pgstatindex
);
43 PG_FUNCTION_INFO_V1(pg_relpages
);
45 #define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
46 #define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)
48 #define CHECK_PAGE_OFFSET_RANGE(pg, offnum) { \
49 if ( !(FirstOffsetNumber <= (offnum) && \
50 (offnum) <= PageGetMaxOffsetNumber(pg)) ) \
51 elog(ERROR, "page offset number out of range"); }
53 /* note: BlockNumber is unsigned, hence can't be negative */
54 #define CHECK_RELATION_BLOCK_RANGE(rel, blkno) { \
55 if ( RelationGetNumberOfBlocks(rel) <= (BlockNumber) (blkno) ) \
56 elog(ERROR, "block number out of range"); }
58 /* ------------------------------------------------
59 * A structure for a whole btree index statistics
60 * used by pgstatindex().
61 * ------------------------------------------------
63 typedef struct BTIndexStat
67 BlockNumber root_blkno
;
70 uint64 internal_pages
;
81 /* ------------------------------------------------------
84 * Usage: SELECT * FROM pgstatindex('t1_pkey');
85 * ------------------------------------------------------
88 pgstatindex(PG_FUNCTION_ARGS
)
90 text
*relname
= PG_GETARG_TEXT_P(0);
96 BTIndexStat indexStat
;
100 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
101 (errmsg("must be superuser to use pgstattuple functions"))));
103 relrv
= makeRangeVarFromNameList(textToQualifiedNameList(relname
));
104 rel
= relation_openrv(relrv
, AccessShareLock
);
106 if (!IS_INDEX(rel
) || !IS_BTREE(rel
))
107 elog(ERROR
, "relation \"%s\" is not a btree index",
108 RelationGetRelationName(rel
));
114 Buffer buffer
= ReadBuffer(rel
, 0);
115 Page page
= BufferGetPage(buffer
);
116 BTMetaPageData
*metad
= BTPageGetMeta(page
);
118 indexStat
.version
= metad
->btm_version
;
119 indexStat
.level
= metad
->btm_level
;
120 indexStat
.root_blkno
= metad
->btm_root
;
122 ReleaseBuffer(buffer
);
125 /* -- init counters -- */
126 indexStat
.root_pages
= 0;
127 indexStat
.internal_pages
= 0;
128 indexStat
.leaf_pages
= 0;
129 indexStat
.empty_pages
= 0;
130 indexStat
.deleted_pages
= 0;
132 indexStat
.max_avail
= 0;
133 indexStat
.free_space
= 0;
135 indexStat
.fragments
= 0;
138 * Scan all blocks except the metapage
140 nblocks
= RelationGetNumberOfBlocks(rel
);
142 for (blkno
= 1; blkno
< nblocks
; blkno
++)
148 /* Read and lock buffer */
149 buffer
= ReadBuffer(rel
, blkno
);
150 LockBuffer(buffer
, BUFFER_LOCK_SHARE
);
152 page
= BufferGetPage(buffer
);
153 opaque
= (BTPageOpaque
) PageGetSpecialPointer(page
);
155 /* Determine page type, and update totals */
157 if (P_ISLEAF(opaque
))
161 max_avail
= BLCKSZ
- (BLCKSZ
- ((PageHeader
) page
)->pd_special
+ SizeOfPageHeaderData
);
162 indexStat
.max_avail
+= max_avail
;
163 indexStat
.free_space
+= PageGetFreeSpace(page
);
165 indexStat
.leaf_pages
++;
168 * If the next leaf is on an earlier block, it means a
171 if (opaque
->btpo_next
!= P_NONE
&& opaque
->btpo_next
< blkno
)
172 indexStat
.fragments
++;
174 else if (P_ISDELETED(opaque
))
175 indexStat
.deleted_pages
++;
176 else if (P_IGNORE(opaque
))
177 indexStat
.empty_pages
++;
178 else if (P_ISROOT(opaque
))
179 indexStat
.root_pages
++;
181 indexStat
.internal_pages
++;
183 /* Unlock and release buffer */
184 LockBuffer(buffer
, BUFFER_LOCK_UNLOCK
);
185 ReleaseBuffer(buffer
);
188 relation_close(rel
, AccessShareLock
);
190 /*----------------------------
191 * Build a result tuple
192 *----------------------------
200 /* Build a tuple descriptor for our result type */
201 if (get_call_result_type(fcinfo
, NULL
, &tupleDesc
) != TYPEFUNC_COMPOSITE
)
202 elog(ERROR
, "return type must be a row type");
205 values
[j
] = palloc(32);
206 snprintf(values
[j
++], 32, "%d", indexStat
.version
);
207 values
[j
] = palloc(32);
208 snprintf(values
[j
++], 32, "%d", indexStat
.level
);
209 values
[j
] = palloc(32);
210 snprintf(values
[j
++], 32, INT64_FORMAT
,
211 (indexStat
.root_pages
+
212 indexStat
.leaf_pages
+
213 indexStat
.internal_pages
+
214 indexStat
.deleted_pages
+
215 indexStat
.empty_pages
) * BLCKSZ
);
216 values
[j
] = palloc(32);
217 snprintf(values
[j
++], 32, "%u", indexStat
.root_blkno
);
218 values
[j
] = palloc(32);
219 snprintf(values
[j
++], 32, INT64_FORMAT
, indexStat
.internal_pages
);
220 values
[j
] = palloc(32);
221 snprintf(values
[j
++], 32, INT64_FORMAT
, indexStat
.leaf_pages
);
222 values
[j
] = palloc(32);
223 snprintf(values
[j
++], 32, INT64_FORMAT
, indexStat
.empty_pages
);
224 values
[j
] = palloc(32);
225 snprintf(values
[j
++], 32, INT64_FORMAT
, indexStat
.deleted_pages
);
226 values
[j
] = palloc(32);
227 snprintf(values
[j
++], 32, "%.2f", 100.0 - (double) indexStat
.free_space
/ (double) indexStat
.max_avail
* 100.0);
228 values
[j
] = palloc(32);
229 snprintf(values
[j
++], 32, "%.2f", (double) indexStat
.fragments
/ (double) indexStat
.leaf_pages
* 100.0);
231 tuple
= BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc
),
234 result
= HeapTupleGetDatum(tuple
);
237 PG_RETURN_DATUM(result
);
240 /* --------------------------------------------------------
243 * Get the number of pages of the table/index.
245 * Usage: SELECT pg_relpages('t1');
246 * SELECT pg_relpages('t1_pkey');
247 * --------------------------------------------------------
250 pg_relpages(PG_FUNCTION_ARGS
)
252 text
*relname
= PG_GETARG_TEXT_P(0);
259 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
260 (errmsg("must be superuser to use pgstattuple functions"))));
262 relrv
= makeRangeVarFromNameList(textToQualifiedNameList(relname
));
263 rel
= relation_openrv(relrv
, AccessShareLock
);
265 relpages
= RelationGetNumberOfBlocks(rel
);
267 relation_close(rel
, AccessShareLock
);
269 PG_RETURN_INT64(relpages
);