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 void GetHashPageStats (Page page, HashIndexStat *stats)
 
static void check_relation_relkind (Relation rel)
 
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.

Referenced by pgstatindex_impl().

◆ IS_GIN

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

Definition at line 72 of file pgstatindex.c.

Referenced by pgstatginindex_internal().

◆ IS_HASH

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

Definition at line 73 of file pgstatindex.c.

Referenced by pgstathashindex().

◆ IS_INDEX

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

Definition at line 70 of file pgstatindex.c.

Referenced by pgstatginindex_internal(), and pgstatindex_impl().

Typedef Documentation

◆ BTIndexStat

typedef struct BTIndexStat BTIndexStat

◆ GinIndexStat

typedef struct GinIndexStat GinIndexStat

◆ HashIndexStat

typedef struct HashIndexStat HashIndexStat

Function Documentation

◆ check_relation_relkind()

static void check_relation_relkind ( Relation  rel)
static

Definition at line 759 of file pgstatindex.c.

References ereport, errcode(), errmsg(), ERROR, RelationData::rd_rel, and RelationGetRelationName.

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

760 {
761  if (rel->rd_rel->relkind != RELKIND_RELATION &&
762  rel->rd_rel->relkind != RELKIND_INDEX &&
763  rel->rd_rel->relkind != RELKIND_MATVIEW &&
764  rel->rd_rel->relkind != RELKIND_SEQUENCE &&
765  rel->rd_rel->relkind != RELKIND_TOASTVALUE)
766  ereport(ERROR,
767  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
768  errmsg("\"%s\" is not a table, index, materialized view, sequence, or TOAST table",
769  RelationGetRelationName(rel))));
770 }
int errcode(int sqlerrcode)
Definition: elog.c:570
Form_pg_class rd_rel
Definition: rel.h:83
#define ERROR
Definition: elog.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:453
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ GetHashPageStats()

static void GetHashPageStats ( Page  page,
HashIndexStat stats 
)
static

Definition at line 736 of file pgstatindex.c.

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

Referenced by pgstathashindex().

737 {
738  OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
739  int off;
740 
741  /* count live and dead tuples, and free space */
742  for (off = FirstOffsetNumber; off <= maxoff; off++)
743  {
744  ItemId id = PageGetItemId(page, off);
745 
746  if (!ItemIdIsDead(id))
747  stats->live_items++;
748  else
749  stats->dead_items++;
750  }
751  stats->free_space += PageGetExactFreeSpace(page);
752 }
uint64 free_space
Definition: pgstatindex.c:127
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
uint16 OffsetNumber
Definition: off.h:24
int64 dead_items
Definition: pgstatindex.c:126
#define FirstOffsetNumber
Definition: off.h:27
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:632
int64 live_items
Definition: pgstatindex.c:125

◆ PG_FUNCTION_INFO_V1() [1/11]

PG_FUNCTION_INFO_V1 ( pgstatindex  )

◆ PG_FUNCTION_INFO_V1() [2/11]

PG_FUNCTION_INFO_V1 ( pgstatindexbyid  )

◆ PG_FUNCTION_INFO_V1() [3/11]

PG_FUNCTION_INFO_V1 ( pg_relpages  )

◆ PG_FUNCTION_INFO_V1() [4/11]

PG_FUNCTION_INFO_V1 ( pg_relpagesbyid  )

◆ PG_FUNCTION_INFO_V1() [5/11]

PG_FUNCTION_INFO_V1 ( pgstatginindex  )

◆ PG_FUNCTION_INFO_V1() [6/11]

PG_FUNCTION_INFO_V1 ( pgstathashindex  )

◆ PG_FUNCTION_INFO_V1() [7/11]

PG_FUNCTION_INFO_V1 ( pgstatindex_v1_5  )

◆ PG_FUNCTION_INFO_V1() [8/11]

PG_FUNCTION_INFO_V1 ( pgstatindexbyid_v1_5  )

◆ PG_FUNCTION_INFO_V1() [9/11]

PG_FUNCTION_INFO_V1 ( pg_relpages_v1_5  )

◆ PG_FUNCTION_INFO_V1() [10/11]

PG_FUNCTION_INFO_V1 ( pg_relpagesbyid_v1_5  )

◆ PG_FUNCTION_INFO_V1() [11/11]

PG_FUNCTION_INFO_V1 ( pgstatginindex_v1_5  )

◆ pg_relpages()

Datum pg_relpages ( PG_FUNCTION_ARGS  )

Definition at line 379 of file pgstatindex.c.

References AccessShareLock, check_relation_relkind(), ereport, errcode(), errmsg(), ERROR, makeRangeVarFromNameList(), PG_GETARG_TEXT_PP, PG_RETURN_INT64, relation_close(), relation_openrv(), RelationGetNumberOfBlocks, relname, relpages, superuser(), and textToQualifiedNameList().

380 {
382  int64 relpages;
383  Relation rel;
384  RangeVar *relrv;
385 
386  if (!superuser())
387  ereport(ERROR,
388  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
389  (errmsg("must be superuser to use pgstattuple functions"))));
390 
392  rel = relation_openrv(relrv, AccessShareLock);
393 
394  /* only some relkinds have storage */
396 
397  /* note: this will work OK on non-local temp tables */
398 
399  relpages = RelationGetNumberOfBlocks(rel);
400 
402 
403  PG_RETURN_INT64(relpages);
404 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
#define AccessShareLock
Definition: lockdefs.h:36
static void check_relation_relkind(Relation rel)
Definition: pgstatindex.c:759
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3054
NameData relname
Definition: pg_class.h:35
int32 relpages
Definition: pg_class.h:60
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3586
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:198
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549

◆ pg_relpages_v1_5()

Datum pg_relpages_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 408 of file pgstatindex.c.

References AccessShareLock, check_relation_relkind(), makeRangeVarFromNameList(), PG_GETARG_TEXT_PP, PG_RETURN_INT64, relation_close(), relation_openrv(), RelationGetNumberOfBlocks, relname, relpages, and textToQualifiedNameList().

409 {
411  int64 relpages;
412  Relation rel;
413  RangeVar *relrv;
414 
416  rel = relation_openrv(relrv, AccessShareLock);
417 
418  /* only some relkinds have storage */
420 
421  /* note: this will work OK on non-local temp tables */
422 
423  relpages = RelationGetNumberOfBlocks(rel);
424 
426 
427  PG_RETURN_INT64(relpages);
428 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
#define AccessShareLock
Definition: lockdefs.h:36
static void check_relation_relkind(Relation rel)
Definition: pgstatindex.c:759
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3054
NameData relname
Definition: pg_class.h:35
int32 relpages
Definition: pg_class.h:60
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3586
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:198
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
Definition: c.h:549

◆ pg_relpagesbyid()

Datum pg_relpagesbyid ( PG_FUNCTION_ARGS  )

Definition at line 432 of file pgstatindex.c.

References AccessShareLock, check_relation_relkind(), ereport, errcode(), errmsg(), ERROR, PG_GETARG_OID, PG_RETURN_INT64, relation_close(), relation_open(), RelationGetNumberOfBlocks, relpages, and superuser().

433 {
434  Oid relid = PG_GETARG_OID(0);
435  int64 relpages;
436  Relation rel;
437 
438  if (!superuser())
439  ereport(ERROR,
440  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
441  (errmsg("must be superuser to use pgstattuple functions"))));
442 
443  rel = relation_open(relid, AccessShareLock);
444 
445  /* only some relkinds have storage */
447 
448  /* note: this will work OK on non-local temp tables */
449 
450  relpages = RelationGetNumberOfBlocks(rel);
451 
453 
454  PG_RETURN_INT64(relpages);
455 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
#define AccessShareLock
Definition: lockdefs.h:36
static void check_relation_relkind(Relation rel)
Definition: pgstatindex.c:759
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
unsigned int Oid
Definition: postgres_ext.h:31
int32 relpages
Definition: pg_class.h:60
#define ERROR
Definition: elog.h:43
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define ereport(elevel, rest)
Definition: elog.h:141
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:198
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ pg_relpagesbyid_v1_5()

Datum pg_relpagesbyid_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 459 of file pgstatindex.c.

References AccessShareLock, check_relation_relkind(), PG_GETARG_OID, PG_RETURN_INT64, relation_close(), relation_open(), RelationGetNumberOfBlocks, and relpages.

460 {
461  Oid relid = PG_GETARG_OID(0);
462  int64 relpages;
463  Relation rel;
464 
465  rel = relation_open(relid, AccessShareLock);
466 
467  /* only some relkinds have storage */
469 
470  /* note: this will work OK on non-local temp tables */
471 
472  relpages = RelationGetNumberOfBlocks(rel);
473 
475 
476  PG_RETURN_INT64(relpages);
477 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
#define AccessShareLock
Definition: lockdefs.h:36
static void check_relation_relkind(Relation rel)
Definition: pgstatindex.c:759
unsigned int Oid
Definition: postgres_ext.h:31
int32 relpages
Definition: pg_class.h:60
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:198
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206

◆ pgstatginindex()

Datum pgstatginindex ( PG_FUNCTION_ARGS  )

Definition at line 488 of file pgstatindex.c.

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

489 {
490  Oid relid = PG_GETARG_OID(0);
491 
492  if (!superuser())
493  ereport(ERROR,
494  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
495  (errmsg("must be superuser to use pgstattuple functions"))));
496 
498 }
Datum pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo)
Definition: pgstatindex.c:510
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ pgstatginindex_internal()

Datum pgstatginindex_internal ( Oid  relid,
FunctionCallInfo  fcinfo 
)

Definition at line 510 of file pgstatindex.c.

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, 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().

511 {
512  Relation rel;
513  Buffer buffer;
514  Page page;
515  GinMetaPageData *metadata;
516  GinIndexStat stats;
517  HeapTuple tuple;
518  TupleDesc tupleDesc;
519  Datum values[3];
520  bool nulls[3] = {false, false, false};
521  Datum result;
522 
523  rel = relation_open(relid, AccessShareLock);
524 
525  if (!IS_INDEX(rel) || !IS_GIN(rel))
526  ereport(ERROR,
527  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
528  errmsg("relation \"%s\" is not a GIN index",
529  RelationGetRelationName(rel))));
530 
531  /*
532  * Reject attempts to read non-local temporary relations; we would be
533  * likely to get wrong data since we have no visibility into the owning
534  * session's local buffers.
535  */
536  if (RELATION_IS_OTHER_TEMP(rel))
537  ereport(ERROR,
538  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
539  errmsg("cannot access temporary indexes of other sessions")));
540 
541  /*
542  * Read metapage
543  */
544  buffer = ReadBuffer(rel, GIN_METAPAGE_BLKNO);
545  LockBuffer(buffer, GIN_SHARE);
546  page = BufferGetPage(buffer);
547  metadata = GinPageGetMeta(page);
548 
549  stats.version = metadata->ginVersion;
550  stats.pending_pages = metadata->nPendingPages;
551  stats.pending_tuples = metadata->nPendingHeapTuples;
552 
553  UnlockReleaseBuffer(buffer);
555 
556  /*
557  * Build a tuple descriptor for our result type
558  */
559  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
560  elog(ERROR, "return type must be a row type");
561 
562  values[0] = Int32GetDatum(stats.version);
563  values[1] = UInt32GetDatum(stats.pending_pages);
564  values[2] = Int64GetDatum(stats.pending_tuples);
565 
566  /*
567  * Build and return the tuple
568  */
569  tuple = heap_form_tuple(tupleDesc, values, nulls);
570  result = HeapTupleGetDatum(tuple);
571 
572  return result;
573 }
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
int64 pending_tuples
Definition: pgstatindex.c:107
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:570
#define GIN_METAPAGE_BLKNO
Definition: ginblock.h:51
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define IS_INDEX(r)
Definition: pgstatindex.c:70
int64 nPendingHeapTuples
Definition: ginblock.h:74
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
#define ERROR
Definition: elog.h:43
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
#define RelationGetRelationName(relation)
Definition: rel.h:453
#define UInt32GetDatum(X)
Definition: postgres.h:493
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1699
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define ereport(elevel, rest)
Definition: elog.h:141
#define GIN_SHARE
Definition: gin_private.h:44
BlockNumber pending_pages
Definition: pgstatindex.c:106
#define IS_GIN(r)
Definition: pgstatindex.c:72
uintptr_t Datum
Definition: postgres.h:367
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:549
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:596
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
int32 ginVersion
Definition: ginblock.h:99
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define GinPageGetMeta(p)
Definition: ginblock.h:104
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78
BlockNumber nPendingPages
Definition: ginblock.h:73

◆ pgstatginindex_v1_5()

Datum pgstatginindex_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 502 of file pgstatindex.c.

References PG_GETARG_OID, PG_RETURN_DATUM, and pgstatginindex_internal().

503 {
504  Oid relid = PG_GETARG_OID(0);
505 
507 }
Datum pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo)
Definition: pgstatindex.c:510
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343

◆ pgstathashindex()

Datum pgstathashindex ( PG_FUNCTION_ARGS  )

Definition at line 582 of file pgstatindex.c.

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, heap_form_tuple(), HeapTupleGetDatum, index_close(), index_open(), Int32GetDatum, Int64GetDatum(), IS_HASH, 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, MemSet, HashIndexStat::overflow_pages, PageGetSpecialPointer, PageGetSpecialSize, PageIsNew, PG_GETARG_OID, PG_RETURN_DATUM, RBM_NORMAL, ReadBufferExtended(), RELATION_IS_OTHER_TEMP, RelationGetNumberOfBlocks, RelationGetRelationName, HashIndexStat::space_per_page, TYPEFUNC_COMPOSITE, UnlockReleaseBuffer(), HashIndexStat::unused_pages, values, and HashIndexStat::version.

583 {
584  Oid relid = PG_GETARG_OID(0);
585  BlockNumber nblocks;
586  BlockNumber blkno;
587  Relation rel;
588  HashIndexStat stats;
589  BufferAccessStrategy bstrategy;
590  HeapTuple tuple;
591  TupleDesc tupleDesc;
592  Datum values[8];
593  bool nulls[8];
594  Buffer metabuf;
595  HashMetaPage metap;
596  float8 free_percent;
597  uint64 total_space;
598 
599  rel = index_open(relid, AccessShareLock);
600 
601  /* index_open() checks that it's an index */
602  if (!IS_HASH(rel))
603  ereport(ERROR,
604  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
605  errmsg("relation \"%s\" is not a hash index",
606  RelationGetRelationName(rel))));
607 
608  /*
609  * Reject attempts to read non-local temporary relations; we would be
610  * likely to get wrong data since we have no visibility into the owning
611  * session's local buffers.
612  */
613  if (RELATION_IS_OTHER_TEMP(rel))
614  ereport(ERROR,
615  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
616  errmsg("cannot access temporary indexes of other sessions")));
617 
618  /* Get the information we need from the metapage. */
619  memset(&stats, 0, sizeof(stats));
621  metap = HashPageGetMeta(BufferGetPage(metabuf));
622  stats.version = metap->hashm_version;
623  stats.space_per_page = metap->hashm_bsize;
624  _hash_relbuf(rel, metabuf);
625 
626  /* Get the current relation length */
627  nblocks = RelationGetNumberOfBlocks(rel);
628 
629  /* prepare access strategy for this index */
630  bstrategy = GetAccessStrategy(BAS_BULKREAD);
631 
632  /* Start from blkno 1 as 0th block is metapage */
633  for (blkno = 1; blkno < nblocks; blkno++)
634  {
635  Buffer buf;
636  Page page;
637 
639 
640  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL,
641  bstrategy);
643  page = (Page) BufferGetPage(buf);
644 
645  if (PageIsNew(page))
646  stats.unused_pages++;
647  else if (PageGetSpecialSize(page) !=
648  MAXALIGN(sizeof(HashPageOpaqueData)))
649  ereport(ERROR,
650  (errcode(ERRCODE_INDEX_CORRUPTED),
651  errmsg("index \"%s\" contains corrupted page at block %u",
653  BufferGetBlockNumber(buf))));
654  else
655  {
656  HashPageOpaque opaque;
657  int pagetype;
658 
659  opaque = (HashPageOpaque) PageGetSpecialPointer(page);
660  pagetype = opaque->hasho_flag & LH_PAGE_TYPE;
661 
662  if (pagetype == LH_BUCKET_PAGE)
663  {
664  stats.bucket_pages++;
665  GetHashPageStats(page, &stats);
666  }
667  else if (pagetype == LH_OVERFLOW_PAGE)
668  {
669  stats.overflow_pages++;
670  GetHashPageStats(page, &stats);
671  }
672  else if (pagetype == LH_BITMAP_PAGE)
673  stats.bitmap_pages++;
674  else if (pagetype == LH_UNUSED_PAGE)
675  stats.unused_pages++;
676  else
677  ereport(ERROR,
678  (errcode(ERRCODE_INDEX_CORRUPTED),
679  errmsg("unexpected page type 0x%04X in HASH index \"%s\" block %u",
680  opaque->hasho_flag, RelationGetRelationName(rel),
681  BufferGetBlockNumber(buf))));
682  }
683  UnlockReleaseBuffer(buf);
684  }
685 
686  /* Done accessing the index */
688 
689  /* Count unused pages as free space. */
690  stats.free_space += (uint64) stats.unused_pages * stats.space_per_page;
691 
692  /*
693  * Total space available for tuples excludes the metapage and the bitmap
694  * pages.
695  */
696  total_space = (uint64) (nblocks - (stats.bitmap_pages + 1)) *
697  stats.space_per_page;
698 
699  if (total_space == 0)
700  free_percent = 0.0;
701  else
702  free_percent = 100.0 * stats.free_space / total_space;
703 
704  /*
705  * Build a tuple descriptor for our result type
706  */
707  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
708  elog(ERROR, "return type must be a row type");
709 
710  tupleDesc = BlessTupleDesc(tupleDesc);
711 
712  /*
713  * Build and return the tuple
714  */
715  MemSet(nulls, 0, sizeof(nulls));
716  values[0] = Int32GetDatum(stats.version);
717  values[1] = Int64GetDatum((int64) stats.bucket_pages);
718  values[2] = Int64GetDatum((int64) stats.overflow_pages);
719  values[3] = Int64GetDatum((int64) stats.bitmap_pages);
720  values[4] = Int64GetDatum((int64) stats.unused_pages);
721  values[5] = Int64GetDatum(stats.live_items);
722  values[6] = Int64GetDatum(stats.dead_items);
723  values[7] = Float8GetDatum(free_percent);
724  tuple = heap_form_tuple(tupleDesc, values, nulls);
725 
727 }
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:542
BlockNumber overflow_pages
Definition: pgstatindex.c:121
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
#define LH_BITMAP_PAGE
Definition: hash.h:55
#define LH_META_PAGE
Definition: hash.h:56
BlockNumber bitmap_pages
Definition: pgstatindex.c:122
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:570
#define MemSet(start, val, len)
Definition: c.h:955
uint32 BlockNumber
Definition: block.h:31
uint64 free_space
Definition: pgstatindex.c:127
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
Buffer _hash_getbuf(Relation rel, BlockNumber blkno, int access, int flags)
Definition: hashpage.c:69
unsigned int Oid
Definition: postgres_ext.h:31
BlockNumber unused_pages
Definition: pgstatindex.c:123
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1723
int32 space_per_page
Definition: pgstatindex.c:118
#define LH_UNUSED_PAGE
Definition: hash.h:52
#define LH_PAGE_TYPE
Definition: hash.h:62
#define HASH_READ
Definition: hash.h:321
int64 dead_items
Definition: pgstatindex.c:126
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:491
static void GetHashPageStats(Page page, HashIndexStat *stats)
Definition: pgstatindex.c:736
uint32 hashm_version
Definition: hash.h:244
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2048
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define RelationGetRelationName(relation)
Definition: rel.h:453
#define IS_HASH(r)
Definition: pgstatindex.c:73
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1699
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define ereport(elevel, rest)
Definition: elog.h:141
uint16 hashm_bsize
Definition: hash.h:247
#define HASH_METAPAGE
Definition: hash.h:195
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define LH_OVERFLOW_PAGE
Definition: hash.h:53
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:198
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:265
#define LH_BUCKET_PAGE
Definition: hash.h:54
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:549
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:85
#define MAXALIGN(LEN)
Definition: c.h:685
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
#define PageGetSpecialSize(page)
Definition: bufpage.h:300
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:152
uint16 hasho_flag
Definition: hash.h:81
static Datum values[MAXATTR]
Definition: bootstrap.c:167
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
#define Int32GetDatum(X)
Definition: postgres.h:479
#define PageIsNew(page)
Definition: bufpage.h:229
#define HashPageGetMeta(page)
Definition: hash.h:305
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
int Buffer
Definition: buf.h:23
BlockNumber bucket_pages
Definition: pgstatindex.c:120
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:126
Pointer Page
Definition: bufpage.h:78
int64 live_items
Definition: pgstatindex.c:125

◆ pgstatindex()

Datum pgstatindex ( PG_FUNCTION_ARGS  )

Definition at line 145 of file pgstatindex.c.

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

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 }
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3054
NameData relname
Definition: pg_class.h:35
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
static Datum pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstatindex.c:216
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3586
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549

◆ pgstatindex_impl()

static Datum pgstatindex_impl ( Relation  rel,
FunctionCallInfo  fcinfo 
)
static

Definition at line 216 of file pgstatindex.c.

References AccessShareLock, BAS_BULKREAD, BTMetaPageData::btm_level, BTMetaPageData::btm_root, BTMetaPageData::btm_version, BTPageGetMeta, 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, BTIndexStat::leaf_pages, BTIndexStat::level, LockBuffer(), MAIN_FORKNUM, BTIndexStat::max_avail, P_IGNORE, P_ISDELETED, P_ISLEAF, P_NONE, PageGetFreeSpace(), PageGetSpecialPointer, psprintf(), pstrdup(), RBM_NORMAL, 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().

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  * Read metapage
242  */
243  {
244  Buffer buffer = ReadBufferExtended(rel, MAIN_FORKNUM, 0, RBM_NORMAL, bstrategy);
245  Page page = BufferGetPage(buffer);
246  BTMetaPageData *metad = BTPageGetMeta(page);
247 
248  indexStat.version = metad->btm_version;
249  indexStat.level = metad->btm_level;
250  indexStat.root_blkno = metad->btm_root;
251 
252  ReleaseBuffer(buffer);
253  }
254 
255  /* -- init counters -- */
256  indexStat.internal_pages = 0;
257  indexStat.leaf_pages = 0;
258  indexStat.empty_pages = 0;
259  indexStat.deleted_pages = 0;
260 
261  indexStat.max_avail = 0;
262  indexStat.free_space = 0;
263 
264  indexStat.fragments = 0;
265 
266  /*
267  * Scan all blocks except the metapage
268  */
269  nblocks = RelationGetNumberOfBlocks(rel);
270 
271  for (blkno = 1; blkno < nblocks; blkno++)
272  {
273  Buffer buffer;
274  Page page;
275  BTPageOpaque opaque;
276 
278 
279  /* Read and lock buffer */
280  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
281  LockBuffer(buffer, BUFFER_LOCK_SHARE);
282 
283  page = BufferGetPage(buffer);
284  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
285 
286  /* Determine page type, and update totals */
287 
288  if (P_ISDELETED(opaque))
289  indexStat.deleted_pages++;
290  else if (P_IGNORE(opaque))
291  indexStat.empty_pages++; /* this is the "half dead" state */
292  else if (P_ISLEAF(opaque))
293  {
294  int max_avail;
295 
296  max_avail = BLCKSZ - (BLCKSZ - ((PageHeader) page)->pd_special + SizeOfPageHeaderData);
297  indexStat.max_avail += max_avail;
298  indexStat.free_space += PageGetFreeSpace(page);
299 
300  indexStat.leaf_pages++;
301 
302  /*
303  * If the next leaf is on an earlier block, it means a
304  * fragmentation.
305  */
306  if (opaque->btpo_next != P_NONE && opaque->btpo_next < blkno)
307  indexStat.fragments++;
308  }
309  else
310  indexStat.internal_pages++;
311 
312  /* Unlock and release buffer */
314  ReleaseBuffer(buffer);
315  }
316 
318 
319  /*----------------------------
320  * Build a result tuple
321  *----------------------------
322  */
323  {
324  TupleDesc tupleDesc;
325  int j;
326  char *values[10];
327  HeapTuple tuple;
328 
329  /* Build a tuple descriptor for our result type */
330  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
331  elog(ERROR, "return type must be a row type");
332 
333  j = 0;
334  values[j++] = psprintf("%d", indexStat.version);
335  values[j++] = psprintf("%d", indexStat.level);
336  values[j++] = psprintf(INT64_FORMAT,
337  (1 + /* include the metapage in index_size */
338  indexStat.leaf_pages +
339  indexStat.internal_pages +
340  indexStat.deleted_pages +
341  indexStat.empty_pages) * BLCKSZ);
342  values[j++] = psprintf("%u", indexStat.root_blkno);
343  values[j++] = psprintf(INT64_FORMAT, indexStat.internal_pages);
344  values[j++] = psprintf(INT64_FORMAT, indexStat.leaf_pages);
345  values[j++] = psprintf(INT64_FORMAT, indexStat.empty_pages);
346  values[j++] = psprintf(INT64_FORMAT, indexStat.deleted_pages);
347  if (indexStat.max_avail > 0)
348  values[j++] = psprintf("%.2f",
349  100.0 - (double) indexStat.free_space / (double) indexStat.max_avail * 100.0);
350  else
351  values[j++] = pstrdup("NaN");
352  if (indexStat.leaf_pages > 0)
353  values[j++] = psprintf("%.2f",
354  (double) indexStat.fragments / (double) indexStat.leaf_pages * 100.0);
355  else
356  values[j++] = pstrdup("NaN");
357 
359  values);
360 
361  result = HeapTupleGetDatum(tuple);
362  }
363 
364  return result;
365 }
uint64 empty_pages
Definition: pgstatindex.c:88
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:542
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
BlockNumber btpo_next
Definition: nbtree.h:58
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
#define P_IGNORE(opaque)
Definition: nbtree.h:194
uint64 fragments
Definition: pgstatindex.c:94
uint32 version
Definition: pgstatindex.c:82
uint32 btm_version
Definition: nbtree.h:100
BlockNumber root_blkno
Definition: pgstatindex.c:84
char * pstrdup(const char *in)
Definition: mcxt.c:1186
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
#define P_NONE
Definition: nbtree.h:181
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:570
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3365
#define IS_INDEX(r)
Definition: pgstatindex.c:70
#define SizeOfPageHeaderData
Definition: bufpage.h:216
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:68
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:581
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2112
#define IS_BTREE(r)
Definition: pgstatindex.c:71
#define ERROR
Definition: elog.h:43
uint64 internal_pages
Definition: pgstatindex.c:86
#define BTPageGetMeta(p)
Definition: nbtree.h:112
#define RelationGetRelationName(relation)
Definition: rel.h:453
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define ereport(elevel, rest)
Definition: elog.h:141
#define P_ISDELETED(opaque)
Definition: nbtree.h:191
uintptr_t Datum
Definition: postgres.h:367
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2063
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
uint64 free_space
Definition: pgstatindex.c:92
BlockNumber btm_root
Definition: nbtree.h:101
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:198
PageHeaderData * PageHeader
Definition: bufpage.h:166
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:549
uint64 leaf_pages
Definition: pgstatindex.c:87
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
uint32 level
Definition: pgstatindex.c:83
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
#define INT64_FORMAT
Definition: c.h:400
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int errmsg(const char *fmt,...)
Definition: elog.c:784
uint32 btm_level
Definition: nbtree.h:102
#define elog(elevel,...)
Definition: elog.h:226
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
uint64 deleted_pages
Definition: pgstatindex.c:89
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78
uint64 max_avail
Definition: pgstatindex.c:91
#define P_ISLEAF(opaque)
Definition: nbtree.h:189

◆ pgstatindex_v1_5()

Datum pgstatindex_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 170 of file pgstatindex.c.

References AccessShareLock, makeRangeVarFromNameList(), PG_GETARG_TEXT_PP, PG_RETURN_DATUM, pgstatindex_impl(), relation_openrv(), relname, and textToQualifiedNameList().

171 {
173  Relation rel;
174  RangeVar *relrv;
175 
177  rel = relation_openrv(relrv, AccessShareLock);
178 
179  PG_RETURN_DATUM(pgstatindex_impl(rel, fcinfo));
180 }
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3054
NameData relname
Definition: pg_class.h:35
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
static Datum pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstatindex.c:216
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3586
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
Definition: c.h:549

◆ pgstatindexbyid()

Datum pgstatindexbyid ( PG_FUNCTION_ARGS  )

Definition at line 188 of file pgstatindex.c.

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

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 }
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
unsigned int Oid
Definition: postgres_ext.h:31
static Datum pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstatindex.c:216
#define ERROR
Definition: elog.h:43
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ pgstatindexbyid_v1_5()

Datum pgstatindexbyid_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 205 of file pgstatindex.c.

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

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 }
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
static Datum pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstatindex.c:216
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343