PostgreSQL Source Code  git master
pgstatapprox.c File Reference
#include "postgres.h"
#include "access/visibilitymap.h"
#include "access/transam.h"
#include "access/xact.h"
#include "access/multixact.h"
#include "access/htup_details.h"
#include "catalog/namespace.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/freespace.h"
#include "storage/procarray.h"
#include "storage/lmgr.h"
#include "utils/builtins.h"
#include "utils/tqual.h"
#include "commands/vacuum.h"
Include dependency graph for pgstatapprox.c:

Go to the source code of this file.

Data Structures

struct  output_type
 

Macros

#define NUM_OUTPUT_COLUMNS   10
 

Typedefs

typedef struct output_type output_type
 

Functions

 PG_FUNCTION_INFO_V1 (pgstattuple_approx)
 
 PG_FUNCTION_INFO_V1 (pgstattuple_approx_v1_5)
 
Datum pgstattuple_approx_internal (Oid relid, FunctionCallInfo fcinfo)
 
static void statapprox_heap (Relation rel, output_type *stat)
 
Datum pgstattuple_approx (PG_FUNCTION_ARGS)
 
Datum pgstattuple_approx_v1_5 (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ NUM_OUTPUT_COLUMNS

#define NUM_OUTPUT_COLUMNS   10

Definition at line 50 of file pgstatapprox.c.

Referenced by pgstattuple_approx_internal().

Typedef Documentation

◆ output_type

Function Documentation

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( pgstattuple_approx  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( pgstattuple_approx_v1_5  )

◆ pgstattuple_approx()

Datum pgstattuple_approx ( PG_FUNCTION_ARGS  )

Definition at line 216 of file pgstatapprox.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_OID, PG_RETURN_DATUM, pgstattuple_approx_internal(), and superuser().

217 {
218  Oid relid = PG_GETARG_OID(0);
219 
220  if (!superuser())
221  ereport(ERROR,
222  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
223  (errmsg("must be superuser to use pgstattuple functions"))));
224 
226 }
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
Datum pgstattuple_approx_internal(Oid relid, FunctionCallInfo fcinfo)
Definition: pgstatapprox.c:244
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ pgstattuple_approx_internal()

Datum pgstattuple_approx_internal ( Oid  relid,
FunctionCallInfo  fcinfo 
)

Definition at line 244 of file pgstatapprox.c.

References AccessShareLock, output_type::dead_tuple_count, output_type::dead_tuple_len, output_type::dead_tuple_percent, elog, ereport, errcode(), errmsg(), ERROR, Float8GetDatum(), output_type::free_percent, output_type::free_space, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum, i, Int64GetDatum(), tupleDesc::natts, NUM_OUTPUT_COLUMNS, RelationData::rd_rel, relation_close(), RELATION_IS_OTHER_TEMP, relation_open(), RelationGetRelationName, RELKIND_MATVIEW, RELKIND_RELATION, output_type::scanned_percent, stat, statapprox_heap(), output_type::table_len, output_type::tuple_count, output_type::tuple_len, output_type::tuple_percent, TYPEFUNC_COMPOSITE, and values.

Referenced by pgstattuple_approx(), and pgstattuple_approx_v1_5().

245 {
246  Relation rel;
247  output_type stat = {0};
248  TupleDesc tupdesc;
249  bool nulls[NUM_OUTPUT_COLUMNS];
251  HeapTuple ret;
252  int i = 0;
253 
254  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
255  elog(ERROR, "return type must be a row type");
256 
257  if (tupdesc->natts != NUM_OUTPUT_COLUMNS)
258  elog(ERROR, "incorrect number of output arguments");
259 
260  rel = relation_open(relid, AccessShareLock);
261 
262  /*
263  * Reject attempts to read non-local temporary relations; we would be
264  * likely to get wrong data since we have no visibility into the owning
265  * session's local buffers.
266  */
267  if (RELATION_IS_OTHER_TEMP(rel))
268  ereport(ERROR,
269  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
270  errmsg("cannot access temporary tables of other sessions")));
271 
272  /*
273  * We support only ordinary relations and materialised views, because we
274  * depend on the visibility map and free space map for our estimates about
275  * unscanned pages.
276  */
277  if (!(rel->rd_rel->relkind == RELKIND_RELATION ||
278  rel->rd_rel->relkind == RELKIND_MATVIEW))
279  ereport(ERROR,
280  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
281  errmsg("\"%s\" is not a table or materialized view",
282  RelationGetRelationName(rel))));
283 
284  statapprox_heap(rel, &stat);
285 
287 
288  memset(nulls, 0, sizeof(nulls));
289 
290  values[i++] = Int64GetDatum(stat.table_len);
291  values[i++] = Float8GetDatum(stat.scanned_percent);
292  values[i++] = Int64GetDatum(stat.tuple_count);
293  values[i++] = Int64GetDatum(stat.tuple_len);
294  values[i++] = Float8GetDatum(stat.tuple_percent);
295  values[i++] = Int64GetDatum(stat.dead_tuple_count);
296  values[i++] = Int64GetDatum(stat.dead_tuple_len);
297  values[i++] = Float8GetDatum(stat.dead_tuple_percent);
298  values[i++] = Int64GetDatum(stat.free_space);
299  values[i++] = Float8GetDatum(stat.free_percent);
300 
301  ret = heap_form_tuple(tupdesc, values, nulls);
302  return HeapTupleGetDatum(ret);
303 }
#define NUM_OUTPUT_COLUMNS
Definition: pgstatapprox.c:50
uint64 dead_tuple_count
Definition: pgstatapprox.c:43
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define RELKIND_MATVIEW
Definition: pg_class.h:165
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1266
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
double dead_tuple_percent
Definition: pgstatapprox.c:45
Form_pg_class rd_rel
Definition: rel.h:114
uint64 tuple_count
Definition: pgstatapprox.c:40
double free_percent
Definition: pgstatapprox.c:47
uint64 table_len
Definition: pgstatapprox.c:38
int natts
Definition: tupdesc.h:79
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1810
double tuple_percent
Definition: pgstatapprox.c:42
uint64 tuple_len
Definition: pgstatapprox.c:41
#define ERROR
Definition: elog.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:445
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1786
#define ereport(elevel, rest)
Definition: elog.h:122
#define stat(a, b)
Definition: win32_port.h:266
uintptr_t Datum
Definition: postgres.h:372
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:542
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:230
static void statapprox_heap(Relation rel, output_type *stat)
Definition: pgstatapprox.c:63
static Datum values[MAXATTR]
Definition: bootstrap.c:164
uint64 free_space
Definition: pgstatapprox.c:46
uint64 dead_tuple_len
Definition: pgstatapprox.c:44
int errmsg(const char *fmt,...)
Definition: elog.c:797
uint64 scanned_percent
Definition: pgstatapprox.c:39
int i
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1120
#define elog
Definition: elog.h:219
#define RELKIND_RELATION
Definition: pg_class.h:160

◆ pgstattuple_approx_v1_5()

Datum pgstattuple_approx_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 236 of file pgstatapprox.c.

References PG_GETARG_OID, PG_RETURN_DATUM, and pgstattuple_approx_internal().

237 {
238  Oid relid = PG_GETARG_OID(0);
239 
241 }
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
Datum pgstattuple_approx_internal(Oid relid, FunctionCallInfo fcinfo)
Definition: pgstatapprox.c:244

◆ statapprox_heap()

static void statapprox_heap ( Relation  rel,
output_type stat 
)
static

Definition at line 63 of file pgstatapprox.c.

References Assert, BAS_BULKREAD, buf, BUFFER_LOCK_SHARE, BufferGetPage, BufferIsValid, CHECK_FOR_INTERRUPTS, output_type::dead_tuple_count, output_type::dead_tuple_len, output_type::dead_tuple_percent, elog, ERROR, FirstOffsetNumber, output_type::free_percent, output_type::free_space, GetAccessStrategy(), GetOldestXmin(), GetRecordedFreeSpace(), HEAPTUPLE_DEAD, HEAPTUPLE_DELETE_IN_PROGRESS, HEAPTUPLE_INSERT_IN_PROGRESS, HEAPTUPLE_LIVE, HEAPTUPLE_RECENTLY_DEAD, HeapTupleSatisfiesVacuum(), InvalidBuffer, ItemIdGetLength, ItemIdIsDead, ItemIdIsNormal, ItemIdIsRedirected, ItemIdIsUsed, ItemPointerSet, LockBuffer(), MAIN_FORKNUM, OffsetNumberNext, OldestXmin, PageGetHeapFreeSpace(), PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageIsEmpty, PageIsNew, PROCARRAY_FLAGS_VACUUM, RBM_NORMAL, ReadBufferExtended(), RelationGetNumberOfBlocks, RelationGetRelid, ReleaseBuffer(), output_type::scanned_percent, SizeOfPageHeaderData, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, output_type::table_len, output_type::tuple_count, output_type::tuple_len, output_type::tuple_percent, UnlockReleaseBuffer(), vac_estimate_reltuples(), and VM_ALL_VISIBLE.

Referenced by pgstattuple_approx_internal().

64 {
65  BlockNumber scanned,
66  nblocks,
67  blkno;
68  Buffer vmbuffer = InvalidBuffer;
69  BufferAccessStrategy bstrategy;
71  uint64 misc_count = 0;
72 
73  OldestXmin = GetOldestXmin(rel, PROCARRAY_FLAGS_VACUUM);
74  bstrategy = GetAccessStrategy(BAS_BULKREAD);
75 
76  nblocks = RelationGetNumberOfBlocks(rel);
77  scanned = 0;
78 
79  for (blkno = 0; blkno < nblocks; blkno++)
80  {
81  Buffer buf;
82  Page page;
83  OffsetNumber offnum,
84  maxoff;
85  Size freespace;
86 
88 
89  /*
90  * If the page has only visible tuples, then we can find out the free
91  * space from the FSM and move on.
92  */
93  if (VM_ALL_VISIBLE(rel, blkno, &vmbuffer))
94  {
95  freespace = GetRecordedFreeSpace(rel, blkno);
96  stat->tuple_len += BLCKSZ - freespace;
97  stat->free_space += freespace;
98  continue;
99  }
100 
101  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno,
102  RBM_NORMAL, bstrategy);
103 
105 
106  page = BufferGetPage(buf);
107 
108  /*
109  * It's not safe to call PageGetHeapFreeSpace() on new pages, so we
110  * treat them as being free space for our purposes.
111  */
112  if (!PageIsNew(page))
113  stat->free_space += PageGetHeapFreeSpace(page);
114  else
115  stat->free_space += BLCKSZ - SizeOfPageHeaderData;
116 
117  if (PageIsNew(page) || PageIsEmpty(page))
118  {
119  UnlockReleaseBuffer(buf);
120  continue;
121  }
122 
123  scanned++;
124 
125  /*
126  * Look at each tuple on the page and decide whether it's live or
127  * dead, then count it and its size. Unlike lazy_scan_heap, we can
128  * afford to ignore problems and special cases.
129  */
130  maxoff = PageGetMaxOffsetNumber(page);
131 
132  for (offnum = FirstOffsetNumber;
133  offnum <= maxoff;
134  offnum = OffsetNumberNext(offnum))
135  {
136  ItemId itemid;
137  HeapTupleData tuple;
138 
139  itemid = PageGetItemId(page, offnum);
140 
141  if (!ItemIdIsUsed(itemid) || ItemIdIsRedirected(itemid) ||
142  ItemIdIsDead(itemid))
143  {
144  continue;
145  }
146 
147  Assert(ItemIdIsNormal(itemid));
148 
149  ItemPointerSet(&(tuple.t_self), blkno, offnum);
150 
151  tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
152  tuple.t_len = ItemIdGetLength(itemid);
153  tuple.t_tableOid = RelationGetRelid(rel);
154 
155  /*
156  * We count live and dead tuples, but we also need to add up
157  * others in order to feed vac_estimate_reltuples.
158  */
159  switch (HeapTupleSatisfiesVacuum(&tuple, OldestXmin, buf))
160  {
162  misc_count++;
163  /* Fall through */
164  case HEAPTUPLE_DEAD:
165  stat->dead_tuple_len += tuple.t_len;
166  stat->dead_tuple_count++;
167  break;
168  case HEAPTUPLE_LIVE:
169  stat->tuple_len += tuple.t_len;
170  stat->tuple_count++;
171  break;
174  misc_count++;
175  break;
176  default:
177  elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
178  break;
179  }
180  }
181 
182  UnlockReleaseBuffer(buf);
183  }
184 
185  stat->table_len = (uint64) nblocks * BLCKSZ;
186 
187  stat->tuple_count = vac_estimate_reltuples(rel, false, nblocks, scanned,
188  stat->tuple_count + misc_count);
189 
190  /*
191  * Calculate percentages if the relation has one or more pages.
192  */
193  if (nblocks != 0)
194  {
195  stat->scanned_percent = 100 * scanned / nblocks;
196  stat->tuple_percent = 100.0 * stat->tuple_len / stat->table_len;
197  stat->dead_tuple_percent = 100.0 * stat->dead_tuple_len / stat->table_len;
198  stat->free_percent = 100.0 * stat->free_space / stat->table_len;
199  }
200 
201  if (BufferIsValid(vmbuffer))
202  {
203  ReleaseBuffer(vmbuffer);
204  vmbuffer = InvalidBuffer;
205  }
206 }
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:542
uint64 dead_tuple_count
Definition: pgstatapprox.c:43
#define PageIsEmpty(page)
Definition: bufpage.h:218
#define ItemIdIsRedirected(itemId)
Definition: itemid.h:105
double vac_estimate_reltuples(Relation relation, bool is_analyze, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
Definition: vacuum.c:777
uint32 TransactionId
Definition: c.h:445
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define InvalidBuffer
Definition: buf.h:25
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
Definition: tqual.c:1164
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
#define PROCARRAY_FLAGS_VACUUM
Definition: procarray.h:52
double dead_tuple_percent
Definition: pgstatapprox.c:45
#define SizeOfPageHeaderData
Definition: bufpage.h:212
#define ItemIdIsDead(itemId)
Definition: itemid.h:112
uint64 tuple_count
Definition: pgstatapprox.c:40
double free_percent
Definition: pgstatapprox.c:47
uint64 table_len
Definition: pgstatapprox.c:38
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
uint16 OffsetNumber
Definition: off.h:24
HeapTupleHeader t_data
Definition: htup.h:67
double tuple_percent
Definition: pgstatapprox.c:42
uint64 tuple_len
Definition: pgstatapprox.c:41
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:662
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
static char * buf
Definition: pg_test_fsync.c:67
#define FirstOffsetNumber
Definition: off.h:27
static TransactionId OldestXmin
Definition: vacuumlazy.c:138
Oid t_tableOid
Definition: htup.h:66
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
Size GetRecordedFreeSpace(Relation rel, BlockNumber heapBlk)
Definition: freespace.c:270
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
TransactionId GetOldestXmin(Relation rel, int flags)
Definition: procarray.c:1315
#define Assert(condition)
Definition: c.h:670
#define ItemIdIsNormal(itemId)
Definition: itemid.h:98
#define VM_ALL_VISIBLE(r, b, v)
Definition: visibilitymap.h:32
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
size_t Size
Definition: c.h:404
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
uint64 free_space
Definition: pgstatapprox.c:46
#define PageIsNew(page)
Definition: bufpage.h:225
uint64 dead_tuple_len
Definition: pgstatapprox.c:44
uint64 scanned_percent
Definition: pgstatapprox.c:39
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define elog
Definition: elog.h:219
int Buffer
Definition: buf.h:23
#define RelationGetRelid(relation)
Definition: rel.h:425
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:105