Fix pgstattuple's handling of unused hash pages.

Hash indexes can contain both pages which are all-zeroes (i.e.
PageIsNew()) and pages which have been initialized but currently
aren't used.  The latter category can happen either when a page
has been reserved but not yet used or when it is used for a time
and then freed.  pgstattuple was only prepared to deal with the
pages that are actually-zeroes, which it called zero_pages.
Rename the column to unused_pages (extension version 1.5 is
as-yet-unreleased) and make it count both kinds of unused pages.

Along the way, slightly tidy up the way we test for pages of
various types.

Robert Haas and Ashutosh Sharma, reviewed by Amit Kapila

Discussion: http://postgr.es/m/CAE9k0PkTtKFB3YndOyQMjwuHx+-FtUP1ynK8E-nHtetoow3NtQ@mail.gmail.com
This commit is contained in:
Robert Haas 2017-04-12 11:53:00 -04:00
parent 1d5fede4a9
commit 9cc27566c1
4 changed files with 22 additions and 16 deletions

View File

@ -132,9 +132,9 @@ select * from pgstatginindex('test_ginidx');
create index test_hashidx on test using hash (b);
select * from pgstathashindex('test_hashidx');
version | bucket_pages | overflow_pages | bitmap_pages | zero_pages | live_items | dead_items | free_percent
---------+--------------+----------------+--------------+------------+------------+------------+--------------
3 | 4 | 0 | 1 | 0 | 0 | 0 | 100
version | bucket_pages | overflow_pages | bitmap_pages | unused_pages | live_items | dead_items | free_percent
---------+--------------+----------------+--------------+--------------+------------+------------+--------------
3 | 4 | 0 | 1 | 0 | 0 | 0 | 100
(1 row)
-- these should error with the wrong type

View File

@ -119,7 +119,7 @@ typedef struct HashIndexStat
BlockNumber bucket_pages;
BlockNumber overflow_pages;
BlockNumber bitmap_pages;
BlockNumber zero_pages;
BlockNumber unused_pages;
int64 live_items;
int64 dead_items;
@ -634,7 +634,6 @@ pgstathashindex(PG_FUNCTION_ARGS)
{
Buffer buf;
Page page;
HashPageOpaque opaque;
CHECK_FOR_INTERRUPTS();
@ -644,7 +643,7 @@ pgstathashindex(PG_FUNCTION_ARGS)
page = (Page) BufferGetPage(buf);
if (PageIsNew(page))
stats.zero_pages++;
stats.unused_pages++;
else if (PageGetSpecialSize(page) !=
MAXALIGN(sizeof(HashPageOpaqueData)))
ereport(ERROR,
@ -654,19 +653,26 @@ pgstathashindex(PG_FUNCTION_ARGS)
BufferGetBlockNumber(buf))));
else
{
HashPageOpaque opaque;
int pagetype;
opaque = (HashPageOpaque) PageGetSpecialPointer(page);
if (opaque->hasho_flag & LH_BUCKET_PAGE)
pagetype = opaque->hasho_flag & LH_PAGE_TYPE;
if (pagetype == LH_BUCKET_PAGE)
{
stats.bucket_pages++;
GetHashPageStats(page, &stats);
}
else if (opaque->hasho_flag & LH_OVERFLOW_PAGE)
else if (pagetype == LH_OVERFLOW_PAGE)
{
stats.overflow_pages++;
GetHashPageStats(page, &stats);
}
else if (opaque->hasho_flag & LH_BITMAP_PAGE)
else if (pagetype == LH_BITMAP_PAGE)
stats.bitmap_pages++;
else if (pagetype == LH_UNUSED_PAGE)
stats.unused_pages++;
else
ereport(ERROR,
(errcode(ERRCODE_INDEX_CORRUPTED),
@ -680,8 +686,8 @@ pgstathashindex(PG_FUNCTION_ARGS)
/* Done accessing the index */
index_close(rel, AccessShareLock);
/* Count zero pages as free space. */
stats.free_space += stats.zero_pages * stats.space_per_page;
/* Count unused pages as free space. */
stats.free_space += stats.unused_pages * stats.space_per_page;
/*
* Total space available for tuples excludes the metapage and the bitmap
@ -710,7 +716,7 @@ pgstathashindex(PG_FUNCTION_ARGS)
values[1] = Int64GetDatum((int64) stats.bucket_pages);
values[2] = Int64GetDatum((int64) stats.overflow_pages);
values[3] = Int64GetDatum((int64) stats.bitmap_pages);
values[4] = Int64GetDatum((int64) stats.zero_pages);
values[4] = Int64GetDatum((int64) stats.unused_pages);
values[5] = Int64GetDatum(stats.live_items);
values[6] = Int64GetDatum(stats.dead_items);
values[7] = Float8GetDatum(free_percent);

View File

@ -125,7 +125,7 @@ CREATE OR REPLACE FUNCTION pgstathashindex(IN relname regclass,
OUT bucket_pages BIGINT,
OUT overflow_pages BIGINT,
OUT bitmap_pages BIGINT,
OUT zero_pages BIGINT,
OUT unused_pages BIGINT,
OUT live_items BIGINT,
OUT dead_items BIGINT,
OUT free_percent FLOAT8)

View File

@ -372,7 +372,7 @@ version | 2
bucket_pages | 33081
overflow_pages | 0
bitmap_pages | 1
zero_pages | 32455
unused_pages | 32455
live_items | 10204006
dead_items | 0
free_percent | 61.8005949100872
@ -418,9 +418,9 @@ free_percent | 61.8005949100872
</row>
<row>
<entry><structfield>zero_pages</structfield></entry>
<entry><structfield>unused_pages</structfield></entry>
<entry><type>bigint</type></entry>
<entry>Number of new or zero pages</entry>
<entry>Number of unused pages</entry>
</row>
<row>