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

◆ NCOLUMNS

#define NCOLUMNS   9

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.

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 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define INT64_FORMAT
Definition: c.h:483
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2135
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2086
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
int i
Definition: isn.c:73
#define NCHARS
#define NCOLUMNS
#define snprintf
Definition: port.h:225

References BuildTupleFromCStrings(), elog, ERROR, get_call_result_type(), HeapTupleGetDatum, i, INT64_FORMAT, NCHARS, NCOLUMNS, snprintf, TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, and values.

Referenced by pgstat_heap(), and pgstat_index().

◆ 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 398 of file pgstattuple.c.

400 {
401  Buffer buf;
402  Page page;
403 
404  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
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 = BTPageGetOpaque(page);
419  if (P_IGNORE(opaque))
420  {
421  /* deleted or half-dead 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  /* internal page */
432  }
433  }
434 
435  _bt_relbuf(rel, buf);
436 }
int Buffer
Definition: buf.h:23
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4156
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:749
@ RBM_NORMAL
Definition: bufmgr.h:39
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
Pointer Page
Definition: bufpage.h:78
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:356
#define PageIsNew(page)
Definition: bufpage.h:228
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:1035
#define P_ISLEAF(opaque)
Definition: nbtree.h:221
#define BTPageGetOpaque(page)
Definition: nbtree.h:73
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:371
#define BT_READ
Definition: nbtree.h:714
#define P_IGNORE(opaque)
Definition: nbtree.h:226
static char * buf
Definition: pg_test_fsync.c:67
static void pgstat_index_page(pgstattuple_type *stat, Page page, OffsetNumber minoff, OffsetNumber maxoff)
Definition: pgstattuple.c:556
@ MAIN_FORKNUM
Definition: relpath.h:43

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

Referenced by pgstat_relation().

◆ pgstat_gist_page()

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

Definition at line 484 of file pgstattuple.c.

486 {
487  Buffer buf;
488  Page page;
489 
490  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
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 
506 }
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3938
#define GistPageIsLeaf(page)
Definition: gist.h:167
#define GIST_SHARE
Definition: gist_private.h:42
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:785
#define FirstOffsetNumber
Definition: off.h:27

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

Referenced by pgstat_relation().

◆ pgstat_hash_page()

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

Definition at line 442 of file pgstattuple.c.

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 = HashPageGetOpaque(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 PageGetSpecialSize(page)
Definition: bufpage.h:299
#define MAXALIGN(LEN)
Definition: c.h:757
#define HashPageGetOpaque(page)
Definition: hash.h:88
#define LH_BUCKET_PAGE
Definition: hash.h:55
#define LH_UNUSED_PAGE
Definition: hash.h:53
#define LH_META_PAGE
Definition: hash.h:57
#define HASH_READ
Definition: hash.h:339
#define LH_PAGE_TYPE
Definition: hash.h:63
#define LH_BITMAP_PAGE
Definition: hash.h:56
#define LH_OVERFLOW_PAGE
Definition: hash.h:54
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:266
Buffer _hash_getbuf_with_strategy(Relation rel, BlockNumber blkno, int access, int flags, BufferAccessStrategy bstrategy)
Definition: hashpage.c:239
uint16 hasho_flag
Definition: hash.h:82

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

Referenced by pgstat_relation().

◆ pgstat_heap()

static Datum pgstat_heap ( Relation  rel,
FunctionCallInfo  fcinfo 
)
static

Definition at line 307 of file pgstattuple.c.

308 {
309  TableScanDesc scan;
310  HeapScanDesc hscan;
311  HeapTuple tuple;
312  BlockNumber nblocks;
313  BlockNumber block = 0; /* next block to count free space in */
314  BlockNumber tupblock;
315  Buffer buffer;
316  pgstattuple_type stat = {0};
317  SnapshotData SnapshotDirty;
318 
319  if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
320  ereport(ERROR,
321  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
322  errmsg("only heap AM is supported")));
323 
324  /* Disable syncscan because we assume we scan from block zero upwards */
325  scan = table_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false);
326  hscan = (HeapScanDesc) scan;
327 
328  InitDirtySnapshot(SnapshotDirty);
329 
330  nblocks = hscan->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, hscan->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, hscan->rs_strategy);
367  LockBuffer(buffer, BUFFER_LOCK_SHARE);
368  stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
369  UnlockReleaseBuffer(buffer);
370  block++;
371  }
372  }
373 
374  while (block < nblocks)
375  {
377 
378  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
379  RBM_NORMAL, hscan->rs_strategy);
380  LockBuffer(buffer, BUFFER_LOCK_SHARE);
381  stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
382  UnlockReleaseBuffer(buffer);
383  block++;
384  }
385 
386  table_endscan(scan);
388 
389  stat.table_len = (uint64) nblocks * BLCKSZ;
390 
391  return build_pgstattuple_type(&stat, fcinfo);
392 }
uint32 BlockNumber
Definition: block.h:31
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:991
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ereport(elevel,...)
Definition: elog.h:143
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1296
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:79
bool HeapTupleSatisfiesVisibility(HeapTuple tup, Snapshot snapshot, Buffer buffer)
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
#define AccessShareLock
Definition: lockdefs.h:36
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static Datum build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:90
@ ForwardScanDirection
Definition: sdir.h:26
#define SnapshotAny
Definition: snapmgr.h:67
#define InitDirtySnapshot(snapshotdata)
Definition: snapmgr.h:74
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
BufferAccessStrategy rs_strategy
Definition: heapam.h:64
Buffer rs_cbuf
Definition: heapam.h:60
BlockNumber rs_nblocks
Definition: heapam.h:52
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
Form_pg_class rd_rel
Definition: rel.h:109
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:993
static TableScanDesc table_beginscan_strat(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key, bool allow_strat, bool allow_sync)
Definition: tableam.h:909

References AccessShareLock, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, build_pgstattuple_type(), CHECK_FOR_INTERRUPTS, ereport, errcode(), errmsg(), ERROR, ForwardScanDirection, 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, HeapTupleData::t_len, HeapTupleData::t_self, table_beginscan_strat(), table_endscan(), and UnlockReleaseBuffer().

Referenced by pgstat_relation().

◆ pgstat_index()

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

Definition at line 512 of file pgstattuple.c.

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 }
@ BAS_BULKREAD
Definition: bufmgr.h:30
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:216
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:541
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:431
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:481
#define ExclusiveLock
Definition: lockdefs.h:42

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

Referenced by pgstat_relation().

◆ pgstat_index_page()

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

Definition at line 556 of file pgstattuple.c.

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 }
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:907
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:234
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
uint16 OffsetNumber
Definition: off.h:24

References i, ItemIdGetLength, ItemIdIsDead, OffsetNumberNext, PageGetFreeSpace(), and PageGetItemId.

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

◆ pgstat_relation()

static Datum pgstat_relation ( Relation  rel,
FunctionCallInfo  fcinfo 
)
static

Definition at line 241 of file pgstattuple.c.

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  if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind) ||
256  rel->rd_rel->relkind == RELKIND_SEQUENCE)
257  {
258  return pgstat_heap(rel, fcinfo);
259  }
260  else if (rel->rd_rel->relkind == RELKIND_INDEX)
261  {
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  ereport(ERROR,
287  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
288  errmsg("index \"%s\" (%s) is not supported",
289  RelationGetRelationName(rel), err)));
290  }
291  else
292  {
293  ereport(ERROR,
294  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
295  errmsg("cannot get tuple-level statistics for relation \"%s\"",
297  errdetail_relkind_not_supported(rel->rd_rel->relkind)));
298  }
299 
300  return 0; /* should not happen */
301 }
#define GIST_ROOT_BLKNO
Definition: gist_private.h:262
#define HASH_METAPAGE
Definition: hash.h:198
#define BTREE_METAPAGE
Definition: nbtree.h:148
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
static Datum pgstat_index(Relation rel, BlockNumber start, pgstat_page pagefn, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:512
static void pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:484
static void pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:398
static Datum pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:307
static void pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:442
#define RelationGetRelationName(relation)
Definition: rel.h:523
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:643

References BTREE_METAPAGE, ereport, errcode(), errdetail_relkind_not_supported(), 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().

◆ pgstattuple()

Datum pgstattuple ( PG_FUNCTION_ARGS  )

Definition at line 167 of file pgstattuple.c.

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 PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3108
NameData relname
Definition: pg_class.h:38
static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:241
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
Definition: c.h:622
bool superuser(void)
Definition: superuser.c:46
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3657

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

◆ pgstattuple_v1_5()

Datum pgstattuple_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 193 of file pgstattuple.c.

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 }

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

◆ pgstattuplebyid()

Datum pgstattuplebyid ( PG_FUNCTION_ARGS  )

Definition at line 208 of file pgstattuple.c.

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 PG_GETARG_OID(n)
Definition: fmgr.h:275
unsigned int Oid
Definition: postgres_ext.h:31
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48

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

◆ pgstattuplebyid_v1_5()

Datum pgstattuplebyid_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 226 of file pgstattuple.c.

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 }

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

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 42 of file pgstattuple.c.