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"
33 #include "catalog/pg_type.h"
35 #include "miscadmin.h"
36 #include "storage/bufmgr.h"
37 #include "utils/builtins.h"
40 extern Datum
bt_metap(PG_FUNCTION_ARGS
);
41 extern Datum
bt_page_items(PG_FUNCTION_ARGS
);
42 extern Datum
bt_page_stats(PG_FUNCTION_ARGS
);
44 PG_FUNCTION_INFO_V1(bt_metap
);
45 PG_FUNCTION_INFO_V1(bt_page_items
);
46 PG_FUNCTION_INFO_V1(bt_page_stats
);
48 #define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
49 #define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)
51 #define CHECK_PAGE_OFFSET_RANGE(pg, offnum) { \
52 if ( !(FirstOffsetNumber <= (offnum) && \
53 (offnum) <= PageGetMaxOffsetNumber(pg)) ) \
54 elog(ERROR, "page offset number out of range"); }
56 /* note: BlockNumber is unsigned, hence can't be negative */
57 #define CHECK_RELATION_BLOCK_RANGE(rel, blkno) { \
58 if ( RelationGetNumberOfBlocks(rel) <= (BlockNumber) (blkno) ) \
59 elog(ERROR, "block number out of range"); }
61 /* ------------------------------------------------
62 * structure for single btree page statistics
63 * ------------------------------------------------
65 typedef struct BTPageStat
77 BlockNumber btpo_prev
;
78 BlockNumber btpo_next
;
85 BTCycleId btpo_cycleid
;
89 /* -------------------------------------------------
90 * GetBTPageStatistics()
92 * Collect statistics of single b-tree page
93 * -------------------------------------------------
96 GetBTPageStatistics(BlockNumber blkno
, Buffer buffer
, BTPageStat
* stat
)
98 Page page
= BufferGetPage(buffer
);
99 PageHeader phdr
= (PageHeader
) page
;
100 OffsetNumber maxoff
= PageGetMaxOffsetNumber(page
);
101 BTPageOpaque opaque
= (BTPageOpaque
) PageGetSpecialPointer(page
);
107 stat
->max_avail
= BLCKSZ
- (BLCKSZ
- phdr
->pd_special
+ SizeOfPageHeaderData
);
109 stat
->dead_items
= stat
->live_items
= 0;
111 stat
->page_size
= PageGetPageSize(page
);
113 /* page type (flags) */
114 if (P_ISDELETED(opaque
))
117 stat
->btpo
.xact
= opaque
->btpo
.xact
;
120 else if (P_IGNORE(opaque
))
122 else if (P_ISLEAF(opaque
))
124 else if (P_ISROOT(opaque
))
129 /* btpage opaque data */
130 stat
->btpo_prev
= opaque
->btpo_prev
;
131 stat
->btpo_next
= opaque
->btpo_next
;
132 stat
->btpo
.level
= opaque
->btpo
.level
;
133 stat
->btpo_flags
= opaque
->btpo_flags
;
134 stat
->btpo_cycleid
= opaque
->btpo_cycleid
;
136 /* count live and dead tuples, and free space */
137 for (off
= FirstOffsetNumber
; off
<= maxoff
; off
++)
141 ItemId id
= PageGetItemId(page
, off
);
143 itup
= (IndexTuple
) PageGetItem(page
, id
);
145 item_size
+= IndexTupleSize(itup
);
147 if (!ItemIdIsDead(id
))
152 stat
->free_size
= PageGetFreeSpace(page
);
154 if ((stat
->live_items
+ stat
->dead_items
) > 0)
155 stat
->avg_item_size
= item_size
/ (stat
->live_items
+ stat
->dead_items
);
157 stat
->avg_item_size
= 0;
160 /* -----------------------------------------------
163 * Usage: SELECT * FROM bt_page('t1_pkey', 1);
164 * -----------------------------------------------
167 bt_page_stats(PG_FUNCTION_ARGS
)
169 text
*relname
= PG_GETARG_TEXT_P(0);
170 uint32 blkno
= PG_GETARG_UINT32(1);
183 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
184 (errmsg("must be superuser to use pageinspect functions"))));
186 relrv
= makeRangeVarFromNameList(textToQualifiedNameList(relname
));
187 rel
= relation_openrv(relrv
, AccessShareLock
);
189 if (!IS_INDEX(rel
) || !IS_BTREE(rel
))
190 elog(ERROR
, "relation \"%s\" is not a btree index",
191 RelationGetRelationName(rel
));
194 elog(ERROR
, "block 0 is a meta page");
196 CHECK_RELATION_BLOCK_RANGE(rel
, blkno
);
198 buffer
= ReadBuffer(rel
, blkno
);
200 /* keep compiler quiet */
201 stat
.btpo_prev
= stat
.btpo_next
= InvalidBlockNumber
;
202 stat
.btpo_flags
= stat
.free_size
= stat
.avg_item_size
= 0;
204 GetBTPageStatistics(blkno
, buffer
, &stat
);
206 /* Build a tuple descriptor for our result type */
207 if (get_call_result_type(fcinfo
, NULL
, &tupleDesc
) != TYPEFUNC_COMPOSITE
)
208 elog(ERROR
, "return type must be a row type");
211 values
[j
] = palloc(32);
212 snprintf(values
[j
++], 32, "%d", stat
.blkno
);
213 values
[j
] = palloc(32);
214 snprintf(values
[j
++], 32, "%c", stat
.type
);
215 values
[j
] = palloc(32);
216 snprintf(values
[j
++], 32, "%d", stat
.live_items
);
217 values
[j
] = palloc(32);
218 snprintf(values
[j
++], 32, "%d", stat
.dead_items
);
219 values
[j
] = palloc(32);
220 snprintf(values
[j
++], 32, "%d", stat
.avg_item_size
);
221 values
[j
] = palloc(32);
222 snprintf(values
[j
++], 32, "%d", stat
.page_size
);
223 values
[j
] = palloc(32);
224 snprintf(values
[j
++], 32, "%d", stat
.free_size
);
225 values
[j
] = palloc(32);
226 snprintf(values
[j
++], 32, "%d", stat
.btpo_prev
);
227 values
[j
] = palloc(32);
228 snprintf(values
[j
++], 32, "%d", stat
.btpo_next
);
229 values
[j
] = palloc(32);
230 if (stat
.type
== 'd')
231 snprintf(values
[j
++], 32, "%d", stat
.btpo
.xact
);
233 snprintf(values
[j
++], 32, "%d", stat
.btpo
.level
);
234 values
[j
] = palloc(32);
235 snprintf(values
[j
++], 32, "%d", stat
.btpo_flags
);
237 tuple
= BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc
),
240 result
= HeapTupleGetDatum(tuple
);
242 ReleaseBuffer(buffer
);
244 relation_close(rel
, AccessShareLock
);
246 PG_RETURN_DATUM(result
);
249 /*-------------------------------------------------------
252 * Get IndexTupleData set in a btree page
254 * Usage: SELECT * FROM bt_page_items('t1_pkey', 1);
255 *-------------------------------------------------------
259 * cross-call data structure for SRF
268 bt_page_items(PG_FUNCTION_ARGS
)
270 text
*relname
= PG_GETARG_TEXT_P(0);
271 uint32 blkno
= PG_GETARG_UINT32(1);
275 FuncCallContext
*fctx
;
277 struct user_args
*uargs
;
281 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
282 (errmsg("must be superuser to use pageinspect functions"))));
284 if (SRF_IS_FIRSTCALL())
292 fctx
= SRF_FIRSTCALL_INIT();
294 relrv
= makeRangeVarFromNameList(textToQualifiedNameList(relname
));
295 rel
= relation_openrv(relrv
, AccessShareLock
);
297 if (!IS_INDEX(rel
) || !IS_BTREE(rel
))
298 elog(ERROR
, "relation \"%s\" is not a btree index",
299 RelationGetRelationName(rel
));
302 elog(ERROR
, "block 0 is a meta page");
304 CHECK_RELATION_BLOCK_RANGE(rel
, blkno
);
306 buffer
= ReadBuffer(rel
, blkno
);
309 * We copy the page into local storage to avoid holding pin on the
310 * buffer longer than we must, and possibly failing to release it at
311 * all if the calling query doesn't fetch all rows.
313 mctx
= MemoryContextSwitchTo(fctx
->multi_call_memory_ctx
);
315 uargs
= palloc(sizeof(struct user_args
));
317 uargs
->page
= palloc(BLCKSZ
);
318 memcpy(uargs
->page
, BufferGetPage(buffer
), BLCKSZ
);
320 ReleaseBuffer(buffer
);
321 relation_close(rel
, AccessShareLock
);
323 uargs
->offset
= FirstOffsetNumber
;
325 opaque
= (BTPageOpaque
) PageGetSpecialPointer(uargs
->page
);
327 if (P_ISDELETED(opaque
))
328 elog(NOTICE
, "page is deleted");
330 fctx
->max_calls
= PageGetMaxOffsetNumber(uargs
->page
);
332 /* Build a tuple descriptor for our result type */
333 if (get_call_result_type(fcinfo
, NULL
, &tupleDesc
) != TYPEFUNC_COMPOSITE
)
334 elog(ERROR
, "return type must be a row type");
336 fctx
->attinmeta
= TupleDescGetAttInMetadata(tupleDesc
);
338 fctx
->user_fctx
= uargs
;
340 MemoryContextSwitchTo(mctx
);
343 fctx
= SRF_PERCALL_SETUP();
344 uargs
= fctx
->user_fctx
;
346 if (fctx
->call_cntr
< fctx
->max_calls
)
356 id
= PageGetItemId(uargs
->page
, uargs
->offset
);
358 if (!ItemIdIsValid(id
))
359 elog(ERROR
, "invalid ItemId");
361 itup
= (IndexTuple
) PageGetItem(uargs
->page
, id
);
364 values
[j
] = palloc(32);
365 snprintf(values
[j
++], 32, "%d", uargs
->offset
);
366 values
[j
] = palloc(32);
367 snprintf(values
[j
++], 32, "(%u,%u)",
368 BlockIdGetBlockNumber(&(itup
->t_tid
.ip_blkid
)),
369 itup
->t_tid
.ip_posid
);
370 values
[j
] = palloc(32);
371 snprintf(values
[j
++], 32, "%d", (int) IndexTupleSize(itup
));
372 values
[j
] = palloc(32);
373 snprintf(values
[j
++], 32, "%c", IndexTupleHasNulls(itup
) ? 't' : 'f');
374 values
[j
] = palloc(32);
375 snprintf(values
[j
++], 32, "%c", IndexTupleHasVarwidths(itup
) ? 't' : 'f');
377 ptr
= (char *) itup
+ IndexInfoFindDataOffset(itup
->t_info
);
378 dlen
= IndexTupleSize(itup
) - IndexInfoFindDataOffset(itup
->t_info
);
379 dump
= palloc0(dlen
* 3 + 1);
381 for (off
= 0; off
< dlen
; off
++)
385 sprintf(dump
, "%02x", *(ptr
+ off
) & 0xff);
389 tuple
= BuildTupleFromCStrings(fctx
->attinmeta
, values
);
390 result
= HeapTupleGetDatum(tuple
);
392 uargs
->offset
= uargs
->offset
+ 1;
394 SRF_RETURN_NEXT(fctx
, result
);
400 SRF_RETURN_DONE(fctx
);
405 /* ------------------------------------------------
408 * Get a btree's meta-page information
410 * Usage: SELECT * FROM bt_metap('t1_pkey')
411 * ------------------------------------------------
414 bt_metap(PG_FUNCTION_ARGS
)
416 text
*relname
= PG_GETARG_TEXT_P(0);
420 BTMetaPageData
*metad
;
430 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
431 (errmsg("must be superuser to use pageinspect functions"))));
433 relrv
= makeRangeVarFromNameList(textToQualifiedNameList(relname
));
434 rel
= relation_openrv(relrv
, AccessShareLock
);
436 if (!IS_INDEX(rel
) || !IS_BTREE(rel
))
437 elog(ERROR
, "relation \"%s\" is not a btree index",
438 RelationGetRelationName(rel
));
440 buffer
= ReadBuffer(rel
, 0);
441 page
= BufferGetPage(buffer
);
442 metad
= BTPageGetMeta(page
);
444 /* Build a tuple descriptor for our result type */
445 if (get_call_result_type(fcinfo
, NULL
, &tupleDesc
) != TYPEFUNC_COMPOSITE
)
446 elog(ERROR
, "return type must be a row type");
449 values
[j
] = palloc(32);
450 snprintf(values
[j
++], 32, "%d", metad
->btm_magic
);
451 values
[j
] = palloc(32);
452 snprintf(values
[j
++], 32, "%d", metad
->btm_version
);
453 values
[j
] = palloc(32);
454 snprintf(values
[j
++], 32, "%d", metad
->btm_root
);
455 values
[j
] = palloc(32);
456 snprintf(values
[j
++], 32, "%d", metad
->btm_level
);
457 values
[j
] = palloc(32);
458 snprintf(values
[j
++], 32, "%d", metad
->btm_fastroot
);
459 values
[j
] = palloc(32);
460 snprintf(values
[j
++], 32, "%d", metad
->btm_fastlevel
);
462 tuple
= BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc
),
465 result
= HeapTupleGetDatum(tuple
);
467 ReleaseBuffer(buffer
);
469 relation_close(rel
, AccessShareLock
);
471 PG_RETURN_DATUM(result
);