2006-09-03 01:05:29 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pgstattuple/pgstatindex.c
|
2008-05-17 09:28:26 +08:00
|
|
|
*
|
|
|
|
*
|
2006-09-03 01:05:29 +08:00
|
|
|
* pgstatindex
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Satoshi Nagayasu <nagayasus@nttdata.co.jp>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software and
|
|
|
|
* its documentation for any purpose, without fee, and without a
|
|
|
|
* written agreement is hereby granted, provided that the above
|
|
|
|
* copyright notice and this paragraph and the following two
|
|
|
|
* paragraphs appear in all copies.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
|
|
|
|
* INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
|
|
|
|
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
|
|
|
|
* DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
|
|
|
|
* IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
|
|
|
|
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "access/nbtree.h"
|
|
|
|
#include "catalog/namespace.h"
|
2007-08-27 07:59:50 +08:00
|
|
|
#include "funcapi.h"
|
|
|
|
#include "miscadmin.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "storage/bufmgr.h"
|
2006-09-03 01:05:29 +08:00
|
|
|
#include "utils/builtins.h"
|
2011-02-24 01:18:09 +08:00
|
|
|
#include "utils/rel.h"
|
2006-09-03 01:05:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
extern Datum pgstatindex(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum pg_relpages(PG_FUNCTION_ARGS);
|
|
|
|
|
2007-08-27 07:59:50 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pgstatindex);
|
|
|
|
PG_FUNCTION_INFO_V1(pg_relpages);
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2007-08-27 07:59:50 +08:00
|
|
|
#define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
|
2006-09-03 01:05:29 +08:00
|
|
|
#define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)
|
|
|
|
|
2007-07-16 07:09:26 +08:00
|
|
|
#define CHECK_PAGE_OFFSET_RANGE(pg, offnum) { \
|
2007-07-16 07:46:20 +08:00
|
|
|
if ( !(FirstOffsetNumber <= (offnum) && \
|
|
|
|
(offnum) <= PageGetMaxOffsetNumber(pg)) ) \
|
2007-07-16 07:09:26 +08:00
|
|
|
elog(ERROR, "page offset number out of range"); }
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2007-07-16 07:46:20 +08:00
|
|
|
/* note: BlockNumber is unsigned, hence can't be negative */
|
2006-09-03 01:05:29 +08:00
|
|
|
#define CHECK_RELATION_BLOCK_RANGE(rel, blkno) { \
|
2007-07-16 07:46:20 +08:00
|
|
|
if ( RelationGetNumberOfBlocks(rel) <= (BlockNumber) (blkno) ) \
|
2007-07-16 07:09:26 +08:00
|
|
|
elog(ERROR, "block number out of range"); }
|
2006-09-03 01:05:29 +08:00
|
|
|
|
|
|
|
/* ------------------------------------------------
|
|
|
|
* A structure for a whole btree index statistics
|
|
|
|
* used by pgstatindex().
|
|
|
|
* ------------------------------------------------
|
|
|
|
*/
|
|
|
|
typedef struct BTIndexStat
|
|
|
|
{
|
|
|
|
uint32 version;
|
|
|
|
uint32 level;
|
2008-03-21 11:23:30 +08:00
|
|
|
BlockNumber root_blkno;
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2008-03-21 11:23:30 +08:00
|
|
|
uint64 root_pages;
|
|
|
|
uint64 internal_pages;
|
|
|
|
uint64 leaf_pages;
|
|
|
|
uint64 empty_pages;
|
|
|
|
uint64 deleted_pages;
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2008-03-21 11:23:30 +08:00
|
|
|
uint64 max_avail;
|
|
|
|
uint64 free_space;
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2008-03-21 11:23:30 +08:00
|
|
|
uint64 fragments;
|
2009-06-11 22:49:15 +08:00
|
|
|
} BTIndexStat;
|
2006-09-03 01:05:29 +08:00
|
|
|
|
|
|
|
/* ------------------------------------------------------
|
|
|
|
* pgstatindex()
|
|
|
|
*
|
|
|
|
* Usage: SELECT * FROM pgstatindex('t1_pkey');
|
|
|
|
* ------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pgstatindex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *relname = PG_GETARG_TEXT_P(0);
|
|
|
|
Relation rel;
|
|
|
|
RangeVar *relrv;
|
|
|
|
Datum result;
|
2009-06-11 22:49:15 +08:00
|
|
|
BlockNumber nblocks;
|
|
|
|
BlockNumber blkno;
|
2006-09-03 01:05:29 +08:00
|
|
|
BTIndexStat indexStat;
|
|
|
|
|
2007-08-27 07:59:50 +08:00
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use pgstattuple functions"))));
|
|
|
|
|
2006-09-03 01:05:29 +08:00
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
|
|
|
|
|
|
|
if (!IS_INDEX(rel) || !IS_BTREE(rel))
|
2007-08-27 07:59:50 +08:00
|
|
|
elog(ERROR, "relation \"%s\" is not a btree index",
|
|
|
|
RelationGetRelationName(rel));
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2009-04-01 06:54:31 +08:00
|
|
|
/*
|
2009-06-11 22:49:15 +08:00
|
|
|
* Reject attempts to read non-local temporary relations; we would be
|
|
|
|
* likely to get wrong data since we have no visibility into the owning
|
|
|
|
* session's local buffers.
|
2009-04-01 06:54:31 +08:00
|
|
|
*/
|
|
|
|
if (RELATION_IS_OTHER_TEMP(rel))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("cannot access temporary tables of other sessions")));
|
|
|
|
|
2007-08-27 07:59:50 +08:00
|
|
|
/*
|
|
|
|
* Read metapage
|
2006-09-03 01:05:29 +08:00
|
|
|
*/
|
|
|
|
{
|
|
|
|
Buffer buffer = ReadBuffer(rel, 0);
|
|
|
|
Page page = BufferGetPage(buffer);
|
|
|
|
BTMetaPageData *metad = BTPageGetMeta(page);
|
|
|
|
|
|
|
|
indexStat.version = metad->btm_version;
|
|
|
|
indexStat.level = metad->btm_level;
|
2008-03-21 11:23:30 +08:00
|
|
|
indexStat.root_blkno = metad->btm_root;
|
2006-09-03 01:05:29 +08:00
|
|
|
|
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
}
|
|
|
|
|
2008-03-21 11:23:30 +08:00
|
|
|
/* -- init counters -- */
|
2006-09-03 01:05:29 +08:00
|
|
|
indexStat.root_pages = 0;
|
|
|
|
indexStat.internal_pages = 0;
|
2008-03-21 11:23:30 +08:00
|
|
|
indexStat.leaf_pages = 0;
|
2006-09-03 01:05:29 +08:00
|
|
|
indexStat.empty_pages = 0;
|
|
|
|
indexStat.deleted_pages = 0;
|
|
|
|
|
|
|
|
indexStat.max_avail = 0;
|
|
|
|
indexStat.free_space = 0;
|
|
|
|
|
2008-03-21 11:23:30 +08:00
|
|
|
indexStat.fragments = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan all blocks except the metapage
|
2006-09-03 01:05:29 +08:00
|
|
|
*/
|
2008-03-21 11:23:30 +08:00
|
|
|
nblocks = RelationGetNumberOfBlocks(rel);
|
|
|
|
|
2006-09-03 01:05:29 +08:00
|
|
|
for (blkno = 1; blkno < nblocks; blkno++)
|
|
|
|
{
|
2007-05-18 03:11:25 +08:00
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
|
|
|
BTPageOpaque opaque;
|
|
|
|
|
2011-10-07 00:08:59 +08:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
|
2007-05-18 03:11:25 +08:00
|
|
|
/* Read and lock buffer */
|
|
|
|
buffer = ReadBuffer(rel, blkno);
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2007-05-18 03:11:25 +08:00
|
|
|
page = BufferGetPage(buffer);
|
|
|
|
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2007-05-18 03:11:25 +08:00
|
|
|
/* Determine page type, and update totals */
|
|
|
|
|
2008-03-21 11:23:30 +08:00
|
|
|
if (P_ISLEAF(opaque))
|
2006-09-03 01:05:29 +08:00
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
int max_avail;
|
|
|
|
|
|
|
|
max_avail = BLCKSZ - (BLCKSZ - ((PageHeader) page)->pd_special + SizeOfPageHeaderData);
|
2007-05-18 03:11:25 +08:00
|
|
|
indexStat.max_avail += max_avail;
|
|
|
|
indexStat.free_space += PageGetFreeSpace(page);
|
|
|
|
|
|
|
|
indexStat.leaf_pages++;
|
|
|
|
|
|
|
|
/*
|
2007-11-16 05:14:46 +08:00
|
|
|
* If the next leaf is on an earlier block, it means a
|
|
|
|
* fragmentation.
|
2007-05-18 03:11:25 +08:00
|
|
|
*/
|
|
|
|
if (opaque->btpo_next != P_NONE && opaque->btpo_next < blkno)
|
|
|
|
indexStat.fragments++;
|
2006-09-03 01:05:29 +08:00
|
|
|
}
|
2008-03-21 11:23:30 +08:00
|
|
|
else if (P_ISDELETED(opaque))
|
|
|
|
indexStat.deleted_pages++;
|
|
|
|
else if (P_IGNORE(opaque))
|
|
|
|
indexStat.empty_pages++;
|
2007-05-18 03:11:25 +08:00
|
|
|
else if (P_ISROOT(opaque))
|
|
|
|
indexStat.root_pages++;
|
|
|
|
else
|
|
|
|
indexStat.internal_pages++;
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2007-05-18 03:11:25 +08:00
|
|
|
/* Unlock and release buffer */
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
2006-09-03 01:05:29 +08:00
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
/*----------------------------
|
|
|
|
* Build a result tuple
|
|
|
|
*----------------------------
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
int j;
|
2007-08-27 07:59:50 +08:00
|
|
|
char *values[10];
|
2007-03-16 23:06:43 +08:00
|
|
|
HeapTuple tuple;
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2007-08-27 07:59:50 +08:00
|
|
|
/* Build a tuple descriptor for our result type */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
2006-09-03 01:05:29 +08:00
|
|
|
|
|
|
|
j = 0;
|
|
|
|
values[j] = palloc(32);
|
|
|
|
snprintf(values[j++], 32, "%d", indexStat.version);
|
|
|
|
values[j] = palloc(32);
|
|
|
|
snprintf(values[j++], 32, "%d", indexStat.level);
|
|
|
|
values[j] = palloc(32);
|
2008-03-21 11:23:30 +08:00
|
|
|
snprintf(values[j++], 32, INT64_FORMAT,
|
|
|
|
(indexStat.root_pages +
|
|
|
|
indexStat.leaf_pages +
|
|
|
|
indexStat.internal_pages +
|
|
|
|
indexStat.deleted_pages +
|
|
|
|
indexStat.empty_pages) * BLCKSZ);
|
2006-09-03 01:05:29 +08:00
|
|
|
values[j] = palloc(32);
|
2008-03-21 11:23:30 +08:00
|
|
|
snprintf(values[j++], 32, "%u", indexStat.root_blkno);
|
2006-09-03 01:05:29 +08:00
|
|
|
values[j] = palloc(32);
|
2008-03-21 11:23:30 +08:00
|
|
|
snprintf(values[j++], 32, INT64_FORMAT, indexStat.internal_pages);
|
2006-09-03 01:05:29 +08:00
|
|
|
values[j] = palloc(32);
|
2008-03-21 11:23:30 +08:00
|
|
|
snprintf(values[j++], 32, INT64_FORMAT, indexStat.leaf_pages);
|
2006-09-03 01:05:29 +08:00
|
|
|
values[j] = palloc(32);
|
2008-03-21 11:23:30 +08:00
|
|
|
snprintf(values[j++], 32, INT64_FORMAT, indexStat.empty_pages);
|
2006-09-03 01:05:29 +08:00
|
|
|
values[j] = palloc(32);
|
2008-03-21 11:23:30 +08:00
|
|
|
snprintf(values[j++], 32, INT64_FORMAT, indexStat.deleted_pages);
|
2006-09-03 01:05:29 +08:00
|
|
|
values[j] = palloc(32);
|
2011-08-25 11:50:10 +08:00
|
|
|
if (indexStat.max_avail > 0)
|
|
|
|
snprintf(values[j++], 32, "%.2f",
|
|
|
|
100.0 - (double) indexStat.free_space / (double) indexStat.max_avail * 100.0);
|
|
|
|
else
|
|
|
|
snprintf(values[j++], 32, "NaN");
|
2006-09-03 01:05:29 +08:00
|
|
|
values[j] = palloc(32);
|
2011-08-25 11:50:10 +08:00
|
|
|
if (indexStat.leaf_pages > 0)
|
|
|
|
snprintf(values[j++], 32, "%.2f",
|
|
|
|
(double) indexStat.fragments / (double) indexStat.leaf_pages * 100.0);
|
|
|
|
else
|
|
|
|
snprintf(values[j++], 32, "NaN");
|
2006-09-03 01:05:29 +08:00
|
|
|
|
|
|
|
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
|
|
|
|
values);
|
|
|
|
|
2007-08-27 07:59:50 +08:00
|
|
|
result = HeapTupleGetDatum(tuple);
|
2006-09-03 01:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------
|
|
|
|
* pg_relpages()
|
|
|
|
*
|
2007-08-27 07:59:50 +08:00
|
|
|
* Get the number of pages of the table/index.
|
2006-09-03 01:05:29 +08:00
|
|
|
*
|
|
|
|
* Usage: SELECT pg_relpages('t1');
|
|
|
|
* SELECT pg_relpages('t1_pkey');
|
|
|
|
* --------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_relpages(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *relname = PG_GETARG_TEXT_P(0);
|
2008-03-21 11:23:30 +08:00
|
|
|
int64 relpages;
|
2006-09-03 01:05:29 +08:00
|
|
|
Relation rel;
|
|
|
|
RangeVar *relrv;
|
|
|
|
|
2007-08-27 07:59:50 +08:00
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use pgstattuple functions"))));
|
|
|
|
|
2006-09-03 01:05:29 +08:00
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
|
|
|
|
2009-04-01 06:54:31 +08:00
|
|
|
/* note: this will work OK on non-local temp tables */
|
|
|
|
|
2006-09-03 01:05:29 +08:00
|
|
|
relpages = RelationGetNumberOfBlocks(rel);
|
|
|
|
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
2008-03-21 11:23:30 +08:00
|
|
|
PG_RETURN_INT64(relpages);
|
2006-09-03 01:05:29 +08:00
|
|
|
}
|