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 "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pageinspect.h"
#include "utils/array.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 DatumGetItemPointer(X)   ((ItemPointer) DatumGetPointer(X))
 
#define ItemPointerGetDatum(X)   PointerGetDatum(X)
 
#define CHECK_RELATION_BLOCK_RANGE(rel, blkno)
 
#define BT_METAP_COLS_V1_8   9
 

Typedefs

typedef struct BTPageStat BTPageStat
 

Functions

 PG_FUNCTION_INFO_V1 (bt_metap)
 
 PG_FUNCTION_INFO_V1 (bt_page_items_1_9)
 
 PG_FUNCTION_INFO_V1 (bt_page_items)
 
 PG_FUNCTION_INFO_V1 (bt_page_items_bytea)
 
 PG_FUNCTION_INFO_V1 (bt_page_stats_1_9)
 
 PG_FUNCTION_INFO_V1 (bt_page_stats)
 
static void GetBTPageStatistics (BlockNumber blkno, Buffer buffer, BTPageStat *stat)
 
static Datum bt_page_stats_internal (PG_FUNCTION_ARGS, enum pageinspect_version ext_version)
 
Datum bt_page_stats_1_9 (PG_FUNCTION_ARGS)
 
Datum bt_page_stats (PG_FUNCTION_ARGS)
 
static Datum bt_page_print_tuples (struct user_args *uargs)
 
static Datum bt_page_items_internal (PG_FUNCTION_ARGS, enum pageinspect_version ext_version)
 
Datum bt_page_items_1_9 (PG_FUNCTION_ARGS)
 
Datum bt_page_items (PG_FUNCTION_ARGS)
 
Datum bt_page_items_bytea (PG_FUNCTION_ARGS)
 
Datum bt_metap (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ BT_METAP_COLS_V1_8

#define BT_METAP_COLS_V1_8   9

Definition at line 667 of file btreefuncs.c.

Referenced by bt_metap().

◆ 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:46
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:212

Definition at line 56 of file btreefuncs.c.

Referenced by bt_page_items_internal(), and bt_page_stats_internal().

◆ DatumGetItemPointer

#define DatumGetItemPointer (   X)    ((ItemPointer) DatumGetPointer(X))

Definition at line 52 of file btreefuncs.c.

◆ IS_BTREE

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

Definition at line 51 of file btreefuncs.c.

Referenced by bt_metap(), bt_page_items_internal(), and bt_page_stats_internal().

◆ IS_INDEX

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

Definition at line 50 of file btreefuncs.c.

Referenced by bt_metap(), bt_page_items_internal(), and bt_page_stats_internal().

◆ ItemPointerGetDatum

#define ItemPointerGetDatum (   X)    PointerGetDatum(X)

Definition at line 53 of file btreefuncs.c.

Referenced by bt_page_print_tuples().

Typedef Documentation

◆ BTPageStat

typedef struct BTPageStat BTPageStat

Function Documentation

◆ bt_metap()

Datum bt_metap ( PG_FUNCTION_ARGS  )

Definition at line 678 of file btreefuncs.c.

References AccessShareLock, BT_METAP_COLS_V1_8, BTMetaPageData::btm_allequalimage, BTMetaPageData::btm_fastlevel, BTMetaPageData::btm_fastroot, BTMetaPageData::btm_last_cleanup_num_delpages, BTMetaPageData::btm_last_cleanup_num_heap_tuples, BTMetaPageData::btm_level, BTMetaPageData::btm_magic, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTPageGetMeta, BTREE_NOVAC_VERSION, BUFFER_LOCK_SHARE, BufferGetPage, BuildTupleFromCStrings(), elog, ereport, errcode(), errhint(), errmsg(), ERROR, get_call_result_type(), HeapTupleGetDatum, INT64_FORMAT, IS_BTREE, IS_INDEX, LockBuffer(), makeRangeVarFromNameList(), TupleDescData::natts, 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.

679 {
681  Datum result;
682  Relation rel;
683  RangeVar *relrv;
684  BTMetaPageData *metad;
685  TupleDesc tupleDesc;
686  int j;
687  char *values[9];
688  Buffer buffer;
689  Page page;
690  HeapTuple tuple;
691 
692  if (!superuser())
693  ereport(ERROR,
694  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
695  errmsg("must be superuser to use pageinspect functions")));
696 
698  rel = relation_openrv(relrv, AccessShareLock);
699 
700  if (!IS_INDEX(rel) || !IS_BTREE(rel))
701  elog(ERROR, "relation \"%s\" is not a btree index",
703 
704  /*
705  * Reject attempts to read non-local temporary relations; we would be
706  * likely to get wrong data since we have no visibility into the owning
707  * session's local buffers.
708  */
709  if (RELATION_IS_OTHER_TEMP(rel))
710  ereport(ERROR,
711  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
712  errmsg("cannot access temporary tables of other sessions")));
713 
714  buffer = ReadBuffer(rel, 0);
715  LockBuffer(buffer, BUFFER_LOCK_SHARE);
716 
717  page = BufferGetPage(buffer);
718  metad = BTPageGetMeta(page);
719 
720  /* Build a tuple descriptor for our result type */
721  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
722  elog(ERROR, "return type must be a row type");
723 
724  /*
725  * We need a kluge here to detect API versions prior to 1.8. Earlier
726  * versions incorrectly used int4 for certain columns.
727  *
728  * There is no way to reliably avoid the problems created by the old
729  * function definition at this point, so insist that the user update the
730  * extension.
731  */
732  if (tupleDesc->natts < BT_METAP_COLS_V1_8)
733  ereport(ERROR,
734  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
735  errmsg("function has wrong number of declared columns"),
736  errhint("To resolve the problem, update the \"pageinspect\" extension to the latest version.")));
737 
738  j = 0;
739  values[j++] = psprintf("%d", metad->btm_magic);
740  values[j++] = psprintf("%d", metad->btm_version);
741  values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_root);
742  values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_level);
743  values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_fastroot);
744  values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_fastlevel);
745 
746  /*
747  * Get values of extended metadata if available, use default values
748  * otherwise. Note that we rely on the assumption that btm_allequalimage
749  * is initialized to zero with indexes that were built on versions prior
750  * to Postgres 13 (just like _bt_metaversion()).
751  */
752  if (metad->btm_version >= BTREE_NOVAC_VERSION)
753  {
754  values[j++] = psprintf(INT64_FORMAT,
755  (int64) metad->btm_last_cleanup_num_delpages);
756  values[j++] = psprintf("%f", metad->btm_last_cleanup_num_heap_tuples);
757  values[j++] = metad->btm_allequalimage ? "t" : "f";
758  }
759  else
760  {
761  values[j++] = "0";
762  values[j++] = "-1";
763  values[j++] = "f";
764  }
765 
767  values);
768 
769  result = HeapTupleGetDatum(tuple);
770 
771  UnlockReleaseBuffer(buffer);
773 
774  PG_RETURN_DATUM(result);
775 }
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
int errhint(const char *fmt,...)
Definition: elog.c:1156
uint32 btm_version
Definition: nbtree.h:104
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
uint32 btm_magic
Definition: nbtree.h:103
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:698
#define IS_BTREE(r)
Definition: btreefuncs.c:51
bool superuser(void)
Definition: superuser.c:46
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3107
NameData relname
Definition: pg_class.h:38
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2146
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
BlockNumber btm_fastroot
Definition: nbtree.h:107
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3791
#define ERROR
Definition: elog.h:46
#define BTPageGetMeta(p)
Definition: nbtree.h:119
bool btm_allequalimage
Definition: nbtree.h:116
#define RelationGetRelationName(relation)
Definition: rel.h:511
#define BTREE_NOVAC_VERSION
Definition: nbtree.h:150
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
uint32 btm_last_cleanup_num_delpages
Definition: nbtree.h:112
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3688
uint32 btm_fastlevel
Definition: nbtree.h:108
#define BT_METAP_COLS_V1_8
Definition: btreefuncs.c:667
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2097
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4007
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
BlockNumber btm_root
Definition: nbtree.h:105
#define ereport(elevel,...)
Definition: elog.h:157
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:631
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:694
float8 btm_last_cleanup_num_heap_tuples
Definition: nbtree.h:114
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
#define INT64_FORMAT
Definition: c.h:483
static Datum values[MAXATTR]
Definition: bootstrap.c:156
int errmsg(const char *fmt,...)
Definition: elog.c:909
uint32 btm_level
Definition: nbtree.h:106
#define elog(elevel,...)
Definition: elog.h:232
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
Definition: c.h:621
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78
#define IS_INDEX(r)
Definition: btreefuncs.c:50

◆ bt_page_items()

Datum bt_page_items ( PG_FUNCTION_ARGS  )

Definition at line 570 of file btreefuncs.c.

References bt_page_items_internal(), and PAGEINSPECT_V1_8.

571 {
573 }
static Datum bt_page_items_internal(PG_FUNCTION_ARGS, enum pageinspect_version ext_version)
Definition: btreefuncs.c:451

◆ bt_page_items_1_9()

Datum bt_page_items_1_9 ( PG_FUNCTION_ARGS  )

Definition at line 563 of file btreefuncs.c.

References bt_page_items_internal(), and PAGEINSPECT_V1_9.

564 {
566 }
static Datum bt_page_items_internal(PG_FUNCTION_ARGS, enum pageinspect_version ext_version)
Definition: btreefuncs.c:451

◆ bt_page_items_bytea()

Datum bt_page_items_bytea ( PG_FUNCTION_ARGS  )

Definition at line 585 of file btreefuncs.c.

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

586 {
587  bytea *raw_page = PG_GETARG_BYTEA_P(0);
588  Datum result;
589  FuncCallContext *fctx;
590  struct user_args *uargs;
591  int raw_page_size;
592 
593  if (!superuser())
594  ereport(ERROR,
595  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
596  errmsg("must be superuser to use raw page functions")));
597 
598  if (SRF_IS_FIRSTCALL())
599  {
600  BTPageOpaque opaque;
601  MemoryContext mctx;
602  TupleDesc tupleDesc;
603 
604  raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
605 
606  if (raw_page_size < SizeOfPageHeaderData)
607  ereport(ERROR,
608  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
609  errmsg("input page too small (%d bytes)", raw_page_size)));
610 
611  fctx = SRF_FIRSTCALL_INIT();
613 
614  uargs = palloc(sizeof(struct user_args));
615 
616  uargs->page = VARDATA(raw_page);
617 
618  uargs->offset = FirstOffsetNumber;
619 
620  opaque = (BTPageOpaque) PageGetSpecialPointer(uargs->page);
621 
622  if (P_ISMETA(opaque))
623  ereport(ERROR,
624  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
625  errmsg("block is a meta page")));
626 
627  if (P_ISDELETED(opaque))
628  elog(NOTICE, "page is deleted");
629 
630  if (!P_ISDELETED(opaque))
631  fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
632  else
633  {
634  /* Don't interpret BTDeletedPageData as index tuples */
635  elog(NOTICE, "page from block is deleted");
636  fctx->max_calls = 0;
637  }
638  uargs->leafpage = P_ISLEAF(opaque);
639  uargs->rightmost = P_RIGHTMOST(opaque);
640 
641  /* Build a tuple descriptor for our result type */
642  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
643  elog(ERROR, "return type must be a row type");
644  tupleDesc = BlessTupleDesc(tupleDesc);
645 
646  uargs->tupd = tupleDesc;
647 
648  fctx->user_fctx = uargs;
649 
650  MemoryContextSwitchTo(mctx);
651  }
652 
653  fctx = SRF_PERCALL_SETUP();
654  uargs = fctx->user_fctx;
655 
656  if (fctx->call_cntr < fctx->max_calls)
657  {
658  result = bt_page_print_tuples(uargs);
659  uargs->offset++;
660  SRF_RETURN_NEXT(fctx, result);
661  }
662 
663  SRF_RETURN_DONE(fctx);
664 }
uint64 call_cntr
Definition: funcapi.h:65
static Datum bt_page_print_tuples(struct user_args *uargs)
Definition: btreefuncs.c:304
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARSIZE(PTR)
Definition: postgres.h:316
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
#define VARHDRSZ
Definition: c.h:627
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
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:297
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:71
#define P_ISMETA(opaque)
Definition: nbtree.h:222
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:335
#define ERROR
Definition: elog.h:46
OffsetNumber offset
Definition: btreefuncs.c:291
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2082
#define FirstOffsetNumber
Definition: off.h:27
#define P_ISDELETED(opaque)
Definition: nbtree.h:221
uintptr_t Datum
Definition: postgres.h:411
Page page
Definition: btreefuncs.c:290
#define ereport(elevel,...)
Definition: elog.h:157
#define NOTICE
Definition: elog.h:37
bool leafpage
Definition: btreefuncs.c:292
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:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
Definition: c.h:621
TupleDesc tupd
Definition: btreefuncs.c:294
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:218
uint64 max_calls
Definition: funcapi.h:74
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define P_ISLEAF(opaque)
Definition: nbtree.h:219
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295
bool rightmost
Definition: btreefuncs.c:293

◆ bt_page_items_internal()

static Datum bt_page_items_internal ( PG_FUNCTION_ARGS  ,
enum pageinspect_version  ext_version 
)
static

Definition at line 451 of file btreefuncs.c.

References AccessShareLock, BlessTupleDesc(), 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(), INT64_FORMAT, IS_BTREE, IS_INDEX, user_args::leafpage, LockBuffer(), makeRangeVarFromNameList(), FuncCallContext::max_calls, MaxBlockNumber, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, NOTICE, user_args::offset, P_ISDELETED, P_ISLEAF, P_RIGHTMOST, user_args::page, PageGetMaxOffsetNumber, PageGetSpecialPointer, PAGEINSPECT_V1_8, palloc(), PG_GETARG_INT64, PG_GETARG_TEXT_PP, PG_GETARG_UINT32, ReadBuffer(), relation_close(), RELATION_IS_OTHER_TEMP, relation_openrv(), RelationGetRelationName, relname, user_args::rightmost, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, superuser(), textToQualifiedNameList(), user_args::tupd, TYPEFUNC_COMPOSITE, UnlockReleaseBuffer(), and FuncCallContext::user_fctx.

Referenced by bt_page_items(), and bt_page_items_1_9().

452 {
454  int64 blkno = (ext_version == PAGEINSPECT_V1_8 ? PG_GETARG_UINT32(1) : PG_GETARG_INT64(1));
455  Datum result;
456  FuncCallContext *fctx;
457  MemoryContext mctx;
458  struct user_args *uargs;
459 
460  if (!superuser())
461  ereport(ERROR,
462  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
463  errmsg("must be superuser to use pageinspect functions")));
464 
465  if (SRF_IS_FIRSTCALL())
466  {
467  RangeVar *relrv;
468  Relation rel;
469  Buffer buffer;
470  BTPageOpaque opaque;
471  TupleDesc tupleDesc;
472 
473  fctx = SRF_FIRSTCALL_INIT();
474 
476  rel = relation_openrv(relrv, AccessShareLock);
477 
478  if (!IS_INDEX(rel) || !IS_BTREE(rel))
479  elog(ERROR, "relation \"%s\" is not a btree index",
481 
482  /*
483  * Reject attempts to read non-local temporary relations; we would be
484  * likely to get wrong data since we have no visibility into the
485  * owning session's local buffers.
486  */
487  if (RELATION_IS_OTHER_TEMP(rel))
488  ereport(ERROR,
489  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
490  errmsg("cannot access temporary tables of other sessions")));
491 
492  if (blkno < 0 || blkno > MaxBlockNumber)
493  ereport(ERROR,
494  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
495  errmsg("invalid block number")));
496 
497  if (blkno == 0)
498  ereport(ERROR,
499  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
500  errmsg("block 0 is a meta page")));
501 
502  CHECK_RELATION_BLOCK_RANGE(rel, blkno);
503 
504  buffer = ReadBuffer(rel, blkno);
505  LockBuffer(buffer, BUFFER_LOCK_SHARE);
506 
507  /*
508  * We copy the page into local storage to avoid holding pin on the
509  * buffer longer than we must, and possibly failing to release it at
510  * all if the calling query doesn't fetch all rows.
511  */
513 
514  uargs = palloc(sizeof(struct user_args));
515 
516  uargs->page = palloc(BLCKSZ);
517  memcpy(uargs->page, BufferGetPage(buffer), BLCKSZ);
518 
519  UnlockReleaseBuffer(buffer);
521 
522  uargs->offset = FirstOffsetNumber;
523 
524  opaque = (BTPageOpaque) PageGetSpecialPointer(uargs->page);
525 
526  if (!P_ISDELETED(opaque))
527  fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
528  else
529  {
530  /* Don't interpret BTDeletedPageData as index tuples */
531  elog(NOTICE, "page from block " INT64_FORMAT " is deleted", blkno);
532  fctx->max_calls = 0;
533  }
534  uargs->leafpage = P_ISLEAF(opaque);
535  uargs->rightmost = P_RIGHTMOST(opaque);
536 
537  /* Build a tuple descriptor for our result type */
538  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
539  elog(ERROR, "return type must be a row type");
540  tupleDesc = BlessTupleDesc(tupleDesc);
541 
542  uargs->tupd = tupleDesc;
543 
544  fctx->user_fctx = uargs;
545 
546  MemoryContextSwitchTo(mctx);
547  }
548 
549  fctx = SRF_PERCALL_SETUP();
550  uargs = fctx->user_fctx;
551 
552  if (fctx->call_cntr < fctx->max_calls)
553  {
554  result = bt_page_print_tuples(uargs);
555  uargs->offset++;
556  SRF_RETURN_NEXT(fctx, result);
557  }
558 
559  SRF_RETURN_DONE(fctx);
560 }
uint64 call_cntr
Definition: funcapi.h:65
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:270
static Datum bt_page_print_tuples(struct user_args *uargs)
Definition: btreefuncs.c:304
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
#define CHECK_RELATION_BLOCK_RANGE(rel, blkno)
Definition: btreefuncs.c:56
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:698
#define IS_BTREE(r)
Definition: btreefuncs.c:51
bool superuser(void)
Definition: superuser.c:46
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3107
NameData relname
Definition: pg_class.h:38
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:71
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3791
#define ERROR
Definition: elog.h:46
OffsetNumber offset
Definition: btreefuncs.c:291
#define MaxBlockNumber
Definition: block.h:35
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2082
#define FirstOffsetNumber
Definition: off.h:27
#define RelationGetRelationName(relation)
Definition: rel.h:511
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define P_ISDELETED(opaque)
Definition: nbtree.h:221
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3688
uintptr_t Datum
Definition: postgres.h:411
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4007
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
Page page
Definition: btreefuncs.c:290
#define ereport(elevel,...)
Definition: elog.h:157
#define NOTICE
Definition: elog.h:37
bool leafpage
Definition: btreefuncs.c:292
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:631
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:694
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define INT64_FORMAT
Definition: c.h:483
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
Definition: c.h:621
TupleDesc tupd
Definition: btreefuncs.c:294
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
int Buffer
Definition: buf.h:23
#define P_RIGHTMOST(opaque)
Definition: nbtree.h:218
uint64 max_calls
Definition: funcapi.h:74
#define IS_INDEX(r)
Definition: btreefuncs.c:50
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define P_ISLEAF(opaque)
Definition: nbtree.h:219
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295
bool rightmost
Definition: btreefuncs.c:293

◆ bt_page_print_tuples()

static Datum bt_page_print_tuples ( struct user_args uargs)
static

Definition at line 304 of file btreefuncs.c.

References Assert, BoolGetDatum, BTreeTupleGetHeapTID(), BTreeTupleGetNPosting(), BTreeTupleGetPosting(), BTreeTupleGetPostingOffset(), BTreeTupleIsPivot(), BTreeTupleIsPosting(), construct_array(), CStringGetTextDatum, DatumGetInt16, elog, ERROR, heap_form_tuple(), HeapTupleGetDatum, i, INDEX_SIZE_MASK, IndexInfoFindDataOffset, IndexTupleHasNulls, IndexTupleHasVarwidths, IndexTupleSize, Int32GetDatum, ItemIdIsDead, ItemIdIsValid, ItemPointerGetDatum, user_args::leafpage, MAXALIGN, user_args::offset, P_HIKEY, user_args::page, PageGetItem, PageGetItemId, palloc(), palloc0(), pfree(), PointerGetDatum, user_args::rightmost, sprintf, IndexTupleData::t_info, IndexTupleData::t_tid, user_args::tupd, and values.

Referenced by bt_page_items_bytea(), and bt_page_items_internal().

305 {
306  Page page = uargs->page;
307  OffsetNumber offset = uargs->offset;
308  bool leafpage = uargs->leafpage;
309  bool rightmost = uargs->rightmost;
310  bool ispivottuple;
311  Datum values[9];
312  bool nulls[9];
313  HeapTuple tuple;
314  ItemId id;
315  IndexTuple itup;
316  int j;
317  int off;
318  int dlen;
319  char *dump,
320  *datacstring;
321  char *ptr;
322  ItemPointer htid;
323 
324  id = PageGetItemId(page, offset);
325 
326  if (!ItemIdIsValid(id))
327  elog(ERROR, "invalid ItemId");
328 
329  itup = (IndexTuple) PageGetItem(page, id);
330 
331  j = 0;
332  memset(nulls, 0, sizeof(nulls));
333  values[j++] = DatumGetInt16(offset);
334  values[j++] = ItemPointerGetDatum(&itup->t_tid);
335  values[j++] = Int32GetDatum((int) IndexTupleSize(itup));
336  values[j++] = BoolGetDatum(IndexTupleHasNulls(itup));
337  values[j++] = BoolGetDatum(IndexTupleHasVarwidths(itup));
338 
339  ptr = (char *) itup + IndexInfoFindDataOffset(itup->t_info);
340  dlen = IndexTupleSize(itup) - IndexInfoFindDataOffset(itup->t_info);
341 
342  /*
343  * Make sure that "data" column does not include posting list or pivot
344  * tuple representation of heap TID(s).
345  *
346  * Note: BTreeTupleIsPivot() won't work reliably on !heapkeyspace indexes
347  * (those built before BTREE_VERSION 4), but we have no way of determining
348  * if this page came from a !heapkeyspace index. We may only have a bytea
349  * nbtree page image to go on, so in general there is no metapage that we
350  * can check.
351  *
352  * That's okay here because BTreeTupleIsPivot() can only return false for
353  * a !heapkeyspace pivot, never true for a !heapkeyspace non-pivot. Since
354  * heap TID isn't part of the keyspace in a !heapkeyspace index anyway,
355  * there cannot possibly be a pivot tuple heap TID representation that we
356  * fail to make an adjustment for. A !heapkeyspace index can have
357  * BTreeTupleIsPivot() return true (due to things like suffix truncation
358  * for INCLUDE indexes in Postgres v11), but when that happens
359  * BTreeTupleGetHeapTID() can be trusted to work reliably (i.e. return
360  * NULL).
361  *
362  * Note: BTreeTupleIsPosting() always works reliably, even with
363  * !heapkeyspace indexes.
364  */
365  if (BTreeTupleIsPosting(itup))
366  dlen -= IndexTupleSize(itup) - BTreeTupleGetPostingOffset(itup);
367  else if (BTreeTupleIsPivot(itup) && BTreeTupleGetHeapTID(itup) != NULL)
368  dlen -= MAXALIGN(sizeof(ItemPointerData));
369 
370  if (dlen < 0 || dlen > INDEX_SIZE_MASK)
371  elog(ERROR, "invalid tuple length %d for tuple at offset number %u",
372  dlen, offset);
373  dump = palloc0(dlen * 3 + 1);
374  datacstring = dump;
375  for (off = 0; off < dlen; off++)
376  {
377  if (off > 0)
378  *dump++ = ' ';
379  sprintf(dump, "%02x", *(ptr + off) & 0xff);
380  dump += 2;
381  }
382  values[j++] = CStringGetTextDatum(datacstring);
383  pfree(datacstring);
384 
385  /*
386  * We need to work around the BTreeTupleIsPivot() !heapkeyspace limitation
387  * again. Deduce whether or not tuple must be a pivot tuple based on
388  * whether or not the page is a leaf page, as well as the page offset
389  * number of the tuple.
390  */
391  ispivottuple = (!leafpage || (!rightmost && offset == P_HIKEY));
392 
393  /* LP_DEAD bit can never be set for pivot tuples, so show a NULL there */
394  if (!ispivottuple)
395  values[j++] = BoolGetDatum(ItemIdIsDead(id));
396  else
397  {
398  Assert(!ItemIdIsDead(id));
399  nulls[j++] = true;
400  }
401 
402  htid = BTreeTupleGetHeapTID(itup);
403  if (ispivottuple && !BTreeTupleIsPivot(itup))
404  {
405  /* Don't show bogus heap TID in !heapkeyspace pivot tuple */
406  htid = NULL;
407  }
408 
409  if (htid)
410  values[j++] = ItemPointerGetDatum(htid);
411  else
412  nulls[j++] = true;
413 
414  if (BTreeTupleIsPosting(itup))
415  {
416  /* Build an array of item pointers */
417  ItemPointer tids;
418  Datum *tids_datum;
419  int nposting;
420 
421  tids = BTreeTupleGetPosting(itup);
422  nposting = BTreeTupleGetNPosting(itup);
423  tids_datum = (Datum *) palloc(nposting * sizeof(Datum));
424  for (int i = 0; i < nposting; i++)
425  tids_datum[i] = ItemPointerGetDatum(&tids[i]);
426  values[j++] = PointerGetDatum(construct_array(tids_datum,
427  nposting,
428  TIDOID,
429  sizeof(ItemPointerData),
430  false, TYPALIGN_SHORT));
431  pfree(tids_datum);
432  }
433  else
434  nulls[j++] = true;
435 
436  /* Build and return the result tuple */
437  tuple = heap_form_tuple(uargs->tupd, values, nulls);
438 
439  return HeapTupleGetDatum(tuple);
440 }
#define IndexInfoFindDataOffset(t_info)
Definition: itup.h:80
static bool BTreeTupleIsPivot(IndexTuple itup)
Definition: nbtree.h:473
static ItemPointer BTreeTupleGetHeapTID(IndexTuple itup)
Definition: nbtree.h:631
#define PointerGetDatum(X)
Definition: postgres.h:600
static ItemPointer BTreeTupleGetPosting(IndexTuple posting)
Definition: nbtree.h:530
ItemPointerData t_tid
Definition: itup.h:37
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3319
#define INDEX_SIZE_MASK
Definition: itup.h:65
#define IndexTupleHasNulls(itup)
Definition: itup.h:72
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
uint16 OffsetNumber
Definition: off.h:24
#define sprintf
Definition: port.h:219
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
OffsetNumber offset
Definition: btreefuncs.c:291
IndexTupleData * IndexTuple
Definition: itup.h:53
#define DatumGetInt16(X)
Definition: postgres.h:488
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
Page page
Definition: btreefuncs.c:290
#define BoolGetDatum(X)
Definition: postgres.h:446
#define ItemIdIsValid(itemId)
Definition: itemid.h:86
static bool BTreeTupleIsPosting(IndexTuple itup)
Definition: nbtree.h:485
static uint32 BTreeTupleGetPostingOffset(IndexTuple posting)
Definition: nbtree.h:522
bool leafpage
Definition: btreefuncs.c:292
#define Assert(condition)
Definition: c.h:804
#define MAXALIGN(LEN)
Definition: c.h:757
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
#define IndexTupleHasVarwidths(itup)
Definition: itup.h:73
#define P_HIKEY
Definition: nbtree.h:367
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define Int32GetDatum(X)
Definition: postgres.h:523
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:86
static uint16 BTreeTupleGetNPosting(IndexTuple posting)
Definition: nbtree.h:511
TupleDesc tupd
Definition: btreefuncs.c:294
#define ItemPointerGetDatum(X)
Definition: btreefuncs.c:53
unsigned short t_info
Definition: itup.h:49
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define IndexTupleSize(itup)
Definition: itup.h:71
bool rightmost
Definition: btreefuncs.c:293

◆ bt_page_stats()

Datum bt_page_stats ( PG_FUNCTION_ARGS  )

Definition at line 279 of file btreefuncs.c.

References bt_page_stats_internal(), and PAGEINSPECT_V1_8.

280 {
282 }
static Datum bt_page_stats_internal(PG_FUNCTION_ARGS, enum pageinspect_version ext_version)
Definition: btreefuncs.c:186

◆ bt_page_stats_1_9()

Datum bt_page_stats_1_9 ( PG_FUNCTION_ARGS  )

Definition at line 272 of file btreefuncs.c.

References bt_page_stats_internal(), and PAGEINSPECT_V1_9.

273 {
275 }
static Datum bt_page_stats_internal(PG_FUNCTION_ARGS, enum pageinspect_version ext_version)
Definition: btreefuncs.c:186

◆ bt_page_stats_internal()

static Datum bt_page_stats_internal ( PG_FUNCTION_ARGS  ,
enum pageinspect_version  ext_version 
)
static

Definition at line 186 of file btreefuncs.c.

References AccessShareLock, BTPageStat::avg_item_size, BTPageStat::blkno, BTPageStat::btpo_flags, BTPageStat::btpo_level, 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::live_items, LockBuffer(), makeRangeVarFromNameList(), MaxBlockNumber, BTPageStat::page_size, PAGEINSPECT_V1_8, PG_GETARG_INT64, 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(), and values.

Referenced by bt_page_stats(), and bt_page_stats_1_9().

187 {
189  int64 blkno = (ext_version == PAGEINSPECT_V1_8 ? PG_GETARG_UINT32(1) : PG_GETARG_INT64(1));
190  Buffer buffer;
191  Relation rel;
192  RangeVar *relrv;
193  Datum result;
194  HeapTuple tuple;
195  TupleDesc tupleDesc;
196  int j;
197  char *values[11];
199 
200  if (!superuser())
201  ereport(ERROR,
202  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
203  errmsg("must be superuser to use pageinspect functions")));
204 
206  rel = relation_openrv(relrv, AccessShareLock);
207 
208  if (!IS_INDEX(rel) || !IS_BTREE(rel))
209  elog(ERROR, "relation \"%s\" is not a btree index",
211 
212  /*
213  * Reject attempts to read non-local temporary relations; we would be
214  * likely to get wrong data since we have no visibility into the owning
215  * session's local buffers.
216  */
217  if (RELATION_IS_OTHER_TEMP(rel))
218  ereport(ERROR,
219  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
220  errmsg("cannot access temporary tables of other sessions")));
221 
222  if (blkno < 0 || blkno > MaxBlockNumber)
223  ereport(ERROR,
224  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
225  errmsg("invalid block number")));
226 
227  if (blkno == 0)
228  ereport(ERROR,
229  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
230  errmsg("block 0 is a meta page")));
231 
232  CHECK_RELATION_BLOCK_RANGE(rel, blkno);
233 
234  buffer = ReadBuffer(rel, blkno);
235  LockBuffer(buffer, BUFFER_LOCK_SHARE);
236 
237  /* keep compiler quiet */
238  stat.btpo_prev = stat.btpo_next = InvalidBlockNumber;
239  stat.btpo_flags = stat.free_size = stat.avg_item_size = 0;
240 
241  GetBTPageStatistics(blkno, buffer, &stat);
242 
243  UnlockReleaseBuffer(buffer);
245 
246  /* Build a tuple descriptor for our result type */
247  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
248  elog(ERROR, "return type must be a row type");
249 
250  j = 0;
251  values[j++] = psprintf("%u", stat.blkno);
252  values[j++] = psprintf("%c", stat.type);
253  values[j++] = psprintf("%u", stat.live_items);
254  values[j++] = psprintf("%u", stat.dead_items);
255  values[j++] = psprintf("%u", stat.avg_item_size);
256  values[j++] = psprintf("%u", stat.page_size);
257  values[j++] = psprintf("%u", stat.free_size);
258  values[j++] = psprintf("%u", stat.btpo_prev);
259  values[j++] = psprintf("%u", stat.btpo_next);
260  values[j++] = psprintf("%u", stat.btpo_level);
261  values[j++] = psprintf("%d", stat.btpo_flags);
262 
264  values);
265 
266  result = HeapTupleGetDatum(tuple);
267 
268  PG_RETURN_DATUM(result);
269 }
uint32 avg_item_size
Definition: btreefuncs.c:72
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:270
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
uint32 dead_items
Definition: btreefuncs.c:68
#define CHECK_RELATION_BLOCK_RANGE(rel, blkno)
Definition: btreefuncs.c:56
uint32 btpo_level
Definition: btreefuncs.c:78
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define AccessShareLock
Definition: lockdefs.h:36
uint32 blkno
Definition: btreefuncs.c:66
int errcode(int sqlerrcode)
Definition: elog.c:698
#define IS_BTREE(r)
Definition: btreefuncs.c:51
bool superuser(void)
Definition: superuser.c:46
char type
Definition: btreefuncs.c:73
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3107
NameData relname
Definition: pg_class.h:38
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2146
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3791
#define ERROR
Definition: elog.h:46
#define MaxBlockNumber
Definition: block.h:35
BlockNumber btpo_next
Definition: btreefuncs.c:77
#define RelationGetRelationName(relation)
Definition: rel.h:511
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3688
uint32 live_items
Definition: btreefuncs.c:67
BlockNumber btpo_prev
Definition: btreefuncs.c:76
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
uint32 free_size
Definition: btreefuncs.c:71
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2097
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4007
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
#define ereport(elevel,...)
Definition: elog.h:157
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:631
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:694
#define InvalidBlockNumber
Definition: block.h:33
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
uint16 btpo_flags
Definition: btreefuncs.c:79
static void GetBTPageStatistics(BlockNumber blkno, Buffer buffer, BTPageStat *stat)
Definition: btreefuncs.c:91
static Datum values[MAXATTR]
Definition: bootstrap.c:156
int errmsg(const char *fmt,...)
Definition: elog.c:909
uint32 page_size
Definition: btreefuncs.c:69
#define elog(elevel,...)
Definition: elog.h:232
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
Definition: c.h:621
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
int Buffer
Definition: buf.h:23
#define stat
Definition: win32_port.h:283
#define IS_INDEX(r)
Definition: btreefuncs.c:50

◆ GetBTPageStatistics()

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

Definition at line 91 of file btreefuncs.c.

References BTPageStat::avg_item_size, BTPageStat::blkno, BTPageGetDeleteXid(), BTPageOpaqueData::btpo_cycleid, BTPageStat::btpo_cycleid, BTPageOpaqueData::btpo_flags, BTPageStat::btpo_flags, BTPageOpaqueData::btpo_level, BTPageStat::btpo_level, BTPageOpaqueData::btpo_next, BTPageStat::btpo_next, BTPageOpaqueData::btpo_prev, BTPageStat::btpo_prev, BufferGetPage, BTPageStat::dead_items, DEBUG2, elog, EpochFromFullTransactionId, FirstOffsetNumber, BTPageStat::free_size, IndexTupleSize, InvalidOffsetNumber, ItemIdIsDead, BTPageStat::live_items, BTPageStat::max_avail, P_HAS_FULLXID, P_IGNORE, P_ISDELETED, P_ISLEAF, P_ISROOT, BTPageStat::page_size, PageGetFreeSpace(), PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageGetPageSize, PageGetSpecialPointer, PageHeaderData::pd_special, SizeOfPageHeaderData, BTPageStat::type, and XidFromFullTransactionId.

Referenced by bt_page_stats_internal().

92 {
93  Page page = BufferGetPage(buffer);
94  PageHeader phdr = (PageHeader) page;
95  OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
97  int item_size = 0;
98  int off;
99 
100  stat->blkno = blkno;
101 
102  stat->max_avail = BLCKSZ - (BLCKSZ - phdr->pd_special + SizeOfPageHeaderData);
103 
104  stat->dead_items = stat->live_items = 0;
105 
106  stat->page_size = PageGetPageSize(page);
107 
108  /* page type (flags) */
109  if (P_ISDELETED(opaque))
110  {
111  /* We divide deleted pages into leaf ('d') or internal ('D') */
112  if (P_ISLEAF(opaque) || !P_HAS_FULLXID(opaque))
113  stat->type = 'd';
114  else
115  stat->type = 'D';
116 
117  /*
118  * Report safexid in a deleted page.
119  *
120  * Handle pg_upgrade'd deleted pages that used the previous safexid
121  * representation in btpo_level field (this used to be a union type
122  * called "bpto").
123  */
124  if (P_HAS_FULLXID(opaque))
125  {
126  FullTransactionId safexid = BTPageGetDeleteXid(page);
127 
128  elog(DEBUG2, "deleted page from block %u has safexid %u:%u",
129  blkno, EpochFromFullTransactionId(safexid),
130  XidFromFullTransactionId(safexid));
131  }
132  else
133  elog(DEBUG2, "deleted page from block %u has safexid %u",
134  blkno, opaque->btpo_level);
135 
136  /* Don't interpret BTDeletedPageData as index tuples */
137  maxoff = InvalidOffsetNumber;
138  }
139  else if (P_IGNORE(opaque))
140  stat->type = 'e';
141  else if (P_ISLEAF(opaque))
142  stat->type = 'l';
143  else if (P_ISROOT(opaque))
144  stat->type = 'r';
145  else
146  stat->type = 'i';
147 
148  /* btpage opaque data */
149  stat->btpo_prev = opaque->btpo_prev;
150  stat->btpo_next = opaque->btpo_next;
151  stat->btpo_level = opaque->btpo_level;
152  stat->btpo_flags = opaque->btpo_flags;
153  stat->btpo_cycleid = opaque->btpo_cycleid;
154 
155  /* count live and dead tuples, and free space */
156  for (off = FirstOffsetNumber; off <= maxoff; off++)
157  {
158  IndexTuple itup;
159 
160  ItemId id = PageGetItemId(page, off);
161 
162  itup = (IndexTuple) PageGetItem(page, id);
163 
164  item_size += IndexTupleSize(itup);
165 
166  if (!ItemIdIsDead(id))
167  stat->live_items++;
168  else
169  stat->dead_items++;
170  }
171  stat->free_size = PageGetFreeSpace(page);
172 
173  if ((stat->live_items + stat->dead_items) > 0)
174  stat->avg_item_size = item_size / (stat->live_items + stat->dead_items);
175  else
176  stat->avg_item_size = 0;
177 }
uint32 avg_item_size
Definition: btreefuncs.c:72
BlockNumber btpo_next
Definition: nbtree.h:65
uint32 dead_items
Definition: btreefuncs.c:68
#define P_IGNORE(opaque)
Definition: nbtree.h:224
uint32 btpo_level
Definition: btreefuncs.c:78
uint32 blkno
Definition: btreefuncs.c:66
char type
Definition: btreefuncs.c:73
#define SizeOfPageHeaderData
Definition: bufpage.h:216
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:71
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:900
#define XidFromFullTransactionId(x)
Definition: transam.h:48
#define P_HAS_FULLXID(opaque)
Definition: nbtree.h:227
uint16 OffsetNumber
Definition: off.h:24
BlockNumber btpo_next
Definition: btreefuncs.c:77
BTCycleId btpo_cycleid
Definition: nbtree.h:68
#define DEBUG2
Definition: elog.h:24
BlockNumber btpo_prev
Definition: nbtree.h:64
uint32 btpo_level
Definition: nbtree.h:66
#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:169
#define P_ISDELETED(opaque)
Definition: nbtree.h:221
#define P_ISROOT(opaque)
Definition: nbtree.h:220
uint32 live_items
Definition: btreefuncs.c:67
LocationIndex pd_special
Definition: bufpage.h:160
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
BlockNumber btpo_prev
Definition: btreefuncs.c:76
uint32 free_size
Definition: btreefuncs.c:71
Page page
Definition: btreefuncs.c:290
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define InvalidOffsetNumber
Definition: off.h:26
PageHeaderData * PageHeader
Definition: bufpage.h:166
uint32 max_avail
Definition: btreefuncs.c:70
static FullTransactionId BTPageGetDeleteXid(Page page)
Definition: nbtree.h:259
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
BTCycleId btpo_cycleid
Definition: btreefuncs.c:80
uint16 btpo_flags
Definition: btreefuncs.c:79
uint32 page_size
Definition: btreefuncs.c:69
#define elog(elevel,...)
Definition: elog.h:232
uint16 btpo_flags
Definition: nbtree.h:67
#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:219

◆ PG_FUNCTION_INFO_V1() [1/6]

PG_FUNCTION_INFO_V1 ( bt_metap  )

◆ PG_FUNCTION_INFO_V1() [2/6]

PG_FUNCTION_INFO_V1 ( bt_page_items_1_9  )

◆ PG_FUNCTION_INFO_V1() [3/6]

PG_FUNCTION_INFO_V1 ( bt_page_items  )

◆ PG_FUNCTION_INFO_V1() [4/6]

PG_FUNCTION_INFO_V1 ( bt_page_items_bytea  )

◆ PG_FUNCTION_INFO_V1() [5/6]

PG_FUNCTION_INFO_V1 ( bt_page_stats_1_9  )

◆ PG_FUNCTION_INFO_V1() [6/6]

PG_FUNCTION_INFO_V1 ( bt_page_stats  )