PostgreSQL Source Code  git master
pgstatapprox.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pgstatapprox.c
4  * Bloat estimation functions
5  *
6  * Copyright (c) 2014-2024, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * contrib/pgstattuple/pgstatapprox.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "postgres.h"
14 
15 #include "access/heapam.h"
16 #include "access/htup_details.h"
17 #include "access/multixact.h"
18 #include "access/relation.h"
19 #include "access/transam.h"
20 #include "access/visibilitymap.h"
21 #include "access/xact.h"
22 #include "catalog/namespace.h"
23 #include "catalog/pg_am_d.h"
24 #include "commands/vacuum.h"
25 #include "funcapi.h"
26 #include "miscadmin.h"
27 #include "storage/bufmgr.h"
28 #include "storage/freespace.h"
29 #include "storage/lmgr.h"
30 #include "storage/procarray.h"
31 #include "utils/builtins.h"
32 
35 
37 
38 typedef struct output_type
39 {
40  uint64 table_len;
42  uint64 tuple_count;
43  uint64 tuple_len;
44  double tuple_percent;
48  uint64 free_space;
49  double free_percent;
51 
52 #define NUM_OUTPUT_COLUMNS 10
53 
54 /*
55  * This function takes an already open relation and scans its pages,
56  * skipping those that have the corresponding visibility map bit set.
57  * For pages we skip, we find the free space from the free space map
58  * and approximate tuple_len on that basis. For the others, we count
59  * the exact number of dead tuples etc.
60  *
61  * This scan is loosely based on vacuumlazy.c:lazy_scan_heap(), but
62  * we do not try to avoid skipping single pages.
63  */
64 static void
66 {
67  BlockNumber scanned,
68  nblocks,
69  blkno;
70  Buffer vmbuffer = InvalidBuffer;
71  BufferAccessStrategy bstrategy;
72  TransactionId OldestXmin;
73 
74  OldestXmin = GetOldestNonRemovableTransactionId(rel);
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  stat->free_space += PageGetExactFreeSpace(page);
110 
111  /* We may count the page as scanned even if it's new/empty */
112  scanned++;
113 
114  if (PageIsNew(page) || PageIsEmpty(page))
115  {
117  continue;
118  }
119 
120  /*
121  * Look at each tuple on the page and decide whether it's live or
122  * dead, then count it and its size. Unlike lazy_scan_heap, we can
123  * afford to ignore problems and special cases.
124  */
125  maxoff = PageGetMaxOffsetNumber(page);
126 
127  for (offnum = FirstOffsetNumber;
128  offnum <= maxoff;
129  offnum = OffsetNumberNext(offnum))
130  {
131  ItemId itemid;
132  HeapTupleData tuple;
133 
134  itemid = PageGetItemId(page, offnum);
135 
136  if (!ItemIdIsUsed(itemid) || ItemIdIsRedirected(itemid) ||
137  ItemIdIsDead(itemid))
138  {
139  continue;
140  }
141 
142  Assert(ItemIdIsNormal(itemid));
143 
144  ItemPointerSet(&(tuple.t_self), blkno, offnum);
145 
146  tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
147  tuple.t_len = ItemIdGetLength(itemid);
148  tuple.t_tableOid = RelationGetRelid(rel);
149 
150  /*
151  * We follow VACUUM's lead in counting INSERT_IN_PROGRESS tuples
152  * as "dead" while DELETE_IN_PROGRESS tuples are "live". We don't
153  * bother distinguishing tuples inserted/deleted by our own
154  * transaction.
155  */
156  switch (HeapTupleSatisfiesVacuum(&tuple, OldestXmin, buf))
157  {
158  case HEAPTUPLE_LIVE:
160  stat->tuple_len += tuple.t_len;
161  stat->tuple_count++;
162  break;
163  case HEAPTUPLE_DEAD:
166  stat->dead_tuple_len += tuple.t_len;
167  stat->dead_tuple_count++;
168  break;
169  default:
170  elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
171  break;
172  }
173  }
174 
176  }
177 
178  stat->table_len = (uint64) nblocks * BLCKSZ;
179 
180  /*
181  * We don't know how many tuples are in the pages we didn't scan, so
182  * extrapolate the live-tuple count to the whole table in the same way
183  * that VACUUM does. (Like VACUUM, we're not taking a random sample, so
184  * just extrapolating linearly seems unsafe.) There should be no dead
185  * tuples in all-visible pages, so no correction is needed for that, and
186  * we already accounted for the space in those pages, too.
187  */
188  stat->tuple_count = vac_estimate_reltuples(rel, nblocks, scanned,
189  stat->tuple_count);
190 
191  /* It's not clear if we could get -1 here, but be safe. */
192  stat->tuple_count = Max(stat->tuple_count, 0);
193 
194  /*
195  * Calculate percentages if the relation has one or more pages.
196  */
197  if (nblocks != 0)
198  {
199  stat->scanned_percent = 100.0 * scanned / nblocks;
200  stat->tuple_percent = 100.0 * stat->tuple_len / stat->table_len;
201  stat->dead_tuple_percent = 100.0 * stat->dead_tuple_len / stat->table_len;
202  stat->free_percent = 100.0 * stat->free_space / stat->table_len;
203  }
204 
205  if (BufferIsValid(vmbuffer))
206  {
207  ReleaseBuffer(vmbuffer);
208  vmbuffer = InvalidBuffer;
209  }
210 }
211 
212 /*
213  * Returns estimated live/dead tuple statistics for the given relid.
214  *
215  * The superuser() check here must be kept as the library might be upgraded
216  * without the extension being upgraded, meaning that in pre-1.5 installations
217  * these functions could be called by any user.
218  */
219 Datum
221 {
222  Oid relid = PG_GETARG_OID(0);
223 
224  if (!superuser())
225  ereport(ERROR,
226  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
227  errmsg("must be superuser to use pgstattuple functions")));
228 
230 }
231 
232 /*
233  * As of pgstattuple version 1.5, we no longer need to check if the user
234  * is a superuser because we REVOKE EXECUTE on the SQL function from PUBLIC.
235  * Users can then grant access to it based on their policies.
236  *
237  * Otherwise identical to pgstattuple_approx (above).
238  */
239 Datum
241 {
242  Oid relid = PG_GETARG_OID(0);
243 
245 }
246 
247 Datum
249 {
250  Relation rel;
251  output_type stat = {0};
252  TupleDesc tupdesc;
253  bool nulls[NUM_OUTPUT_COLUMNS];
255  HeapTuple ret;
256  int i = 0;
257 
258  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
259  elog(ERROR, "return type must be a row type");
260 
261  if (tupdesc->natts != NUM_OUTPUT_COLUMNS)
262  elog(ERROR, "incorrect number of output arguments");
263 
264  rel = relation_open(relid, AccessShareLock);
265 
266  /*
267  * Reject attempts to read non-local temporary relations; we would be
268  * likely to get wrong data since we have no visibility into the owning
269  * session's local buffers.
270  */
271  if (RELATION_IS_OTHER_TEMP(rel))
272  ereport(ERROR,
273  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
274  errmsg("cannot access temporary tables of other sessions")));
275 
276  /*
277  * We support only relation kinds with a visibility map and a free space
278  * map.
279  */
280  if (!(rel->rd_rel->relkind == RELKIND_RELATION ||
281  rel->rd_rel->relkind == RELKIND_MATVIEW ||
282  rel->rd_rel->relkind == RELKIND_TOASTVALUE))
283  ereport(ERROR,
284  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
285  errmsg("relation \"%s\" is of wrong relation kind",
287  errdetail_relkind_not_supported(rel->rd_rel->relkind)));
288 
289  if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
290  ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
291  errmsg("only heap AM is supported")));
292 
293  statapprox_heap(rel, &stat);
294 
296 
297  memset(nulls, 0, sizeof(nulls));
298 
299  values[i++] = Int64GetDatum(stat.table_len);
300  values[i++] = Float8GetDatum(stat.scanned_percent);
301  values[i++] = Int64GetDatum(stat.tuple_count);
302  values[i++] = Int64GetDatum(stat.tuple_len);
303  values[i++] = Float8GetDatum(stat.tuple_percent);
304  values[i++] = Int64GetDatum(stat.dead_tuple_count);
305  values[i++] = Int64GetDatum(stat.dead_tuple_len);
306  values[i++] = Float8GetDatum(stat.dead_tuple_percent);
307  values[i++] = Int64GetDatum(stat.free_space);
308  values[i++] = Float8GetDatum(stat.free_percent);
309 
310  ret = heap_form_tuple(tupdesc, values, nulls);
311  return HeapTupleGetDatum(ret);
312 }
uint32 BlockNumber
Definition: block.h:31
static Datum values[MAXATTR]
Definition: bootstrap.c:150
int Buffer
Definition: buf.h:23
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4924
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4941
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5158
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:793
@ BAS_BULKREAD
Definition: bufmgr.h:36
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:190
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:273
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:400
@ RBM_NORMAL
Definition: bufmgr.h:45
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:351
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:958
static bool PageIsEmpty(Page page)
Definition: bufpage.h:223
Pointer Page
Definition: bufpage.h:81
static Item PageGetItem(Page page, ItemId itemId)
Definition: bufpage.h:354
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:243
static bool PageIsNew(Page page)
Definition: bufpage.h:233
static OffsetNumber PageGetMaxOffsetNumber(Page page)
Definition: bufpage.h:372
#define Max(x, y)
Definition: c.h:989
#define Assert(condition)
Definition: c.h:849
uint32 TransactionId
Definition: c.h:643
size_t Size
Definition: c.h:596
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1816
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:541
Size GetRecordedFreeSpace(Relation rel, BlockNumber heapBlk)
Definition: freespace.c:244
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
@ HEAPTUPLE_RECENTLY_DEAD
Definition: heapam.h:128
@ HEAPTUPLE_INSERT_IN_PROGRESS
Definition: heapam.h:129
@ HEAPTUPLE_LIVE
Definition: heapam.h:127
@ HEAPTUPLE_DELETE_IN_PROGRESS
Definition: heapam.h:130
@ HEAPTUPLE_DEAD
Definition: heapam.h:126
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
int i
Definition: isn.c:73
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define ItemIdIsNormal(itemId)
Definition: itemid.h:99
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
#define ItemIdIsUsed(itemId)
Definition: itemid.h:92
#define ItemIdIsRedirected(itemId)
Definition: itemid.h:106
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition: itemptr.h:135
#define AccessShareLock
Definition: lockdefs.h:36
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
static char * buf
Definition: pg_test_fsync.c:73
static void statapprox_heap(Relation rel, output_type *stat)
Definition: pgstatapprox.c:65
Datum pgstattuple_approx(PG_FUNCTION_ARGS)
Definition: pgstatapprox.c:220
#define NUM_OUTPUT_COLUMNS
Definition: pgstatapprox.c:52
struct output_type output_type
Datum pgstattuple_approx_internal(Oid relid, FunctionCallInfo fcinfo)
Definition: pgstatapprox.c:248
PG_FUNCTION_INFO_V1(pgstattuple_approx)
Datum pgstattuple_approx_v1_5(PG_FUNCTION_ARGS)
Definition: pgstatapprox.c:240
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
Definition: procarray.c:2005
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:658
@ MAIN_FORKNUM
Definition: relpath.h:58
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
Oid t_tableOid
Definition: htup.h:66
Form_pg_class rd_rel
Definition: rel.h:111
uint64 dead_tuple_count
Definition: pgstatapprox.c:45
double free_percent
Definition: pgstatapprox.c:49
double tuple_percent
Definition: pgstatapprox.c:44
double dead_tuple_percent
Definition: pgstatapprox.c:47
uint64 dead_tuple_len
Definition: pgstatapprox.c:46
uint64 tuple_count
Definition: pgstatapprox.c:42
uint64 free_space
Definition: pgstatapprox.c:48
uint64 table_len
Definition: pgstatapprox.c:40
uint64 tuple_len
Definition: pgstatapprox.c:43
double scanned_percent
Definition: pgstatapprox.c:41
bool superuser(void)
Definition: superuser.c:46
double vac_estimate_reltuples(Relation relation, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
Definition: vacuum.c:1314
#define VM_ALL_VISIBLE(r, b, v)
Definition: visibilitymap.h:24