PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 "catalog/namespace.h"
#include "catalog/pg_am.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.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 68 of file pgstatindex.c.

◆ IS_GIN

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

Definition at line 69 of file pgstatindex.c.

◆ IS_HASH

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

Definition at line 70 of file pgstatindex.c.

◆ IS_INDEX

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

Definition at line 67 of file pgstatindex.c.

Typedef Documentation

◆ BTIndexStat

◆ GinIndexStat

◆ HashIndexStat

Function Documentation

◆ GetHashPageStats()

static void GetHashPageStats ( Page  page,
HashIndexStat stats 
)
static

Definition at line 750 of file pgstatindex.c.

751{
753 int off;
754
755 /* count live and dead tuples, and free space */
756 for (off = FirstOffsetNumber; off <= maxoff; off++)
757 {
758 ItemId id = PageGetItemId(page, off);
759
760 if (!ItemIdIsDead(id))
761 stats->live_items++;
762 else
763 stats->dead_items++;
764 }
765 stats->free_space += PageGetExactFreeSpace(page);
766}
Size PageGetExactFreeSpace(const PageData *page)
Definition bufpage.c:957
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition bufpage.h:243
static OffsetNumber PageGetMaxOffsetNumber(const PageData *page)
Definition bufpage.h:371
#define ItemIdIsDead(itemId)
Definition itemid.h:113
uint16 OffsetNumber
Definition off.h:24
#define FirstOffsetNumber
Definition off.h:27
uint64 free_space

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 392 of file pgstatindex.c.

393{
395 Relation rel;
397
398 if (!superuser())
401 errmsg("must be superuser to use pgstattuple functions")));
402
405
407}
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_RETURN_INT64(x)
Definition fmgr.h:370
#define AccessShareLock
Definition lockdefs.h:36
RangeVar * makeRangeVarFromNameList(const List *names)
Definition namespace.c:3624
NameData relname
Definition pg_class.h:38
static int64 pg_relpages_impl(Relation rel)
static int fb(int x)
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition relation.c:137
Definition c.h:706
bool superuser(void)
Definition superuser.c:46
List * textToQualifiedNameList(text *textval)
Definition varlena.c:2672

References AccessShareLock, ereport, errcode(), errmsg(), ERROR, fb(), 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 453 of file pgstatindex.c.

454{
455 int64 relpages;
456
457 if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
460 errmsg("cannot get page count of relation \"%s\"",
463
464 /* note: this will work OK on non-local temp tables */
465
466 relpages = RelationGetNumberOfBlocks(rel);
467
469
470 return relpages;
471}
#define RelationGetNumberOfBlocks(reln)
Definition bufmgr.h:307
int64_t int64
Definition c.h:543
int errdetail_relkind_not_supported(char relkind)
Definition pg_class.c:24
#define RelationGetRelationName(relation)
Definition rel.h:548
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, fb(), 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 425 of file pgstatindex.c.

426{
427 Oid relid = PG_GETARG_OID(0);
428 Relation rel;
429
430 if (!superuser())
433 errmsg("must be superuser to use pgstattuple functions")));
434
435 rel = relation_open(relid, AccessShareLock);
436
438}
#define PG_GETARG_OID(n)
Definition fmgr.h:275
unsigned int Oid
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:47

References AccessShareLock, ereport, errcode(), errmsg(), ERROR, fb(), 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 442 of file pgstatindex.c.

443{
444 Oid relid = PG_GETARG_OID(0);
445 Relation rel;
446
447 rel = relation_open(relid, AccessShareLock);
448
450}

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

◆ pgstatginindex()

Datum pgstatginindex ( PG_FUNCTION_ARGS  )

Definition at line 482 of file pgstatindex.c.

483{
484 Oid relid = PG_GETARG_OID(0);
485
486 if (!superuser())
489 errmsg("must be superuser to use pgstattuple functions")));
490
492}
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354
Datum pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo)

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

◆ pgstatginindex_internal()

Datum pgstatginindex_internal ( Oid  relid,
FunctionCallInfo  fcinfo 
)

Definition at line 504 of file pgstatindex.c.

505{
506 Relation rel;
507 Buffer buffer;
508 Page page;
509 GinMetaPageData *metadata;
510 GinIndexStat stats;
511 HeapTuple tuple;
513 Datum values[3];
514 bool nulls[3] = {false, false, false};
515 Datum result;
516
517 /*
518 * This uses relation_open() and not index_open(). The latter allows
519 * partitioned indexes, and these are forbidden here.
520 */
521 rel = relation_open(relid, AccessShareLock);
522
523 if (!IS_INDEX(rel) || !IS_GIN(rel))
526 errmsg("relation \"%s\" is not a GIN index",
528
529 /*
530 * Reject attempts to read non-local temporary relations; we would be
531 * likely to get wrong data since we have no visibility into the owning
532 * session's local buffers.
533 */
534 if (RELATION_IS_OTHER_TEMP(rel))
537 errmsg("cannot access temporary indexes of other sessions")));
538
539 /* see pgstatindex_impl */
540 if (!rel->rd_index->indisvalid)
543 errmsg("index \"%s\" is not valid",
545
546 /*
547 * Read metapage
548 */
549 buffer = ReadBuffer(rel, GIN_METAPAGE_BLKNO);
550 LockBuffer(buffer, GIN_SHARE);
551 page = BufferGetPage(buffer);
552 metadata = GinPageGetMeta(page);
553
554 stats.version = metadata->ginVersion;
555 stats.pending_pages = metadata->nPendingPages;
556 stats.pending_tuples = metadata->nPendingHeapTuples;
557
558 UnlockReleaseBuffer(buffer);
560
561 /*
562 * Build a tuple descriptor for our result type
563 */
565 elog(ERROR, "return type must be a row type");
566
567 values[0] = Int32GetDatum(stats.version);
570
571 /*
572 * Build and return the tuple
573 */
574 tuple = heap_form_tuple(tupleDesc, values, nulls);
575 result = HeapTupleGetDatum(tuple);
576
577 return result;
578}
static Datum values[MAXATTR]
Definition bootstrap.c:155
int Buffer
Definition buf.h:23
void UnlockReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5518
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition bufmgr.c:864
static Page BufferGetPage(Buffer buffer)
Definition bufmgr.h:466
static void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition bufmgr.h:328
PageData * Page
Definition bufpage.h:81
#define elog(elevel,...)
Definition elog.h:226
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:51
#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:1117
#define IS_INDEX(r)
Definition pgstatindex.c:67
#define IS_GIN(r)
Definition pgstatindex.c:69
static Datum Int64GetDatum(int64 X)
Definition postgres.h:423
uint64_t Datum
Definition postgres.h:70
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
static Datum UInt32GetDatum(uint32 X)
Definition postgres.h:242
#define RELATION_IS_OTHER_TEMP(relation)
Definition rel.h:667
BlockNumber pending_pages
int64 pending_tuples
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, fb(), 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 496 of file pgstatindex.c.

497{
498 Oid relid = PG_GETARG_OID(0);
499
501}

References PG_GETARG_OID, PG_RETURN_DATUM, and pgstatginindex_internal().

◆ pgstathashindex()

Datum pgstathashindex ( PG_FUNCTION_ARGS  )

Definition at line 587 of file pgstatindex.c.

588{
589 Oid relid = PG_GETARG_OID(0);
590 BlockNumber nblocks;
591 BlockNumber blkno;
592 Relation rel;
593 HashIndexStat stats;
594 BufferAccessStrategy bstrategy;
595 HeapTuple tuple;
597 Datum values[8];
598 bool nulls[8] = {0};
601 float8 free_percent;
603
604 /*
605 * This uses relation_open() and not index_open(). The latter allows
606 * partitioned indexes, and these are forbidden here.
607 */
608 rel = relation_open(relid, AccessShareLock);
609
610 if (!IS_INDEX(rel) || !IS_HASH(rel))
613 errmsg("relation \"%s\" is not a hash index",
615
616 /*
617 * Reject attempts to read non-local temporary relations; we would be
618 * likely to get wrong data since we have no visibility into the owning
619 * session's local buffers.
620 */
621 if (RELATION_IS_OTHER_TEMP(rel))
624 errmsg("cannot access temporary indexes of other sessions")));
625
626 /* see pgstatindex_impl */
627 if (!rel->rd_index->indisvalid)
630 errmsg("index \"%s\" is not valid",
632
633 /* Get the information we need from the metapage. */
634 memset(&stats, 0, sizeof(stats));
637 stats.version = metap->hashm_version;
638 stats.space_per_page = metap->hashm_bsize;
639 _hash_relbuf(rel, metabuf);
640
641 /* Get the current relation length */
642 nblocks = RelationGetNumberOfBlocks(rel);
643
644 /* prepare access strategy for this index */
645 bstrategy = GetAccessStrategy(BAS_BULKREAD);
646
647 /* Start from blkno 1 as 0th block is metapage */
648 for (blkno = 1; blkno < nblocks; blkno++)
649 {
650 Buffer buf;
651 Page page;
652
654
656 bstrategy);
658 page = BufferGetPage(buf);
659
660 if (PageIsNew(page))
661 stats.unused_pages++;
662 else if (PageGetSpecialSize(page) !=
666 errmsg("index \"%s\" contains corrupted page at block %u",
669 else
670 {
671 HashPageOpaque opaque;
672 int pagetype;
673
674 opaque = HashPageGetOpaque(page);
675 pagetype = opaque->hasho_flag & LH_PAGE_TYPE;
676
678 {
679 stats.bucket_pages++;
680 GetHashPageStats(page, &stats);
681 }
682 else if (pagetype == LH_OVERFLOW_PAGE)
683 {
684 stats.overflow_pages++;
685 GetHashPageStats(page, &stats);
686 }
687 else if (pagetype == LH_BITMAP_PAGE)
688 stats.bitmap_pages++;
689 else if (pagetype == LH_UNUSED_PAGE)
690 stats.unused_pages++;
691 else
694 errmsg("unexpected page type 0x%04X in HASH index \"%s\" block %u",
697 }
699 }
700
701 /* Done accessing the index */
703
704 /* Count unused pages as free space. */
705 stats.free_space += (uint64) stats.unused_pages * stats.space_per_page;
706
707 /*
708 * Total space available for tuples excludes the metapage and the bitmap
709 * pages.
710 */
711 total_space = (uint64) (nblocks - (stats.bitmap_pages + 1)) *
712 stats.space_per_page;
713
714 if (total_space == 0)
715 free_percent = 0.0;
716 else
717 free_percent = 100.0 * stats.free_space / total_space;
718
719 /*
720 * Build a tuple descriptor for our result type
721 */
723 elog(ERROR, "return type must be a row type");
724
726
727 /*
728 * Build and return the tuple
729 */
730 values[0] = Int32GetDatum(stats.version);
735 values[5] = Int64GetDatum(stats.live_items);
736 values[6] = Int64GetDatum(stats.dead_items);
737 values[7] = Float8GetDatum(free_percent);
738 tuple = heap_form_tuple(tupleDesc, values, nulls);
739
741}
uint32 BlockNumber
Definition block.h:31
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition bufmgr.c:4356
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition bufmgr.c:911
@ BAS_BULKREAD
Definition bufmgr.h:37
@ BUFFER_LOCK_SHARE
Definition bufmgr.h:210
@ RBM_NORMAL
Definition bufmgr.h:46
static uint16 PageGetSpecialSize(const PageData *page)
Definition bufpage.h:316
static bool PageIsNew(const PageData *page)
Definition bufpage.h:233
#define MAXALIGN(LEN)
Definition c.h:826
double float8
Definition c.h:644
uint64_t uint64
Definition c.h:547
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition freelist.c:461
#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
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define IS_HASH(r)
Definition pgstatindex.c:70
static void GetHashPageStats(Page page, HashIndexStat *stats)
static Datum Float8GetDatum(float8 X)
Definition postgres.h:512
@ MAIN_FORKNUM
Definition relpath.h:58
BlockNumber bitmap_pages
BlockNumber overflow_pages
BlockNumber bucket_pages
BlockNumber unused_pages
int32 space_per_page
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, fb(), Float8GetDatum(), HashIndexStat::free_space, get_call_result_type(), GetAccessStrategy(), GetHashPageStats(), HASH_METAPAGE, HASH_READ, HashPageOpaqueData::hasho_flag, HashPageGetMeta, HashPageGetOpaque, heap_form_tuple(), HeapTupleGetDatum(), 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_close(), 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 142 of file pgstatindex.c.

143{
145 Relation rel;
147
148 if (!superuser())
151 errmsg("must be superuser to use pgstattuple functions")));
152
155
156 PG_RETURN_DATUM(pgstatindex_impl(rel, fcinfo));
157}
static Datum pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)

References AccessShareLock, ereport, errcode(), errmsg(), ERROR, fb(), 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 213 of file pgstatindex.c.

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

References AccessShareLock, BAS_BULKREAD, BTPageGetMeta, BTPageGetOpaque, BTPageOpaqueData::btpo_next, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage(), BuildTupleFromCStrings(), CHECK_FOR_INTERRUPTS, elog, ereport, errcode(), errmsg(), ERROR, fb(), get_call_result_type(), GetAccessStrategy(), HeapTupleGetDatum(), INT64_FORMAT, IS_BTREE, IS_INDEX, j, LockBuffer(), MAIN_FORKNUM, P_IGNORE, P_ISDELETED, P_ISLEAF, P_NONE, PageGetExactFreeSpace(), psprintf(), pstrdup(), RBM_NORMAL, RelationData::rd_index, ReadBufferExtended(), relation_close(), RELATION_IS_OTHER_TEMP, RelationGetNumberOfBlocks, RelationGetRelationName, ReleaseBuffer(), SizeOfPageHeaderData, TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, and values.

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

◆ pgstatindex_v1_5()

◆ pgstatindexbyid()

Datum pgstatindexbyid ( PG_FUNCTION_ARGS  )

Definition at line 185 of file pgstatindex.c.

186{
187 Oid relid = PG_GETARG_OID(0);
188 Relation rel;
189
190 if (!superuser())
193 errmsg("must be superuser to use pgstattuple functions")));
194
195 rel = relation_open(relid, AccessShareLock);
196
197 PG_RETURN_DATUM(pgstatindex_impl(rel, fcinfo));
198}

References AccessShareLock, ereport, errcode(), errmsg(), ERROR, fb(), 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 202 of file pgstatindex.c.

203{
204 Oid relid = PG_GETARG_OID(0);
205 Relation rel;
206
207 rel = relation_open(relid, AccessShareLock);
208
209 PG_RETURN_DATUM(pgstatindex_impl(rel, fcinfo));
210}

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