PostgreSQL Source Code  git master
pgstatindex.c File Reference
#include "postgres.h"
#include "access/gin_private.h"
#include "access/hash.h"
#include "access/htup_details.h"
#include "access/nbtree.h"
#include "access/relation.h"
#include "access/table.h"
#include "catalog/namespace.h"
#include "catalog/pg_am.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/varlena.h"
Include dependency graph for pgstatindex.c:

Go to the source code of this file.

Data Structures

struct  BTIndexStat
 
struct  GinIndexStat
 
struct  HashIndexStat
 

Macros

#define IS_INDEX(r)   ((r)->rd_rel->relkind == RELKIND_INDEX)
 
#define IS_BTREE(r)   ((r)->rd_rel->relam == BTREE_AM_OID)
 
#define IS_GIN(r)   ((r)->rd_rel->relam == GIN_AM_OID)
 
#define IS_HASH(r)   ((r)->rd_rel->relam == HASH_AM_OID)
 

Typedefs

typedef struct BTIndexStat BTIndexStat
 
typedef struct GinIndexStat GinIndexStat
 
typedef struct HashIndexStat HashIndexStat
 

Functions

 PG_FUNCTION_INFO_V1 (pgstatindex)
 
 PG_FUNCTION_INFO_V1 (pgstatindexbyid)
 
 PG_FUNCTION_INFO_V1 (pg_relpages)
 
 PG_FUNCTION_INFO_V1 (pg_relpagesbyid)
 
 PG_FUNCTION_INFO_V1 (pgstatginindex)
 
 PG_FUNCTION_INFO_V1 (pgstathashindex)
 
 PG_FUNCTION_INFO_V1 (pgstatindex_v1_5)
 
 PG_FUNCTION_INFO_V1 (pgstatindexbyid_v1_5)
 
 PG_FUNCTION_INFO_V1 (pg_relpages_v1_5)
 
 PG_FUNCTION_INFO_V1 (pg_relpagesbyid_v1_5)
 
 PG_FUNCTION_INFO_V1 (pgstatginindex_v1_5)
 
Datum pgstatginindex_internal (Oid relid, FunctionCallInfo fcinfo)
 
static Datum pgstatindex_impl (Relation rel, FunctionCallInfo fcinfo)
 
static int64 pg_relpages_impl (Relation rel)
 
static void GetHashPageStats (Page page, HashIndexStat *stats)
 
Datum pgstatindex (PG_FUNCTION_ARGS)
 
Datum pgstatindex_v1_5 (PG_FUNCTION_ARGS)
 
Datum pgstatindexbyid (PG_FUNCTION_ARGS)
 
Datum pgstatindexbyid_v1_5 (PG_FUNCTION_ARGS)
 
Datum pg_relpages (PG_FUNCTION_ARGS)
 
Datum pg_relpages_v1_5 (PG_FUNCTION_ARGS)
 
Datum pg_relpagesbyid (PG_FUNCTION_ARGS)
 
Datum pg_relpagesbyid_v1_5 (PG_FUNCTION_ARGS)
 
Datum pgstatginindex (PG_FUNCTION_ARGS)
 
Datum pgstatginindex_v1_5 (PG_FUNCTION_ARGS)
 
Datum pgstathashindex (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ IS_BTREE

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

Definition at line 71 of file pgstatindex.c.

◆ IS_GIN

#define IS_GIN (   r)    ((r)->rd_rel->relam == GIN_AM_OID)

Definition at line 72 of file pgstatindex.c.

◆ IS_HASH

#define IS_HASH (   r)    ((r)->rd_rel->relam == HASH_AM_OID)

Definition at line 73 of file pgstatindex.c.

◆ IS_INDEX

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

Definition at line 70 of file pgstatindex.c.

Typedef Documentation

◆ BTIndexStat

typedef struct BTIndexStat BTIndexStat

◆ GinIndexStat

typedef struct GinIndexStat GinIndexStat

◆ HashIndexStat

typedef struct HashIndexStat HashIndexStat

Function Documentation

◆ GetHashPageStats()

static void GetHashPageStats ( Page  page,
HashIndexStat stats 
)
static

Definition at line 745 of file pgstatindex.c.

746 {
747  OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
748  int off;
749 
750  /* count live and dead tuples, and free space */
751  for (off = FirstOffsetNumber; off <= maxoff; off++)
752  {
753  ItemId id = PageGetItemId(page, off);
754 
755  if (!ItemIdIsDead(id))
756  stats->live_items++;
757  else
758  stats->dead_items++;
759  }
760  stats->free_space += PageGetExactFreeSpace(page);
761 }
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:958
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:240
static OffsetNumber PageGetMaxOffsetNumber(Page page)
Definition: bufpage.h:369
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
uint64 free_space
Definition: pgstatindex.c:127
int64 dead_items
Definition: pgstatindex.c:126
int64 live_items
Definition: pgstatindex.c:125

References HashIndexStat::dead_items, FirstOffsetNumber, HashIndexStat::free_space, ItemIdIsDead, HashIndexStat::live_items, PageGetExactFreeSpace(), PageGetItemId(), and PageGetMaxOffsetNumber().

Referenced by pgstathashindex().

◆ PG_FUNCTION_INFO_V1() [1/11]

PG_FUNCTION_INFO_V1 ( pg_relpages  )

◆ PG_FUNCTION_INFO_V1() [2/11]

PG_FUNCTION_INFO_V1 ( pg_relpages_v1_5  )

◆ PG_FUNCTION_INFO_V1() [3/11]

PG_FUNCTION_INFO_V1 ( pg_relpagesbyid  )

◆ PG_FUNCTION_INFO_V1() [4/11]

PG_FUNCTION_INFO_V1 ( pg_relpagesbyid_v1_5  )

◆ PG_FUNCTION_INFO_V1() [5/11]

PG_FUNCTION_INFO_V1 ( pgstatginindex  )

◆ PG_FUNCTION_INFO_V1() [6/11]

PG_FUNCTION_INFO_V1 ( pgstatginindex_v1_5  )

◆ PG_FUNCTION_INFO_V1() [7/11]

PG_FUNCTION_INFO_V1 ( pgstathashindex  )

◆ PG_FUNCTION_INFO_V1() [8/11]

PG_FUNCTION_INFO_V1 ( pgstatindex  )

◆ PG_FUNCTION_INFO_V1() [9/11]

PG_FUNCTION_INFO_V1 ( pgstatindex_v1_5  )

◆ PG_FUNCTION_INFO_V1() [10/11]

PG_FUNCTION_INFO_V1 ( pgstatindexbyid  )

◆ PG_FUNCTION_INFO_V1() [11/11]

PG_FUNCTION_INFO_V1 ( pgstatindexbyid_v1_5  )

◆ pg_relpages()

Datum pg_relpages ( PG_FUNCTION_ARGS  )

Definition at line 395 of file pgstatindex.c.

396 {
398  Relation rel;
399  RangeVar *relrv;
400 
401  if (!superuser())
402  ereport(ERROR,
403  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
404  errmsg("must be superuser to use pgstattuple functions")));
405 
407  rel = relation_openrv(relrv, AccessShareLock);
408 
410 }
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(const List *names)
Definition: namespace.c:3539
NameData relname
Definition: pg_class.h:38
static int64 pg_relpages_impl(Relation rel)
Definition: pgstatindex.c:456
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:137
Definition: c.h:687
bool superuser(void)
Definition: superuser.c:46
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3399

References AccessShareLock, ereport, errcode(), errmsg(), ERROR, makeRangeVarFromNameList(), PG_GETARG_TEXT_PP, pg_relpages_impl(), PG_RETURN_INT64, relation_openrv(), relname, superuser(), and textToQualifiedNameList().

◆ pg_relpages_impl()

static int64 pg_relpages_impl ( Relation  rel)
static

Definition at line 456 of file pgstatindex.c.

457 {
458  int64 relpages;
459 
460  if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
461  ereport(ERROR,
462  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
463  errmsg("cannot get page count of relation \"%s\"",
465  errdetail_relkind_not_supported(rel->rd_rel->relkind)));
466 
467  /* note: this will work OK on non-local temp tables */
468 
469  relpages = RelationGetNumberOfBlocks(rel);
470 
472 
473  return relpages;
474 }
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:281
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
#define RelationGetRelationName(relation)
Definition: rel.h:539
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Form_pg_class rd_rel
Definition: rel.h:111

References AccessShareLock, ereport, errcode(), errdetail_relkind_not_supported(), errmsg(), ERROR, RelationData::rd_rel, relation_close(), RelationGetNumberOfBlocks, and RelationGetRelationName.

Referenced by pg_relpages(), pg_relpages_v1_5(), pg_relpagesbyid(), and pg_relpagesbyid_v1_5().

◆ pg_relpages_v1_5()

◆ pg_relpagesbyid()

Datum pg_relpagesbyid ( PG_FUNCTION_ARGS  )

Definition at line 428 of file pgstatindex.c.

429 {
430  Oid relid = PG_GETARG_OID(0);
431  Relation rel;
432 
433  if (!superuser())
434  ereport(ERROR,
435  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
436  errmsg("must be superuser to use pgstattuple functions")));
437 
438  rel = relation_open(relid, AccessShareLock);
439 
441 }
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
unsigned int Oid
Definition: postgres_ext.h:31
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47

References AccessShareLock, ereport, errcode(), errmsg(), ERROR, PG_GETARG_OID, pg_relpages_impl(), PG_RETURN_INT64, relation_open(), and superuser().

◆ pg_relpagesbyid_v1_5()

Datum pg_relpagesbyid_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 445 of file pgstatindex.c.

446 {
447  Oid relid = PG_GETARG_OID(0);
448  Relation rel;
449 
450  rel = relation_open(relid, AccessShareLock);
451 
453 }

References AccessShareLock, PG_GETARG_OID, pg_relpages_impl(), PG_RETURN_INT64, and relation_open().

◆ pgstatginindex()

Datum pgstatginindex ( PG_FUNCTION_ARGS  )

Definition at line 485 of file pgstatindex.c.

486 {
487  Oid relid = PG_GETARG_OID(0);
488 
489  if (!superuser())
490  ereport(ERROR,
491  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
492  errmsg("must be superuser to use pgstattuple functions")));
493 
495 }
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
Datum pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo)
Definition: pgstatindex.c:507

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_OID, PG_RETURN_DATUM, pgstatginindex_internal(), and superuser().

◆ pgstatginindex_internal()

Datum pgstatginindex_internal ( Oid  relid,
FunctionCallInfo  fcinfo 
)

Definition at line 507 of file pgstatindex.c.

508 {
509  Relation rel;
510  Buffer buffer;
511  Page page;
512  GinMetaPageData *metadata;
513  GinIndexStat stats;
514  HeapTuple tuple;
515  TupleDesc tupleDesc;
516  Datum values[3];
517  bool nulls[3] = {false, false, false};
518  Datum result;
519 
520  rel = relation_open(relid, AccessShareLock);
521 
522  if (!IS_INDEX(rel) || !IS_GIN(rel))
523  ereport(ERROR,
524  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
525  errmsg("relation \"%s\" is not a GIN index",
526  RelationGetRelationName(rel))));
527 
528  /*
529  * Reject attempts to read non-local temporary relations; we would be
530  * likely to get wrong data since we have no visibility into the owning
531  * session's local buffers.
532  */
533  if (RELATION_IS_OTHER_TEMP(rel))
534  ereport(ERROR,
535  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
536  errmsg("cannot access temporary indexes of other sessions")));
537 
538  /* see pgstatindex_impl */
539  if (!rel->rd_index->indisvalid)
540  ereport(ERROR,
541  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
542  errmsg("index \"%s\" is not valid",
543  RelationGetRelationName(rel))));
544 
545  /*
546  * Read metapage
547  */
548  buffer = ReadBuffer(rel, GIN_METAPAGE_BLKNO);
549  LockBuffer(buffer, GIN_SHARE);
550  page = BufferGetPage(buffer);
551  metadata = GinPageGetMeta(page);
552 
553  stats.version = metadata->ginVersion;
554  stats.pending_pages = metadata->nPendingPages;
555  stats.pending_tuples = metadata->nPendingHeapTuples;
556 
557  UnlockReleaseBuffer(buffer);
559 
560  /*
561  * Build a tuple descriptor for our result type
562  */
563  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
564  elog(ERROR, "return type must be a row type");
565 
566  values[0] = Int32GetDatum(stats.version);
567  values[1] = UInt32GetDatum(stats.pending_pages);
568  values[2] = Int64GetDatum(stats.pending_tuples);
569 
570  /*
571  * Build and return the tuple
572  */
573  tuple = heap_form_tuple(tupleDesc, values, nulls);
574  result = HeapTupleGetDatum(tuple);
575 
576  return result;
577 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
int Buffer
Definition: buf.h:23
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4867
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5085
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:745
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:408
Pointer Page
Definition: bufpage.h:78
#define elog(elevel,...)
Definition: elog.h:224
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define GIN_SHARE
Definition: gin_private.h:50
#define GIN_METAPAGE_BLKNO
Definition: ginblock.h:51
#define GinPageGetMeta(p)
Definition: ginblock.h:104
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
#define IS_INDEX(r)
Definition: pgstatindex.c:70
#define IS_GIN(r)
Definition: pgstatindex.c:72
uintptr_t Datum
Definition: postgres.h:64
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
static Datum UInt32GetDatum(uint32 X)
Definition: postgres.h:232
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:658
BlockNumber pending_pages
Definition: pgstatindex.c:106
int64 pending_tuples
Definition: pgstatindex.c:107
int32 ginVersion
Definition: ginblock.h:99
BlockNumber nPendingPages
Definition: ginblock.h:73
int64 nPendingHeapTuples
Definition: ginblock.h:74
Form_pg_index rd_index
Definition: rel.h:192

References AccessShareLock, BufferGetPage(), elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), GIN_METAPAGE_BLKNO, GIN_SHARE, GinPageGetMeta, GinMetaPageData::ginVersion, heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), Int64GetDatum(), IS_GIN, IS_INDEX, LockBuffer(), GinMetaPageData::nPendingHeapTuples, GinMetaPageData::nPendingPages, GinIndexStat::pending_pages, GinIndexStat::pending_tuples, RelationData::rd_index, ReadBuffer(), relation_close(), RELATION_IS_OTHER_TEMP, relation_open(), RelationGetRelationName, TYPEFUNC_COMPOSITE, UInt32GetDatum(), UnlockReleaseBuffer(), values, and GinIndexStat::version.

Referenced by pgstatginindex(), and pgstatginindex_v1_5().

◆ pgstatginindex_v1_5()

Datum pgstatginindex_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 499 of file pgstatindex.c.

500 {
501  Oid relid = PG_GETARG_OID(0);
502 
504 }

References PG_GETARG_OID, PG_RETURN_DATUM, and pgstatginindex_internal().

◆ pgstathashindex()

Datum pgstathashindex ( PG_FUNCTION_ARGS  )

Definition at line 586 of file pgstatindex.c.

587 {
588  Oid relid = PG_GETARG_OID(0);
589  BlockNumber nblocks;
590  BlockNumber blkno;
591  Relation rel;
592  HashIndexStat stats;
593  BufferAccessStrategy bstrategy;
594  HeapTuple tuple;
595  TupleDesc tupleDesc;
596  Datum values[8];
597  bool nulls[8] = {0};
598  Buffer metabuf;
599  HashMetaPage metap;
600  float8 free_percent;
601  uint64 total_space;
602 
603  rel = relation_open(relid, AccessShareLock);
604 
605  if (!IS_INDEX(rel) || !IS_HASH(rel))
606  ereport(ERROR,
607  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
608  errmsg("relation \"%s\" is not a hash index",
609  RelationGetRelationName(rel))));
610 
611  /*
612  * Reject attempts to read non-local temporary relations; we would be
613  * likely to get wrong data since we have no visibility into the owning
614  * session's local buffers.
615  */
616  if (RELATION_IS_OTHER_TEMP(rel))
617  ereport(ERROR,
618  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
619  errmsg("cannot access temporary indexes of other sessions")));
620 
621  /* see pgstatindex_impl */
622  if (!rel->rd_index->indisvalid)
623  ereport(ERROR,
624  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
625  errmsg("index \"%s\" is not valid",
626  RelationGetRelationName(rel))));
627 
628  /* Get the information we need from the metapage. */
629  memset(&stats, 0, sizeof(stats));
631  metap = HashPageGetMeta(BufferGetPage(metabuf));
632  stats.version = metap->hashm_version;
633  stats.space_per_page = metap->hashm_bsize;
634  _hash_relbuf(rel, metabuf);
635 
636  /* Get the current relation length */
637  nblocks = RelationGetNumberOfBlocks(rel);
638 
639  /* prepare access strategy for this index */
640  bstrategy = GetAccessStrategy(BAS_BULKREAD);
641 
642  /* Start from blkno 1 as 0th block is metapage */
643  for (blkno = 1; blkno < nblocks; blkno++)
644  {
645  Buffer buf;
646  Page page;
647 
649 
651  bstrategy);
653  page = (Page) BufferGetPage(buf);
654 
655  if (PageIsNew(page))
656  stats.unused_pages++;
657  else if (PageGetSpecialSize(page) !=
658  MAXALIGN(sizeof(HashPageOpaqueData)))
659  ereport(ERROR,
660  (errcode(ERRCODE_INDEX_CORRUPTED),
661  errmsg("index \"%s\" contains corrupted page at block %u",
664  else
665  {
666  HashPageOpaque opaque;
667  int pagetype;
668 
669  opaque = HashPageGetOpaque(page);
670  pagetype = opaque->hasho_flag & LH_PAGE_TYPE;
671 
672  if (pagetype == LH_BUCKET_PAGE)
673  {
674  stats.bucket_pages++;
675  GetHashPageStats(page, &stats);
676  }
677  else if (pagetype == LH_OVERFLOW_PAGE)
678  {
679  stats.overflow_pages++;
680  GetHashPageStats(page, &stats);
681  }
682  else if (pagetype == LH_BITMAP_PAGE)
683  stats.bitmap_pages++;
684  else if (pagetype == LH_UNUSED_PAGE)
685  stats.unused_pages++;
686  else
687  ereport(ERROR,
688  (errcode(ERRCODE_INDEX_CORRUPTED),
689  errmsg("unexpected page type 0x%04X in HASH index \"%s\" block %u",
690  opaque->hasho_flag, RelationGetRelationName(rel),
692  }
694  }
695 
696  /* Done accessing the index */
698 
699  /* Count unused pages as free space. */
700  stats.free_space += (uint64) stats.unused_pages * stats.space_per_page;
701 
702  /*
703  * Total space available for tuples excludes the metapage and the bitmap
704  * pages.
705  */
706  total_space = (uint64) (nblocks - (stats.bitmap_pages + 1)) *
707  stats.space_per_page;
708 
709  if (total_space == 0)
710  free_percent = 0.0;
711  else
712  free_percent = 100.0 * stats.free_space / total_space;
713 
714  /*
715  * Build a tuple descriptor for our result type
716  */
717  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
718  elog(ERROR, "return type must be a row type");
719 
720  tupleDesc = BlessTupleDesc(tupleDesc);
721 
722  /*
723  * Build and return the tuple
724  */
725  values[0] = Int32GetDatum(stats.version);
726  values[1] = Int64GetDatum((int64) stats.bucket_pages);
727  values[2] = Int64GetDatum((int64) stats.overflow_pages);
728  values[3] = Int64GetDatum((int64) stats.bitmap_pages);
729  values[4] = Int64GetDatum((int64) stats.unused_pages);
730  values[5] = Int64GetDatum(stats.live_items);
731  values[6] = Int64GetDatum(stats.dead_items);
732  values[7] = Float8GetDatum(free_percent);
733  tuple = heap_form_tuple(tupleDesc, values, nulls);
734 
736 }
uint32 BlockNumber
Definition: block.h:31
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3667
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:792
@ BAS_BULKREAD
Definition: bufmgr.h:36
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:198
@ RBM_NORMAL
Definition: bufmgr.h:45
static bool PageIsNew(Page page)
Definition: bufpage.h:230
static uint16 PageGetSpecialSize(Page page)
Definition: bufpage.h:313
#define MAXALIGN(LEN)
Definition: c.h:811
double float8
Definition: c.h:630
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2158
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1816
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:541
#define HashPageGetOpaque(page)
Definition: hash.h:88
#define LH_BUCKET_PAGE
Definition: hash.h:55
#define LH_UNUSED_PAGE
Definition: hash.h:53
#define LH_META_PAGE
Definition: hash.h:57
#define HashPageGetMeta(page)
Definition: hash.h:323
#define HASH_READ
Definition: hash.h:339
#define HASH_METAPAGE
Definition: hash.h:198
#define LH_PAGE_TYPE
Definition: hash.h:63
#define LH_BITMAP_PAGE
Definition: hash.h:56
#define LH_OVERFLOW_PAGE
Definition: hash.h:54
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:266
Buffer _hash_getbuf(Relation rel, BlockNumber blkno, int access, int flags)
Definition: hashpage.c:70
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
static char * buf
Definition: pg_test_fsync.c:73
#define IS_HASH(r)
Definition: pgstatindex.c:73
static void GetHashPageStats(Page page, HashIndexStat *stats)
Definition: pgstatindex.c:745
@ MAIN_FORKNUM
Definition: relpath.h:50
BlockNumber bitmap_pages
Definition: pgstatindex.c:122
BlockNumber overflow_pages
Definition: pgstatindex.c:121
BlockNumber bucket_pages
Definition: pgstatindex.c:120
BlockNumber unused_pages
Definition: pgstatindex.c:123
int32 space_per_page
Definition: pgstatindex.c:118
uint32 hashm_version
Definition: hash.h:247
uint16 hashm_bsize
Definition: hash.h:250
uint16 hasho_flag
Definition: hash.h:82

References _hash_getbuf(), _hash_relbuf(), AccessShareLock, BAS_BULKREAD, HashIndexStat::bitmap_pages, BlessTupleDesc(), HashIndexStat::bucket_pages, buf, BUFFER_LOCK_SHARE, BufferGetBlockNumber(), BufferGetPage(), CHECK_FOR_INTERRUPTS, HashIndexStat::dead_items, elog, ereport, errcode(), errmsg(), ERROR, Float8GetDatum(), HashIndexStat::free_space, get_call_result_type(), GetAccessStrategy(), GetHashPageStats(), HASH_METAPAGE, HASH_READ, HashMetaPageData::hashm_bsize, HashMetaPageData::hashm_version, HashPageOpaqueData::hasho_flag, HashPageGetMeta, HashPageGetOpaque, heap_form_tuple(), HeapTupleGetDatum(), if(), index_close(), Int32GetDatum(), Int64GetDatum(), IS_HASH, IS_INDEX, LH_BITMAP_PAGE, LH_BUCKET_PAGE, LH_META_PAGE, LH_OVERFLOW_PAGE, LH_PAGE_TYPE, LH_UNUSED_PAGE, HashIndexStat::live_items, LockBuffer(), MAIN_FORKNUM, MAXALIGN, HashIndexStat::overflow_pages, PageGetSpecialSize(), PageIsNew(), PG_GETARG_OID, PG_RETURN_DATUM, RBM_NORMAL, RelationData::rd_index, ReadBufferExtended(), RELATION_IS_OTHER_TEMP, relation_open(), RelationGetNumberOfBlocks, RelationGetRelationName, HashIndexStat::space_per_page, TYPEFUNC_COMPOSITE, UnlockReleaseBuffer(), HashIndexStat::unused_pages, values, and HashIndexStat::version.

◆ pgstatindex()

Datum pgstatindex ( PG_FUNCTION_ARGS  )

Definition at line 145 of file pgstatindex.c.

146 {
148  Relation rel;
149  RangeVar *relrv;
150 
151  if (!superuser())
152  ereport(ERROR,
153  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
154  errmsg("must be superuser to use pgstattuple functions")));
155 
157  rel = relation_openrv(relrv, AccessShareLock);
158 
159  PG_RETURN_DATUM(pgstatindex_impl(rel, fcinfo));
160 }
static Datum pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstatindex.c:216

References AccessShareLock, ereport, errcode(), errmsg(), ERROR, makeRangeVarFromNameList(), PG_GETARG_TEXT_PP, PG_RETURN_DATUM, pgstatindex_impl(), relation_openrv(), relname, superuser(), and textToQualifiedNameList().

◆ pgstatindex_impl()

static Datum pgstatindex_impl ( Relation  rel,
FunctionCallInfo  fcinfo 
)
static

Definition at line 216 of file pgstatindex.c.

217 {
218  Datum result;
219  BlockNumber nblocks;
220  BlockNumber blkno;
221  BTIndexStat indexStat;
223 
224  if (!IS_INDEX(rel) || !IS_BTREE(rel))
225  ereport(ERROR,
226  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
227  errmsg("relation \"%s\" is not a btree index",
228  RelationGetRelationName(rel))));
229 
230  /*
231  * Reject attempts to read non-local temporary relations; we would be
232  * likely to get wrong data since we have no visibility into the owning
233  * session's local buffers.
234  */
235  if (RELATION_IS_OTHER_TEMP(rel))
236  ereport(ERROR,
237  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
238  errmsg("cannot access temporary tables of other sessions")));
239 
240  /*
241  * A !indisready index could lead to ERRCODE_DATA_CORRUPTED later, so exit
242  * early. We're capable of assessing an indisready&&!indisvalid index,
243  * but the results could be confusing. For example, the index's size
244  * could be too low for a valid index of the table.
245  */
246  if (!rel->rd_index->indisvalid)
247  ereport(ERROR,
248  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
249  errmsg("index \"%s\" is not valid",
250  RelationGetRelationName(rel))));
251 
252  /*
253  * Read metapage
254  */
255  {
256  Buffer buffer = ReadBufferExtended(rel, MAIN_FORKNUM, 0, RBM_NORMAL, bstrategy);
257  Page page = BufferGetPage(buffer);
258  BTMetaPageData *metad = BTPageGetMeta(page);
259 
260  indexStat.version = metad->btm_version;
261  indexStat.level = metad->btm_level;
262  indexStat.root_blkno = metad->btm_root;
263 
264  ReleaseBuffer(buffer);
265  }
266 
267  /* -- init counters -- */
268  indexStat.internal_pages = 0;
269  indexStat.leaf_pages = 0;
270  indexStat.empty_pages = 0;
271  indexStat.deleted_pages = 0;
272 
273  indexStat.max_avail = 0;
274  indexStat.free_space = 0;
275 
276  indexStat.fragments = 0;
277 
278  /*
279  * Scan all blocks except the metapage
280  */
281  nblocks = RelationGetNumberOfBlocks(rel);
282 
283  for (blkno = 1; blkno < nblocks; blkno++)
284  {
285  Buffer buffer;
286  Page page;
287  BTPageOpaque opaque;
288 
290 
291  /* Read and lock buffer */
292  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
293  LockBuffer(buffer, BUFFER_LOCK_SHARE);
294 
295  page = BufferGetPage(buffer);
296  opaque = BTPageGetOpaque(page);
297 
298  /*
299  * Determine page type, and update totals.
300  *
301  * Note that we arbitrarily bucket deleted pages together without
302  * considering if they're leaf pages or internal pages.
303  */
304  if (P_ISDELETED(opaque))
305  indexStat.deleted_pages++;
306  else if (P_IGNORE(opaque))
307  indexStat.empty_pages++; /* this is the "half dead" state */
308  else if (P_ISLEAF(opaque))
309  {
310  int max_avail;
311 
312  max_avail = BLCKSZ - (BLCKSZ - ((PageHeader) page)->pd_special + SizeOfPageHeaderData);
313  indexStat.max_avail += max_avail;
314  indexStat.free_space += PageGetFreeSpace(page);
315 
316  indexStat.leaf_pages++;
317 
318  /*
319  * If the next leaf is on an earlier block, it means a
320  * fragmentation.
321  */
322  if (opaque->btpo_next != P_NONE && opaque->btpo_next < blkno)
323  indexStat.fragments++;
324  }
325  else
326  indexStat.internal_pages++;
327 
328  /* Unlock and release buffer */
330  ReleaseBuffer(buffer);
331  }
332 
334 
335  /*----------------------------
336  * Build a result tuple
337  *----------------------------
338  */
339  {
340  TupleDesc tupleDesc;
341  int j;
342  char *values[10];
343  HeapTuple tuple;
344 
345  /* Build a tuple descriptor for our result type */
346  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
347  elog(ERROR, "return type must be a row type");
348 
349  j = 0;
350  values[j++] = psprintf("%d", indexStat.version);
351  values[j++] = psprintf("%d", indexStat.level);
353  (1 + /* include the metapage in index_size */
354  indexStat.leaf_pages +
355  indexStat.internal_pages +
356  indexStat.deleted_pages +
357  indexStat.empty_pages) * BLCKSZ);
358  values[j++] = psprintf("%u", indexStat.root_blkno);
359  values[j++] = psprintf(INT64_FORMAT, indexStat.internal_pages);
360  values[j++] = psprintf(INT64_FORMAT, indexStat.leaf_pages);
361  values[j++] = psprintf(INT64_FORMAT, indexStat.empty_pages);
362  values[j++] = psprintf(INT64_FORMAT, indexStat.deleted_pages);
363  if (indexStat.max_avail > 0)
364  values[j++] = psprintf("%.2f",
365  100.0 - (double) indexStat.free_space / (double) indexStat.max_avail * 100.0);
366  else
367  values[j++] = pstrdup("NaN");
368  if (indexStat.leaf_pages > 0)
369  values[j++] = psprintf("%.2f",
370  (double) indexStat.fragments / (double) indexStat.leaf_pages * 100.0);
371  else
372  values[j++] = pstrdup("NaN");
373 
375  values);
376 
377  result = HeapTupleGetDatum(tuple);
378  }
379 
380  return result;
381 }
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4850
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:197
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:907
PageHeaderData * PageHeader
Definition: bufpage.h:170
#define SizeOfPageHeaderData
Definition: bufpage.h:213
#define INT64_FORMAT
Definition: c.h:548
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2222
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2173
int j
Definition: isn.c:74
char * pstrdup(const char *in)
Definition: mcxt.c:1695
#define BTPageGetMeta(p)
Definition: nbtree.h:121
#define P_ISLEAF(opaque)
Definition: nbtree.h:220
#define BTPageGetOpaque(page)
Definition: nbtree.h:73
#define P_ISDELETED(opaque)
Definition: nbtree.h:222
#define P_NONE
Definition: nbtree.h:212
#define P_IGNORE(opaque)
Definition: nbtree.h:225
#define IS_BTREE(r)
Definition: pgstatindex.c:71
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
uint64 max_avail
Definition: pgstatindex.c:91
uint32 level
Definition: pgstatindex.c:83
uint64 empty_pages
Definition: pgstatindex.c:88
uint64 deleted_pages
Definition: pgstatindex.c:89
uint64 internal_pages
Definition: pgstatindex.c:86
uint64 leaf_pages
Definition: pgstatindex.c:87
uint32 version
Definition: pgstatindex.c:82
uint64 fragments
Definition: pgstatindex.c:94
BlockNumber root_blkno
Definition: pgstatindex.c:84
uint64 free_space
Definition: pgstatindex.c:92
uint32 btm_level
Definition: nbtree.h:108
uint32 btm_version
Definition: nbtree.h:106
BlockNumber btm_root
Definition: nbtree.h:107
BlockNumber btpo_next
Definition: nbtree.h:65

References AccessShareLock, BAS_BULKREAD, BTMetaPageData::btm_level, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTPageGetMeta, BTPageGetOpaque, BTPageOpaqueData::btpo_next, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage(), BuildTupleFromCStrings(), CHECK_FOR_INTERRUPTS, BTIndexStat::deleted_pages, elog, BTIndexStat::empty_pages, ereport, errcode(), errmsg(), ERROR, BTIndexStat::fragments, BTIndexStat::free_space, get_call_result_type(), GetAccessStrategy(), HeapTupleGetDatum(), INT64_FORMAT, BTIndexStat::internal_pages, IS_BTREE, IS_INDEX, j, BTIndexStat::leaf_pages, BTIndexStat::level, LockBuffer(), MAIN_FORKNUM, BTIndexStat::max_avail, P_IGNORE, P_ISDELETED, P_ISLEAF, P_NONE, PageGetFreeSpace(), psprintf(), pstrdup(), RBM_NORMAL, RelationData::rd_index, ReadBufferExtended(), relation_close(), RELATION_IS_OTHER_TEMP, RelationGetNumberOfBlocks, RelationGetRelationName, ReleaseBuffer(), BTIndexStat::root_blkno, SizeOfPageHeaderData, TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, values, and BTIndexStat::version.

Referenced by pgstatindex(), pgstatindex_v1_5(), pgstatindexbyid(), and pgstatindexbyid_v1_5().

◆ pgstatindex_v1_5()

◆ pgstatindexbyid()

Datum pgstatindexbyid ( PG_FUNCTION_ARGS  )

Definition at line 188 of file pgstatindex.c.

189 {
190  Oid relid = PG_GETARG_OID(0);
191  Relation rel;
192 
193  if (!superuser())
194  ereport(ERROR,
195  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
196  errmsg("must be superuser to use pgstattuple functions")));
197 
198  rel = relation_open(relid, AccessShareLock);
199 
200  PG_RETURN_DATUM(pgstatindex_impl(rel, fcinfo));
201 }

References AccessShareLock, ereport, errcode(), errmsg(), ERROR, PG_GETARG_OID, PG_RETURN_DATUM, pgstatindex_impl(), relation_open(), and superuser().

◆ pgstatindexbyid_v1_5()

Datum pgstatindexbyid_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 205 of file pgstatindex.c.

206 {
207  Oid relid = PG_GETARG_OID(0);
208  Relation rel;
209 
210  rel = relation_open(relid, AccessShareLock);
211 
212  PG_RETURN_DATUM(pgstatindex_impl(rel, fcinfo));
213 }

References AccessShareLock, PG_GETARG_OID, PG_RETURN_DATUM, pgstatindex_impl(), and relation_open().