34 #include "catalog/pg_am_d.h"
100 double dead_tuple_percent;
107 elog(
ERROR,
"return type must be a row type");
115 if (
stat->table_len == 0)
118 dead_tuple_percent = 0.0;
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;
175 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
176 errmsg(
"must be superuser to use pgstattuple functions")));
215 (
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
216 errmsg(
"must be superuser to use pgstattuple functions")));
252 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
253 errmsg(
"cannot access temporary tables of other sessions")));
255 if (RELKIND_HAS_TABLE_AM(rel->
rd_rel->relkind) ||
256 rel->
rd_rel->relkind == RELKIND_SEQUENCE)
260 else if (rel->
rd_rel->relkind == RELKIND_INDEX)
265 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
266 errmsg(
"index \"%s\" is not valid",
269 switch (rel->
rd_rel->relam)
284 err =
"spgist index";
290 err =
"unknown index";
294 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
295 errmsg(
"index \"%s\" (%s) is not supported",
301 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
302 errmsg(
"cannot get tuple-level statistics for relation \"%s\"",
329 if (rel->
rd_rel->relkind != RELKIND_SEQUENCE &&
330 rel->
rd_rel->relam != HEAP_TABLE_AM_OID)
332 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
333 errmsg(
"only heap AM is supported")));
359 stat.dead_tuple_count++;
372 while (block <= tupblock)
385 while (block < nblocks)
400 stat.table_len = (uint64) nblocks * BLCKSZ;
423 stat->free_space += BLCKSZ;
433 stat->free_space += BLCKSZ;
470 stat->free_space += BLCKSZ;
543 if (blkno >= nblocks)
545 stat.table_len = (uint64) nblocks * BLCKSZ;
550 for (; blkno < nblocks; blkno++)
554 pagefn(&
stat, rel, blkno, bstrategy);
580 stat->dead_tuple_count++;
static Datum values[MAXATTR]
struct BufferAccessStrategyData * BufferAccessStrategy
void UnlockReleaseBuffer(Buffer buffer)
void LockBuffer(Buffer buffer, int mode)
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
#define BUFFER_LOCK_UNLOCK
#define BUFFER_LOCK_SHARE
#define RelationGetNumberOfBlocks(reln)
static Page BufferGetPage(Buffer buffer)
Size PageGetExactFreeSpace(Page page)
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
static bool PageIsNew(Page page)
static OffsetNumber PageGetMaxOffsetNumber(Page page)
static uint16 PageGetSpecialSize(Page page)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void err(int eval, const char *fmt,...)
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
#define PG_GETARG_TEXT_PP(n)
#define PG_RETURN_DATUM(x)
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
#define GistPageIsLeaf(page)
void gistcheckpage(Relation rel, Buffer buf)
#define HashPageGetOpaque(page)
void _hash_relbuf(Relation rel, Buffer buf)
Buffer _hash_getbuf_with_strategy(Relation rel, BlockNumber blkno, int access, int flags, BufferAccessStrategy bstrategy)
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
struct HeapScanDescData * HeapScanDesc
bool HeapTupleSatisfiesVisibility(HeapTuple htup, Snapshot snapshot, Buffer buffer)
#define ItemIdGetLength(itemId)
#define ItemIdIsDead(itemId)
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
#define CHECK_FOR_INTERRUPTS()
RangeVar * makeRangeVarFromNameList(const List *names)
void _bt_relbuf(Relation rel, Buffer buf)
#define BTPageGetOpaque(page)
#define P_FIRSTDATAKEY(opaque)
#define OffsetNumberNext(offsetNumber)
#define FirstOffsetNumber
int errdetail_relkind_not_supported(char relkind)
Datum pgstattuplebyid(PG_FUNCTION_ARGS)
static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
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_v1_5(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(pgstattuple)
static void pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
struct pgstattuple_type pgstattuple_type
void(* pgstat_page)(pgstattuple_type *, Relation, BlockNumber, BufferAccessStrategy)
static Datum build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
static void pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
Datum pgstattuplebyid_v1_5(PG_FUNCTION_ARGS)
static Datum pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
Datum pgstattuple(PG_FUNCTION_ARGS)
static void pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BufferAccessStrategy bstrategy)
#define RelationGetRelationName(relation)
#define RELATION_IS_OTHER_TEMP(relation)
struct RelationData * Relation
#define InitDirtySnapshot(snapshotdata)
void relation_close(Relation relation, LOCKMODE lockmode)
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Relation relation_open(Oid relationId, LOCKMODE lockmode)
BufferAccessStrategy rs_strategy
static void table_endscan(TableScanDesc scan)
static TableScanDesc table_beginscan_strat(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key, bool allow_strat, bool allow_sync)
List * textToQualifiedNameList(text *textval)