PostgreSQL Source Code  git master
pgstatapprox.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/relation.h"
#include "access/transam.h"
#include "access/visibilitymap.h"
#include "access/xact.h"
#include "access/multixact.h"
#include "access/htup_details.h"
#include "catalog/namespace.h"
#include "catalog/pg_am_d.h"
#include "commands/vacuum.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 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 52 of file pgstatapprox.c.

Referenced by pgstattuple_approx_internal().

Typedef Documentation

◆ output_type

typedef struct output_type 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 224 of file pgstatapprox.c.

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

225 {
226  Oid relid = PG_GETARG_OID(0);
227 
228  if (!superuser())
229  ereport(ERROR,
230  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
231  (errmsg("must be superuser to use pgstattuple functions"))));
232 
234 }
int errcode(int sqlerrcode)
Definition: elog.c:570
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:270
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
Datum pgstattuple_approx_internal(Oid relid, FunctionCallInfo fcinfo)
Definition: pgstatapprox.c:252
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ pgstattuple_approx_internal()

Datum pgstattuple_approx_internal ( Oid  relid,
FunctionCallInfo  fcinfo 
)

Definition at line 252 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(), TupleDescData::natts, NUM_OUTPUT_COLUMNS, RelationData::rd_rel, relation_close(), RELATION_IS_OTHER_TEMP, relation_open(), RelationGetRelationName, 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().

253 {
254  Relation rel;
255  output_type stat = {0};
256  TupleDesc tupdesc;
257  bool nulls[NUM_OUTPUT_COLUMNS];
259  HeapTuple ret;
260  int i = 0;
261 
262  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
263  elog(ERROR, "return type must be a row type");
264 
265  if (tupdesc->natts != NUM_OUTPUT_COLUMNS)
266  elog(ERROR, "incorrect number of output arguments");
267 
268  rel = relation_open(relid, AccessShareLock);
269 
270  /*
271  * Reject attempts to read non-local temporary relations; we would be
272  * likely to get wrong data since we have no visibility into the owning
273  * session's local buffers.
274  */
275  if (RELATION_IS_OTHER_TEMP(rel))
276  ereport(ERROR,
277  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
278  errmsg("cannot access temporary tables of other sessions")));
279 
280  /*
281  * We support only ordinary relations and materialised views, because we
282  * depend on the visibility map and free space map for our estimates about
283  * unscanned pages.
284  */
285  if (!(rel->rd_rel->relkind == RELKIND_RELATION ||
286  rel->rd_rel->relkind == RELKIND_MATVIEW))
287  ereport(ERROR,
288  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
289  errmsg("\"%s\" is not a table or materialized view",
290  RelationGetRelationName(rel))));
291 
292  if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
293  ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
294  errmsg("only heap AM is supported")));
295 
296  statapprox_heap(rel, &stat);
297 
299 
300  memset(nulls, 0, sizeof(nulls));
301 
302  values[i++] = Int64GetDatum(stat.table_len);
303  values[i++] = Float8GetDatum(stat.scanned_percent);
304  values[i++] = Int64GetDatum(stat.tuple_count);
305  values[i++] = Int64GetDatum(stat.tuple_len);
306  values[i++] = Float8GetDatum(stat.tuple_percent);
307  values[i++] = Int64GetDatum(stat.dead_tuple_count);
308  values[i++] = Int64GetDatum(stat.dead_tuple_len);
309  values[i++] = Float8GetDatum(stat.dead_tuple_percent);
310  values[i++] = Int64GetDatum(stat.free_space);
311  values[i++] = Float8GetDatum(stat.free_percent);
312 
313  ret = heap_form_tuple(tupdesc, values, nulls);
314  return HeapTupleGetDatum(ret);
315 }
#define NUM_OUTPUT_COLUMNS
Definition: pgstatapprox.c:52
uint64 dead_tuple_count
Definition: pgstatapprox.c:45
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:570
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
double dead_tuple_percent
Definition: pgstatapprox.c:47
Form_pg_class rd_rel
Definition: rel.h:83
uint64 tuple_count
Definition: pgstatapprox.c:42
double free_percent
Definition: pgstatapprox.c:49
uint64 table_len
Definition: pgstatapprox.c:40
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1723
double tuple_percent
Definition: pgstatapprox.c:44
uint64 tuple_len
Definition: pgstatapprox.c:43
#define ERROR
Definition: elog.h:43
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
#define RelationGetRelationName(relation)
Definition: rel.h:453
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1699
#define ereport(elevel, rest)
Definition: elog.h:141
#define stat(a, b)
Definition: win32_port.h:255
uintptr_t Datum
Definition: postgres.h:367
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:549
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
static void statapprox_heap(Relation rel, output_type *stat)
Definition: pgstatapprox.c:65
static Datum values[MAXATTR]
Definition: bootstrap.c:167
uint64 free_space
Definition: pgstatapprox.c:48
uint64 dead_tuple_len
Definition: pgstatapprox.c:46
int errmsg(const char *fmt,...)
Definition: elog.c:784
uint64 scanned_percent
Definition: pgstatapprox.c:41
#define elog(elevel,...)
Definition: elog.h:226
int i

◆ pgstattuple_approx_v1_5()

Datum pgstattuple_approx_v1_5 ( PG_FUNCTION_ARGS  )

Definition at line 244 of file pgstatapprox.c.

References PG_GETARG_OID, PG_RETURN_DATUM, and pgstattuple_approx_internal().

245 {
246  Oid relid = PG_GETARG_OID(0);
247 
249 }
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
Datum pgstattuple_approx_internal(Oid relid, FunctionCallInfo fcinfo)
Definition: pgstatapprox.c:252

◆ statapprox_heap()

static void statapprox_heap ( Relation  rel,
output_type stat 
)
static

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

66 {
67  BlockNumber scanned,
68  nblocks,
69  blkno;
70  Buffer vmbuffer = InvalidBuffer;
71  BufferAccessStrategy bstrategy;
73 
74  OldestXmin = GetOldestXmin(rel, PROCARRAY_FLAGS_VACUUM);
75  bstrategy = GetAccessStrategy(BAS_BULKREAD);
76 
77  nblocks = RelationGetNumberOfBlocks(rel);
78  scanned = 0;
79 
80  for (blkno = 0; blkno < nblocks; blkno++)
81  {
82  Buffer buf;
83  Page page;
84  OffsetNumber offnum,
85  maxoff;
86  Size freespace;
87 
89 
90  /*
91  * If the page has only visible tuples, then we can find out the free
92  * space from the FSM and move on.
93  */
94  if (VM_ALL_VISIBLE(rel, blkno, &vmbuffer))
95  {
96  freespace = GetRecordedFreeSpace(rel, blkno);
97  stat->tuple_len += BLCKSZ - freespace;
98  stat->free_space += freespace;
99  continue;
100  }
101 
102  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno,
103  RBM_NORMAL, bstrategy);
104 
106 
107  page = BufferGetPage(buf);
108 
109  /*
110  * It's not safe to call PageGetHeapFreeSpace() on new pages, so we
111  * treat them as being free space for our purposes.
112  */
113  if (!PageIsNew(page))
114  stat->free_space += PageGetHeapFreeSpace(page);
115  else
116  stat->free_space += BLCKSZ - SizeOfPageHeaderData;
117 
118  /* We may count the page as scanned even if it's new/empty */
119  scanned++;
120 
121  if (PageIsNew(page) || PageIsEmpty(page))
122  {
123  UnlockReleaseBuffer(buf);
124  continue;
125  }
126 
127  /*
128  * Look at each tuple on the page and decide whether it's live or
129  * dead, then count it and its size. Unlike lazy_scan_heap, we can
130  * afford to ignore problems and special cases.
131  */
132  maxoff = PageGetMaxOffsetNumber(page);
133 
134  for (offnum = FirstOffsetNumber;
135  offnum <= maxoff;
136  offnum = OffsetNumberNext(offnum))
137  {
138  ItemId itemid;
139  HeapTupleData tuple;
140 
141  itemid = PageGetItemId(page, offnum);
142 
143  if (!ItemIdIsUsed(itemid) || ItemIdIsRedirected(itemid) ||
144  ItemIdIsDead(itemid))
145  {
146  continue;
147  }
148 
149  Assert(ItemIdIsNormal(itemid));
150 
151  ItemPointerSet(&(tuple.t_self), blkno, offnum);
152 
153  tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
154  tuple.t_len = ItemIdGetLength(itemid);
155  tuple.t_tableOid = RelationGetRelid(rel);
156 
157  /*
158  * We follow VACUUM's lead in counting INSERT_IN_PROGRESS tuples
159  * as "dead" while DELETE_IN_PROGRESS tuples are "live". We don't
160  * bother distinguishing tuples inserted/deleted by our own
161  * transaction.
162  */
163  switch (HeapTupleSatisfiesVacuum(&tuple, OldestXmin, buf))
164  {
165  case HEAPTUPLE_LIVE:
167  stat->tuple_len += tuple.t_len;
168  stat->tuple_count++;
169  break;
170  case HEAPTUPLE_DEAD:
173  stat->dead_tuple_len += tuple.t_len;
174  stat->dead_tuple_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  /*
188  * We don't know how many tuples are in the pages we didn't scan, so
189  * extrapolate the live-tuple count to the whole table in the same way
190  * that VACUUM does. (Like VACUUM, we're not taking a random sample, so
191  * just extrapolating linearly seems unsafe.) There should be no dead
192  * tuples in all-visible pages, so no correction is needed for that, and
193  * we already accounted for the space in those pages, too.
194  */
195  stat->tuple_count = vac_estimate_reltuples(rel, nblocks, scanned,
196  stat->tuple_count);
197 
198  /*
199  * Calculate percentages if the relation has one or more pages.
200  */
201  if (nblocks != 0)
202  {
203  stat->scanned_percent = 100 * scanned / nblocks;
204  stat->tuple_percent = 100.0 * stat->tuple_len / stat->table_len;
205  stat->dead_tuple_percent = 100.0 * stat->dead_tuple_len / stat->table_len;
206  stat->free_percent = 100.0 * stat->free_space / stat->table_len;
207  }
208 
209  if (BufferIsValid(vmbuffer))
210  {
211  ReleaseBuffer(vmbuffer);
212  vmbuffer = InvalidBuffer;
213  }
214 }
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:542
uint64 dead_tuple_count
Definition: pgstatapprox.c:45
#define PageIsEmpty(page)
Definition: bufpage.h:222
double vac_estimate_reltuples(Relation relation, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
Definition: vacuum.c:1072
#define ItemIdIsRedirected(itemId)
Definition: itemid.h:106
uint32 TransactionId
Definition: c.h:507
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
#define ItemIdIsUsed(itemId)
Definition: itemid.h:92
#define InvalidBuffer
Definition: buf.h:25
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3365
#define PROCARRAY_FLAGS_VACUUM
Definition: procarray.h:52
double dead_tuple_percent
Definition: pgstatapprox.c:47
#define SizeOfPageHeaderData
Definition: bufpage.h:216
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
uint64 tuple_count
Definition: pgstatapprox.c:42
double free_percent
Definition: pgstatapprox.c:49
uint64 table_len
Definition: pgstatapprox.c:40
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
uint16 OffsetNumber
Definition: off.h:24
HeapTupleHeader t_data
Definition: htup.h:68
double tuple_percent
Definition: pgstatapprox.c:44
uint64 tuple_len
Definition: pgstatapprox.c:43
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
#define ERROR
Definition: elog.h:43
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:665
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
static char * buf
Definition: pg_test_fsync.c:68
#define FirstOffsetNumber
Definition: off.h:27
static TransactionId OldestXmin
Definition: vacuumlazy.c:145
Oid t_tableOid
Definition: htup.h:66
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
Size GetRecordedFreeSpace(Relation rel, BlockNumber heapBlk)
Definition: freespace.c:230
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:198
TransactionId GetOldestXmin(Relation rel, int flags)
Definition: procarray.c:1304
#define Assert(condition)
Definition: c.h:732
#define ItemIdIsNormal(itemId)
Definition: itemid.h:99
#define VM_ALL_VISIBLE(r, b, v)
Definition: visibilitymap.h:32
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
size_t Size
Definition: c.h:466
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
uint64 free_space
Definition: pgstatapprox.c:48
#define PageIsNew(page)
Definition: bufpage.h:229
uint64 dead_tuple_len
Definition: pgstatapprox.c:46
uint64 scanned_percent
Definition: pgstatapprox.c:41
#define elog(elevel,...)
Definition: elog.h:226
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
int Buffer
Definition: buf.h:23
#define RelationGetRelid(relation)
Definition: rel.h:419
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:127