PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
btreefuncs.c File Reference
#include "postgres.h"
#include "pageinspect.h"
#include "access/nbtree.h"
#include "catalog/namespace.h"
#include "catalog/pg_am.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/varlena.h"
Include dependency graph for btreefuncs.c:

Go to the source code of this file.

Data Structures

struct  BTPageStat
 
struct  user_args
 

Macros

#define IS_INDEX(r)   ((r)->rd_rel->relkind == RELKIND_INDEX)
 
#define IS_BTREE(r)   ((r)->rd_rel->relam == BTREE_AM_OID)
 
#define CHECK_RELATION_BLOCK_RANGE(rel, blkno)
 

Typedefs

typedef struct BTPageStat BTPageStat
 

Functions

 PG_FUNCTION_INFO_V1 (bt_metap)
 
 PG_FUNCTION_INFO_V1 (bt_page_items)
 
 PG_FUNCTION_INFO_V1 (bt_page_items_bytea)
 
 PG_FUNCTION_INFO_V1 (bt_page_stats)
 
static void GetBTPageStatistics (BlockNumber blkno, Buffer buffer, BTPageStat *stat)
 
Datum bt_page_stats (PG_FUNCTION_ARGS)
 
static Datum bt_page_print_tuples (FuncCallContext *fctx, Page page, OffsetNumber offset)
 
Datum bt_page_items (PG_FUNCTION_ARGS)
 
Datum bt_page_items_bytea (PG_FUNCTION_ARGS)
 
Datum bt_metap (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

#define CHECK_RELATION_BLOCK_RANGE (   rel,
  blkno 
)
Value:
{ \
if ( RelationGetNumberOfBlocks(rel) <= (BlockNumber) (blkno) ) \
elog(ERROR, "block number out of range"); }
uint32 BlockNumber
Definition: block.h:31
#define ERROR
Definition: elog.h:43
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define elog
Definition: elog.h:219

Definition at line 51 of file btreefuncs.c.

Referenced by bt_page_items(), and bt_page_stats().

#define IS_BTREE (   r)    ((r)->rd_rel->relam == BTREE_AM_OID)

Definition at line 48 of file btreefuncs.c.

Referenced by bt_metap(), bt_page_items(), and bt_page_stats().

#define IS_INDEX (   r)    ((r)->rd_rel->relkind == RELKIND_INDEX)

Definition at line 47 of file btreefuncs.c.

Referenced by bt_metap(), bt_page_items(), and bt_page_stats().

Typedef Documentation

Function Documentation

Datum bt_metap ( PG_FUNCTION_ARGS  )

Definition at line 505 of file btreefuncs.c.

References AccessShareLock, BTMetaPageData::btm_fastlevel, BTMetaPageData::btm_fastroot, BTMetaPageData::btm_level, BTMetaPageData::btm_magic, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTPageGetMeta, buffer, BUFFER_LOCK_SHARE, BufferGetPage, BuildTupleFromCStrings(), elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), HeapTupleGetDatum, IS_BTREE, IS_INDEX, LockBuffer(), makeRangeVarFromNameList(), user_args::page, PG_GETARG_TEXT_PP, PG_RETURN_DATUM, psprintf(), ReadBuffer(), relation_close(), RELATION_IS_OTHER_TEMP, relation_openrv(), RelationGetRelationName, superuser(), textToQualifiedNameList(), TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, UnlockReleaseBuffer(), and values.

506 {
507  text *relname = PG_GETARG_TEXT_PP(0);
508  Datum result;
509  Relation rel;
510  RangeVar *relrv;
511  BTMetaPageData *metad;
513  int j;
514  char *values[6];
515  Buffer buffer;
516  Page page;
517  HeapTuple tuple;
518 
519  if (!superuser())
520  ereport(ERROR,
521  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
522  (errmsg("must be superuser to use pageinspect functions"))));
523 
525  rel = relation_openrv(relrv, AccessShareLock);
526 
527  if (!IS_INDEX(rel) || !IS_BTREE(rel))
528  elog(ERROR, "relation \"%s\" is not a btree index",
530 
531  /*
532  * Reject attempts to read non-local temporary relations; we would be
533  * likely to get wrong data since we have no visibility into the owning
534  * session's local buffers.
535  */
536  if (RELATION_IS_OTHER_TEMP(rel))
537  ereport(ERROR,
538  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
539  errmsg("cannot access temporary tables of other sessions")));
540 
541  buffer = ReadBuffer(rel, 0);
542  LockBuffer(buffer, BUFFER_LOCK_SHARE);
543 
544  page = BufferGetPage(buffer);
545  metad = BTPageGetMeta(page);
546 
547  /* Build a tuple descriptor for our result type */
548  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
549  elog(ERROR, "return type must be a row type");
550 
551  j = 0;
552  values[j++] = psprintf("%d", metad->btm_magic);
553  values[j++] = psprintf("%d", metad->btm_version);
554  values[j++] = psprintf("%d", metad->btm_root);
555  values[j++] = psprintf("%d", metad->btm_level);
556  values[j++] = psprintf("%d", metad->btm_fastroot);
557  values[j++] = psprintf("%d", metad->btm_fastlevel);
558 
560  values);
561 
562  result = HeapTupleGetDatum(tuple);
563 
564  UnlockReleaseBuffer(buffer);
566 
567  PG_RETURN_DATUM(result);
568 }
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1198
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
uint32 btm_version
Definition: nbtree.h:99
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
uint32 btm_magic
Definition: nbtree.h:98
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
#define IS_BTREE(r)
Definition: btreefuncs.c:48
bool superuser(void)
Definition: superuser.c:47
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1266
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3023
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1118
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
BlockNumber btm_fastroot
Definition: nbtree.h:102
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
#define BTPageGetMeta(p)
Definition: nbtree.h:106
#define RelationGetRelationName(relation)
Definition: rel.h:436
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3206
uint32 btm_fastlevel
Definition: nbtree.h:103
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1069
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
BlockNumber btm_root
Definition: nbtree.h:100
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:533
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int errmsg(const char *fmt,...)
Definition: elog.c:797
uint32 btm_level
Definition: nbtree.h:101
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
Definition: c.h:433
#define elog
Definition: elog.h:219
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
#define IS_INDEX(r)
Definition: btreefuncs.c:47
Datum bt_page_items ( PG_FUNCTION_ARGS  )

Definition at line 310 of file btreefuncs.c.

References AccessShareLock, FuncCallContext::attinmeta, bt_page_print_tuples(), buffer, BUFFER_LOCK_SHARE, BufferGetPage, FuncCallContext::call_cntr, CHECK_RELATION_BLOCK_RANGE, elog, ereport, errcode(), errmsg(), ERROR, FirstOffsetNumber, get_call_result_type(), IS_BTREE, IS_INDEX, LockBuffer(), makeRangeVarFromNameList(), FuncCallContext::max_calls, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, NOTICE, user_args::offset, P_ISDELETED, user_args::page, PageGetMaxOffsetNumber, PageGetSpecialPointer, palloc(), pfree(), PG_GETARG_TEXT_PP, PG_GETARG_UINT32, ReadBuffer(), relation_close(), RELATION_IS_OTHER_TEMP, relation_openrv(), RelationGetRelationName, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, superuser(), textToQualifiedNameList(), TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, UnlockReleaseBuffer(), and FuncCallContext::user_fctx.

311 {
312  text *relname = PG_GETARG_TEXT_PP(0);
313  uint32 blkno = PG_GETARG_UINT32(1);
314  Datum result;
315  FuncCallContext *fctx;
316  MemoryContext mctx;
317  struct user_args *uargs;
318 
319  if (!superuser())
320  ereport(ERROR,
321  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
322  (errmsg("must be superuser to use pageinspect functions"))));
323 
324  if (SRF_IS_FIRSTCALL())
325  {
326  RangeVar *relrv;
327  Relation rel;
328  Buffer buffer;
329  BTPageOpaque opaque;
331 
332  fctx = SRF_FIRSTCALL_INIT();
333 
335  rel = relation_openrv(relrv, AccessShareLock);
336 
337  if (!IS_INDEX(rel) || !IS_BTREE(rel))
338  elog(ERROR, "relation \"%s\" is not a btree index",
340 
341  /*
342  * Reject attempts to read non-local temporary relations; we would be
343  * likely to get wrong data since we have no visibility into the
344  * owning session's local buffers.
345  */
346  if (RELATION_IS_OTHER_TEMP(rel))
347  ereport(ERROR,
348  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
349  errmsg("cannot access temporary tables of other sessions")));
350 
351  if (blkno == 0)
352  elog(ERROR, "block 0 is a meta page");
353 
354  CHECK_RELATION_BLOCK_RANGE(rel, blkno);
355 
356  buffer = ReadBuffer(rel, blkno);
357  LockBuffer(buffer, BUFFER_LOCK_SHARE);
358 
359  /*
360  * We copy the page into local storage to avoid holding pin on the
361  * buffer longer than we must, and possibly failing to release it at
362  * all if the calling query doesn't fetch all rows.
363  */
365 
366  uargs = palloc(sizeof(struct user_args));
367 
368  uargs->page = palloc(BLCKSZ);
369  memcpy(uargs->page, BufferGetPage(buffer), BLCKSZ);
370 
371  UnlockReleaseBuffer(buffer);
373 
374  uargs->offset = FirstOffsetNumber;
375 
376  opaque = (BTPageOpaque) PageGetSpecialPointer(uargs->page);
377 
378  if (P_ISDELETED(opaque))
379  elog(NOTICE, "page is deleted");
380 
381  fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
382 
383  /* Build a tuple descriptor for our result type */
384  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
385  elog(ERROR, "return type must be a row type");
386 
387  fctx->attinmeta = TupleDescGetAttInMetadata(tupleDesc);
388 
389  fctx->user_fctx = uargs;
390 
391  MemoryContextSwitchTo(mctx);
392  }
393 
394  fctx = SRF_PERCALL_SETUP();
395  uargs = fctx->user_fctx;
396 
397  if (fctx->call_cntr < fctx->max_calls)
398  {
399  result = bt_page_print_tuples(fctx, uargs->page, uargs->offset);
400  uargs->offset++;
401  SRF_RETURN_NEXT(fctx, result);
402  }
403  else
404  {
405  pfree(uargs->page);
406  pfree(uargs);
407  SRF_RETURN_DONE(fctx);
408  }
409 }
uint64 call_cntr
Definition: funcapi.h:65
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:235
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1198
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define CHECK_RELATION_BLOCK_RANGE(rel, blkno)
Definition: btreefuncs.c:51
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
#define IS_BTREE(r)
Definition: btreefuncs.c:48
bool superuser(void)
Definition: superuser.c:47
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1266
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3023
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:67
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
void pfree(void *pointer)
Definition: mcxt.c:949
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
OffsetNumber offset
Definition: btreefuncs.c:246
#define FirstOffsetNumber
Definition: off.h:27
AttInMetadata * attinmeta
Definition: funcapi.h:99
#define RelationGetRelationName(relation)
Definition: rel.h:436
unsigned int uint32
Definition: c.h:258
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
#define P_ISDELETED(opaque)
Definition: nbtree.h:178
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3206
uintptr_t Datum
Definition: postgres.h:372
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1069
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
Page page
Definition: btreefuncs.c:245
#define NOTICE
Definition: elog.h:37
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:533
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
static Datum bt_page_print_tuples(FuncCallContext *fctx, Page page, OffsetNumber offset)
Definition: btreefuncs.c:256
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
Definition: c.h:433
#define elog
Definition: elog.h:219
int Buffer
Definition: buf.h:23
uint64 max_calls
Definition: funcapi.h:74
#define IS_INDEX(r)
Definition: btreefuncs.c:47
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
Datum bt_page_items_bytea ( PG_FUNCTION_ARGS  )

Definition at line 421 of file btreefuncs.c.

References FuncCallContext::attinmeta, bt_page_print_tuples(), FuncCallContext::call_cntr, elog, ereport, errcode(), errmsg(), ERROR, FirstOffsetNumber, get_call_result_type(), FuncCallContext::max_calls, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, NOTICE, user_args::offset, P_ISDELETED, P_ISMETA, user_args::page, PageGetMaxOffsetNumber, PageGetSpecialPointer, palloc(), pfree(), PG_GETARG_BYTEA_P, SizeOfPageHeaderData, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, superuser(), TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, FuncCallContext::user_fctx, VARDATA, VARHDRSZ, and VARSIZE.

422 {
423  bytea *raw_page = PG_GETARG_BYTEA_P(0);
424  Datum result;
425  FuncCallContext *fctx;
426  struct user_args *uargs;
427  int raw_page_size;
428 
429  if (!superuser())
430  ereport(ERROR,
431  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
432  (errmsg("must be superuser to use pageinspect functions"))));
433 
434  if (SRF_IS_FIRSTCALL())
435  {
436  BTPageOpaque opaque;
437  MemoryContext mctx;
439 
440  raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
441 
442  if (raw_page_size < SizeOfPageHeaderData)
443  ereport(ERROR,
444  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
445  errmsg("input page too small (%d bytes)", raw_page_size)));
446 
447  fctx = SRF_FIRSTCALL_INIT();
449 
450  uargs = palloc(sizeof(struct user_args));
451 
452  uargs->page = VARDATA(raw_page);
453 
454  uargs->offset = FirstOffsetNumber;
455 
456  opaque = (BTPageOpaque) PageGetSpecialPointer(uargs->page);
457 
458  if (P_ISMETA(opaque))
459  ereport(ERROR,
460  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
461  errmsg("block is a meta page")));
462 
463  if (P_ISDELETED(opaque))
464  elog(NOTICE, "page is deleted");
465 
466  fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
467 
468  /* Build a tuple descriptor for our result type */
469  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
470  elog(ERROR, "return type must be a row type");
471 
472  fctx->attinmeta = TupleDescGetAttInMetadata(tupleDesc);
473 
474  fctx->user_fctx = uargs;
475 
476  MemoryContextSwitchTo(mctx);
477  }
478 
479  fctx = SRF_PERCALL_SETUP();
480  uargs = fctx->user_fctx;
481 
482  if (fctx->call_cntr < fctx->max_calls)
483  {
484  result = bt_page_print_tuples(fctx, uargs->page, uargs->offset);
485  uargs->offset++;
486  SRF_RETURN_NEXT(fctx, result);
487  }
488  else
489  {
490  pfree(uargs);
491  SRF_RETURN_DONE(fctx);
492  }
493 }
uint64 call_cntr
Definition: funcapi.h:65
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define VARHDRSZ
Definition: c.h:439
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define SizeOfPageHeaderData
Definition: bufpage.h:212
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:67
#define P_ISMETA(opaque)
Definition: nbtree.h:179
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:299
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
OffsetNumber offset
Definition: btreefuncs.c:246
#define FirstOffsetNumber
Definition: off.h:27
AttInMetadata * attinmeta
Definition: funcapi.h:99
#define ereport(elevel, rest)
Definition: elog.h:122
#define P_ISDELETED(opaque)
Definition: nbtree.h:178
uintptr_t Datum
Definition: postgres.h:372
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1069
Page page
Definition: btreefuncs.c:245
#define NOTICE
Definition: elog.h:37
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
static Datum bt_page_print_tuples(FuncCallContext *fctx, Page page, OffsetNumber offset)
Definition: btreefuncs.c:256
Definition: c.h:433
#define elog
Definition: elog.h:219
uint64 max_calls
Definition: funcapi.h:74
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
static Datum bt_page_print_tuples ( FuncCallContext fctx,
Page  page,
OffsetNumber  offset 
)
static

Definition at line 256 of file btreefuncs.c.

References FuncCallContext::attinmeta, BuildTupleFromCStrings(), elog, ERROR, HeapTupleGetDatum, IndexInfoFindDataOffset, IndexTupleHasNulls, IndexTupleHasVarwidths, IndexTupleSize, ItemIdIsValid, ItemPointerGetBlockNumberNoCheck, ItemPointerGetOffsetNumberNoCheck, PageGetItem, PageGetItemId, palloc0(), psprintf(), IndexTupleData::t_info, IndexTupleData::t_tid, and values.

Referenced by bt_page_items(), and bt_page_items_bytea().

257 {
258  char *values[6];
259  HeapTuple tuple;
260  ItemId id;
261  IndexTuple itup;
262  int j;
263  int off;
264  int dlen;
265  char *dump;
266  char *ptr;
267 
268  id = PageGetItemId(page, offset);
269 
270  if (!ItemIdIsValid(id))
271  elog(ERROR, "invalid ItemId");
272 
273  itup = (IndexTuple) PageGetItem(page, id);
274 
275  j = 0;
276  values[j++] = psprintf("%d", offset);
277  values[j++] = psprintf("(%u,%u)",
280  values[j++] = psprintf("%d", (int) IndexTupleSize(itup));
281  values[j++] = psprintf("%c", IndexTupleHasNulls(itup) ? 't' : 'f');
282  values[j++] = psprintf("%c", IndexTupleHasVarwidths(itup) ? 't' : 'f');
283 
284  ptr = (char *) itup + IndexInfoFindDataOffset(itup->t_info);
285  dlen = IndexTupleSize(itup) - IndexInfoFindDataOffset(itup->t_info);
286  dump = palloc0(dlen * 3 + 1);
287  values[j] = dump;
288  for (off = 0; off < dlen; off++)
289  {
290  if (off > 0)
291  *dump++ = ' ';
292  sprintf(dump, "%02x", *(ptr + off) & 0xff);
293  dump += 2;
294  }
295 
296  tuple = BuildTupleFromCStrings(fctx->attinmeta, values);
297 
298  return HeapTupleGetDatum(tuple);
299 }
#define ItemPointerGetOffsetNumberNoCheck(pointer)
Definition: itemptr.h:86
#define IndexInfoFindDataOffset(t_info)
Definition: itup.h:80
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
ItemPointerData t_tid
Definition: itup.h:37
#define IndexTupleHasNulls(itup)
Definition: itup.h:72
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1118
#define ERROR
Definition: elog.h:43
OffsetNumber offset
Definition: btreefuncs.c:246
AttInMetadata * attinmeta
Definition: funcapi.h:99
IndexTupleData * IndexTuple
Definition: itup.h:53
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
void * palloc0(Size size)
Definition: mcxt.c:877
Page page
Definition: btreefuncs.c:245
#define ItemIdIsValid(itemId)
Definition: itemid.h:85
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
#define IndexTupleHasVarwidths(itup)
Definition: itup.h:73
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define ItemPointerGetBlockNumberNoCheck(pointer)
Definition: itemptr.h:67
#define elog
Definition: elog.h:219
unsigned short t_info
Definition: itup.h:49
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
#define IndexTupleSize(itup)
Definition: itup.h:70
Datum bt_page_stats ( PG_FUNCTION_ARGS  )

Definition at line 161 of file btreefuncs.c.

References AccessShareLock, BTPageStat::avg_item_size, BTPageStat::blkno, BTPageStat::btpo, BTPageStat::btpo_flags, BTPageStat::btpo_next, BTPageStat::btpo_prev, buffer, BUFFER_LOCK_SHARE, BuildTupleFromCStrings(), CHECK_RELATION_BLOCK_RANGE, BTPageStat::dead_items, elog, ereport, errcode(), errmsg(), ERROR, BTPageStat::free_size, get_call_result_type(), GetBTPageStatistics(), HeapTupleGetDatum, InvalidBlockNumber, IS_BTREE, IS_INDEX, BTPageStat::level, BTPageStat::live_items, LockBuffer(), makeRangeVarFromNameList(), BTPageStat::page_size, PG_GETARG_TEXT_PP, PG_GETARG_UINT32, PG_RETURN_DATUM, psprintf(), ReadBuffer(), relation_close(), RELATION_IS_OTHER_TEMP, relation_openrv(), RelationGetRelationName, superuser(), textToQualifiedNameList(), TupleDescGetAttInMetadata(), BTPageStat::type, TYPEFUNC_COMPOSITE, UnlockReleaseBuffer(), values, and BTPageStat::xact.

162 {
163  text *relname = PG_GETARG_TEXT_PP(0);
164  uint32 blkno = PG_GETARG_UINT32(1);
165  Buffer buffer;
166  Relation rel;
167  RangeVar *relrv;
168  Datum result;
169  HeapTuple tuple;
171  int j;
172  char *values[11];
173  BTPageStat stat;
174 
175  if (!superuser())
176  ereport(ERROR,
177  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
178  (errmsg("must be superuser to use pageinspect functions"))));
179 
181  rel = relation_openrv(relrv, AccessShareLock);
182 
183  if (!IS_INDEX(rel) || !IS_BTREE(rel))
184  elog(ERROR, "relation \"%s\" is not a btree index",
186 
187  /*
188  * Reject attempts to read non-local temporary relations; we would be
189  * likely to get wrong data since we have no visibility into the owning
190  * session's local buffers.
191  */
192  if (RELATION_IS_OTHER_TEMP(rel))
193  ereport(ERROR,
194  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
195  errmsg("cannot access temporary tables of other sessions")));
196 
197  if (blkno == 0)
198  elog(ERROR, "block 0 is a meta page");
199 
200  CHECK_RELATION_BLOCK_RANGE(rel, blkno);
201 
202  buffer = ReadBuffer(rel, blkno);
203  LockBuffer(buffer, BUFFER_LOCK_SHARE);
204 
205  /* keep compiler quiet */
206  stat.btpo_prev = stat.btpo_next = InvalidBlockNumber;
207  stat.btpo_flags = stat.free_size = stat.avg_item_size = 0;
208 
209  GetBTPageStatistics(blkno, buffer, &stat);
210 
211  UnlockReleaseBuffer(buffer);
213 
214  /* Build a tuple descriptor for our result type */
215  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
216  elog(ERROR, "return type must be a row type");
217 
218  j = 0;
219  values[j++] = psprintf("%d", stat.blkno);
220  values[j++] = psprintf("%c", stat.type);
221  values[j++] = psprintf("%d", stat.live_items);
222  values[j++] = psprintf("%d", stat.dead_items);
223  values[j++] = psprintf("%d", stat.avg_item_size);
224  values[j++] = psprintf("%d", stat.page_size);
225  values[j++] = psprintf("%d", stat.free_size);
226  values[j++] = psprintf("%d", stat.btpo_prev);
227  values[j++] = psprintf("%d", stat.btpo_next);
228  values[j++] = psprintf("%d", (stat.type == 'd') ? stat.btpo.xact : stat.btpo.level);
229  values[j++] = psprintf("%d", stat.btpo_flags);
230 
232  values);
233 
234  result = HeapTupleGetDatum(tuple);
235 
236  PG_RETURN_DATUM(result);
237 }
uint32 avg_item_size
Definition: btreefuncs.c:67
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:235
union BTPageStat::@1 btpo
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1198
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
uint32 dead_items
Definition: btreefuncs.c:63
#define CHECK_RELATION_BLOCK_RANGE(rel, blkno)
Definition: btreefuncs.c:51
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define AccessShareLock
Definition: lockdefs.h:36
uint32 blkno
Definition: btreefuncs.c:61
int errcode(int sqlerrcode)
Definition: elog.c:575
#define IS_BTREE(r)
Definition: btreefuncs.c:48
bool superuser(void)
Definition: superuser.c:47
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1266
char type
Definition: btreefuncs.c:68
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3023
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1118
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
BlockNumber btpo_next
Definition: btreefuncs.c:72
#define RelationGetRelationName(relation)
Definition: rel.h:436
unsigned int uint32
Definition: c.h:258
#define ereport(elevel, rest)
Definition: elog.h:122
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3206
uint32 live_items
Definition: btreefuncs.c:62
TransactionId xact
Definition: btreefuncs.c:76
BlockNumber btpo_prev
Definition: btreefuncs.c:71
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
uint32 free_size
Definition: btreefuncs.c:66
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1069
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:533
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:594
#define InvalidBlockNumber
Definition: block.h:33
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
uint16 btpo_flags
Definition: btreefuncs.c:78
static void GetBTPageStatistics(BlockNumber blkno, Buffer buffer, BTPageStat *stat)
Definition: btreefuncs.c:90
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int errmsg(const char *fmt,...)
Definition: elog.c:797
uint32 page_size
Definition: btreefuncs.c:64
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
Definition: c.h:433
#define elog
Definition: elog.h:219
uint32 level
Definition: btreefuncs.c:75
int Buffer
Definition: buf.h:23
#define IS_INDEX(r)
Definition: btreefuncs.c:47
static void GetBTPageStatistics ( BlockNumber  blkno,
Buffer  buffer,
BTPageStat stat 
)
static

Definition at line 90 of file btreefuncs.c.

References BTPageStat::avg_item_size, BTPageStat::blkno, BTPageOpaqueData::btpo, BTPageStat::btpo, BTPageOpaqueData::btpo_cycleid, BTPageStat::btpo_cycleid, BTPageOpaqueData::btpo_flags, BTPageStat::btpo_flags, BTPageOpaqueData::btpo_next, BTPageStat::btpo_next, BTPageOpaqueData::btpo_prev, BTPageStat::btpo_prev, BufferGetPage, BTPageStat::dead_items, FirstOffsetNumber, BTPageStat::free_size, IndexTupleSize, ItemIdIsDead, BTPageOpaqueData::level, BTPageStat::level, BTPageStat::live_items, BTPageStat::max_avail, P_IGNORE, P_ISDELETED, P_ISLEAF, P_ISROOT, BTPageStat::page_size, PageGetFreeSpace(), PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageGetPageSize, PageGetSpecialPointer, PageHeaderData::pd_special, SizeOfPageHeaderData, BTPageStat::type, BTPageOpaqueData::xact, and BTPageStat::xact.

Referenced by bt_page_stats().

91 {
93  PageHeader phdr = (PageHeader) page;
94  OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
96  int item_size = 0;
97  int off;
98 
99  stat->blkno = blkno;
100 
101  stat->max_avail = BLCKSZ - (BLCKSZ - phdr->pd_special + SizeOfPageHeaderData);
102 
103  stat->dead_items = stat->live_items = 0;
104 
105  stat->page_size = PageGetPageSize(page);
106 
107  /* page type (flags) */
108  if (P_ISDELETED(opaque))
109  {
110  stat->type = 'd';
111  stat->btpo.xact = opaque->btpo.xact;
112  return;
113  }
114  else if (P_IGNORE(opaque))
115  stat->type = 'e';
116  else if (P_ISLEAF(opaque))
117  stat->type = 'l';
118  else if (P_ISROOT(opaque))
119  stat->type = 'r';
120  else
121  stat->type = 'i';
122 
123  /* btpage opaque data */
124  stat->btpo_prev = opaque->btpo_prev;
125  stat->btpo_next = opaque->btpo_next;
126  stat->btpo.level = opaque->btpo.level;
127  stat->btpo_flags = opaque->btpo_flags;
128  stat->btpo_cycleid = opaque->btpo_cycleid;
129 
130  /* count live and dead tuples, and free space */
131  for (off = FirstOffsetNumber; off <= maxoff; off++)
132  {
133  IndexTuple itup;
134 
135  ItemId id = PageGetItemId(page, off);
136 
137  itup = (IndexTuple) PageGetItem(page, id);
138 
139  item_size += IndexTupleSize(itup);
140 
141  if (!ItemIdIsDead(id))
142  stat->live_items++;
143  else
144  stat->dead_items++;
145  }
146  stat->free_size = PageGetFreeSpace(page);
147 
148  if ((stat->live_items + stat->dead_items) > 0)
149  stat->avg_item_size = item_size / (stat->live_items + stat->dead_items);
150  else
151  stat->avg_item_size = 0;
152 }
uint32 avg_item_size
Definition: btreefuncs.c:67
union BTPageStat::@1 btpo
BlockNumber btpo_next
Definition: nbtree.h:57
uint32 dead_items
Definition: btreefuncs.c:63
#define P_IGNORE(opaque)
Definition: nbtree.h:181
union BTPageOpaqueData::@46 btpo
uint32 blkno
Definition: btreefuncs.c:61
char type
Definition: btreefuncs.c:68
#define SizeOfPageHeaderData
Definition: bufpage.h:212
#define ItemIdIsDead(itemId)
Definition: itemid.h:112
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
TransactionId xact
Definition: nbtree.h:61
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:67
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:582
uint16 OffsetNumber
Definition: off.h:24
BlockNumber btpo_next
Definition: btreefuncs.c:72
BTCycleId btpo_cycleid
Definition: nbtree.h:64
BlockNumber btpo_prev
Definition: nbtree.h:56
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
#define PageGetPageSize(page)
Definition: bufpage.h:264
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define P_ISDELETED(opaque)
Definition: nbtree.h:178
#define P_ISROOT(opaque)
Definition: nbtree.h:177
uint32 live_items
Definition: btreefuncs.c:62
TransactionId xact
Definition: btreefuncs.c:76
LocationIndex pd_special
Definition: bufpage.h:156
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
uint32 level
Definition: nbtree.h:60
BlockNumber btpo_prev
Definition: btreefuncs.c:71
uint32 free_size
Definition: btreefuncs.c:66
Page page
Definition: btreefuncs.c:245
PageHeaderData * PageHeader
Definition: bufpage.h:162
uint32 max_avail
Definition: btreefuncs.c:65
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
BTCycleId btpo_cycleid
Definition: btreefuncs.c:79
uint16 btpo_flags
Definition: btreefuncs.c:78
uint32 page_size
Definition: btreefuncs.c:64
uint32 level
Definition: btreefuncs.c:75
uint16 btpo_flags
Definition: nbtree.h:63
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74
#define IndexTupleSize(itup)
Definition: itup.h:70
#define P_ISLEAF(opaque)
Definition: nbtree.h:176
PG_FUNCTION_INFO_V1 ( bt_metap  )
PG_FUNCTION_INFO_V1 ( bt_page_items  )
PG_FUNCTION_INFO_V1 ( bt_page_items_bytea  )
PG_FUNCTION_INFO_V1 ( bt_page_stats  )