PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgstattuple.c File Reference
#include "postgres.h"
#include "access/gist_private.h"
#include "access/hash.h"
#include "access/nbtree.h"
#include "access/relscan.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/tqual.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   32
 

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

#define NCHARS   32

Referenced by build_pgstattuple_type().

#define NCOLUMNS   9

Referenced by build_pgstattuple_type().

Typedef Documentation

Definition at line 64 of file pgstattuple.c.

Function Documentation

static Datum build_pgstattuple_type ( pgstattuple_type stat,
FunctionCallInfo  fcinfo 
)
static

Definition at line 89 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, NULL, 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().

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

Definition at line 398 of file pgstattuple.c.

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

Referenced by pgstat_relation().

400 {
401  Buffer buf;
402  Page page;
403 
404  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
405  LockBuffer(buf, BT_READ);
406  page = BufferGetPage(buf);
407 
408  /* Page is valid, see what to do with it */
409  if (PageIsNew(page))
410  {
411  /* fully empty page */
412  stat->free_space += BLCKSZ;
413  }
414  else
415  {
416  BTPageOpaque opaque;
417 
418  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
419  if (opaque->btpo_flags & (BTP_DELETED | BTP_HALF_DEAD))
420  {
421  /* recyclable page */
422  stat->free_space += BLCKSZ;
423  }
424  else if (P_ISLEAF(opaque))
425  {
426  pgstat_index_page(stat, page, P_FIRSTDATAKEY(opaque),
427  PageGetMaxOffsetNumber(page));
428  }
429  else
430  {
431  /* root or node */
432  }
433  }
434 
435  _bt_relbuf(rel, buf);
436 }
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:205
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define BTP_HALF_DEAD
Definition: nbtree.h:74
#define BTP_DELETED
Definition: nbtree.h:72
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:67
#define BT_READ
Definition: nbtree.h:238
static char * buf
Definition: pg_test_fsync.c:66
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:720
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
static void pgstat_index_page(pgstattuple_type *stat, Page page, OffsetNumber minoff, OffsetNumber maxoff)
Definition: pgstattuple.c:556
#define PageIsNew(page)
Definition: bufpage.h:225
uint16 btpo_flags
Definition: nbtree.h:63
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
#define P_ISLEAF(opaque)
Definition: nbtree.h:176
static void pgstat_gist_page ( pgstattuple_type stat,
Relation  rel,
BlockNumber  blkno,
BufferAccessStrategy  bstrategy 
)
static

Definition at line 484 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().

486 {
487  Buffer buf;
488  Page page;
489 
490  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
491  LockBuffer(buf, GIST_SHARE);
492  gistcheckpage(rel, buf);
493  page = BufferGetPage(buf);
494 
495  if (GistPageIsLeaf(page))
496  {
498  PageGetMaxOffsetNumber(page));
499  }
500  else
501  {
502  /* root or node */
503  }
504 
505  UnlockReleaseBuffer(buf);
506 }
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
static char * buf
Definition: pg_test_fsync.c:66
#define FirstOffsetNumber
Definition: off.h:27
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define GistPageIsLeaf(page)
Definition: gist.h:132
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:723
#define GIST_SHARE
Definition: gist_private.h:43
static void pgstat_index_page(pgstattuple_type *stat, Page page, OffsetNumber minoff, OffsetNumber maxoff)
Definition: pgstattuple.c:556
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
static void pgstat_hash_page ( pgstattuple_type stat,
Relation  rel,
BlockNumber  blkno,
BufferAccessStrategy  bstrategy 
)
static

Definition at line 442 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().

444 {
445  Buffer buf;
446  Page page;
447 
448  buf = _hash_getbuf_with_strategy(rel, blkno, HASH_READ, 0, bstrategy);
449  page = BufferGetPage(buf);
450 
451  if (PageGetSpecialSize(page) == MAXALIGN(sizeof(HashPageOpaqueData)))
452  {
453  HashPageOpaque opaque;
454 
455  opaque = (HashPageOpaque) PageGetSpecialPointer(page);
456  switch (opaque->hasho_flag & LH_PAGE_TYPE)
457  {
458  case LH_UNUSED_PAGE:
459  stat->free_space += BLCKSZ;
460  break;
461  case LH_BUCKET_PAGE:
462  case LH_OVERFLOW_PAGE:
464  PageGetMaxOffsetNumber(page));
465  break;
466  case LH_BITMAP_PAGE:
467  case LH_META_PAGE:
468  default:
469  break;
470  }
471  }
472  else
473  {
474  /* maybe corrupted */
475  }
476 
477  _hash_relbuf(rel, buf);
478 }
#define LH_BITMAP_PAGE
Definition: hash.h:55
#define LH_META_PAGE
Definition: hash.h:56
Buffer _hash_getbuf_with_strategy(Relation rel, BlockNumber blkno, int access, int flags, BufferAccessStrategy bstrategy)
Definition: hashpage.c:247
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
#define LH_UNUSED_PAGE
Definition: hash.h:52
#define LH_PAGE_TYPE
Definition: hash.h:62
#define HASH_READ
Definition: hash.h:281
static char * buf
Definition: pg_test_fsync.c:66
#define FirstOffsetNumber
Definition: off.h:27
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define LH_OVERFLOW_PAGE
Definition: hash.h:53
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:274
#define LH_BUCKET_PAGE
Definition: hash.h:54
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:85
#define MAXALIGN(LEN)
Definition: c.h:588
#define PageGetSpecialSize(page)
Definition: bufpage.h:296
static void pgstat_index_page(pgstattuple_type *stat, Page page, OffsetNumber minoff, OffsetNumber maxoff)
Definition: pgstattuple.c:556
uint16 hasho_flag
Definition: hash.h:81
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
static Datum pgstat_heap ( Relation  rel,
FunctionCallInfo  fcinfo 
)
static

Definition at line 315 of file pgstattuple.c.

References AccessShareLock, buffer, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, build_pgstattuple_type(), CHECK_FOR_INTERRUPTS, pgstattuple_type::dead_tuple_count, pgstattuple_type::dead_tuple_len, ForwardScanDirection, pgstattuple_type::free_space, heap_beginscan_strat(), heap_endscan(), heap_getnext(), HeapTupleSatisfiesVisibility, InitDirtySnapshot, ItemPointerGetBlockNumber, LockBuffer(), MAIN_FORKNUM, NULL, PageGetHeapFreeSpace(), RBM_NORMAL, ReadBufferExtended(), relation_close(), HeapScanDescData::rs_cbuf, HeapScanDescData::rs_nblocks, HeapScanDescData::rs_strategy, SnapshotAny, HeapTupleData::t_len, HeapTupleData::t_self, pgstattuple_type::table_len, pgstattuple_type::tuple_count, pgstattuple_type::tuple_len, and UnlockReleaseBuffer().

Referenced by pgstat_relation().

316 {
317  HeapScanDesc scan;
318  HeapTuple tuple;
319  BlockNumber nblocks;
320  BlockNumber block = 0; /* next block to count free space in */
321  BlockNumber tupblock;
322  Buffer buffer;
323  pgstattuple_type stat = {0};
324  SnapshotData SnapshotDirty;
325 
326  /* Disable syncscan because we assume we scan from block zero upwards */
327  scan = heap_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false);
328  InitDirtySnapshot(SnapshotDirty);
329 
330  nblocks = scan->rs_nblocks; /* # blocks to be scanned */
331 
332  /* scan the relation */
333  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
334  {
336 
337  /* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
339 
340  if (HeapTupleSatisfiesVisibility(tuple, &SnapshotDirty, scan->rs_cbuf))
341  {
342  stat.tuple_len += tuple->t_len;
343  stat.tuple_count++;
344  }
345  else
346  {
347  stat.dead_tuple_len += tuple->t_len;
348  stat.dead_tuple_count++;
349  }
350 
352 
353  /*
354  * To avoid physically reading the table twice, try to do the
355  * free-space scan in parallel with the heap scan. However,
356  * heap_getnext may find no tuples on a given page, so we cannot
357  * simply examine the pages returned by the heap scan.
358  */
359  tupblock = ItemPointerGetBlockNumber(&tuple->t_self);
360 
361  while (block <= tupblock)
362  {
364 
365  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
366  RBM_NORMAL, scan->rs_strategy);
367  LockBuffer(buffer, BUFFER_LOCK_SHARE);
369  UnlockReleaseBuffer(buffer);
370  block++;
371  }
372  }
373 
374  while (block < nblocks)
375  {
377 
378  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
379  RBM_NORMAL, scan->rs_strategy);
380  LockBuffer(buffer, BUFFER_LOCK_SHARE);
382  UnlockReleaseBuffer(buffer);
383  block++;
384  }
385 
386  heap_endscan(scan);
388 
389  stat.table_len = (uint64) nblocks * BLCKSZ;
390 
391  return build_pgstattuple_type(&stat, fcinfo);
392 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1580
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
uint64 dead_tuple_count
Definition: pgstattuple.c:59
#define AccessShareLock
Definition: lockdefs.h:36
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1266
uint32 BlockNumber
Definition: block.h:31
#define HeapTupleSatisfiesVisibility(tuple, snapshot, buffer)
Definition: tqual.h:45
uint64 dead_tuple_len
Definition: pgstattuple.c:60
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:666
#define InitDirtySnapshot(snapshotdata)
Definition: tqual.h:100
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define SnapshotAny
Definition: tqual.h:28
BlockNumber rs_nblocks
Definition: relscan.h:60
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1810
BufferAccessStrategy rs_strategy
Definition: relscan.h:64
HeapScanDesc heap_beginscan_strat(Relation relation, Snapshot snapshot, int nkeys, ScanKey key, bool allow_strat, bool allow_sync)
Definition: heapam.c:1415
Buffer rs_cbuf
Definition: relscan.h:71
#define NULL
Definition: c.h:229
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:76
static Datum build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:89
int Buffer
Definition: buf.h:23
uint64 tuple_count
Definition: pgstattuple.c:57
Pointer Page
Definition: bufpage.h:74
static Datum pgstat_index ( Relation  rel,
BlockNumber  start,
pgstat_page  pagefn,
FunctionCallInfo  fcinfo 
)
static

Definition at line 512 of file pgstattuple.c.

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

Referenced by pgstat_relation().

514 {
515  BlockNumber nblocks;
516  BlockNumber blkno;
517  BufferAccessStrategy bstrategy;
518  pgstattuple_type stat = {0};
519 
520  /* prepare access strategy for this index */
521  bstrategy = GetAccessStrategy(BAS_BULKREAD);
522 
523  blkno = start;
524  for (;;)
525  {
526  /* Get the current relation length */
528  nblocks = RelationGetNumberOfBlocks(rel);
530 
531  /* Quit if we've scanned the whole relation */
532  if (blkno >= nblocks)
533  {
534  stat.table_len = (uint64) nblocks * BLCKSZ;
535 
536  break;
537  }
538 
539  for (; blkno < nblocks; blkno++)
540  {
542 
543  pagefn(&stat, rel, blkno, bstrategy);
544  }
545  }
546 
548 
549  return build_pgstattuple_type(&stat, fcinfo);
550 }
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:542
#define ExclusiveLock
Definition: lockdefs.h:44
#define AccessShareLock
Definition: lockdefs.h:36
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1266
uint32 BlockNumber
Definition: block.h:31
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:332
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:382
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
static Datum build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:89
static void pgstat_index_page ( pgstattuple_type stat,
Page  page,
OffsetNumber  minoff,
OffsetNumber  maxoff 
)
static

Definition at line 556 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().

558 {
559  OffsetNumber i;
560 
561  stat->free_space += PageGetFreeSpace(page);
562 
563  for (i = minoff; i <= maxoff; i = OffsetNumberNext(i))
564  {
565  ItemId itemid = PageGetItemId(page, i);
566 
567  if (ItemIdIsDead(itemid))
568  {
569  stat->dead_tuple_count++;
570  stat->dead_tuple_len += ItemIdGetLength(itemid);
571  }
572  else
573  {
574  stat->tuple_count++;
575  stat->tuple_len += ItemIdGetLength(itemid);
576  }
577  }
578 }
uint64 dead_tuple_count
Definition: pgstattuple.c:59
#define ItemIdIsDead(itemId)
Definition: itemid.h:112
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:582
uint16 OffsetNumber
Definition: off.h:24
uint64 dead_tuple_len
Definition: pgstattuple.c:60
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
int i
uint64 tuple_count
Definition: pgstattuple.c:57
static Datum pgstat_relation ( Relation  rel,
FunctionCallInfo  fcinfo 
)
static

Definition at line 240 of file pgstattuple.c.

References BRIN_AM_OID, BTREE_AM_OID, BTREE_METAPAGE, ereport, errcode(), errmsg(), ERROR, GIN_AM_OID, GIST_AM_OID, GIST_ROOT_BLKNO, HASH_AM_OID, HASH_METAPAGE, pgstat_btree_page(), pgstat_gist_page(), pgstat_hash_page(), pgstat_heap(), pgstat_index(), RelationData::rd_rel, RELATION_IS_OTHER_TEMP, RelationGetRelationName, RELKIND_COMPOSITE_TYPE, RELKIND_FOREIGN_TABLE, RELKIND_INDEX, RELKIND_MATVIEW, RELKIND_PARTITIONED_TABLE, RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_TOASTVALUE, RELKIND_VIEW, and SPGIST_AM_OID.

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

241 {
242  const char *err;
243 
244  /*
245  * Reject attempts to read non-local temporary relations; we would be
246  * likely to get wrong data since we have no visibility into the owning
247  * session's local buffers.
248  */
249  if (RELATION_IS_OTHER_TEMP(rel))
250  ereport(ERROR,
251  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
252  errmsg("cannot access temporary tables of other sessions")));
253 
254  switch (rel->rd_rel->relkind)
255  {
256  case RELKIND_RELATION:
257  case RELKIND_MATVIEW:
258  case RELKIND_TOASTVALUE:
259  case RELKIND_SEQUENCE:
260  return pgstat_heap(rel, fcinfo);
261  case RELKIND_INDEX:
262  switch (rel->rd_rel->relam)
263  {
264  case BTREE_AM_OID:
265  return pgstat_index(rel, BTREE_METAPAGE + 1,
266  pgstat_btree_page, fcinfo);
267  case HASH_AM_OID:
268  return pgstat_index(rel, HASH_METAPAGE + 1,
269  pgstat_hash_page, fcinfo);
270  case GIST_AM_OID:
271  return pgstat_index(rel, GIST_ROOT_BLKNO + 1,
272  pgstat_gist_page, fcinfo);
273  case GIN_AM_OID:
274  err = "gin index";
275  break;
276  case SPGIST_AM_OID:
277  err = "spgist index";
278  break;
279  case BRIN_AM_OID:
280  err = "brin index";
281  break;
282  default:
283  err = "unknown index";
284  break;
285  }
286  break;
287  case RELKIND_VIEW:
288  err = "view";
289  break;
291  err = "composite type";
292  break;
294  err = "foreign table";
295  break;
297  err = "partitioned table";
298  break;
299  default:
300  err = "unknown";
301  break;
302  }
303 
304  ereport(ERROR,
305  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
306  errmsg("\"%s\" (%s) is not supported",
307  RelationGetRelationName(rel), err)));
308  return 0; /* should not happen */
309 }
static void pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:442
static void pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:398
#define BRIN_AM_OID
Definition: pg_am.h:85
#define BTREE_AM_OID
Definition: pg_am.h:70
#define RELKIND_MATVIEW
Definition: pg_class.h:165
int errcode(int sqlerrcode)
Definition: elog.c:575
Form_pg_class rd_rel
Definition: rel.h:114
#define HASH_AM_OID
Definition: pg_am.h:73
#define RELKIND_COMPOSITE_TYPE
Definition: pg_class.h:166
#define GIN_AM_OID
Definition: pg_am.h:79
#define ERROR
Definition: elog.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:436
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
#define ereport(elevel, rest)
Definition: elog.h:122
#define BTREE_METAPAGE
Definition: nbtree.h:109
static Datum pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:315
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
#define RELKIND_TOASTVALUE
Definition: pg_class.h:163
#define HASH_METAPAGE
Definition: hash.h:158
static Datum pgstat_index(Relation rel, BlockNumber start, pgstat_page pagefn, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:512
#define GIST_AM_OID
Definition: pg_am.h:76
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:533
static void pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:484
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define RELKIND_VIEW
Definition: pg_class.h:164
#define GIST_ROOT_BLKNO
Definition: gist_private.h:249
#define RELKIND_INDEX
Definition: pg_class.h:161
#define SPGIST_AM_OID
Definition: pg_am.h:82
#define RELKIND_RELATION
Definition: pg_class.h:160
#define RELKIND_SEQUENCE
Definition: pg_class.h:162
Datum pgstattuple ( PG_FUNCTION_ARGS  )

Definition at line 166 of file pgstattuple.c.

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

167 {
168  text *relname = PG_GETARG_TEXT_PP(0);
169  RangeVar *relrv;
170  Relation rel;
171 
172  if (!superuser())
173  ereport(ERROR,
174  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
175  (errmsg("must be superuser to use pgstattuple functions"))));
176 
177  /* open relation */
179  rel = relation_openrv(relrv, AccessShareLock);
180 
181  PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
182 }
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1198
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3023
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3202
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:240
Datum pgstattuple_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 192 of file pgstattuple.c.

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

193 {
194  text *relname = PG_GETARG_TEXT_PP(0);
195  RangeVar *relrv;
196  Relation rel;
197 
198  /* open relation */
200  rel = relation_openrv(relrv, AccessShareLock);
201 
202  PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
203 }
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1198
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3023
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3202
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Definition: c.h:439
static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:240
Datum pgstattuplebyid ( PG_FUNCTION_ARGS  )

Definition at line 207 of file pgstattuple.c.

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

208 {
209  Oid relid = PG_GETARG_OID(0);
210  Relation rel;
211 
212  if (!superuser())
213  ereport(ERROR,
214  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
215  (errmsg("must be superuser to use pgstattuple functions"))));
216 
217  /* open relation */
218  rel = relation_open(relid, AccessShareLock);
219 
220  PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
221 }
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
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:240
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1120
static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:240
Datum pgstattuplebyid_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 225 of file pgstattuple.c.

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

226 {
227  Oid relid = PG_GETARG_OID(0);
228  Relation rel;
229 
230  /* open relation */
231  rel = relation_open(relid, AccessShareLock);
232 
233  PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
234 }
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1120
static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:240

Variable Documentation

PG_MODULE_MAGIC

Definition at line 41 of file pgstattuple.c.