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:1115
uint64 dead_tuple_len
Definition: pgstattuple.c:60
#define ERROR
Definition: elog.h:43
#define NCHARS
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1068
#define NULL
Definition: c.h:226
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
#define INT64_FORMAT
Definition: c.h:312
static Datum values[MAXATTR]
Definition: bootstrap.c:162
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 395 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().

397 {
398  Buffer buf;
399  Page page;
400 
401  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
402  LockBuffer(buf, BT_READ);
403  page = BufferGetPage(buf);
404 
405  /* Page is valid, see what to do with it */
406  if (PageIsNew(page))
407  {
408  /* fully empty page */
409  stat->free_space += BLCKSZ;
410  }
411  else
412  {
413  BTPageOpaque opaque;
414 
415  opaque = (BTPageOpaque) PageGetSpecialPointer(page);
416  if (opaque->btpo_flags & (BTP_DELETED | BTP_HALF_DEAD))
417  {
418  /* recyclable page */
419  stat->free_space += BLCKSZ;
420  }
421  else if (P_ISLEAF(opaque))
422  {
423  pgstat_index_page(stat, page, P_FIRSTDATAKEY(opaque),
424  PageGetMaxOffsetNumber(page));
425  }
426  else
427  {
428  /* root or node */
429  }
430  }
431 
432  _bt_relbuf(rel, buf);
433 }
#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:354
BTPageOpaqueData * BTPageOpaque
Definition: nbtree.h:67
#define BT_READ
Definition: nbtree.h:238
static char * buf
Definition: pg_test_fsync.c:65
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:720
#define PageGetSpecialPointer(page)
Definition: bufpage.h:323
static void pgstat_index_page(pgstattuple_type *stat, Page page, OffsetNumber minoff, OffsetNumber maxoff)
Definition: pgstattuple.c:553
#define PageIsNew(page)
Definition: bufpage.h:226
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:177
static void pgstat_gist_page ( pgstattuple_type stat,
Relation  rel,
BlockNumber  blkno,
BufferAccessStrategy  bstrategy 
)
static

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

483 {
484  Buffer buf;
485  Page page;
486 
487  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
488  LockBuffer(buf, GIST_SHARE);
489  gistcheckpage(rel, buf);
490  page = BufferGetPage(buf);
491 
492  if (GistPageIsLeaf(page))
493  {
495  PageGetMaxOffsetNumber(page));
496  }
497  else
498  {
499  /* root or node */
500  }
501 
502  UnlockReleaseBuffer(buf);
503 }
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3315
static char * buf
Definition: pg_test_fsync.c:65
#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:3529
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:722
#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:553
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 439 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_UNUSED_PAGE, MAXALIGN, PageGetMaxOffsetNumber, PageGetSpecialPointer, PageGetSpecialSize, and pgstat_index_page().

Referenced by pgstat_relation().

441 {
442  Buffer buf;
443  Page page;
444 
445  buf = _hash_getbuf_with_strategy(rel, blkno, HASH_READ, 0, bstrategy);
446  page = BufferGetPage(buf);
447 
448  if (PageGetSpecialSize(page) == MAXALIGN(sizeof(HashPageOpaqueData)))
449  {
450  HashPageOpaque opaque;
451 
452  opaque = (HashPageOpaque) PageGetSpecialPointer(page);
453  switch (opaque->hasho_flag)
454  {
455  case LH_UNUSED_PAGE:
456  stat->free_space += BLCKSZ;
457  break;
458  case LH_BUCKET_PAGE:
459  case LH_OVERFLOW_PAGE:
461  PageGetMaxOffsetNumber(page));
462  break;
463  case LH_BITMAP_PAGE:
464  case LH_META_PAGE:
465  default:
466  break;
467  }
468  }
469  else
470  {
471  /* maybe corrupted */
472  }
473 
474  _hash_relbuf(rel, buf);
475 }
#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:218
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
#define LH_UNUSED_PAGE
Definition: hash.h:52
#define HASH_READ
Definition: hash.h:254
static char * buf
Definition: pg_test_fsync.c:65
#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:245
#define LH_BUCKET_PAGE
Definition: hash.h:54
#define PageGetSpecialPointer(page)
Definition: bufpage.h:323
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:84
#define MAXALIGN(LEN)
Definition: c.h:584
#define PageGetSpecialSize(page)
Definition: bufpage.h:297
static void pgstat_index_page(pgstattuple_type *stat, Page page, OffsetNumber minoff, OffsetNumber maxoff)
Definition: pgstattuple.c:553
uint16 hasho_flag
Definition: hash.h:80
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74
static Datum pgstat_heap ( Relation  rel,
FunctionCallInfo  fcinfo 
)
static

Definition at line 312 of file pgstattuple.c.

References AccessShareLock, BlockIdGetBlockNumber, 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, ItemPointerData::ip_blkid, 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().

313 {
314  HeapScanDesc scan;
315  HeapTuple tuple;
316  BlockNumber nblocks;
317  BlockNumber block = 0; /* next block to count free space in */
318  BlockNumber tupblock;
319  Buffer buffer;
320  pgstattuple_type stat = {0};
321  SnapshotData SnapshotDirty;
322 
323  /* Disable syncscan because we assume we scan from block zero upwards */
324  scan = heap_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false);
325  InitDirtySnapshot(SnapshotDirty);
326 
327  nblocks = scan->rs_nblocks; /* # blocks to be scanned */
328 
329  /* scan the relation */
330  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
331  {
333 
334  /* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
336 
337  if (HeapTupleSatisfiesVisibility(tuple, &SnapshotDirty, scan->rs_cbuf))
338  {
339  stat.tuple_len += tuple->t_len;
340  stat.tuple_count++;
341  }
342  else
343  {
344  stat.dead_tuple_len += tuple->t_len;
345  stat.dead_tuple_count++;
346  }
347 
349 
350  /*
351  * To avoid physically reading the table twice, try to do the
352  * free-space scan in parallel with the heap scan. However,
353  * heap_getnext may find no tuples on a given page, so we cannot
354  * simply examine the pages returned by the heap scan.
355  */
356  tupblock = BlockIdGetBlockNumber(&tuple->t_self.ip_blkid);
357 
358  while (block <= tupblock)
359  {
361 
362  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
363  RBM_NORMAL, scan->rs_strategy);
364  LockBuffer(buffer, BUFFER_LOCK_SHARE);
366  UnlockReleaseBuffer(buffer);
367  block++;
368  }
369  }
370 
371  while (block < nblocks)
372  {
374 
375  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
376  RBM_NORMAL, scan->rs_strategy);
377  LockBuffer(buffer, BUFFER_LOCK_SHARE);
379  UnlockReleaseBuffer(buffer);
380  block++;
381  }
382 
383  heap_endscan(scan);
385 
386  stat.table_len = (uint64) nblocks *BLCKSZ;
387 
388  return build_pgstattuple_type(&stat, fcinfo);
389 }
#define BlockIdGetBlockNumber(blockId)
Definition: block.h:115
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1581
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:1263
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:3315
BlockIdData ip_blkid
Definition: itemptr.h:38
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:639
#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:59
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1781
BufferAccessStrategy rs_strategy
Definition: relscan.h:63
HeapScanDesc heap_beginscan_strat(Relation relation, Snapshot snapshot, int nkeys, ScanKey key, bool allow_strat, bool allow_sync)
Definition: heapam.c:1412
Buffer rs_cbuf
Definition: relscan.h:70
#define NULL
Definition: c.h:226
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
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 509 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().

511 {
512  BlockNumber nblocks;
513  BlockNumber blkno;
514  BufferAccessStrategy bstrategy;
515  pgstattuple_type stat = {0};
516 
517  /* prepare access strategy for this index */
518  bstrategy = GetAccessStrategy(BAS_BULKREAD);
519 
520  blkno = start;
521  for (;;)
522  {
523  /* Get the current relation length */
525  nblocks = RelationGetNumberOfBlocks(rel);
527 
528  /* Quit if we've scanned the whole relation */
529  if (blkno >= nblocks)
530  {
531  stat.table_len = (uint64) nblocks *BLCKSZ;
532 
533  break;
534  }
535 
536  for (; blkno < nblocks; blkno++)
537  {
539 
540  pagefn(&stat, rel, blkno, bstrategy);
541  }
542  }
543 
545 
546  return build_pgstattuple_type(&stat, fcinfo);
547 }
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:525
#define ExclusiveLock
Definition: lockdefs.h:44
#define AccessShareLock
Definition: lockdefs.h:36
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1263
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:97
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 553 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().

555 {
556  OffsetNumber i;
557 
558  stat->free_space += PageGetFreeSpace(page);
559 
560  for (i = minoff; i <= maxoff; i = OffsetNumberNext(i))
561  {
562  ItemId itemid = PageGetItemId(page, i);
563 
564  if (ItemIdIsDead(itemid))
565  {
566  stat->dead_tuple_count++;
567  stat->dead_tuple_len += ItemIdGetLength(itemid);
568  }
569  else
570  {
571  stat->tuple_count++;
572  stat->tuple_len += ItemIdGetLength(itemid);
573  }
574  }
575 }
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:232
#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_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;
296  default:
297  err = "unknown";
298  break;
299  }
300 
301  ereport(ERROR,
302  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
303  errmsg("\"%s\" (%s) is not supported",
304  RelationGetRelationName(rel), err)));
305  return 0; /* should not happen */
306 }
static void pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:439
static void pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:395
#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:167
int errcode(int sqlerrcode)
Definition: elog.c:575
Form_pg_class rd_rel
Definition: rel.h:113
#define HASH_AM_OID
Definition: pg_am.h:73
#define RELKIND_COMPOSITE_TYPE
Definition: pg_class.h:165
#define GIN_AM_OID
Definition: pg_am.h:79
#define ERROR
Definition: elog.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:433
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:166
#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:312
#define RELKIND_TOASTVALUE
Definition: pg_class.h:163
#define HASH_METAPAGE
Definition: hash.h:146
static Datum pgstat_index(Relation rel, BlockNumber start, pgstat_page pagefn, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:509
#define GIST_AM_OID
Definition: pg_am.h:76
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:530
static void pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:481
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_P, PG_RETURN_DATUM, pgstat_relation(), relation_openrv(), superuser(), and textToQualifiedNameList().

167 {
168  text *relname = PG_GETARG_TEXT_P(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:1195
#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:2857
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3071
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:435
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_P, PG_RETURN_DATUM, pgstat_relation(), relation_openrv(), and textToQualifiedNameList().

193 {
194  text *relname = PG_GETARG_TEXT_P(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:1195
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:2857
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3071
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:435
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:231
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1117
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:231
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1117
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.