Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9dfb5b9

Browse files
committed
Fix multiple bugs in contrib/pgstattuple's pgstatindex() function.
Dead or half-dead index leaf pages were incorrectly reported as live, as aconsequence of a code rearrangement I made (during a moment of severe brainfade, evidently) in commitd287818.The index metapage was not counted in index_size, causing that result tonot agree with the actual index size on-disk.Index root pages were not counted in internal_pages, which is inconsistentcompared to the case of a root that's also a leaf (one-page index), wherethe root would be counted in leaf_pages. Aside from that inconsistency,this could lead to additional transient discrepancies between the reportedpage counts and index_size, since it's possible for pgstatindex's scan tosee zero or multiple pages marked as BTP_ROOT, if the root moves due toa split during the scan. With these fixes, index_size will always beexactly one page more than the sum of the displayed page counts.Also, the index_size result was incorrectly documented as being measured inpages; it's always been measured in bytes. (While fixing that, I couldn'tresist doing some small additional wordsmithing on the pgstattuple docs.)Including the metapage causes the reported index_size to not be zero foran empty index. To preserve the desired property that the pgstattupleregression test results are platform-independent (ie, BLCKSZ configurationindependent), scale the index_size result in the regression tests.The documentation issue was reported by Otsuka Kenji, and the inconsistentroot page counting by Peter Geoghegan; the other problems noted by me.Back-patch to all supported branches, because this has been broken fora long time.
1 parentb3ec98c commit9dfb5b9

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

‎contrib/pgstattuple/expected/pgstattuple.out

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ select * from pgstattuple('test'::regclass);
1717
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
1818
(1 row)
1919

20-
select * from pgstatindex('test_pkey');
20+
select version, tree_level,
21+
index_size / current_setting('block_size')::int as index_size,
22+
root_block_no, internal_pages, leaf_pages, empty_pages, deleted_pages,
23+
avg_leaf_density, leaf_fragmentation
24+
from pgstatindex('test_pkey');
2125
version | tree_level | index_size | root_block_no | internal_pages | leaf_pages | empty_pages | deleted_pages | avg_leaf_density | leaf_fragmentation
2226
---------+------------+------------+---------------+----------------+------------+-------------+---------------+------------------+--------------------
23-
2 | 0 |0 | 0 | 0 | 0 | 0 | 0 | NaN | NaN
27+
2 | 0 |1 | 0 | 0 | 0 | 0 | 0 | NaN | NaN
2428
(1 row)
2529

2630
select pg_relpages('test');

‎contrib/pgstattuple/pgstatindex.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ typedef struct BTIndexStat
7272
uint32level;
7373
BlockNumberroot_blkno;
7474

75-
uint64root_pages;
7675
uint64internal_pages;
7776
uint64leaf_pages;
7877
uint64empty_pages;
@@ -153,7 +152,6 @@ pgstatindex(PG_FUNCTION_ARGS)
153152
}
154153

155154
/* -- init counters -- */
156-
indexStat.root_pages=0;
157155
indexStat.internal_pages=0;
158156
indexStat.leaf_pages=0;
159157
indexStat.empty_pages=0;
@@ -186,7 +184,11 @@ pgstatindex(PG_FUNCTION_ARGS)
186184

187185
/* Determine page type, and update totals */
188186

189-
if (P_ISLEAF(opaque))
187+
if (P_ISDELETED(opaque))
188+
indexStat.deleted_pages++;
189+
elseif (P_IGNORE(opaque))
190+
indexStat.empty_pages++;/* this is the "half dead" state */
191+
elseif (P_ISLEAF(opaque))
190192
{
191193
intmax_avail;
192194

@@ -203,12 +205,6 @@ pgstatindex(PG_FUNCTION_ARGS)
203205
if (opaque->btpo_next!=P_NONE&&opaque->btpo_next<blkno)
204206
indexStat.fragments++;
205207
}
206-
elseif (P_ISDELETED(opaque))
207-
indexStat.deleted_pages++;
208-
elseif (P_IGNORE(opaque))
209-
indexStat.empty_pages++;
210-
elseif (P_ISROOT(opaque))
211-
indexStat.root_pages++;
212208
else
213209
indexStat.internal_pages++;
214210

@@ -240,7 +236,7 @@ pgstatindex(PG_FUNCTION_ARGS)
240236
snprintf(values[j++],32,"%d",indexStat.level);
241237
values[j]=palloc(32);
242238
snprintf(values[j++],32,INT64_FORMAT,
243-
(indexStat.root_pages+
239+
(1+/* include the metapage in index_size */
244240
indexStat.leaf_pages+
245241
indexStat.internal_pages+
246242
indexStat.deleted_pages+

‎contrib/pgstattuple/sql/pgstattuple.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ create table test (a int primary key, b int[]);
1111
select*from pgstattuple('test'::text);
1212
select*from pgstattuple('test'::regclass);
1313

14-
select*from pgstatindex('test_pkey');
14+
select version, tree_level,
15+
index_size/ current_setting('block_size')::intas index_size,
16+
root_block_no, internal_pages, leaf_pages, empty_pages, deleted_pages,
17+
avg_leaf_density, leaf_fragmentation
18+
from pgstatindex('test_pkey');
1519

1620
select pg_relpages('test');
1721
select pg_relpages('test_pkey');

‎doc/src/sgml/pgstattuple.sgml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ test=> SELECT * FROM pgstatindex('pg_cast_oid_index');
153153
-[ RECORD 1 ]------+------
154154
version | 2
155155
tree_level | 0
156-
index_size |8192
156+
index_size |16384
157157
root_block_no | 1
158158
internal_pages | 0
159159
leaf_pages | 1
160160
empty_pages | 0
161161
deleted_pages | 0
162-
avg_leaf_density |50.27
162+
avg_leaf_density |54.27
163163
leaf_fragmentation | 0
164164
</programlisting>
165165
</para>
@@ -193,13 +193,13 @@ leaf_fragmentation | 0
193193
<row>
194194
<entry><structfield>index_size</structfield></entry>
195195
<entry><type>bigint</type></entry>
196-
<entry>Totalnumber of pagesinindex</entry>
196+
<entry>Totalindex sizeinbytes</entry>
197197
</row>
198198

199199
<row>
200200
<entry><structfield>root_block_no</structfield></entry>
201201
<entry><type>bigint</type></entry>
202-
<entry>Location of rootblock</entry>
202+
<entry>Location of rootpage (zero if none)</entry>
203203
</row>
204204

205205
<row>
@@ -243,6 +243,13 @@ leaf_fragmentation | 0
243243
</informaltable>
244244
</para>
245245

246+
<para>
247+
The reported <literal>index_size</> will normally correspond to one more
248+
page than is accounted for by <literal>internal_pages + leaf_pages +
249+
empty_pages + deleted_pages</literal>, because it also includes the
250+
index's metapage.
251+
</para>
252+
246253
<para>
247254
As with <function>pgstattuple</>, the results are accumulated
248255
page-by-page, and should not be expected to represent an

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp