Fix pg_dump bug in the database-level collation patch. "datcollate" and
[PostgreSQL.git] / contrib / pageinspect / btreefuncs.c
blob794704af5941ad7fdc23aa5a80ad21d4b56cb76d
1 /*
2 * $PostgreSQL:$
5 * btreefuncs.c
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.
28 #include "postgres.h"
30 #include "access/heapam.h"
31 #include "access/nbtree.h"
32 #include "catalog/namespace.h"
33 #include "catalog/pg_type.h"
34 #include "funcapi.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
67 uint32 blkno;
68 uint32 live_items;
69 uint32 dead_items;
70 uint32 page_size;
71 uint32 max_avail;
72 uint32 free_size;
73 uint32 avg_item_size;
74 char type;
76 /* opaque data */
77 BlockNumber btpo_prev;
78 BlockNumber btpo_next;
79 union
81 uint32 level;
82 TransactionId xact;
83 } btpo;
84 uint16 btpo_flags;
85 BTCycleId btpo_cycleid;
86 } BTPageStat;
89 /* -------------------------------------------------
90 * GetBTPageStatistics()
92 * Collect statistics of single b-tree page
93 * -------------------------------------------------
95 static void
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);
102 int item_size = 0;
103 int off;
105 stat->blkno = blkno;
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))
116 stat->type = 'd';
117 stat->btpo.xact = opaque->btpo.xact;
118 return;
120 else if (P_IGNORE(opaque))
121 stat->type = 'e';
122 else if (P_ISLEAF(opaque))
123 stat->type = 'l';
124 else if (P_ISROOT(opaque))
125 stat->type = 'r';
126 else
127 stat->type = 'i';
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++)
139 IndexTuple itup;
141 ItemId id = PageGetItemId(page, off);
143 itup = (IndexTuple) PageGetItem(page, id);
145 item_size += IndexTupleSize(itup);
147 if (!ItemIdIsDead(id))
148 stat->live_items++;
149 else
150 stat->dead_items++;
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);
156 else
157 stat->avg_item_size = 0;
160 /* -----------------------------------------------
161 * bt_page()
163 * Usage: SELECT * FROM bt_page('t1_pkey', 1);
164 * -----------------------------------------------
166 Datum
167 bt_page_stats(PG_FUNCTION_ARGS)
169 text *relname = PG_GETARG_TEXT_P(0);
170 uint32 blkno = PG_GETARG_UINT32(1);
171 Buffer buffer;
172 Relation rel;
173 RangeVar *relrv;
174 Datum result;
175 HeapTuple tuple;
176 TupleDesc tupleDesc;
177 int j;
178 char *values[11];
179 BTPageStat stat;
181 if (!superuser())
182 ereport(ERROR,
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));
193 if (blkno == 0)
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");
210 j = 0;
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);
232 else
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),
238 values);
240 result = HeapTupleGetDatum(tuple);
242 ReleaseBuffer(buffer);
244 relation_close(rel, AccessShareLock);
246 PG_RETURN_DATUM(result);
249 /*-------------------------------------------------------
250 * bt_page_items()
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
261 struct user_args
263 Page page;
264 OffsetNumber offset;
267 Datum
268 bt_page_items(PG_FUNCTION_ARGS)
270 text *relname = PG_GETARG_TEXT_P(0);
271 uint32 blkno = PG_GETARG_UINT32(1);
272 Datum result;
273 char *values[6];
274 HeapTuple tuple;
275 FuncCallContext *fctx;
276 MemoryContext mctx;
277 struct user_args *uargs;
279 if (!superuser())
280 ereport(ERROR,
281 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
282 (errmsg("must be superuser to use pageinspect functions"))));
284 if (SRF_IS_FIRSTCALL())
286 RangeVar *relrv;
287 Relation rel;
288 Buffer buffer;
289 BTPageOpaque opaque;
290 TupleDesc tupleDesc;
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));
301 if (blkno == 0)
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)
348 ItemId id;
349 IndexTuple itup;
350 int j;
351 int off;
352 int dlen;
353 char *dump;
354 char *ptr;
356 id = PageGetItemId(uargs->page, uargs->offset);
358 if (!ItemIdIsValid(id))
359 elog(ERROR, "invalid ItemId");
361 itup = (IndexTuple) PageGetItem(uargs->page, id);
363 j = 0;
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);
380 values[j] = dump;
381 for (off = 0; off < dlen; off++)
383 if (off > 0)
384 *dump++ = ' ';
385 sprintf(dump, "%02x", *(ptr + off) & 0xff);
386 dump += 2;
389 tuple = BuildTupleFromCStrings(fctx->attinmeta, values);
390 result = HeapTupleGetDatum(tuple);
392 uargs->offset = uargs->offset + 1;
394 SRF_RETURN_NEXT(fctx, result);
396 else
398 pfree(uargs->page);
399 pfree(uargs);
400 SRF_RETURN_DONE(fctx);
405 /* ------------------------------------------------
406 * bt_metap()
408 * Get a btree's meta-page information
410 * Usage: SELECT * FROM bt_metap('t1_pkey')
411 * ------------------------------------------------
413 Datum
414 bt_metap(PG_FUNCTION_ARGS)
416 text *relname = PG_GETARG_TEXT_P(0);
417 Datum result;
418 Relation rel;
419 RangeVar *relrv;
420 BTMetaPageData *metad;
421 TupleDesc tupleDesc;
422 int j;
423 char *values[6];
424 Buffer buffer;
425 Page page;
426 HeapTuple tuple;
428 if (!superuser())
429 ereport(ERROR,
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");
448 j = 0;
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),
463 values);
465 result = HeapTupleGetDatum(tuple);
467 ReleaseBuffer(buffer);
469 relation_close(rel, AccessShareLock);
471 PG_RETURN_DATUM(result);