2001-10-01 05:52:38 +04:00
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* $PostgreSQL: pgsql/contrib/pgstattuple/pgstattuple.c,v 1.20 2005/10/15 02:49:06 momjian Exp $
|
2001-10-01 05:52:38 +04:00
|
|
|
*
|
2002-09-05 00:31:48 +04:00
|
|
|
* Copyright (c) 2001,2002 Tatsuo Ishii
|
2001-10-01 05:52:38 +04: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-19 23:28:41 +03:00
|
|
|
|
2001-10-01 05:52:38 +04:00
|
|
|
#include "fmgr.h"
|
2002-08-29 21:14:33 +04:00
|
|
|
#include "funcapi.h"
|
2001-10-01 05:52:38 +04:00
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "access/transam.h"
|
2002-03-30 04:02:42 +03:00
|
|
|
#include "catalog/namespace.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
2001-10-01 05:52:38 +04:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(pgstattuple);
|
2003-06-12 12:02:57 +04:00
|
|
|
PG_FUNCTION_INFO_V1(pgstattuplebyid);
|
2001-10-01 05:52:38 +04:00
|
|
|
|
|
|
|
extern Datum pgstattuple(PG_FUNCTION_ARGS);
|
2003-06-12 12:02:57 +04:00
|
|
|
extern Datum pgstattuplebyid(PG_FUNCTION_ARGS);
|
|
|
|
|
2005-05-31 03:09:07 +04:00
|
|
|
static Datum pgstattuple_real(Relation rel, FunctionCallInfo fcinfo);
|
|
|
|
|
2001-10-01 05:52:38 +04:00
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* pgstattuple:
|
2002-08-23 12:19:49 +04:00
|
|
|
* returns live/dead tuples info
|
2001-10-01 05:52:38 +04:00
|
|
|
*
|
|
|
|
* C FUNCTION definition
|
2002-08-29 21:14:33 +04:00
|
|
|
* pgstattuple(text) returns pgstattuple_type
|
|
|
|
* see pgstattuple.sql for pgstattuple_type
|
2001-10-01 05:52:38 +04:00
|
|
|
* ----------
|
|
|
|
*/
|
2002-08-23 12:19:49 +04:00
|
|
|
|
|
|
|
#define NCOLUMNS 9
|
|
|
|
#define NCHARS 32
|
|
|
|
|
2001-10-01 05:52:38 +04:00
|
|
|
Datum
|
|
|
|
pgstattuple(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-08-29 21:14:33 +04:00
|
|
|
text *relname = PG_GETARG_TEXT_P(0);
|
2002-03-30 04:02:42 +03:00
|
|
|
RangeVar *relrv;
|
2001-10-25 09:50:21 +04:00
|
|
|
Relation rel;
|
2003-06-12 12:02:57 +04:00
|
|
|
Datum result;
|
|
|
|
|
|
|
|
/* open relation */
|
2005-05-27 04:57:49 +04:00
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
2003-06-12 12:02:57 +04:00
|
|
|
rel = heap_openrv(relrv, AccessShareLock);
|
|
|
|
|
2005-05-31 03:09:07 +04:00
|
|
|
result = pgstattuple_real(rel, fcinfo);
|
2003-06-12 12:02:57 +04:00
|
|
|
|
|
|
|
PG_RETURN_DATUM(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pgstattuplebyid(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid = PG_GETARG_OID(0);
|
|
|
|
Relation rel;
|
|
|
|
Datum result;
|
|
|
|
|
|
|
|
/* open relation */
|
|
|
|
rel = heap_open(relid, AccessShareLock);
|
|
|
|
|
2005-05-31 03:09:07 +04:00
|
|
|
result = pgstattuple_real(rel, fcinfo);
|
2003-06-12 12:02:57 +04:00
|
|
|
|
|
|
|
PG_RETURN_DATUM(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pgstattuple_real
|
|
|
|
*
|
|
|
|
* The real work occurs here
|
|
|
|
*/
|
|
|
|
static Datum
|
2005-05-31 03:09:07 +04:00
|
|
|
pgstattuple_real(Relation rel, FunctionCallInfo fcinfo)
|
2003-06-12 12:02:57 +04:00
|
|
|
{
|
2001-10-25 09:50:21 +04:00
|
|
|
HeapScanDesc scan;
|
|
|
|
HeapTuple tuple;
|
|
|
|
BlockNumber nblocks;
|
2001-12-19 23:28:41 +03:00
|
|
|
BlockNumber block = 0; /* next block to count free space in */
|
|
|
|
BlockNumber tupblock;
|
|
|
|
Buffer buffer;
|
2002-08-23 12:19:49 +04:00
|
|
|
uint64 table_len;
|
2001-10-25 09:50:21 +04:00
|
|
|
uint64 tuple_len = 0;
|
|
|
|
uint64 dead_tuple_len = 0;
|
2001-12-19 23:28:41 +03:00
|
|
|
uint64 tuple_count = 0;
|
|
|
|
uint64 dead_tuple_count = 0;
|
2001-10-25 09:50:21 +04:00
|
|
|
double tuple_percent;
|
|
|
|
double dead_tuple_percent;
|
|
|
|
uint64 free_space = 0; /* free/reusable space in bytes */
|
|
|
|
double free_percent; /* free/reusable space in % */
|
2002-09-05 00:31:48 +04:00
|
|
|
TupleDesc tupdesc;
|
|
|
|
AttInMetadata *attinmeta;
|
|
|
|
char **values;
|
|
|
|
int i;
|
|
|
|
Datum result;
|
2002-08-23 12:19:49 +04:00
|
|
|
|
2005-05-31 03:09:07 +04:00
|
|
|
/* 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");
|
|
|
|
|
|
|
|
/* make sure we have a persistent copy of the tupdesc */
|
|
|
|
tupdesc = CreateTupleDescCopy(tupdesc);
|
2002-08-23 12:19:49 +04:00
|
|
|
|
2002-08-29 21:14:33 +04:00
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* Generate attribute metadata needed later to produce tuples from raw C
|
|
|
|
* strings
|
2002-08-29 21:14:33 +04:00
|
|
|
*/
|
|
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
2002-08-23 12:19:49 +04:00
|
|
|
|
2002-05-21 03:51:44 +04:00
|
|
|
scan = heap_beginscan(rel, SnapshotAny, 0, NULL);
|
2001-10-25 09:50:21 +04:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
nblocks = scan->rs_nblocks; /* # blocks to be scanned */
|
2004-05-08 23:09:25 +04:00
|
|
|
|
2002-08-23 12:19:49 +04:00
|
|
|
/* scan the relation */
|
2002-05-21 03:51:44 +04:00
|
|
|
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
|
2001-10-01 05:52:38 +04:00
|
|
|
{
|
2004-10-16 02:40:29 +04:00
|
|
|
/* must hold a buffer lock to call HeapTupleSatisfiesNow */
|
|
|
|
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
|
|
|
|
|
|
|
|
if (HeapTupleSatisfiesNow(tuple->t_data, scan->rs_cbuf))
|
2001-10-25 09:50:21 +04:00
|
|
|
{
|
|
|
|
tuple_len += tuple->t_len;
|
|
|
|
tuple_count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dead_tuple_len += tuple->t_len;
|
|
|
|
dead_tuple_count++;
|
|
|
|
}
|
|
|
|
|
2004-10-16 02:40:29 +04:00
|
|
|
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
2001-12-19 23:28:41 +03:00
|
|
|
/*
|
|
|
|
* To avoid physically reading the table twice, try to do the
|
2002-09-05 00:31:48 +04:00
|
|
|
* free-space scan in parallel with the heap scan. However,
|
2001-12-19 23:28:41 +03: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 09:50:21 +04:00
|
|
|
{
|
|
|
|
buffer = ReadBuffer(rel, block);
|
2004-10-16 02:40:29 +04:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
2001-10-25 09:50:21 +04:00
|
|
|
free_space += PageGetFreeSpace((Page) BufferGetPage(buffer));
|
2004-10-16 02:40:29 +04:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
2001-10-25 09:50:21 +04:00
|
|
|
ReleaseBuffer(buffer);
|
2001-12-19 23:28:41 +03:00
|
|
|
block++;
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
2001-10-01 05:52:38 +04:00
|
|
|
}
|
2001-10-25 09:50:21 +04:00
|
|
|
heap_endscan(scan);
|
2001-12-19 23:28:41 +03:00
|
|
|
|
|
|
|
while (block < nblocks)
|
|
|
|
{
|
|
|
|
buffer = ReadBuffer(rel, block);
|
|
|
|
free_space += PageGetFreeSpace((Page) BufferGetPage(buffer));
|
|
|
|
ReleaseBuffer(buffer);
|
|
|
|
block++;
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_close(rel, AccessShareLock);
|
2001-10-25 09:50:21 +04:00
|
|
|
|
2002-09-05 00:31:48 +04:00
|
|
|
table_len = (uint64) nblocks *BLCKSZ;
|
2001-10-25 09:50:21 +04:00
|
|
|
|
|
|
|
if (nblocks == 0)
|
2001-10-01 05:52:38 +04:00
|
|
|
{
|
2001-10-25 09:50:21 +04:00
|
|
|
tuple_percent = 0.0;
|
|
|
|
dead_tuple_percent = 0.0;
|
|
|
|
free_percent = 0.0;
|
2001-10-01 05:52:38 +04:00
|
|
|
}
|
2001-10-25 09:50:21 +04:00
|
|
|
else
|
2001-10-01 05:52:38 +04:00
|
|
|
{
|
2001-10-25 09:50:21 +04:00
|
|
|
tuple_percent = (double) tuple_len *100.0 / table_len;
|
|
|
|
dead_tuple_percent = (double) dead_tuple_len *100.0 / table_len;
|
|
|
|
free_percent = (double) free_space *100.0 / table_len;
|
2001-10-01 05:52:38 +04:00
|
|
|
}
|
|
|
|
|
2002-08-23 12:19:49 +04:00
|
|
|
/*
|
2005-10-15 06:49:52 +04:00
|
|
|
* 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.
|
2002-08-23 12:19:49 +04:00
|
|
|
*/
|
|
|
|
values = (char **) palloc(NCOLUMNS * sizeof(char *));
|
2002-09-05 00:31:48 +04:00
|
|
|
for (i = 0; i < NCOLUMNS; i++)
|
2002-08-23 12:19:49 +04:00
|
|
|
values[i] = (char *) palloc(NCHARS * sizeof(char));
|
|
|
|
i = 0;
|
2003-08-01 06:21:17 +04:00
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, table_len);
|
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, tuple_count);
|
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, tuple_len);
|
2002-08-23 12:19:49 +04:00
|
|
|
snprintf(values[i++], NCHARS, "%.2f", tuple_percent);
|
2003-08-01 06:21:17 +04:00
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, dead_tuple_count);
|
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, dead_tuple_len);
|
2002-08-23 12:19:49 +04:00
|
|
|
snprintf(values[i++], NCHARS, "%.2f", dead_tuple_percent);
|
2003-08-01 06:21:17 +04:00
|
|
|
snprintf(values[i++], NCHARS, INT64_FORMAT, free_space);
|
2002-08-23 12:19:49 +04:00
|
|
|
snprintf(values[i++], NCHARS, "%.2f", free_percent);
|
2002-09-05 00:31:48 +04:00
|
|
|
|
2002-08-23 12:19:49 +04:00
|
|
|
/* build a tuple */
|
|
|
|
tuple = BuildTupleFromCStrings(attinmeta, values);
|
2002-09-05 00:31:48 +04:00
|
|
|
|
2002-08-23 12:19:49 +04:00
|
|
|
/* make the tuple into a datum */
|
2004-04-02 01:28:47 +04:00
|
|
|
result = HeapTupleGetDatum(tuple);
|
2002-09-05 00:31:48 +04:00
|
|
|
|
2002-08-23 12:19:49 +04:00
|
|
|
/* Clean up */
|
2002-09-05 00:31:48 +04:00
|
|
|
for (i = 0; i < NCOLUMNS; i++)
|
2002-08-23 12:19:49 +04:00
|
|
|
pfree(values[i]);
|
|
|
|
pfree(values);
|
2002-09-05 00:31:48 +04:00
|
|
|
|
2003-08-04 04:43:34 +04:00
|
|
|
return (result);
|
2001-10-01 05:52:38 +04:00
|
|
|
}
|