PostgreSQL Source Code  git master
pgstattuple.c File Reference
#include "postgres.h"
#include "access/gist_private.h"
#include "access/hash.h"
#include "access/heapam.h"
#include "access/nbtree.h"
#include "access/relscan.h"
#include "access/tableam.h"
#include "catalog/namespace.h"
#include "catalog/pg_am_d.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "utils/builtins.h"
#include "utils/varlena.h"
Include dependency graph for pgstattuple.c:

Go to the source code of this file.

Data Structures

struct  pgstattuple_type
 

Macros

#define NCOLUMNS   9
 
#define NCHARS   314
 

Typedefs

typedef struct pgstattuple_type pgstattuple_type
 
typedef void(* pgstat_page) (pgstattuple_type *, Relation, BlockNumber, BufferAccessStrategy)
 

Functions

 PG_FUNCTION_INFO_V1 (pgstattuple)
 
 PG_FUNCTION_INFO_V1 (pgstattuple_v1_5)
 
 PG_FUNCTION_INFO_V1 (pgstattuplebyid)
 
 PG_FUNCTION_INFO_V1 (pgstattuplebyid_v1_5)
 
static Datum build_pgstattuple_type (pgstattuple_type *stat, FunctionCallInfo fcinfo)
 
static Datum pgstat_relation (Relation rel, FunctionCallInfo fcinfo)
 
static Datum pgstat_heap (Relation rel, FunctionCallInfo fcinfo)
 
static void pgstat_btree_page (pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
 
static void pgstat_hash_page (pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
 
static void pgstat_gist_page (pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
 
static Datum pgstat_index (Relation rel, BlockNumber start, pgstat_page pagefn, FunctionCallInfo fcinfo)
 
static void pgstat_index_page (pgstattuple_type *stat, Page page, OffsetNumber minoff, OffsetNumber maxoff)
 
Datum pgstattuple (PG_FUNCTION_ARGS)
 
Datum pgstattuple_v1_5 (PG_FUNCTION_ARGS)
 
Datum pgstattuplebyid (PG_FUNCTION_ARGS)
 
Datum pgstattuplebyid_v1_5 (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

◆ NCHARS

#define NCHARS   314

Referenced by build_pgstattuple_type().

◆ NCOLUMNS

#define NCOLUMNS   9

Referenced by build_pgstattuple_type().

Typedef Documentation

◆ pgstat_page

typedef void(* pgstat_page) (pgstattuple_type *, Relation, BlockNumber, BufferAccessStrategy)

Definition at line 65 of file pgstattuple.c.

◆ pgstattuple_type

Function Documentation

◆ build_pgstattuple_type()

static Datum build_pgstattuple_type ( pgstattuple_type stat,
FunctionCallInfo  fcinfo 
)
static

Definition at line 90 of file pgstattuple.c.

References BuildTupleFromCStrings(), pgstattuple_type::dead_tuple_count, pgstattuple_type::dead_tuple_len, elog, ERROR, pgstattuple_type::free_space, get_call_result_type(), HeapTupleGetDatum, i, INT64_FORMAT, NCHARS, NCOLUMNS, snprintf, pgstattuple_type::table_len, pgstattuple_type::tuple_count, pgstattuple_type::tuple_len, TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, and values.

Referenced by pgstat_heap(), and pgstat_index().

91 {
92 #define NCOLUMNS 9
93 #define NCHARS 314
94 
95  HeapTuple tuple;
96  char *values[NCOLUMNS];
97  char values_buf[NCOLUMNS][NCHARS];
98  int i;
99  double tuple_percent;
100  double dead_tuple_percent;
101  double free_percent; /* free/reusable space in % */
102  TupleDesc tupdesc;
103  AttInMetadata *attinmeta;
104 
105  /* Build a tuple descriptor for our result type */
106  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
107  elog(ERROR, "return type must be a row type");
108 
109  /*
110  * Generate attribute metadata needed later to produce tuples from raw C
111  * strings
112  */
113  attinmeta = TupleDescGetAttInMetadata(tupdesc);
114 
115  if (stat->table_len == 0)
116  {
117  tuple_percent = 0.0;
118  dead_tuple_percent = 0.0;
119  free_percent = 0.0;
120  }
121  else
122  {
123  tuple_percent = 100.0 * stat->tuple_len / stat->table_len;
124  dead_tuple_percent = 100.0 * stat->dead_tuple_len / stat->table_len;
125  free_percent = 100.0 * stat->free_space / stat->table_len;
126  }
127 
128  /*
129  * Prepare a values array for constructing the tuple. This should be an
130  * array of C strings which will be processed later by the appropriate
131  * "in" functions.
132  */
133  for (i = 0; i < NCOLUMNS; i++)
134  values[i] = values_buf[i];
135  i = 0;
136  snprintf(values[i++], NCHARS, INT64_FORMAT, stat->table_len);
137  snprintf(values[i++], NCHARS, INT64_FORMAT, stat->tuple_count);
138  snprintf(values[i++], NCHARS, INT64_FORMAT, stat->tuple_len);
139  snprintf(values[i++], NCHARS, "%.2f", tuple_percent);
140  snprintf(values[i++], NCHARS, INT64_FORMAT, stat->dead_tuple_count);
141  snprintf(values[i++], NCHARS, INT64_FORMAT, stat->dead_tuple_len);
142  snprintf(values[i++], NCHARS, "%.2f", dead_tuple_percent);
143  snprintf(values[i++], NCHARS, INT64_FORMAT, stat->free_space);
144  snprintf(values[i++], NCHARS, "%.2f", free_percent);
145 
146  /* build a tuple */
147  tuple = BuildTupleFromCStrings(attinmeta, values);
148 
149  /* make the tuple into a datum */
150  return HeapTupleGetDatum(tuple);
151 }
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
#define NCOLUMNS
uint64 dead_tuple_count
Definition: pgstattuple.c:60
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2116
uint64 dead_tuple_len
Definition: pgstattuple.c:61
#define ERROR
Definition: elog.h:43
#define NCHARS
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2067
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
#define INT64_FORMAT
Definition: c.h:401
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define elog(elevel,...)
Definition: elog.h:228
int i
#define snprintf
Definition: port.h:192
uint64 tuple_count
Definition: pgstattuple.c:58

◆ PG_FUNCTION_INFO_V1() [1/4]

PG_FUNCTION_INFO_V1 ( pgstattuple  )

◆ PG_FUNCTION_INFO_V1() [2/4]

PG_FUNCTION_INFO_V1 ( pgstattuple_v1_5  )

◆ PG_FUNCTION_INFO_V1() [3/4]

PG_FUNCTION_INFO_V1 ( pgstattuplebyid  )

◆ PG_FUNCTION_INFO_V1() [4/4]

PG_FUNCTION_INFO_V1 ( pgstattuplebyid_v1_5  )

◆ pgstat_btree_page()

static void pgstat_btree_page ( pgstattuple_type stat,
Relation  rel,
BlockNumber  blkno,
BufferAccessStrategy  bstrategy 
)
static

Definition at line 410 of file pgstattuple.c.

References _bt_relbuf(), BT_READ, buf, BufferGetPage, pgstattuple_type::free_space, LockBuffer(), MAIN_FORKNUM, P_FIRSTDATAKEY, P_IGNORE, P_ISLEAF, PageGetMaxOffsetNumber, PageGetSpecialPointer, PageIsNew, pgstat_index_page(), RBM_NORMAL, and ReadBufferExtended().

Referenced by pgstat_relation().

412 {
413  Buffer buf;
414  Page page;
415 
416  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
417  LockBuffer(buf, BT_READ);
418  page = BufferGetPage(buf);
419 
420  /* Page is valid, see what to do with it */
421  if (PageIsNew(page))
422  {
423  /* fully empty page */
424  stat->free_space += BLCKSZ;
425  }
426  else
427  {
428  BTPageOpaque opaque;
429 
430  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
431  if (P_IGNORE(opaque))
432  {
433  /* recyclable page */
434  stat->free_space += BLCKSZ;
435  }
436  else if (P_ISLEAF(opaque))
437  {
438  pgstat_index_page(stat, page, P_FIRSTDATAKEY(opaque),
439  PageGetMaxOffsetNumber(page));
440  }
441  else
442  {
443  /* root or node */
444  }
445  }
446 
447  _bt_relbuf(rel, buf);
448 }
#define P_IGNORE(opaque)
Definition: nbtree.h:195
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:220
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:69
#define BT_READ
Definition: nbtree.h:403
static char * buf
Definition: pg_test_fsync.c:67
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:912
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
static void pgstat_index_page(pgstattuple_type *stat, Page page, OffsetNumber minoff, OffsetNumber maxoff)
Definition: pgstattuple.c:568
#define PageIsNew(page)
Definition: bufpage.h:229
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78
#define P_ISLEAF(opaque)
Definition: nbtree.h:190

◆ pgstat_gist_page()

static void pgstat_gist_page ( pgstattuple_type stat,
Relation  rel,
BlockNumber  blkno,
BufferAccessStrategy  bstrategy 
)
static

Definition at line 496 of file pgstattuple.c.

References buf, BufferGetPage, FirstOffsetNumber, GIST_SHARE, gistcheckpage(), GistPageIsLeaf, LockBuffer(), MAIN_FORKNUM, PageGetMaxOffsetNumber, pgstat_index_page(), RBM_NORMAL, ReadBufferExtended(), and UnlockReleaseBuffer().

Referenced by pgstat_relation().

498 {
499  Buffer buf;
500  Page page;
501 
502  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
503  LockBuffer(buf, GIST_SHARE);
504  gistcheckpage(rel, buf);
505  page = BufferGetPage(buf);
506 
507  if (GistPageIsLeaf(page))
508  {
510  PageGetMaxOffsetNumber(page));
511  }
512  else
513  {
514  /* root or node */
515  }
516 
517  UnlockReleaseBuffer(buf);
518 }
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
static char * buf
Definition: pg_test_fsync.c:67
#define FirstOffsetNumber
Definition: off.h:27
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define GistPageIsLeaf(page)
Definition: gist.h:140
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:770
#define GIST_SHARE
Definition: gist_private.h:42
static void pgstat_index_page(pgstattuple_type *stat, Page page, OffsetNumber minoff, OffsetNumber maxoff)
Definition: pgstattuple.c:568
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78

◆ pgstat_hash_page()

static void pgstat_hash_page ( pgstattuple_type stat,
Relation  rel,
BlockNumber  blkno,
BufferAccessStrategy  bstrategy 
)
static

Definition at line 454 of file pgstattuple.c.

References _hash_getbuf_with_strategy(), _hash_relbuf(), buf, BufferGetPage, FirstOffsetNumber, pgstattuple_type::free_space, HASH_READ, HashPageOpaqueData::hasho_flag, LH_BITMAP_PAGE, LH_BUCKET_PAGE, LH_META_PAGE, LH_OVERFLOW_PAGE, LH_PAGE_TYPE, LH_UNUSED_PAGE, MAXALIGN, PageGetMaxOffsetNumber, PageGetSpecialPointer, PageGetSpecialSize, and pgstat_index_page().

Referenced by pgstat_relation().

456 {
457  Buffer buf;
458  Page page;
459 
460  buf = _hash_getbuf_with_strategy(rel, blkno, HASH_READ, 0, bstrategy);
461  page = BufferGetPage(buf);
462 
463  if (PageGetSpecialSize(page) == MAXALIGN(sizeof(HashPageOpaqueData)))
464  {
465  HashPageOpaque opaque;
466 
467  opaque = (HashPageOpaque) PageGetSpecialPointer(page);
468  switch (opaque->hasho_flag & LH_PAGE_TYPE)
469  {
470  case LH_UNUSED_PAGE:
471  stat->free_space += BLCKSZ;
472  break;
473  case LH_BUCKET_PAGE:
474  case LH_OVERFLOW_PAGE:
476  PageGetMaxOffsetNumber(page));
477  break;
478  case LH_BITMAP_PAGE:
479  case LH_META_PAGE:
480  default:
481  break;
482  }
483  }
484  else
485  {
486  /* maybe corrupted */
487  }
488 
489  _hash_relbuf(rel, buf);
490 }
#define LH_BITMAP_PAGE
Definition: hash.h:56
#define LH_META_PAGE
Definition: hash.h:57
Buffer _hash_getbuf_with_strategy(Relation rel, BlockNumber blkno, int access, int flags, BufferAccessStrategy bstrategy)
Definition: hashpage.c:237
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
#define LH_UNUSED_PAGE
Definition: hash.h:53
#define LH_PAGE_TYPE
Definition: hash.h:63
#define HASH_READ
Definition: hash.h:337
static char * buf
Definition: pg_test_fsync.c:67
#define FirstOffsetNumber
Definition: off.h:27
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define LH_OVERFLOW_PAGE
Definition: hash.h:54
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:264
#define LH_BUCKET_PAGE
Definition: hash.h:55
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:86
#define MAXALIGN(LEN)
Definition: c.h:692
#define PageGetSpecialSize(page)
Definition: bufpage.h:300
static void pgstat_index_page(pgstattuple_type *stat, Page page, OffsetNumber minoff, OffsetNumber maxoff)
Definition: pgstattuple.c:568
uint16 hasho_flag
Definition: hash.h:82
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78

◆ pgstat_heap()

static Datum pgstat_heap ( Relation  rel,
FunctionCallInfo  fcinfo 
)
static

Definition at line 319 of file pgstattuple.c.

References AccessShareLock, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, build_pgstattuple_type(), CHECK_FOR_INTERRUPTS, pgstattuple_type::dead_tuple_count, pgstattuple_type::dead_tuple_len, ereport, errcode(), errmsg(), ERROR, ForwardScanDirection, pgstattuple_type::free_space, heap_getnext(), HeapTupleSatisfiesVisibility(), InitDirtySnapshot, ItemPointerGetBlockNumber, LockBuffer(), MAIN_FORKNUM, PageGetHeapFreeSpace(), RBM_NORMAL, RelationData::rd_rel, ReadBufferExtended(), relation_close(), HeapScanDescData::rs_cbuf, HeapScanDescData::rs_nblocks, HeapScanDescData::rs_strategy, SnapshotAny, stat, HeapTupleData::t_len, HeapTupleData::t_self, table_beginscan_strat(), table_endscan(), pgstattuple_type::table_len, pgstattuple_type::tuple_count, pgstattuple_type::tuple_len, and UnlockReleaseBuffer().

Referenced by pgstat_relation().

320 {
321  TableScanDesc scan;
322  HeapScanDesc hscan;
323  HeapTuple tuple;
324  BlockNumber nblocks;
325  BlockNumber block = 0; /* next block to count free space in */
326  BlockNumber tupblock;
327  Buffer buffer;
328  pgstattuple_type stat = {0};
329  SnapshotData SnapshotDirty;
330 
331  if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
332  ereport(ERROR,
333  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
334  errmsg("only heap AM is supported")));
335 
336  /* Disable syncscan because we assume we scan from block zero upwards */
337  scan = table_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false);
338  hscan = (HeapScanDesc) scan;
339 
340  InitDirtySnapshot(SnapshotDirty);
341 
342  nblocks = hscan->rs_nblocks; /* # blocks to be scanned */
343 
344  /* scan the relation */
345  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
346  {
348 
349  /* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
351 
352  if (HeapTupleSatisfiesVisibility(tuple, &SnapshotDirty, hscan->rs_cbuf))
353  {
354  stat.tuple_len += tuple->t_len;
355  stat.tuple_count++;
356  }
357  else
358  {
359  stat.dead_tuple_len += tuple->t_len;
360  stat.dead_tuple_count++;
361  }
362 
364 
365  /*
366  * To avoid physically reading the table twice, try to do the
367  * free-space scan in parallel with the heap scan. However,
368  * heap_getnext may find no tuples on a given page, so we cannot
369  * simply examine the pages returned by the heap scan.
370  */
371  tupblock = ItemPointerGetBlockNumber(&tuple->t_self);
372 
373  while (block <= tupblock)
374  {
376 
377  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
378  RBM_NORMAL, hscan->rs_strategy);
379  LockBuffer(buffer, BUFFER_LOCK_SHARE);
381  UnlockReleaseBuffer(buffer);
382  block++;
383  }
384  }
385 
386  while (block < nblocks)
387  {
389 
390  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
391  RBM_NORMAL, hscan->rs_strategy);
392  LockBuffer(buffer, BUFFER_LOCK_SHARE);
394  UnlockReleaseBuffer(buffer);
395  block++;
396  }
397 
398  table_endscan(scan);
400 
401  stat.table_len = (uint64) nblocks * BLCKSZ;
402 
403  return build_pgstattuple_type(&stat, fcinfo);
404 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
#define InitDirtySnapshot(snapshotdata)
Definition: snapmgr.h:76
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
uint64 dead_tuple_count
Definition: pgstattuple.c:60
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:608
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:83
static TableScanDesc table_beginscan_strat(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key, bool allow_strat, bool allow_sync)
Definition: tableam.h:760
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
uint64 dead_tuple_len
Definition: pgstattuple.c:61
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
#define ERROR
Definition: elog.h:43
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:665
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1290
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define ereport(elevel, rest)
Definition: elog.h:141
#define stat(a, b)
Definition: win32_port.h:255
BlockNumber rs_nblocks
Definition: heapam.h:51
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
BufferAccessStrategy rs_strategy
Definition: heapam.h:63
Buffer rs_cbuf
Definition: heapam.h:59
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define SnapshotAny
Definition: snapmgr.h:69
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:831
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
static Datum build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:90
int Buffer
Definition: buf.h:23
uint64 tuple_count
Definition: pgstattuple.c:58
Pointer Page
Definition: bufpage.h:78
bool HeapTupleSatisfiesVisibility(HeapTuple tup, Snapshot snapshot, Buffer buffer)

◆ pgstat_index()

static Datum pgstat_index ( Relation  rel,
BlockNumber  start,
pgstat_page  pagefn,
FunctionCallInfo  fcinfo 
)
static

Definition at line 524 of file pgstattuple.c.

References AccessShareLock, BAS_BULKREAD, build_pgstattuple_type(), CHECK_FOR_INTERRUPTS, ExclusiveLock, GetAccessStrategy(), LockRelationForExtension(), relation_close(), RelationGetNumberOfBlocks, stat, pgstattuple_type::table_len, and UnlockRelationForExtension().

Referenced by pgstat_relation().

526 {
527  BlockNumber nblocks;
528  BlockNumber blkno;
529  BufferAccessStrategy bstrategy;
530  pgstattuple_type stat = {0};
531 
532  /* prepare access strategy for this index */
533  bstrategy = GetAccessStrategy(BAS_BULKREAD);
534 
535  blkno = start;
536  for (;;)
537  {
538  /* Get the current relation length */
540  nblocks = RelationGetNumberOfBlocks(rel);
542 
543  /* Quit if we've scanned the whole relation */
544  if (blkno >= nblocks)
545  {
546  stat.table_len = (uint64) nblocks * BLCKSZ;
547 
548  break;
549  }
550 
551  for (; blkno < nblocks; blkno++)
552  {
554 
555  pagefn(&stat, rel, blkno, bstrategy);
556  }
557  }
558 
560 
561  return build_pgstattuple_type(&stat, fcinfo);
562 }
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:542
#define ExclusiveLock
Definition: lockdefs.h:44
#define AccessShareLock
Definition: lockdefs.h:36
uint32 BlockNumber
Definition: block.h:31
#define stat(a, b)
Definition: win32_port.h:255
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:402
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:452
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:198
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
static Datum build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:90

◆ pgstat_index_page()

static void pgstat_index_page ( pgstattuple_type stat,
Page  page,
OffsetNumber  minoff,
OffsetNumber  maxoff 
)
static

Definition at line 568 of file pgstattuple.c.

References pgstattuple_type::dead_tuple_count, pgstattuple_type::dead_tuple_len, pgstattuple_type::free_space, i, ItemIdGetLength, ItemIdIsDead, OffsetNumberNext, PageGetFreeSpace(), PageGetItemId, pgstattuple_type::tuple_count, and pgstattuple_type::tuple_len.

Referenced by pgstat_btree_page(), pgstat_gist_page(), and pgstat_hash_page().

570 {
571  OffsetNumber i;
572 
573  stat->free_space += PageGetFreeSpace(page);
574 
575  for (i = minoff; i <= maxoff; i = OffsetNumberNext(i))
576  {
577  ItemId itemid = PageGetItemId(page, i);
578 
579  if (ItemIdIsDead(itemid))
580  {
581  stat->dead_tuple_count++;
582  stat->dead_tuple_len += ItemIdGetLength(itemid);
583  }
584  else
585  {
586  stat->tuple_count++;
587  stat->tuple_len += ItemIdGetLength(itemid);
588  }
589  }
590 }
uint64 dead_tuple_count
Definition: pgstattuple.c:60
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:581
uint16 OffsetNumber
Definition: off.h:24
uint64 dead_tuple_len
Definition: pgstattuple.c:61
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
int i
uint64 tuple_count
Definition: pgstattuple.c:58

◆ pgstat_relation()

static Datum pgstat_relation ( Relation  rel,
FunctionCallInfo  fcinfo 
)
static

Definition at line 241 of file pgstattuple.c.

References BTREE_METAPAGE, ereport, errcode(), errmsg(), ERROR, GIST_ROOT_BLKNO, HASH_METAPAGE, pgstat_btree_page(), pgstat_gist_page(), pgstat_hash_page(), pgstat_heap(), pgstat_index(), RelationData::rd_rel, RELATION_IS_OTHER_TEMP, and RelationGetRelationName.

Referenced by pgstattuple(), pgstattuple_v1_5(), pgstattuplebyid(), and pgstattuplebyid_v1_5().

242 {
243  const char *err;
244 
245  /*
246  * Reject attempts to read non-local temporary relations; we would be
247  * likely to get wrong data since we have no visibility into the owning
248  * session's local buffers.
249  */
250  if (RELATION_IS_OTHER_TEMP(rel))
251  ereport(ERROR,
252  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
253  errmsg("cannot access temporary tables of other sessions")));
254 
255  switch (rel->rd_rel->relkind)
256  {
257  case RELKIND_RELATION:
258  case RELKIND_MATVIEW:
259  case RELKIND_TOASTVALUE:
260  case RELKIND_SEQUENCE:
261  return pgstat_heap(rel, fcinfo);
262  case RELKIND_INDEX:
263  switch (rel->rd_rel->relam)
264  {
265  case BTREE_AM_OID:
266  return pgstat_index(rel, BTREE_METAPAGE + 1,
267  pgstat_btree_page, fcinfo);
268  case HASH_AM_OID:
269  return pgstat_index(rel, HASH_METAPAGE + 1,
270  pgstat_hash_page, fcinfo);
271  case GIST_AM_OID:
272  return pgstat_index(rel, GIST_ROOT_BLKNO + 1,
273  pgstat_gist_page, fcinfo);
274  case GIN_AM_OID:
275  err = "gin index";
276  break;
277  case SPGIST_AM_OID:
278  err = "spgist index";
279  break;
280  case BRIN_AM_OID:
281  err = "brin index";
282  break;
283  default:
284  err = "unknown index";
285  break;
286  }
287  break;
288  case RELKIND_VIEW:
289  err = "view";
290  break;
291  case RELKIND_COMPOSITE_TYPE:
292  err = "composite type";
293  break;
294  case RELKIND_FOREIGN_TABLE:
295  err = "foreign table";
296  break;
297  case RELKIND_PARTITIONED_TABLE:
298  err = "partitioned table";
299  break;
300  case RELKIND_PARTITIONED_INDEX:
301  err = "partitioned index";
302  break;
303  default:
304  err = "unknown";
305  break;
306  }
307 
308  ereport(ERROR,
309  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
310  errmsg("\"%s\" (%s) is not supported",
311  RelationGetRelationName(rel), err)));
312  return 0; /* should not happen */
313 }
static void pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:454
static void pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:410
int errcode(int sqlerrcode)
Definition: elog.c:608
Form_pg_class rd_rel
Definition: rel.h:83
#define ERROR
Definition: elog.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:456
#define ereport(elevel, rest)
Definition: elog.h:141
#define BTREE_METAPAGE
Definition: nbtree.h:132
static Datum pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:319
#define HASH_METAPAGE
Definition: hash.h:196
static Datum pgstat_index(Relation rel, BlockNumber start, pgstat_page pagefn, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:524
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:552
static void pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:496
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define GIST_ROOT_BLKNO
Definition: gist_private.h:262

◆ pgstattuple()

Datum pgstattuple ( PG_FUNCTION_ARGS  )

Definition at line 167 of file pgstattuple.c.

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

168 {
170  RangeVar *relrv;
171  Relation rel;
172 
173  if (!superuser())
174  ereport(ERROR,
175  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
176  (errmsg("must be superuser to use pgstattuple functions"))));
177 
178  /* open relation */
180  rel = relation_openrv(relrv, AccessShareLock);
181 
182  PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
183 }
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:608
bool superuser(void)
Definition: superuser.c:46
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
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3594
#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:822
Definition: c.h:556
static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:241

◆ pgstattuple_v1_5()

Datum pgstattuple_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 193 of file pgstattuple.c.

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

194 {
196  RangeVar *relrv;
197  Relation rel;
198 
199  /* open relation */
201  rel = relation_openrv(relrv, AccessShareLock);
202 
203  PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
204 }
#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
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3594
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
Definition: c.h:556
static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:241

◆ pgstattuplebyid()

Datum pgstattuplebyid ( PG_FUNCTION_ARGS  )

Definition at line 208 of file pgstattuple.c.

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

209 {
210  Oid relid = PG_GETARG_OID(0);
211  Relation rel;
212 
213  if (!superuser())
214  ereport(ERROR,
215  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
216  (errmsg("must be superuser to use pgstattuple functions"))));
217 
218  /* open relation */
219  rel = relation_open(relid, AccessShareLock);
220 
221  PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
222 }
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:608
bool superuser(void)
Definition: superuser.c:46
unsigned int Oid
Definition: postgres_ext.h:31
#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:822
static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:241

◆ pgstattuplebyid_v1_5()

Datum pgstattuplebyid_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 226 of file pgstattuple.c.

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

227 {
228  Oid relid = PG_GETARG_OID(0);
229  Relation rel;
230 
231  /* open relation */
232  rel = relation_open(relid, AccessShareLock);
233 
234  PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
235 }
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
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
static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:241

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 42 of file pgstattuple.c.