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"
|
|
|
|
|
2012-12-05 15:58:03 +08:00
|
|
|
#include "access/gin_private.h"
|
2006-09-03 01:05:29 +08:00
|
|
|
#include "access/heapam.h"
|
2017-02-04 03:35:25 +08:00
|
|
|
#include "access/hash.h"
|
2012-12-05 15:58:03 +08:00
|
|
|
#include "access/htup_details.h"
|
2006-09-03 01:05:29 +08:00
|
|
|
#include "access/nbtree.h"
|
|
|
|
#include "catalog/namespace.h"
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 08:36:59 +08:00
|
|
|
#include "catalog/pg_am.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"
|
2017-02-04 03:35:25 +08:00
|
|
|
#include "storage/lmgr.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"
|
2017-01-21 09:29:53 +08:00
|
|
|
#include "utils/varlena.h"
|
2006-09-03 01:05:29 +08:00
|
|
|
|
|
|
|
|
2013-07-19 02:50:20 +08:00
|
|
|
/*
|
|
|
|
* Because of backward-compatibility issue, we have decided to have
|
|
|
|
* two types of interfaces, with regclass-type input arg and text-type
|
|
|
|
* input arg, for each function.
|
|
|
|
*
|
|
|
|
* Those functions which have text-type input arg will be deprecated
|
|
|
|
* in the future release.
|
|
|
|
*/
|
2007-08-27 07:59:50 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pgstatindex);
|
2013-07-19 02:50:20 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pgstatindexbyid);
|
2007-08-27 07:59:50 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pg_relpages);
|
2013-07-19 02:50:20 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pg_relpagesbyid);
|
2012-12-05 15:58:03 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pgstatginindex);
|
2017-02-04 03:35:25 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pgstathashindex);
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2016-09-30 10:13:38 +08:00
|
|
|
PG_FUNCTION_INFO_V1(pgstatindex_v1_5);
|
|
|
|
PG_FUNCTION_INFO_V1(pgstatindexbyid_v1_5);
|
|
|
|
PG_FUNCTION_INFO_V1(pg_relpages_v1_5);
|
|
|
|
PG_FUNCTION_INFO_V1(pg_relpagesbyid_v1_5);
|
|
|
|
PG_FUNCTION_INFO_V1(pgstatginindex_v1_5);
|
|
|
|
|
2017-05-18 04:31:56 +08:00
|
|
|
Datum pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo);
|
2016-09-30 10:13:38 +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)
|
2012-12-05 15:58:03 +08:00
|
|
|
#define IS_GIN(r) ((r)->rd_rel->relam == GIN_AM_OID)
|
2017-02-04 03:35:25 +08:00
|
|
|
#define IS_HASH(r) ((r)->rd_rel->relam == HASH_AM_OID)
|
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 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
|
|
|
|
2012-12-05 15:58:03 +08:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* A structure for a whole GIN index statistics
|
|
|
|
* used by pgstatginindex().
|
|
|
|
* ------------------------------------------------
|
|
|
|
*/
|
|
|
|
typedef struct GinIndexStat
|
|
|
|
{
|
|
|
|
int32 version;
|
|
|
|
|
2013-05-30 04:58:43 +08:00
|
|
|
BlockNumber pending_pages;
|
2012-12-05 15:58:03 +08:00
|
|
|
int64 pending_tuples;
|
|
|
|
} GinIndexStat;
|
|
|
|
|
2017-02-04 03:35:25 +08:00
|
|
|
/* ------------------------------------------------
|
|
|
|
* A structure for a whole HASH index statistics
|
|
|
|
* used by pgstathashindex().
|
|
|
|
* ------------------------------------------------
|
|
|
|
*/
|
|
|
|
typedef struct HashIndexStat
|
|
|
|
{
|
2017-05-18 04:31:56 +08:00
|
|
|
int32 version;
|
|
|
|
int32 space_per_page;
|
2017-02-04 03:35:25 +08:00
|
|
|
|
2017-05-18 04:31:56 +08:00
|
|
|
BlockNumber bucket_pages;
|
2017-02-04 03:35:25 +08:00
|
|
|
BlockNumber overflow_pages;
|
|
|
|
BlockNumber bitmap_pages;
|
2017-04-12 23:53:00 +08:00
|
|
|
BlockNumber unused_pages;
|
2017-02-04 03:35:25 +08:00
|
|
|
|
2017-05-18 04:31:56 +08:00
|
|
|
int64 live_items;
|
|
|
|
int64 dead_items;
|
|
|
|
uint64 free_space;
|
2017-02-04 03:35:25 +08:00
|
|
|
} HashIndexStat;
|
|
|
|
|
2013-07-19 02:50:20 +08:00
|
|
|
static Datum pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo);
|
2017-02-04 03:35:25 +08:00
|
|
|
static void GetHashPageStats(Page page, HashIndexStat *stats);
|
2017-03-10 05:34:25 +08:00
|
|
|
static void check_relation_relkind(Relation rel);
|
2013-07-19 02:50:20 +08:00
|
|
|
|
2006-09-03 01:05:29 +08:00
|
|
|
/* ------------------------------------------------------
|
|
|
|
* pgstatindex()
|
|
|
|
*
|
|
|
|
* Usage: SELECT * FROM pgstatindex('t1_pkey');
|
2016-09-30 10:13:38 +08:00
|
|
|
*
|
|
|
|
* The superuser() check here must be kept as the library might be upgraded
|
|
|
|
* without the extension being upgraded, meaning that in pre-1.5 installations
|
|
|
|
* these functions could be called by any user.
|
2006-09-03 01:05:29 +08:00
|
|
|
* ------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pgstatindex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-03-13 07:35:34 +08:00
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
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);
|
|
|
|
|
2013-07-19 02:50:20 +08:00
|
|
|
PG_RETURN_DATUM(pgstatindex_impl(rel, fcinfo));
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:13:38 +08:00
|
|
|
/*
|
|
|
|
* As of pgstattuple version 1.5, we no longer need to check if the user
|
|
|
|
* is a superuser because we REVOKE EXECUTE on the function from PUBLIC.
|
|
|
|
* Users can then grant access to it based on their policies.
|
|
|
|
*
|
|
|
|
* Otherwise identical to pgstatindex (above).
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pgstatindex_v1_5(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-03-13 07:35:34 +08:00
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
2016-09-30 10:13:38 +08:00
|
|
|
Relation rel;
|
|
|
|
RangeVar *relrv;
|
|
|
|
|
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(pgstatindex_impl(rel, fcinfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The superuser() check here must be kept as the library might be upgraded
|
|
|
|
* without the extension being upgraded, meaning that in pre-1.5 installations
|
|
|
|
* these functions could be called by any user.
|
|
|
|
*/
|
2013-07-19 02:50:20 +08:00
|
|
|
Datum
|
|
|
|
pgstatindexbyid(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid = PG_GETARG_OID(0);
|
|
|
|
Relation rel;
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use pgstattuple functions"))));
|
|
|
|
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(pgstatindex_impl(rel, fcinfo));
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:13:38 +08:00
|
|
|
/* No need for superuser checks in v1.5, see above */
|
|
|
|
Datum
|
|
|
|
pgstatindexbyid_v1_5(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid = PG_GETARG_OID(0);
|
|
|
|
Relation rel;
|
|
|
|
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(pgstatindex_impl(rel, fcinfo));
|
|
|
|
}
|
|
|
|
|
2013-07-19 02:50:20 +08:00
|
|
|
static Datum
|
|
|
|
pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
|
|
|
|
{
|
|
|
|
Datum result;
|
|
|
|
BlockNumber nblocks;
|
|
|
|
BlockNumber blkno;
|
|
|
|
BTIndexStat indexStat;
|
|
|
|
BufferAccessStrategy bstrategy = GetAccessStrategy(BAS_BULKREAD);
|
|
|
|
|
2006-09-03 01:05:29 +08:00
|
|
|
if (!IS_INDEX(rel) || !IS_BTREE(rel))
|
2017-03-10 05:34:25 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("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
|
|
|
*/
|
|
|
|
{
|
2012-03-13 21:51:03 +08:00
|
|
|
Buffer buffer = ReadBufferExtended(rel, MAIN_FORKNUM, 0, RBM_NORMAL, bstrategy);
|
2016-04-20 21:31:19 +08:00
|
|
|
Page page = BufferGetPage(buffer);
|
2006-09-03 01:05:29 +08:00
|
|
|
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.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 */
|
2012-06-11 03:20:04 +08:00
|
|
|
buffer = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
|
2007-05-18 03:11:25 +08:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
2006-09-03 01:05:29 +08:00
|
|
|
|
2016-04-20 21:31:19 +08:00
|
|
|
page = BufferGetPage(buffer);
|
2007-05-18 03:11:25 +08:00
|
|
|
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 */
|
|
|
|
|
Fix multiple bugs in contrib/pgstattuple's pgstatindex() function.
Dead or half-dead index leaf pages were incorrectly reported as live, as a
consequence of a code rearrangement I made (during a moment of severe brain
fade, evidently) in commit d287818eb514d431.
The index metapage was not counted in index_size, causing that result to
not agree with the actual index size on-disk.
Index root pages were not counted in internal_pages, which is inconsistent
compared to the case of a root that's also a leaf (one-page index), where
the root would be counted in leaf_pages. Aside from that inconsistency,
this could lead to additional transient discrepancies between the reported
page counts and index_size, since it's possible for pgstatindex's scan to
see zero or multiple pages marked as BTP_ROOT, if the root moves due to
a split during the scan. With these fixes, index_size will always be
exactly one page more than the sum of the displayed page counts.
Also, the index_size result was incorrectly documented as being measured in
pages; it's always been measured in bytes. (While fixing that, I couldn't
resist doing some small additional wordsmithing on the pgstattuple docs.)
Including the metapage causes the reported index_size to not be zero for
an empty index. To preserve the desired property that the pgstattuple
regression test results are platform-independent (ie, BLCKSZ configuration
independent), scale the index_size result in the regression tests.
The documentation issue was reported by Otsuka Kenji, and the inconsistent
root page counting by Peter Geoghegan; the other problems noted by me.
Back-patch to all supported branches, because this has been broken for
a long time.
2016-02-19 04:40:35 +08:00
|
|
|
if (P_ISDELETED(opaque))
|
|
|
|
indexStat.deleted_pages++;
|
|
|
|
else if (P_IGNORE(opaque))
|
|
|
|
indexStat.empty_pages++; /* this is the "half dead" state */
|
|
|
|
else 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
|
|
|
}
|
2007-05-18 03:11:25 +08:00
|
|
|
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;
|
2014-01-07 10:30:26 +08:00
|
|
|
values[j++] = psprintf("%d", indexStat.version);
|
|
|
|
values[j++] = psprintf("%d", indexStat.level);
|
|
|
|
values[j++] = psprintf(INT64_FORMAT,
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-22 03:18:54 +08:00
|
|
|
(1 + /* include the metapage in index_size */
|
2014-05-07 00:12:18 +08:00
|
|
|
indexStat.leaf_pages +
|
|
|
|
indexStat.internal_pages +
|
|
|
|
indexStat.deleted_pages +
|
|
|
|
indexStat.empty_pages) * BLCKSZ);
|
2014-01-07 10:30:26 +08:00
|
|
|
values[j++] = psprintf("%u", indexStat.root_blkno);
|
|
|
|
values[j++] = psprintf(INT64_FORMAT, indexStat.internal_pages);
|
|
|
|
values[j++] = psprintf(INT64_FORMAT, indexStat.leaf_pages);
|
|
|
|
values[j++] = psprintf(INT64_FORMAT, indexStat.empty_pages);
|
|
|
|
values[j++] = psprintf(INT64_FORMAT, indexStat.deleted_pages);
|
2011-08-25 11:50:10 +08:00
|
|
|
if (indexStat.max_avail > 0)
|
2014-01-07 10:30:26 +08:00
|
|
|
values[j++] = psprintf("%.2f",
|
2014-05-07 00:12:18 +08:00
|
|
|
100.0 - (double) indexStat.free_space / (double) indexStat.max_avail * 100.0);
|
2011-08-25 11:50:10 +08:00
|
|
|
else
|
2014-01-07 10:30:26 +08:00
|
|
|
values[j++] = pstrdup("NaN");
|
2011-08-25 11:50:10 +08:00
|
|
|
if (indexStat.leaf_pages > 0)
|
2014-01-07 10:30:26 +08:00
|
|
|
values[j++] = psprintf("%.2f",
|
2014-05-07 00:12:18 +08:00
|
|
|
(double) indexStat.fragments / (double) indexStat.leaf_pages * 100.0);
|
2011-08-25 11:50:10 +08:00
|
|
|
else
|
2014-01-07 10:30:26 +08:00
|
|
|
values[j++] = pstrdup("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
|
|
|
}
|
|
|
|
|
2013-07-19 02:50:20 +08:00
|
|
|
return result;
|
2006-09-03 01:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------
|
|
|
|
* 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');
|
2016-09-30 10:13:38 +08:00
|
|
|
*
|
|
|
|
* Must keep superuser() check, see above.
|
2006-09-03 01:05:29 +08:00
|
|
|
* --------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_relpages(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-03-13 07:35:34 +08:00
|
|
|
text *relname = PG_GETARG_TEXT_PP(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);
|
|
|
|
|
2017-03-10 05:34:25 +08:00
|
|
|
/* only some relkinds have storage */
|
|
|
|
check_relation_relkind(rel);
|
|
|
|
|
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);
|
|
|
|
|
2013-07-19 02:50:20 +08:00
|
|
|
PG_RETURN_INT64(relpages);
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:13:38 +08:00
|
|
|
/* No need for superuser checks in v1.5, see above */
|
|
|
|
Datum
|
|
|
|
pg_relpages_v1_5(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-03-13 07:35:34 +08:00
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
2016-09-30 10:13:38 +08:00
|
|
|
int64 relpages;
|
|
|
|
Relation rel;
|
|
|
|
RangeVar *relrv;
|
|
|
|
|
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
|
|
|
|
2017-03-10 05:34:25 +08:00
|
|
|
/* only some relkinds have storage */
|
|
|
|
check_relation_relkind(rel);
|
|
|
|
|
2016-09-30 10:13:38 +08:00
|
|
|
/* note: this will work OK on non-local temp tables */
|
|
|
|
|
|
|
|
relpages = RelationGetNumberOfBlocks(rel);
|
|
|
|
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
PG_RETURN_INT64(relpages);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must keep superuser() check, see above. */
|
2013-07-19 02:50:20 +08:00
|
|
|
Datum
|
|
|
|
pg_relpagesbyid(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid = PG_GETARG_OID(0);
|
|
|
|
int64 relpages;
|
|
|
|
Relation rel;
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use pgstattuple functions"))));
|
|
|
|
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
|
2017-03-10 05:34:25 +08:00
|
|
|
/* only some relkinds have storage */
|
|
|
|
check_relation_relkind(rel);
|
|
|
|
|
2013-07-19 02:50:20 +08:00
|
|
|
/* note: this will work OK on non-local temp tables */
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2012-12-05 15:58:03 +08:00
|
|
|
|
2016-09-30 10:13:38 +08:00
|
|
|
/* No need for superuser checks in v1.5, see above */
|
|
|
|
Datum
|
|
|
|
pg_relpagesbyid_v1_5(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid = PG_GETARG_OID(0);
|
|
|
|
int64 relpages;
|
|
|
|
Relation rel;
|
|
|
|
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
|
2017-03-10 05:34:25 +08:00
|
|
|
/* only some relkinds have storage */
|
|
|
|
check_relation_relkind(rel);
|
|
|
|
|
2016-09-30 10:13:38 +08:00
|
|
|
/* note: this will work OK on non-local temp tables */
|
|
|
|
|
|
|
|
relpages = RelationGetNumberOfBlocks(rel);
|
|
|
|
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
PG_RETURN_INT64(relpages);
|
|
|
|
}
|
|
|
|
|
2012-12-05 15:58:03 +08:00
|
|
|
/* ------------------------------------------------------
|
|
|
|
* pgstatginindex()
|
|
|
|
*
|
|
|
|
* Usage: SELECT * FROM pgstatginindex('ginindex');
|
2016-09-30 10:13:38 +08:00
|
|
|
*
|
|
|
|
* Must keep superuser() check, see above.
|
2012-12-05 15:58:03 +08:00
|
|
|
* ------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pgstatginindex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid = PG_GETARG_OID(0);
|
2016-09-30 10:13:38 +08:00
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use pgstattuple functions"))));
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(pgstatginindex_internal(relid, fcinfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No need for superuser checks in v1.5, see above */
|
|
|
|
Datum
|
|
|
|
pgstatginindex_v1_5(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid = PG_GETARG_OID(0);
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(pgstatginindex_internal(relid, fcinfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo)
|
|
|
|
{
|
2012-12-05 15:58:03 +08:00
|
|
|
Relation rel;
|
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
2013-05-30 04:58:43 +08:00
|
|
|
GinMetaPageData *metadata;
|
2012-12-05 15:58:03 +08:00
|
|
|
GinIndexStat stats;
|
|
|
|
HeapTuple tuple;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
Datum values[3];
|
|
|
|
bool nulls[3] = {false, false, false};
|
|
|
|
Datum result;
|
|
|
|
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
|
|
|
|
if (!IS_INDEX(rel) || !IS_GIN(rel))
|
2017-03-10 05:34:25 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("relation \"%s\" is not a GIN index",
|
|
|
|
RelationGetRelationName(rel))));
|
2012-12-05 15:58:03 +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),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-22 03:35:54 +08:00
|
|
|
errmsg("cannot access temporary indexes of other sessions")));
|
2012-12-05 15:58:03 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read metapage
|
|
|
|
*/
|
|
|
|
buffer = ReadBuffer(rel, GIN_METAPAGE_BLKNO);
|
|
|
|
LockBuffer(buffer, GIN_SHARE);
|
2016-04-20 21:31:19 +08:00
|
|
|
page = BufferGetPage(buffer);
|
2012-12-05 15:58:03 +08:00
|
|
|
metadata = GinPageGetMeta(page);
|
|
|
|
|
|
|
|
stats.version = metadata->ginVersion;
|
|
|
|
stats.pending_pages = metadata->nPendingPages;
|
|
|
|
stats.pending_tuples = metadata->nPendingHeapTuples;
|
|
|
|
|
|
|
|
UnlockReleaseBuffer(buffer);
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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");
|
|
|
|
|
|
|
|
values[0] = Int32GetDatum(stats.version);
|
|
|
|
values[1] = UInt32GetDatum(stats.pending_pages);
|
|
|
|
values[2] = Int64GetDatum(stats.pending_tuples);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build and return the tuple
|
|
|
|
*/
|
|
|
|
tuple = heap_form_tuple(tupleDesc, values, nulls);
|
|
|
|
result = HeapTupleGetDatum(tuple);
|
|
|
|
|
2017-08-18 00:39:20 +08:00
|
|
|
return result;
|
2012-12-05 15:58:03 +08:00
|
|
|
}
|
2017-02-04 03:35:25 +08:00
|
|
|
|
|
|
|
/* ------------------------------------------------------
|
|
|
|
* pgstathashindex()
|
|
|
|
*
|
|
|
|
* Usage: SELECT * FROM pgstathashindex('hashindex');
|
|
|
|
* ------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pgstathashindex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Oid relid = PG_GETARG_OID(0);
|
2017-05-18 04:31:56 +08:00
|
|
|
BlockNumber nblocks;
|
|
|
|
BlockNumber blkno;
|
2017-02-04 03:35:25 +08:00
|
|
|
Relation rel;
|
|
|
|
HashIndexStat stats;
|
|
|
|
BufferAccessStrategy bstrategy;
|
|
|
|
HeapTuple tuple;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
Datum values[8];
|
|
|
|
bool nulls[8];
|
|
|
|
Buffer metabuf;
|
2017-05-18 04:31:56 +08:00
|
|
|
HashMetaPage metap;
|
2017-02-04 03:35:25 +08:00
|
|
|
float8 free_percent;
|
|
|
|
uint64 total_space;
|
|
|
|
|
|
|
|
rel = index_open(relid, AccessShareLock);
|
|
|
|
|
2017-03-10 05:34:25 +08:00
|
|
|
/* index_open() checks that it's an index */
|
2017-02-04 03:35:25 +08:00
|
|
|
if (!IS_HASH(rel))
|
2017-03-10 05:34:25 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
2018-05-10 00:44:50 +08:00
|
|
|
errmsg("relation \"%s\" is not a hash index",
|
2017-03-10 05:34:25 +08:00
|
|
|
RelationGetRelationName(rel))));
|
|
|
|
|
2017-02-04 03:35:25 +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),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-22 03:35:54 +08:00
|
|
|
errmsg("cannot access temporary indexes of other sessions")));
|
2017-02-04 03:35:25 +08:00
|
|
|
|
|
|
|
/* Get the information we need from the metapage. */
|
|
|
|
memset(&stats, 0, sizeof(stats));
|
|
|
|
metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_READ, LH_META_PAGE);
|
|
|
|
metap = HashPageGetMeta(BufferGetPage(metabuf));
|
|
|
|
stats.version = metap->hashm_version;
|
|
|
|
stats.space_per_page = metap->hashm_bsize;
|
|
|
|
_hash_relbuf(rel, metabuf);
|
|
|
|
|
|
|
|
/* Get the current relation length */
|
|
|
|
nblocks = RelationGetNumberOfBlocks(rel);
|
|
|
|
|
|
|
|
/* prepare access strategy for this index */
|
|
|
|
bstrategy = GetAccessStrategy(BAS_BULKREAD);
|
|
|
|
|
|
|
|
/* Start from blkno 1 as 0th block is metapage */
|
|
|
|
for (blkno = 1; blkno < nblocks; blkno++)
|
|
|
|
{
|
|
|
|
Buffer buf;
|
|
|
|
Page page;
|
|
|
|
|
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
|
|
|
|
buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL,
|
|
|
|
bstrategy);
|
|
|
|
LockBuffer(buf, BUFFER_LOCK_SHARE);
|
|
|
|
page = (Page) BufferGetPage(buf);
|
|
|
|
|
|
|
|
if (PageIsNew(page))
|
2017-04-12 23:53:00 +08:00
|
|
|
stats.unused_pages++;
|
2017-02-04 03:35:25 +08:00
|
|
|
else if (PageGetSpecialSize(page) !=
|
|
|
|
MAXALIGN(sizeof(HashPageOpaqueData)))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INDEX_CORRUPTED),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-22 03:35:54 +08:00
|
|
|
errmsg("index \"%s\" contains corrupted page at block %u",
|
|
|
|
RelationGetRelationName(rel),
|
|
|
|
BufferGetBlockNumber(buf))));
|
2017-02-04 03:35:25 +08:00
|
|
|
else
|
|
|
|
{
|
2017-05-18 04:31:56 +08:00
|
|
|
HashPageOpaque opaque;
|
|
|
|
int pagetype;
|
2017-04-12 23:53:00 +08:00
|
|
|
|
2017-02-04 03:35:25 +08:00
|
|
|
opaque = (HashPageOpaque) PageGetSpecialPointer(page);
|
2017-04-12 23:53:00 +08:00
|
|
|
pagetype = opaque->hasho_flag & LH_PAGE_TYPE;
|
|
|
|
|
|
|
|
if (pagetype == LH_BUCKET_PAGE)
|
2017-02-04 03:35:25 +08:00
|
|
|
{
|
|
|
|
stats.bucket_pages++;
|
|
|
|
GetHashPageStats(page, &stats);
|
|
|
|
}
|
2017-04-12 23:53:00 +08:00
|
|
|
else if (pagetype == LH_OVERFLOW_PAGE)
|
2017-02-04 03:35:25 +08:00
|
|
|
{
|
|
|
|
stats.overflow_pages++;
|
|
|
|
GetHashPageStats(page, &stats);
|
|
|
|
}
|
2017-04-12 23:53:00 +08:00
|
|
|
else if (pagetype == LH_BITMAP_PAGE)
|
2017-02-04 03:35:25 +08:00
|
|
|
stats.bitmap_pages++;
|
2017-04-12 23:53:00 +08:00
|
|
|
else if (pagetype == LH_UNUSED_PAGE)
|
|
|
|
stats.unused_pages++;
|
2017-02-04 03:35:25 +08:00
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INDEX_CORRUPTED),
|
2017-05-18 04:31:56 +08:00
|
|
|
errmsg("unexpected page type 0x%04X in HASH index \"%s\" block %u",
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-22 03:35:54 +08:00
|
|
|
opaque->hasho_flag, RelationGetRelationName(rel),
|
2017-05-18 04:31:56 +08:00
|
|
|
BufferGetBlockNumber(buf))));
|
2017-02-04 03:35:25 +08:00
|
|
|
}
|
|
|
|
UnlockReleaseBuffer(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Done accessing the index */
|
|
|
|
index_close(rel, AccessShareLock);
|
|
|
|
|
2017-04-12 23:53:00 +08:00
|
|
|
/* Count unused pages as free space. */
|
2017-08-10 23:48:42 +08:00
|
|
|
stats.free_space += (uint64) stats.unused_pages * stats.space_per_page;
|
2017-02-04 03:35:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Total space available for tuples excludes the metapage and the bitmap
|
|
|
|
* pages.
|
|
|
|
*/
|
2017-08-10 23:48:42 +08:00
|
|
|
total_space = (uint64) (nblocks - (stats.bitmap_pages + 1)) *
|
|
|
|
stats.space_per_page;
|
2017-02-04 03:35:25 +08:00
|
|
|
|
|
|
|
if (total_space == 0)
|
|
|
|
free_percent = 0.0;
|
|
|
|
else
|
|
|
|
free_percent = 100.0 * stats.free_space / total_space;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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");
|
|
|
|
|
|
|
|
tupleDesc = BlessTupleDesc(tupleDesc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build and return the tuple
|
|
|
|
*/
|
|
|
|
MemSet(nulls, 0, sizeof(nulls));
|
|
|
|
values[0] = Int32GetDatum(stats.version);
|
|
|
|
values[1] = Int64GetDatum((int64) stats.bucket_pages);
|
|
|
|
values[2] = Int64GetDatum((int64) stats.overflow_pages);
|
|
|
|
values[3] = Int64GetDatum((int64) stats.bitmap_pages);
|
2017-04-12 23:53:00 +08:00
|
|
|
values[4] = Int64GetDatum((int64) stats.unused_pages);
|
2017-02-04 03:35:25 +08:00
|
|
|
values[5] = Int64GetDatum(stats.live_items);
|
|
|
|
values[6] = Int64GetDatum(stats.dead_items);
|
|
|
|
values[7] = Float8GetDatum(free_percent);
|
|
|
|
tuple = heap_form_tuple(tupleDesc, values, nulls);
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------
|
|
|
|
* GetHashPageStatis()
|
|
|
|
*
|
|
|
|
* Collect statistics of single hash page
|
|
|
|
* -------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
GetHashPageStats(Page page, HashIndexStat *stats)
|
|
|
|
{
|
|
|
|
OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
|
2017-05-18 04:31:56 +08:00
|
|
|
int off;
|
2017-02-04 03:35:25 +08:00
|
|
|
|
|
|
|
/* count live and dead tuples, and free space */
|
|
|
|
for (off = FirstOffsetNumber; off <= maxoff; off++)
|
|
|
|
{
|
2017-05-18 04:31:56 +08:00
|
|
|
ItemId id = PageGetItemId(page, off);
|
2017-02-04 03:35:25 +08:00
|
|
|
|
|
|
|
if (!ItemIdIsDead(id))
|
|
|
|
stats->live_items++;
|
|
|
|
else
|
|
|
|
stats->dead_items++;
|
|
|
|
}
|
|
|
|
stats->free_space += PageGetExactFreeSpace(page);
|
|
|
|
}
|
2017-03-10 05:34:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check_relation_relkind - convenience routine to check that relation
|
|
|
|
* is of the relkind supported by the callers
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
check_relation_relkind(Relation rel)
|
|
|
|
{
|
|
|
|
if (rel->rd_rel->relkind != RELKIND_RELATION &&
|
|
|
|
rel->rd_rel->relkind != RELKIND_INDEX &&
|
|
|
|
rel->rd_rel->relkind != RELKIND_MATVIEW &&
|
|
|
|
rel->rd_rel->relkind != RELKIND_SEQUENCE &&
|
|
|
|
rel->rd_rel->relkind != RELKIND_TOASTVALUE)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is not a table, index, materialized view, sequence, or TOAST table",
|
|
|
|
RelationGetRelationName(rel))));
|
|
|
|
}
|