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:152
#define INT64_FORMAT
Definition: c.h:548
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2222
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2173
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
int i
Definition: isn.c:73
#define NCHARS
#define NCOLUMNS
#define snprintf
Definition: port.h:238

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

407 {
408  Buffer buf;
409  Page page;
410 
411  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
413  page = BufferGetPage(buf);
414 
415  /* Page is valid, see what to do with it */
416  if (PageIsNew(page))
417  {
418  /* fully empty page */
419  stat->free_space += BLCKSZ;
420  }
421  else
422  {
423  BTPageOpaque opaque;
424 
425  opaque = BTPageGetOpaque(page);
426  if (P_IGNORE(opaque))
427  {
428  /* deleted or half-dead page */
429  stat->free_space += BLCKSZ;
430  }
431  else if (P_ISLEAF(opaque))
432  {
433  pgstat_index_page(stat, page, P_FIRSTDATAKEY(opaque),
434  PageGetMaxOffsetNumber(page));
435  }
436  else
437  {
438  /* internal page */
439  }
440  }
441 
442  _bt_relbuf(rel, buf);
443 }
int Buffer
Definition: buf.h:23
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5131
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:792
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:404
@ RBM_NORMAL
Definition: bufmgr.h:45
Pointer Page
Definition: bufpage.h:78
static bool PageIsNew(Page page)
Definition: bufpage.h:230
static OffsetNumber PageGetMaxOffsetNumber(Page page)
Definition: bufpage.h:369
void _bt_relbuf(Relation rel, Buffer buf)
Definition: nbtpage.c:1023
#define P_ISLEAF(opaque)
Definition: nbtree.h:220
#define BTPageGetOpaque(page)
Definition: nbtree.h:73
#define P_FIRSTDATAKEY(opaque)
Definition: nbtree.h:369
#define BT_READ
Definition: nbtree.h:719
#define P_IGNORE(opaque)
Definition: nbtree.h:225
static char * buf
Definition: pg_test_fsync.c:73
static void pgstat_index_page(pgstattuple_type *stat, Page page, OffsetNumber minoff, OffsetNumber maxoff)
Definition: pgstattuple.c:563
@ MAIN_FORKNUM
Definition: relpath.h:50

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

493 {
494  Buffer buf;
495  Page page;
496 
497  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
499  gistcheckpage(rel, buf);
500  page = BufferGetPage(buf);
501 
502  if (GistPageIsLeaf(page))
503  {
505  PageGetMaxOffsetNumber(page));
506  }
507  else
508  {
509  /* root or node */
510  }
511 
513 }
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4913
#define GistPageIsLeaf(page)
Definition: gist.h:167
#define GIST_SHARE
Definition: gist_private.h:42
void gistcheckpage(Relation rel, Buffer buf)
Definition: gistutil.c:784
#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 449 of file pgstattuple.c.

451 {
452  Buffer buf;
453  Page page;
454 
455  buf = _hash_getbuf_with_strategy(rel, blkno, HASH_READ, 0, bstrategy);
456  page = BufferGetPage(buf);
457 
458  if (PageGetSpecialSize(page) == MAXALIGN(sizeof(HashPageOpaqueData)))
459  {
460  HashPageOpaque opaque;
461 
462  opaque = HashPageGetOpaque(page);
463  switch (opaque->hasho_flag & LH_PAGE_TYPE)
464  {
465  case LH_UNUSED_PAGE:
466  stat->free_space += BLCKSZ;
467  break;
468  case LH_BUCKET_PAGE:
469  case LH_OVERFLOW_PAGE:
471  PageGetMaxOffsetNumber(page));
472  break;
473  case LH_BITMAP_PAGE:
474  case LH_META_PAGE:
475  default:
476  break;
477  }
478  }
479  else
480  {
481  /* maybe corrupted */
482  }
483 
484  _hash_relbuf(rel, buf);
485 }
static uint16 PageGetSpecialSize(Page page)
Definition: bufpage.h:313
#define MAXALIGN(LEN)
Definition: c.h:811
#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 314 of file pgstattuple.c.

315 {
316  TableScanDesc scan;
317  HeapScanDesc hscan;
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  if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
327  ereport(ERROR,
328  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
329  errmsg("only heap AM is supported")));
330 
331  /* Disable syncscan because we assume we scan from block zero upwards */
332  scan = table_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false);
333  hscan = (HeapScanDesc) scan;
334 
335  InitDirtySnapshot(SnapshotDirty);
336 
337  nblocks = hscan->rs_nblocks; /* # blocks to be scanned */
338 
339  /* scan the relation */
340  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
341  {
343 
344  /* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
346 
347  if (HeapTupleSatisfiesVisibility(tuple, &SnapshotDirty, hscan->rs_cbuf))
348  {
349  stat.tuple_len += tuple->t_len;
350  stat.tuple_count++;
351  }
352  else
353  {
354  stat.dead_tuple_len += tuple->t_len;
355  stat.dead_tuple_count++;
356  }
357 
359 
360  /*
361  * To avoid physically reading the table twice, try to do the
362  * free-space scan in parallel with the heap scan. However,
363  * heap_getnext may find no tuples on a given page, so we cannot
364  * simply examine the pages returned by the heap scan.
365  */
366  tupblock = ItemPointerGetBlockNumber(&tuple->t_self);
367 
368  while (block <= tupblock)
369  {
371 
372  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
373  RBM_NORMAL, hscan->rs_strategy);
374  LockBuffer(buffer, BUFFER_LOCK_SHARE);
375  stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
376  UnlockReleaseBuffer(buffer);
377  block++;
378  }
379  }
380 
381  while (block < nblocks)
382  {
384 
385  buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
386  RBM_NORMAL, hscan->rs_strategy);
387  LockBuffer(buffer, BUFFER_LOCK_SHARE);
388  stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
389  UnlockReleaseBuffer(buffer);
390  block++;
391  }
392 
393  table_endscan(scan);
395 
396  stat.table_len = (uint64) nblocks * BLCKSZ;
397 
398  return build_pgstattuple_type(&stat, fcinfo);
399 }
uint32 BlockNumber
Definition: block.h:31
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:193
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:194
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:991
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1251
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:109
bool HeapTupleSatisfiesVisibility(HeapTuple htup, Snapshot snapshot, Buffer buffer)
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition: itemptr.h:103
#define AccessShareLock
Definition: lockdefs.h:36
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
static Datum build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:90
@ ForwardScanDirection
Definition: sdir.h:28
#define SnapshotAny
Definition: snapmgr.h:33
#define InitDirtySnapshot(snapshotdata)
Definition: snapmgr.h:40
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
BufferAccessStrategy rs_strategy
Definition: heapam.h:70
Buffer rs_cbuf
Definition: heapam.h:67
BlockNumber rs_nblocks
Definition: heapam.h:58
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
Form_pg_class rd_rel
Definition: rel.h:111
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:1029
static TableScanDesc table_beginscan_strat(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key, bool allow_strat, bool allow_sync)
Definition: tableam.h:942

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

521 {
522  BlockNumber nblocks;
523  BlockNumber blkno;
524  BufferAccessStrategy bstrategy;
525  pgstattuple_type stat = {0};
526 
527  /* prepare access strategy for this index */
528  bstrategy = GetAccessStrategy(BAS_BULKREAD);
529 
530  blkno = start;
531  for (;;)
532  {
533  /* Get the current relation length */
535  nblocks = RelationGetNumberOfBlocks(rel);
537 
538  /* Quit if we've scanned the whole relation */
539  if (blkno >= nblocks)
540  {
541  stat.table_len = (uint64) nblocks * BLCKSZ;
542 
543  break;
544  }
545 
546  for (; blkno < nblocks; blkno++)
547  {
549 
550  pagefn(&stat, rel, blkno, bstrategy);
551  }
552  }
553 
555 
556  return build_pgstattuple_type(&stat, fcinfo);
557 }
@ BAS_BULKREAD
Definition: bufmgr.h:36
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:277
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:541
return str start
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:430
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:480
#define ExclusiveLock
Definition: lockdefs.h:42

References AccessShareLock, BAS_BULKREAD, build_pgstattuple_type(), CHECK_FOR_INTERRUPTS, ExclusiveLock, GetAccessStrategy(), LockRelationForExtension(), relation_close(), RelationGetNumberOfBlocks, start, 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 563 of file pgstattuple.c.

565 {
566  OffsetNumber i;
567 
568  stat->free_space += PageGetFreeSpace(page);
569 
570  for (i = minoff; i <= maxoff; i = OffsetNumberNext(i))
571  {
572  ItemId itemid = PageGetItemId(page, i);
573 
574  if (ItemIdIsDead(itemid))
575  {
576  stat->dead_tuple_count++;
577  stat->dead_tuple_len += ItemIdGetLength(itemid);
578  }
579  else
580  {
581  stat->tuple_count++;
582  stat->tuple_len += ItemIdGetLength(itemid);
583  }
584  }
585 }
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:907
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:240
#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  /* see pgstatindex_impl */
263  if (!rel->rd_index->indisvalid)
264  ereport(ERROR,
265  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
266  errmsg("index \"%s\" is not valid",
267  RelationGetRelationName(rel))));
268 
269  switch (rel->rd_rel->relam)
270  {
271  case BTREE_AM_OID:
272  return pgstat_index(rel, BTREE_METAPAGE + 1,
273  pgstat_btree_page, fcinfo);
274  case HASH_AM_OID:
275  return pgstat_index(rel, HASH_METAPAGE + 1,
276  pgstat_hash_page, fcinfo);
277  case GIST_AM_OID:
278  return pgstat_index(rel, GIST_ROOT_BLKNO + 1,
279  pgstat_gist_page, fcinfo);
280  case GIN_AM_OID:
281  err = "gin index";
282  break;
283  case SPGIST_AM_OID:
284  err = "spgist index";
285  break;
286  case BRIN_AM_OID:
287  err = "brin index";
288  break;
289  default:
290  err = "unknown index";
291  break;
292  }
293  ereport(ERROR,
294  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
295  errmsg("index \"%s\" (%s) is not supported",
296  RelationGetRelationName(rel), err)));
297  }
298  else
299  {
300  ereport(ERROR,
301  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
302  errmsg("cannot get tuple-level statistics for relation \"%s\"",
304  errdetail_relkind_not_supported(rel->rd_rel->relkind)));
305  }
306 
307  return 0; /* should not happen */
308 }
void err(int eval, const char *fmt,...)
Definition: err.c:43
#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:519
static void pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:491
static void pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:405
static Datum pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
Definition: pgstattuple.c:314
static void pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Definition: pgstattuple.c:449
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:658
Form_pg_index rd_index
Definition: rel.h:192

References BTREE_METAPAGE, ereport, err(), 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_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(const List *names)
Definition: namespace.c:3539
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:137
Definition: c.h:687
bool superuser(void)
Definition: superuser.c:46
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3399

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:47

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.