PostgreSQL Source Code  git master
btreefuncs.c File Reference
#include "postgres.h"
#include "access/nbtree.h"
#include "access/relation.h"
#include "catalog/namespace.h"
#include "catalog/pg_am.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pageinspect.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

◆ CHECK_RELATION_BLOCK_RANGE

#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:198

Definition at line 50 of file btreefuncs.c.

Referenced by bt_page_items(), and bt_page_stats().

◆ IS_BTREE

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

Definition at line 47 of file btreefuncs.c.

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

◆ IS_INDEX

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

Definition at line 46 of file btreefuncs.c.

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

Typedef Documentation

◆ BTPageStat

typedef struct BTPageStat BTPageStat

Function Documentation

◆ bt_metap()

Datum bt_metap ( PG_FUNCTION_ARGS  )

Definition at line 504 of file btreefuncs.c.

References AccessShareLock, BTMetaPageData::btm_fastlevel, BTMetaPageData::btm_fastroot, BTMetaPageData::btm_last_cleanup_num_heap_tuples, BTMetaPageData::btm_level, BTMetaPageData::btm_magic, BTMetaPageData::btm_oldest_btpo_xact, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTPageGetMeta, BTREE_NOVAC_VERSION, 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, relname, superuser(), textToQualifiedNameList(), TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, UnlockReleaseBuffer(), and values.

505 {
507  Datum result;
508  Relation rel;
509  RangeVar *relrv;
510  BTMetaPageData *metad;
511  TupleDesc tupleDesc;
512  int j;
513  char *values[8];
514  Buffer buffer;
515  Page page;
516  HeapTuple tuple;
517 
518  if (!superuser())
519  ereport(ERROR,
520  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
521  (errmsg("must be superuser to use pageinspect functions"))));
522 
524  rel = relation_openrv(relrv, AccessShareLock);
525 
526  if (!IS_INDEX(rel) || !IS_BTREE(rel))
527  elog(ERROR, "relation \"%s\" is not a btree index",
529 
530  /*
531  * Reject attempts to read non-local temporary relations; we would be
532  * likely to get wrong data since we have no visibility into the owning
533  * session's local buffers.
534  */
535  if (RELATION_IS_OTHER_TEMP(rel))
536  ereport(ERROR,
537  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
538  errmsg("cannot access temporary tables of other sessions")));
539 
540  buffer = ReadBuffer(rel, 0);
541  LockBuffer(buffer, BUFFER_LOCK_SHARE);
542 
543  page = BufferGetPage(buffer);
544  metad = BTPageGetMeta(page);
545 
546  /* Build a tuple descriptor for our result type */
547  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
548  elog(ERROR, "return type must be a row type");
549 
550  j = 0;
551  values[j++] = psprintf("%d", metad->btm_magic);
552  values[j++] = psprintf("%d", metad->btm_version);
553  values[j++] = psprintf("%d", metad->btm_root);
554  values[j++] = psprintf("%d", metad->btm_level);
555  values[j++] = psprintf("%d", metad->btm_fastroot);
556  values[j++] = psprintf("%d", metad->btm_fastlevel);
557 
558  /*
559  * Get values of extended metadata if available, use default values
560  * otherwise.
561  */
562  if (metad->btm_version >= BTREE_NOVAC_VERSION)
563  {
564  values[j++] = psprintf("%u", metad->btm_oldest_btpo_xact);
565  values[j++] = psprintf("%f", metad->btm_last_cleanup_num_heap_tuples);
566  }
567  else
568  {
569  values[j++] = "0";
570  values[j++] = "-1";
571  }
572 
574  values);
575 
576  result = HeapTupleGetDatum(tuple);
577 
578  UnlockReleaseBuffer(buffer);
580 
581  PG_RETURN_DATUM(result);
582 }
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
uint32 btm_version
Definition: nbtree.h:101
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
uint32 btm_magic
Definition: nbtree.h:100
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:608
#define IS_BTREE(r)
Definition: btreefuncs.c:47
bool superuser(void)
Definition: superuser.c:46
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3054
NameData relname
Definition: pg_class.h:35
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2116
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
BlockNumber btm_fastroot
Definition: nbtree.h:104
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
#define ERROR
Definition: elog.h:43
#define BTPageGetMeta(p)
Definition: nbtree.h:113
#define RelationGetRelationName(relation)
Definition: rel.h:456
#define BTREE_NOVAC_VERSION
Definition: nbtree.h:136
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define ereport(elevel, rest)
Definition: elog.h:141
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3594
uint32 btm_fastlevel
Definition: nbtree.h:105
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2067
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
BlockNumber btm_root
Definition: nbtree.h:102
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:552
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:596
float8 btm_last_cleanup_num_heap_tuples
Definition: nbtree.h:109
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int errmsg(const char *fmt,...)
Definition: elog.c:822
uint32 btm_level
Definition: nbtree.h:103
#define elog(elevel,...)
Definition: elog.h:228
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
Definition: c.h:556
TransactionId btm_oldest_btpo_xact
Definition: nbtree.h:107
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78
#define IS_INDEX(r)
Definition: btreefuncs.c:46

◆ bt_page_items()

Datum bt_page_items ( PG_FUNCTION_ARGS  )

Definition at line 309 of file btreefuncs.c.

References AccessShareLock, FuncCallContext::attinmeta, BTPageStat::blkno, bt_page_print_tuples(), 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, relname, 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.

310 {
312  uint32 blkno = PG_GETARG_UINT32(1);
313  Datum result;
314  FuncCallContext *fctx;
315  MemoryContext mctx;
316  struct user_args *uargs;
317 
318  if (!superuser())
319  ereport(ERROR,
320  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
321  (errmsg("must be superuser to use pageinspect functions"))));
322 
323  if (SRF_IS_FIRSTCALL())
324  {
325  RangeVar *relrv;
326  Relation rel;
327  Buffer buffer;
328  BTPageOpaque opaque;
329  TupleDesc tupleDesc;
330 
331  fctx = SRF_FIRSTCALL_INIT();
332 
334  rel = relation_openrv(relrv, AccessShareLock);
335 
336  if (!IS_INDEX(rel) || !IS_BTREE(rel))
337  elog(ERROR, "relation \"%s\" is not a btree index",
339 
340  /*
341  * Reject attempts to read non-local temporary relations; we would be
342  * likely to get wrong data since we have no visibility into the
343  * owning session's local buffers.
344  */
345  if (RELATION_IS_OTHER_TEMP(rel))
346  ereport(ERROR,
347  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
348  errmsg("cannot access temporary tables of other sessions")));
349 
350  if (blkno == 0)
351  elog(ERROR, "block 0 is a meta page");
352 
353  CHECK_RELATION_BLOCK_RANGE(rel, blkno);
354 
355  buffer = ReadBuffer(rel, blkno);
356  LockBuffer(buffer, BUFFER_LOCK_SHARE);
357 
358  /*
359  * We copy the page into local storage to avoid holding pin on the
360  * buffer longer than we must, and possibly failing to release it at
361  * all if the calling query doesn't fetch all rows.
362  */
364 
365  uargs = palloc(sizeof(struct user_args));
366 
367  uargs->page = palloc(BLCKSZ);
368  memcpy(uargs->page, BufferGetPage(buffer), BLCKSZ);
369 
370  UnlockReleaseBuffer(buffer);
372 
373  uargs->offset = FirstOffsetNumber;
374 
375  opaque = (BTPageOpaque) PageGetSpecialPointer(uargs->page);
376 
377  if (P_ISDELETED(opaque))
378  elog(NOTICE, "page is deleted");
379 
380  fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
381 
382  /* Build a tuple descriptor for our result type */
383  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
384  elog(ERROR, "return type must be a row type");
385 
386  fctx->attinmeta = TupleDescGetAttInMetadata(tupleDesc);
387 
388  fctx->user_fctx = uargs;
389 
390  MemoryContextSwitchTo(mctx);
391  }
392 
393  fctx = SRF_PERCALL_SETUP();
394  uargs = fctx->user_fctx;
395 
396  if (fctx->call_cntr < fctx->max_calls)
397  {
398  result = bt_page_print_tuples(fctx, uargs->page, uargs->offset);
399  uargs->offset++;
400  SRF_RETURN_NEXT(fctx, result);
401  }
402  else
403  {
404  pfree(uargs->page);
405  pfree(uargs);
406  SRF_RETURN_DONE(fctx);
407  }
408 }
uint64 call_cntr
Definition: funcapi.h:65
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:265
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
#define CHECK_RELATION_BLOCK_RANGE(rel, blkno)
Definition: btreefuncs.c:50
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:282
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:608
#define IS_BTREE(r)
Definition: btreefuncs.c:47
bool superuser(void)
Definition: superuser.c:46
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3054
NameData relname
Definition: pg_class.h:35
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:286
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:288
void pfree(void *pointer)
Definition: mcxt.c:1056
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
#define ERROR
Definition: elog.h:43
OffsetNumber offset
Definition: btreefuncs.c:245
#define FirstOffsetNumber
Definition: off.h:27
AttInMetadata * attinmeta
Definition: funcapi.h:91
#define RelationGetRelationName(relation)
Definition: rel.h:456
unsigned int uint32
Definition: c.h:359
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define ereport(elevel, rest)
Definition: elog.h:141
#define P_ISDELETED(opaque)
Definition: nbtree.h:192
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3594
uintptr_t Datum
Definition: postgres.h:367
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2067
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
Page page
Definition: btreefuncs.c:244
#define NOTICE
Definition: elog.h:37
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:552
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:596
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
static Datum bt_page_print_tuples(FuncCallContext *fctx, Page page, OffsetNumber offset)
Definition: btreefuncs.c:255
#define elog(elevel,...)
Definition: elog.h:228
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
Definition: c.h:556
int Buffer
Definition: buf.h:23
uint64 max_calls
Definition: funcapi.h:74
#define IS_INDEX(r)
Definition: btreefuncs.c:46
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:306
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:284

◆ bt_page_items_bytea()

Datum bt_page_items_bytea ( PG_FUNCTION_ARGS  )

Definition at line 420 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.

421 {
422  bytea *raw_page = PG_GETARG_BYTEA_P(0);
423  Datum result;
424  FuncCallContext *fctx;
425  struct user_args *uargs;
426  int raw_page_size;
427 
428  if (!superuser())
429  ereport(ERROR,
430  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
431  (errmsg("must be superuser to use raw page functions"))));
432 
433  if (SRF_IS_FIRSTCALL())
434  {
435  BTPageOpaque opaque;
436  MemoryContext mctx;
437  TupleDesc tupleDesc;
438 
439  raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
440 
441  if (raw_page_size < SizeOfPageHeaderData)
442  ereport(ERROR,
443  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
444  errmsg("input page too small (%d bytes)", raw_page_size)));
445 
446  fctx = SRF_FIRSTCALL_INIT();
448 
449  uargs = palloc(sizeof(struct user_args));
450 
451  uargs->page = VARDATA(raw_page);
452 
453  uargs->offset = FirstOffsetNumber;
454 
455  opaque = (BTPageOpaque) PageGetSpecialPointer(uargs->page);
456 
457  if (P_ISMETA(opaque))
458  ereport(ERROR,
459  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
460  errmsg("block is a meta page")));
461 
462  if (P_ISDELETED(opaque))
463  elog(NOTICE, "page is deleted");
464 
465  fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
466 
467  /* Build a tuple descriptor for our result type */
468  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
469  elog(ERROR, "return type must be a row type");
470 
471  fctx->attinmeta = TupleDescGetAttInMetadata(tupleDesc);
472 
473  fctx->user_fctx = uargs;
474 
475  MemoryContextSwitchTo(mctx);
476  }
477 
478  fctx = SRF_PERCALL_SETUP();
479  uargs = fctx->user_fctx;
480 
481  if (fctx->call_cntr < fctx->max_calls)
482  {
483  result = bt_page_print_tuples(fctx, uargs->page, uargs->offset);
484  uargs->offset++;
485  SRF_RETURN_NEXT(fctx, result);
486  }
487  else
488  {
489  pfree(uargs);
490  SRF_RETURN_DONE(fctx);
491  }
492 }
uint64 call_cntr
Definition: funcapi.h:65
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:282
#define VARHDRSZ
Definition: c.h:562
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:608
bool superuser(void)
Definition: superuser.c:46
#define SizeOfPageHeaderData
Definition: bufpage.h:216
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:286
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
#define P_ISMETA(opaque)
Definition: nbtree.h:193
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:288
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:329
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
OffsetNumber offset
Definition: btreefuncs.c:245
#define FirstOffsetNumber
Definition: off.h:27
AttInMetadata * attinmeta
Definition: funcapi.h:91
#define ereport(elevel, rest)
Definition: elog.h:141
#define P_ISDELETED(opaque)
Definition: nbtree.h:192
uintptr_t Datum
Definition: postgres.h:367
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2067
Page page
Definition: btreefuncs.c:244
#define NOTICE
Definition: elog.h:37
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
static Datum bt_page_print_tuples(FuncCallContext *fctx, Page page, OffsetNumber offset)
Definition: btreefuncs.c:255
#define elog(elevel,...)
Definition: elog.h:228
Definition: c.h:556
uint64 max_calls
Definition: funcapi.h:74
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:306
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:284

◆ bt_page_print_tuples()

static Datum bt_page_print_tuples ( FuncCallContext fctx,
Page  page,
OffsetNumber  offset 
)
static

Definition at line 255 of file btreefuncs.c.

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

Referenced by bt_page_items(), and bt_page_items_bytea().

256 {
257  char *values[6];
258  HeapTuple tuple;
259  ItemId id;
260  IndexTuple itup;
261  int j;
262  int off;
263  int dlen;
264  char *dump;
265  char *ptr;
266 
267  id = PageGetItemId(page, offset);
268 
269  if (!ItemIdIsValid(id))
270  elog(ERROR, "invalid ItemId");
271 
272  itup = (IndexTuple) PageGetItem(page, id);
273 
274  j = 0;
275  values[j++] = psprintf("%d", offset);
276  values[j++] = psprintf("(%u,%u)",
279  values[j++] = psprintf("%d", (int) IndexTupleSize(itup));
280  values[j++] = psprintf("%c", IndexTupleHasNulls(itup) ? 't' : 'f');
281  values[j++] = psprintf("%c", IndexTupleHasVarwidths(itup) ? 't' : 'f');
282 
283  ptr = (char *) itup + IndexInfoFindDataOffset(itup->t_info);
284  dlen = IndexTupleSize(itup) - IndexInfoFindDataOffset(itup->t_info);
285  dump = palloc0(dlen * 3 + 1);
286  values[j] = dump;
287  for (off = 0; off < dlen; off++)
288  {
289  if (off > 0)
290  *dump++ = ' ';
291  sprintf(dump, "%02x", *(ptr + off) & 0xff);
292  dump += 2;
293  }
294 
295  tuple = BuildTupleFromCStrings(fctx->attinmeta, values);
296 
297  return HeapTupleGetDatum(tuple);
298 }
#define ItemPointerGetOffsetNumberNoCheck(pointer)
Definition: itemptr.h:108
#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:2116
#define sprintf
Definition: port.h:194
#define ERROR
Definition: elog.h:43
OffsetNumber offset
Definition: btreefuncs.c:245
AttInMetadata * attinmeta
Definition: funcapi.h:91
IndexTupleData * IndexTuple
Definition: itup.h:53
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
void * palloc0(Size size)
Definition: mcxt.c:980
Page page
Definition: btreefuncs.c:244
#define ItemIdIsValid(itemId)
Definition: itemid.h:86
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
#define IndexTupleHasVarwidths(itup)
Definition: itup.h:73
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define ItemPointerGetBlockNumberNoCheck(pointer)
Definition: itemptr.h:89
#define elog(elevel,...)
Definition: elog.h:228
unsigned short t_info
Definition: itup.h:49
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
#define IndexTupleSize(itup)
Definition: itup.h:71

◆ bt_page_stats()

Datum bt_page_stats ( PG_FUNCTION_ARGS  )

Definition at line 160 of file btreefuncs.c.

References AccessShareLock, BTPageStat::avg_item_size, BTPageStat::blkno, BTPageStat::btpo, BTPageStat::btpo_flags, BTPageStat::btpo_next, BTPageStat::btpo_prev, 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, relname, stat, superuser(), textToQualifiedNameList(), TupleDescGetAttInMetadata(), BTPageStat::type, TYPEFUNC_COMPOSITE, UnlockReleaseBuffer(), values, and BTPageStat::xact.

161 {
163  uint32 blkno = PG_GETARG_UINT32(1);
164  Buffer buffer;
165  Relation rel;
166  RangeVar *relrv;
167  Datum result;
168  HeapTuple tuple;
169  TupleDesc tupleDesc;
170  int j;
171  char *values[11];
173 
174  if (!superuser())
175  ereport(ERROR,
176  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
177  (errmsg("must be superuser to use pageinspect functions"))));
178 
180  rel = relation_openrv(relrv, AccessShareLock);
181 
182  if (!IS_INDEX(rel) || !IS_BTREE(rel))
183  elog(ERROR, "relation \"%s\" is not a btree index",
185 
186  /*
187  * Reject attempts to read non-local temporary relations; we would be
188  * likely to get wrong data since we have no visibility into the owning
189  * session's local buffers.
190  */
191  if (RELATION_IS_OTHER_TEMP(rel))
192  ereport(ERROR,
193  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
194  errmsg("cannot access temporary tables of other sessions")));
195 
196  if (blkno == 0)
197  elog(ERROR, "block 0 is a meta page");
198 
199  CHECK_RELATION_BLOCK_RANGE(rel, blkno);
200 
201  buffer = ReadBuffer(rel, blkno);
202  LockBuffer(buffer, BUFFER_LOCK_SHARE);
203 
204  /* keep compiler quiet */
205  stat.btpo_prev = stat.btpo_next = InvalidBlockNumber;
206  stat.btpo_flags = stat.free_size = stat.avg_item_size = 0;
207 
208  GetBTPageStatistics(blkno, buffer, &stat);
209 
210  UnlockReleaseBuffer(buffer);
212 
213  /* Build a tuple descriptor for our result type */
214  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
215  elog(ERROR, "return type must be a row type");
216 
217  j = 0;
218  values[j++] = psprintf("%d", stat.blkno);
219  values[j++] = psprintf("%c", stat.type);
220  values[j++] = psprintf("%d", stat.live_items);
221  values[j++] = psprintf("%d", stat.dead_items);
222  values[j++] = psprintf("%d", stat.avg_item_size);
223  values[j++] = psprintf("%d", stat.page_size);
224  values[j++] = psprintf("%d", stat.free_size);
225  values[j++] = psprintf("%d", stat.btpo_prev);
226  values[j++] = psprintf("%d", stat.btpo_next);
227  values[j++] = psprintf("%d", (stat.type == 'd') ? stat.btpo.xact : stat.btpo.level);
228  values[j++] = psprintf("%d", stat.btpo_flags);
229 
231  values);
232 
233  result = HeapTupleGetDatum(tuple);
234 
235  PG_RETURN_DATUM(result);
236 }
uint32 avg_item_size
Definition: btreefuncs.c:66
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:265
union BTPageStat::@1 btpo
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
uint32 dead_items
Definition: btreefuncs.c:62
#define CHECK_RELATION_BLOCK_RANGE(rel, blkno)
Definition: btreefuncs.c:50
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define AccessShareLock
Definition: lockdefs.h:36
uint32 blkno
Definition: btreefuncs.c:60
int errcode(int sqlerrcode)
Definition: elog.c:608
#define IS_BTREE(r)
Definition: btreefuncs.c:47
bool superuser(void)
Definition: superuser.c:46
char type
Definition: btreefuncs.c:67
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3054
NameData relname
Definition: pg_class.h:35
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2116
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
#define ERROR
Definition: elog.h:43
BlockNumber btpo_next
Definition: btreefuncs.c:71
#define RelationGetRelationName(relation)
Definition: rel.h:456
unsigned int uint32
Definition: c.h:359
#define ereport(elevel, rest)
Definition: elog.h:141
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3594
uint32 live_items
Definition: btreefuncs.c:61
TransactionId xact
Definition: btreefuncs.c:75
#define stat(a, b)
Definition: win32_port.h:255
BlockNumber btpo_prev
Definition: btreefuncs.c:70
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
uint32 free_size
Definition: btreefuncs.c:65
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2067
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:552
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:596
#define InvalidBlockNumber
Definition: block.h:33
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
uint16 btpo_flags
Definition: btreefuncs.c:77
static void GetBTPageStatistics(BlockNumber blkno, Buffer buffer, BTPageStat *stat)
Definition: btreefuncs.c:89
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int errmsg(const char *fmt,...)
Definition: elog.c:822
uint32 page_size
Definition: btreefuncs.c:63
#define elog(elevel,...)
Definition: elog.h:228
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
Definition: c.h:556
uint32 level
Definition: btreefuncs.c:74
int Buffer
Definition: buf.h:23
#define IS_INDEX(r)
Definition: btreefuncs.c:46

◆ GetBTPageStatistics()

static void GetBTPageStatistics ( BlockNumber  blkno,
Buffer  buffer,
BTPageStat stat 
)
static

Definition at line 89 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().

90 {
91  Page page = BufferGetPage(buffer);
92  PageHeader phdr = (PageHeader) page;
93  OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
95  int item_size = 0;
96  int off;
97 
98  stat->blkno = blkno;
99 
100  stat->max_avail = BLCKSZ - (BLCKSZ - phdr->pd_special + SizeOfPageHeaderData);
101 
102  stat->dead_items = stat->live_items = 0;
103 
104  stat->page_size = PageGetPageSize(page);
105 
106  /* page type (flags) */
107  if (P_ISDELETED(opaque))
108  {
109  stat->type = 'd';
110  stat->btpo.xact = opaque->btpo.xact;
111  return;
112  }
113  else if (P_IGNORE(opaque))
114  stat->type = 'e';
115  else if (P_ISLEAF(opaque))
116  stat->type = 'l';
117  else if (P_ISROOT(opaque))
118  stat->type = 'r';
119  else
120  stat->type = 'i';
121 
122  /* btpage opaque data */
123  stat->btpo_prev = opaque->btpo_prev;
124  stat->btpo_next = opaque->btpo_next;
125  stat->btpo.level = opaque->btpo.level;
126  stat->btpo_flags = opaque->btpo_flags;
127  stat->btpo_cycleid = opaque->btpo_cycleid;
128 
129  /* count live and dead tuples, and free space */
130  for (off = FirstOffsetNumber; off <= maxoff; off++)
131  {
132  IndexTuple itup;
133 
134  ItemId id = PageGetItemId(page, off);
135 
136  itup = (IndexTuple) PageGetItem(page, id);
137 
138  item_size += IndexTupleSize(itup);
139 
140  if (!ItemIdIsDead(id))
141  stat->live_items++;
142  else
143  stat->dead_items++;
144  }
145  stat->free_size = PageGetFreeSpace(page);
146 
147  if ((stat->live_items + stat->dead_items) > 0)
148  stat->avg_item_size = item_size / (stat->live_items + stat->dead_items);
149  else
150  stat->avg_item_size = 0;
151 }
uint32 avg_item_size
Definition: btreefuncs.c:66
union BTPageStat::@1 btpo
BlockNumber btpo_next
Definition: nbtree.h:59
uint32 dead_items
Definition: btreefuncs.c:62
#define P_IGNORE(opaque)
Definition: nbtree.h:195
union BTPageOpaqueData::@46 btpo
uint32 blkno
Definition: btreefuncs.c:60
char type
Definition: btreefuncs.c:67
#define SizeOfPageHeaderData
Definition: bufpage.h:216
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
TransactionId xact
Definition: nbtree.h:63
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:581
uint16 OffsetNumber
Definition: off.h:24
BlockNumber btpo_next
Definition: btreefuncs.c:71
BTCycleId btpo_cycleid
Definition: nbtree.h:66
BlockNumber btpo_prev
Definition: nbtree.h:58
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
#define PageGetPageSize(page)
Definition: bufpage.h:268
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define P_ISDELETED(opaque)
Definition: nbtree.h:192
#define P_ISROOT(opaque)
Definition: nbtree.h:191
uint32 live_items
Definition: btreefuncs.c:61
TransactionId xact
Definition: btreefuncs.c:75
LocationIndex pd_special
Definition: bufpage.h:160
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
uint32 level
Definition: nbtree.h:62
BlockNumber btpo_prev
Definition: btreefuncs.c:70
uint32 free_size
Definition: btreefuncs.c:65
Page page
Definition: btreefuncs.c:244
PageHeaderData * PageHeader
Definition: bufpage.h:166
uint32 max_avail
Definition: btreefuncs.c:64
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
BTCycleId btpo_cycleid
Definition: btreefuncs.c:78
uint16 btpo_flags
Definition: btreefuncs.c:77
uint32 page_size
Definition: btreefuncs.c:63
uint32 level
Definition: btreefuncs.c:74
uint16 btpo_flags
Definition: nbtree.h:65
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define IndexTupleSize(itup)
Definition: itup.h:71
#define P_ISLEAF(opaque)
Definition: nbtree.h:190

◆ PG_FUNCTION_INFO_V1() [1/4]

PG_FUNCTION_INFO_V1 ( bt_metap  )

◆ PG_FUNCTION_INFO_V1() [2/4]

PG_FUNCTION_INFO_V1 ( bt_page_items  )

◆ PG_FUNCTION_INFO_V1() [3/4]

PG_FUNCTION_INFO_V1 ( bt_page_items_bytea  )

◆ PG_FUNCTION_INFO_V1() [4/4]

PG_FUNCTION_INFO_V1 ( bt_page_stats  )