2001-10-01 09:52:38 +08:00
|
|
|
/*
|
2009-04-01 06:54:31 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/pgstattuple/pgstattuple.c,v 1.37 2009/03/31 22:54:31 tgl Exp $
|
2001-10-01 09:52:38 +08:00
|
|
|
*
|
2002-09-05 04:31:48 +08:00
|
|
|
* Copyright (c) 2001,2002 Tatsuo Ishii
|
2001-10-01 09:52:38 +08:00
|
|
|
*
|
|
|
|
* 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"
|
2001-12-20 04:28:41 +08:00
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
#include "access/gist_private.h"
|
|
|
|
#include "access/hash.h"
|
2006-07-12 01:26:59 +08:00
|
|
|
#include "access/nbtree.h"
|
2008-06-19 08:46:06 +08:00
|
|
|
#include "access/relscan.h"
|
2002-03-30 09:02:42 +08:00
|
|
|
#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"
|
2008-05-17 01:31:17 +08:00
|
|
|
#include "storage/lmgr.h"
|
2002-03-30 09:02:42 +08:00
|
|
|
#include "utils/builtins.h"
|
2008-03-27 05:10:39 +08:00
|
|
|
#include "utils/tqual.h"
|
2002-03-30 09:02:42 +08:00
|
|
|
|
2001-10-01 09:52:38 +08:00
|
|
|
|
2006-05-31 06:12:16 +08:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2001-10-01 09:52:38 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pgstattuple);
|
2003-06-12 16:02:57 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pgstattuplebyid);
|
2001-10-01 09:52:38 +08:00
|
|
|
|
|
|
|
extern Datum pgstattuple(PG_FUNCTION_ARGS);
|
2003-06-12 16:02:57 +08:00
|
|
|
extern Datum pgstattuplebyid(PG_FUNCTION_ARGS);
|
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
/*
|
|
|
|
* struct pgstattuple_type
|
|
|
|
*
|
|
|
|
* tuple_percent, dead_tuple_percent and free_percent are computable,
|
|
|
|
* so not defined here.
|
|
|
|
*/
|
|
|
|
typedef struct pgstattuple_type
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
uint64 table_len;
|
|
|
|
uint64 tuple_count;
|
|
|
|
uint64 tuple_len;
|
|
|
|
uint64 dead_tuple_count;
|
|
|
|
uint64 dead_tuple_len;
|
|
|
|
uint64 free_space; /* free/reusable space in bytes */
|
|
|
|
} pgstattuple_type;
|
|
|
|
|
|
|
|
typedef void (*pgstat_page) (pgstattuple_type *, Relation, BlockNumber);
|
|
|
|
|
|
|
|
static Datum build_pgstattuple_type(pgstattuple_type * stat,
|
|
|
|
FunctionCallInfo fcinfo);
|
2006-07-06 10:18:07 +08:00
|
|
|
static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo);
|
|
|
|
static Datum pgstat_heap(Relation rel, FunctionCallInfo fcinfo);
|
2006-10-04 08:30:14 +08:00
|
|
|
static void pgstat_btree_page(pgstattuple_type * stat,
|
|
|
|
Relation rel, BlockNumber blkno);
|
|
|
|
static void pgstat_hash_page(pgstattuple_type * stat,
|
|
|
|
Relation rel, BlockNumber blkno);
|
|
|
|
static void pgstat_gist_page(pgstattuple_type * stat,
|
|
|
|
Relation rel, BlockNumber blkno);
|
2006-09-04 10:03:04 +08:00
|
|
|
static Datum pgstat_index(Relation rel, BlockNumber start,
|
2006-10-04 08:30:14 +08:00
|
|
|
pgstat_page pagefn, FunctionCallInfo fcinfo);
|
|
|
|
static void pgstat_index_page(pgstattuple_type * stat, Page page,
|
|
|
|
OffsetNumber minoff, OffsetNumber maxoff);
|
2006-07-06 10:18:07 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* build_pgstattuple_type -- build a pgstattuple_type tuple
|
|
|
|
*/
|
|
|
|
static Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
build_pgstattuple_type(pgstattuple_type * stat, FunctionCallInfo fcinfo)
|
2006-07-06 10:18:07 +08:00
|
|
|
{
|
|
|
|
#define NCOLUMNS 9
|
|
|
|
#define NCHARS 32
|
|
|
|
|
|
|
|
HeapTuple tuple;
|
|
|
|
char *values[NCOLUMNS];
|
|
|
|
char values_buf[NCOLUMNS][NCHARS];
|
|
|
|
int i;
|
|
|
|
double tuple_percent;
|
|
|
|
double dead_tuple_percent;
|
2006-10-04 08:30:14 +08:00
|
|
|
double free_percent; /* free/reusable space in % */
|
2006-07-06 10:18:07 +08:00
|
|
|
TupleDesc tupdesc;
|
|
|
|
AttInMetadata *attinmeta;
|
|
|
|
|
|
|
|
/* Build a tuple descriptor for our result type */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate attribute metadata needed later to produce tuples from raw C
|
|
|
|
* strings
|
|
|
|
*/
|
|
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
|
|
|
|
|
|
|
if (stat->table_len == 0)
|
|
|
|
{
|
|
|
|
tuple_percent = 0.0;
|
|
|
|
dead_tuple_percent = 0.0;
|
|
|
|
free_percent = 0.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tuple_percent = 100.0 * stat->tuple_len / stat->table_len;
|
|
|
|
dead_tuple_percent = 100.0 * stat->dead_tuple_len / stat->table_len;
|
|
|
|
free_percent = 100.0 * stat->free_space / stat->table_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare a values array for constructing the tuple. This should be an
|
|
|
|
* array of C strings which will be processed later by the appropriate
|
|
|
|
* "in" functions.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < NCOLUMNS; i++)
|
|
|
|
values[i] = values_buf[i];
|
|
|
|
i = 0;
|
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, stat->table_len);
|
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, stat->tuple_count);
|
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, stat->tuple_len);
|
|
|
|
snprintf(values[i++], NCHARS, "%.2f", tuple_percent);
|
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, stat->dead_tuple_count);
|
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, stat->dead_tuple_len);
|
|
|
|
snprintf(values[i++], NCHARS, "%.2f", dead_tuple_percent);
|
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, stat->free_space);
|
|
|
|
snprintf(values[i++], NCHARS, "%.2f", free_percent);
|
2005-05-31 07:09:07 +08:00
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
/* build a tuple */
|
|
|
|
tuple = BuildTupleFromCStrings(attinmeta, values);
|
|
|
|
|
|
|
|
/* make the tuple into a datum */
|
|
|
|
return HeapTupleGetDatum(tuple);
|
|
|
|
}
|
2001-10-01 09:52:38 +08:00
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* pgstattuple:
|
2002-08-23 16:19:49 +08:00
|
|
|
* returns live/dead tuples info
|
2001-10-01 09:52:38 +08:00
|
|
|
*
|
|
|
|
* C FUNCTION definition
|
2002-08-30 01:14:33 +08:00
|
|
|
* pgstattuple(text) returns pgstattuple_type
|
|
|
|
* see pgstattuple.sql for pgstattuple_type
|
2001-10-01 09:52:38 +08:00
|
|
|
* ----------
|
|
|
|
*/
|
2002-08-23 16:19:49 +08:00
|
|
|
|
2001-10-01 09:52:38 +08:00
|
|
|
Datum
|
|
|
|
pgstattuple(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-08-30 01:14:33 +08:00
|
|
|
text *relname = PG_GETARG_TEXT_P(0);
|
2002-03-30 09:02:42 +08:00
|
|
|
RangeVar *relrv;
|
2001-10-25 13:50:21 +08:00
|
|
|
Relation rel;
|
2003-06-12 16:02:57 +08:00
|
|
|
|
2007-08-27 07:59:50 +08:00
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use pgstattuple functions"))));
|
|
|
|
|
2003-06-12 16:02:57 +08:00
|
|
|
/* open relation */
|
2005-05-27 08:57:49 +08:00
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
2006-07-06 10:18:07 +08:00
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
2003-06-12 16:02:57 +08:00
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
|
2003-06-12 16:02:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pgstattuplebyid(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid = PG_GETARG_OID(0);
|
|
|
|
Relation rel;
|
|
|
|
|
2007-08-27 07:59:50 +08:00
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use pgstattuple functions"))));
|
|
|
|
|
2003-06-12 16:02:57 +08:00
|
|
|
/* open relation */
|
2006-07-06 10:18:07 +08:00
|
|
|
rel = relation_open(relid, AccessShareLock);
|
2003-06-12 16:02:57 +08:00
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
|
2003-06-12 16:02:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-07-06 10:18:07 +08:00
|
|
|
* pgstat_relation
|
2003-06-12 16:02:57 +08:00
|
|
|
*/
|
|
|
|
static Datum
|
2006-07-06 10:18:07 +08:00
|
|
|
pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
|
2003-06-12 16:02:57 +08:00
|
|
|
{
|
2006-07-06 10:18:07 +08:00
|
|
|
const char *err;
|
2002-08-23 16:19:49 +08:00
|
|
|
|
2009-04-01 06:54:31 +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.
|
|
|
|
*/
|
|
|
|
if (RELATION_IS_OTHER_TEMP(rel))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("cannot access temporary tables of other sessions")));
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
switch (rel->rd_rel->relkind)
|
2006-07-06 10:18:07 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
case RELKIND_RELATION:
|
|
|
|
case RELKIND_TOASTVALUE:
|
|
|
|
case RELKIND_UNCATALOGED:
|
|
|
|
case RELKIND_SEQUENCE:
|
|
|
|
return pgstat_heap(rel, fcinfo);
|
|
|
|
case RELKIND_INDEX:
|
|
|
|
switch (rel->rd_rel->relam)
|
|
|
|
{
|
|
|
|
case BTREE_AM_OID:
|
|
|
|
return pgstat_index(rel, BTREE_METAPAGE + 1,
|
|
|
|
pgstat_btree_page, fcinfo);
|
|
|
|
case HASH_AM_OID:
|
|
|
|
return pgstat_index(rel, HASH_METAPAGE + 1,
|
|
|
|
pgstat_hash_page, fcinfo);
|
|
|
|
case GIST_AM_OID:
|
|
|
|
return pgstat_index(rel, GIST_ROOT_BLKNO + 1,
|
|
|
|
pgstat_gist_page, fcinfo);
|
|
|
|
case GIN_AM_OID:
|
|
|
|
err = "gin index";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = "unknown index";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RELKIND_VIEW:
|
|
|
|
err = "view";
|
|
|
|
break;
|
|
|
|
case RELKIND_COMPOSITE_TYPE:
|
|
|
|
err = "composite type";
|
2006-07-06 10:18:07 +08:00
|
|
|
break;
|
|
|
|
default:
|
2006-10-04 08:30:14 +08:00
|
|
|
err = "unknown";
|
2006-07-06 10:18:07 +08:00
|
|
|
break;
|
|
|
|
}
|
2005-05-31 07:09:07 +08:00
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("\"%s\" (%s) is not supported",
|
2006-10-04 08:30:14 +08:00
|
|
|
RelationGetRelationName(rel), err)));
|
|
|
|
return 0; /* should not happen */
|
2006-07-06 10:18:07 +08:00
|
|
|
}
|
2002-08-23 16:19:49 +08:00
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
/*
|
|
|
|
* pgstat_heap -- returns live/dead tuples info in a heap
|
|
|
|
*/
|
|
|
|
static Datum
|
|
|
|
pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
HeapScanDesc scan;
|
|
|
|
HeapTuple tuple;
|
|
|
|
BlockNumber nblocks;
|
|
|
|
BlockNumber block = 0; /* next block to count free space in */
|
|
|
|
BlockNumber tupblock;
|
|
|
|
Buffer buffer;
|
|
|
|
pgstattuple_type stat = {0};
|
2002-08-23 16:19:49 +08:00
|
|
|
|
2008-01-14 10:53:31 +08:00
|
|
|
/* Disable syncscan because we assume we scan from block zero upwards */
|
|
|
|
scan = heap_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false);
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
nblocks = scan->rs_nblocks; /* # blocks to be scanned */
|
2004-05-09 03:09:25 +08:00
|
|
|
|
2002-08-23 16:19:49 +08:00
|
|
|
/* scan the relation */
|
2002-05-21 07:51:44 +08:00
|
|
|
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
|
2001-10-01 09:52:38 +08:00
|
|
|
{
|
2007-03-26 03:45:14 +08:00
|
|
|
/* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
|
2004-10-16 06:40:29 +08:00
|
|
|
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
|
|
|
|
|
2007-03-26 03:45:14 +08:00
|
|
|
if (HeapTupleSatisfiesVisibility(tuple, SnapshotNow, scan->rs_cbuf))
|
2001-10-25 13:50:21 +08:00
|
|
|
{
|
2006-07-06 10:18:07 +08:00
|
|
|
stat.tuple_len += tuple->t_len;
|
|
|
|
stat.tuple_count++;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-07-06 10:18:07 +08:00
|
|
|
stat.dead_tuple_len += tuple->t_len;
|
|
|
|
stat.dead_tuple_count++;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
|
2004-10-16 06:40:29 +08:00
|
|
|
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
2001-12-20 04:28:41 +08:00
|
|
|
/*
|
|
|
|
* To avoid physically reading the table twice, try to do the
|
2002-09-05 04:31:48 +08:00
|
|
|
* free-space scan in parallel with the heap scan. However,
|
2001-12-20 04:28:41 +08:00
|
|
|
* heap_getnext may find no tuples on a given page, so we cannot
|
|
|
|
* simply examine the pages returned by the heap scan.
|
|
|
|
*/
|
|
|
|
tupblock = BlockIdGetBlockNumber(&tuple->t_self.ip_blkid);
|
|
|
|
|
|
|
|
while (block <= tupblock)
|
2001-10-25 13:50:21 +08:00
|
|
|
{
|
|
|
|
buffer = ReadBuffer(rel, block);
|
2004-10-16 06:40:29 +08:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
2007-09-21 01:56:33 +08:00
|
|
|
stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
|
2007-10-23 01:29:35 +08:00
|
|
|
UnlockReleaseBuffer(buffer);
|
2001-12-20 04:28:41 +08:00
|
|
|
block++;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
2001-10-01 09:52:38 +08:00
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
heap_endscan(scan);
|
2001-12-20 04:28:41 +08:00
|
|
|
|
|
|
|
while (block < nblocks)
|
|
|
|
{
|
|
|
|
buffer = ReadBuffer(rel, block);
|
2007-10-23 01:29:35 +08:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
2007-09-21 01:56:33 +08:00
|
|
|
stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
|
2007-10-23 01:29:35 +08:00
|
|
|
UnlockReleaseBuffer(buffer);
|
2001-12-20 04:28:41 +08:00
|
|
|
block++;
|
|
|
|
}
|
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
stat.table_len = (uint64) nblocks *BLCKSZ;
|
2006-07-06 10:18:07 +08:00
|
|
|
|
|
|
|
return build_pgstattuple_type(&stat, fcinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-09-04 10:03:04 +08:00
|
|
|
* pgstat_btree_page -- check tuples in a btree page
|
2006-07-06 10:18:07 +08:00
|
|
|
*/
|
|
|
|
static void
|
2006-10-04 08:30:14 +08:00
|
|
|
pgstat_btree_page(pgstattuple_type * stat, Relation rel, BlockNumber blkno)
|
2006-07-06 10:18:07 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
Buffer buf;
|
|
|
|
Page page;
|
2006-07-06 10:18:07 +08:00
|
|
|
|
|
|
|
buf = ReadBuffer(rel, blkno);
|
|
|
|
LockBuffer(buf, BT_READ);
|
|
|
|
page = BufferGetPage(buf);
|
|
|
|
|
|
|
|
/* Page is valid, see what to do with it */
|
|
|
|
if (PageIsNew(page))
|
2001-10-01 09:52:38 +08:00
|
|
|
{
|
2006-07-06 10:18:07 +08:00
|
|
|
/* fully empty page */
|
|
|
|
stat->free_space += BLCKSZ;
|
2001-10-01 09:52:38 +08:00
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
else
|
2001-10-01 09:52:38 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
BTPageOpaque opaque;
|
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
|
|
|
|
if (opaque->btpo_flags & (BTP_DELETED | BTP_HALF_DEAD))
|
|
|
|
{
|
|
|
|
/* recyclable page */
|
|
|
|
stat->free_space += BLCKSZ;
|
|
|
|
}
|
|
|
|
else if (P_ISLEAF(opaque))
|
|
|
|
{
|
|
|
|
pgstat_index_page(stat, page, P_FIRSTDATAKEY(opaque),
|
2006-10-04 08:30:14 +08:00
|
|
|
PageGetMaxOffsetNumber(page));
|
2006-07-06 10:18:07 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* root or node */
|
|
|
|
}
|
2001-10-01 09:52:38 +08:00
|
|
|
}
|
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
_bt_relbuf(rel, buf);
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
/*
|
2006-09-04 10:03:04 +08:00
|
|
|
* pgstat_hash_page -- check tuples in a hash page
|
2006-07-06 10:18:07 +08:00
|
|
|
*/
|
|
|
|
static void
|
2006-10-04 08:30:14 +08:00
|
|
|
pgstat_hash_page(pgstattuple_type * stat, Relation rel, BlockNumber blkno)
|
2006-07-06 10:18:07 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
Buffer buf;
|
|
|
|
Page page;
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
_hash_getlock(rel, blkno, HASH_SHARE);
|
2007-05-04 00:45:58 +08:00
|
|
|
buf = _hash_getbuf(rel, blkno, HASH_READ, 0);
|
2006-07-06 10:18:07 +08:00
|
|
|
page = BufferGetPage(buf);
|
|
|
|
|
|
|
|
if (PageGetSpecialSize(page) == MAXALIGN(sizeof(HashPageOpaqueData)))
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
HashPageOpaque opaque;
|
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
opaque = (HashPageOpaque) PageGetSpecialPointer(page);
|
|
|
|
switch (opaque->hasho_flag)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
case LH_UNUSED_PAGE:
|
|
|
|
stat->free_space += BLCKSZ;
|
|
|
|
break;
|
|
|
|
case LH_BUCKET_PAGE:
|
|
|
|
case LH_OVERFLOW_PAGE:
|
|
|
|
pgstat_index_page(stat, page, FirstOffsetNumber,
|
|
|
|
PageGetMaxOffsetNumber(page));
|
|
|
|
break;
|
|
|
|
case LH_BITMAP_PAGE:
|
|
|
|
case LH_META_PAGE:
|
|
|
|
default:
|
|
|
|
break;
|
2006-07-06 10:18:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* maybe corrupted */
|
|
|
|
}
|
|
|
|
|
|
|
|
_hash_relbuf(rel, buf);
|
|
|
|
_hash_droplock(rel, blkno, HASH_SHARE);
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
/*
|
2006-09-04 10:03:04 +08:00
|
|
|
* pgstat_gist_page -- check tuples in a gist page
|
2006-07-06 10:18:07 +08:00
|
|
|
*/
|
|
|
|
static void
|
2006-10-04 08:30:14 +08:00
|
|
|
pgstat_gist_page(pgstattuple_type * stat, Relation rel, BlockNumber blkno)
|
2006-07-06 10:18:07 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
Buffer buf;
|
|
|
|
Page page;
|
2006-07-06 10:18:07 +08:00
|
|
|
|
|
|
|
buf = ReadBuffer(rel, blkno);
|
|
|
|
LockBuffer(buf, GIST_SHARE);
|
|
|
|
gistcheckpage(rel, buf);
|
|
|
|
page = BufferGetPage(buf);
|
|
|
|
|
|
|
|
if (GistPageIsLeaf(page))
|
|
|
|
{
|
|
|
|
pgstat_index_page(stat, page, FirstOffsetNumber,
|
2006-10-04 08:30:14 +08:00
|
|
|
PageGetMaxOffsetNumber(page));
|
2006-07-06 10:18:07 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* root or node */
|
|
|
|
}
|
|
|
|
|
|
|
|
UnlockReleaseBuffer(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pgstat_index -- returns live/dead tuples info in a generic index
|
|
|
|
*/
|
|
|
|
static Datum
|
2006-09-04 10:03:04 +08:00
|
|
|
pgstat_index(Relation rel, BlockNumber start, pgstat_page pagefn,
|
|
|
|
FunctionCallInfo fcinfo)
|
2006-07-06 10:18:07 +08:00
|
|
|
{
|
|
|
|
BlockNumber nblocks;
|
|
|
|
BlockNumber blkno;
|
2006-10-04 08:30:14 +08:00
|
|
|
pgstattuple_type stat = {0};
|
2006-07-06 10:18:07 +08:00
|
|
|
|
|
|
|
blkno = start;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Get the current relation length */
|
|
|
|
LockRelationForExtension(rel, ExclusiveLock);
|
|
|
|
nblocks = RelationGetNumberOfBlocks(rel);
|
|
|
|
UnlockRelationForExtension(rel, ExclusiveLock);
|
|
|
|
|
|
|
|
/* Quit if we've scanned the whole relation */
|
|
|
|
if (blkno >= nblocks)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
stat.table_len = (uint64) nblocks *BLCKSZ;
|
|
|
|
|
2006-07-06 10:18:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; blkno < nblocks; blkno++)
|
2006-09-04 10:03:04 +08:00
|
|
|
pagefn(&stat, rel, blkno);
|
2006-07-06 10:18:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
2006-09-04 10:03:04 +08:00
|
|
|
return build_pgstattuple_type(&stat, fcinfo);
|
2006-07-06 10:18:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pgstat_index_page -- for generic index page
|
|
|
|
*/
|
|
|
|
static void
|
2006-10-04 08:30:14 +08:00
|
|
|
pgstat_index_page(pgstattuple_type * stat, Page page,
|
|
|
|
OffsetNumber minoff, OffsetNumber maxoff)
|
2006-07-06 10:18:07 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
OffsetNumber i;
|
2006-07-06 10:18:07 +08:00
|
|
|
|
|
|
|
stat->free_space += PageGetFreeSpace(page);
|
|
|
|
|
|
|
|
for (i = minoff; i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
ItemId itemid = PageGetItemId(page, i);
|
2006-07-06 10:18:07 +08:00
|
|
|
|
2007-09-13 06:10:26 +08:00
|
|
|
if (ItemIdIsDead(itemid))
|
2006-07-06 10:18:07 +08:00
|
|
|
{
|
|
|
|
stat->dead_tuple_count++;
|
|
|
|
stat->dead_tuple_len += ItemIdGetLength(itemid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stat->tuple_count++;
|
|
|
|
stat->tuple_len += ItemIdGetLength(itemid);
|
|
|
|
}
|
|
|
|
}
|
2001-10-01 09:52:38 +08:00
|
|
|
}
|