11/*
2- * $PostgreSQL: pgsql/contrib/pgstattuple/pgstattuple.c,v 1.23 2006/07/11 17:26:58 momjian Exp $
2+ * $PostgreSQL: pgsql/contrib/pgstattuple/pgstattuple.c,v 1.24 2006/09/04 02:03:04 tgl Exp $
33 *
44 * Copyright (c) 2001,2002Tatsuo Ishii
55 *
@@ -59,35 +59,19 @@ typedef struct pgstattuple_type
5959uint64 free_space ;/* free/reusable space in bytes */
6060}pgstattuple_type ;
6161
62- /*
63- * struct pgstat_btree_type
64- */
65- typedef struct pgstat_btree_type
66- {
67- pgstattuple_type base ;/* inherits pgstattuple_type */
68-
69- uint64 continuous ;
70- uint64 forward ;
71- uint64 backward ;
72- }pgstat_btree_type ;
73-
7462typedef void (* pgstat_page )(pgstattuple_type * ,Relation ,BlockNumber );
7563
7664static Datum build_pgstattuple_type (pgstattuple_type * stat ,
7765FunctionCallInfo fcinfo );
7866static Datum pgstat_relation (Relation rel ,FunctionCallInfo fcinfo );
7967static Datum pgstat_heap (Relation rel ,FunctionCallInfo fcinfo );
80- static Datum pgstat_btree (Relation rel ,FunctionCallInfo fcinfo );
8168static void pgstat_btree_page (pgstattuple_type * stat ,
8269Relation rel ,BlockNumber blkno );
83- static Datum pgstat_hash (Relation rel ,FunctionCallInfo fcinfo );
8470static void pgstat_hash_page (pgstattuple_type * stat ,
8571Relation rel ,BlockNumber blkno );
86- static Datum pgstat_gist (Relation rel ,FunctionCallInfo fcinfo );
8772static void pgstat_gist_page (pgstattuple_type * stat ,
8873Relation rel ,BlockNumber blkno );
89- static Datum pgstat_index (pgstattuple_type * stat ,
90- Relation rel ,BlockNumber start ,
74+ static Datum pgstat_index (Relation rel ,BlockNumber start ,
9175pgstat_page pagefn ,FunctionCallInfo fcinfo );
9276static void pgstat_index_page (pgstattuple_type * stat ,Page page ,
9377OffsetNumber minoff ,OffsetNumber maxoff );
@@ -217,11 +201,14 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
217201switch (rel -> rd_rel -> relam )
218202{
219203case BTREE_AM_OID :
220- return pgstat_btree (rel ,fcinfo );
204+ return pgstat_index (rel ,BTREE_METAPAGE + 1 ,
205+ pgstat_btree_page ,fcinfo );
221206case HASH_AM_OID :
222- return pgstat_hash (rel ,fcinfo );
207+ return pgstat_index (rel ,HASH_METAPAGE + 1 ,
208+ pgstat_hash_page ,fcinfo );
223209case GIST_AM_OID :
224- return pgstat_gist (rel ,fcinfo );
210+ return pgstat_index (rel ,GIST_ROOT_BLKNO + 1 ,
211+ pgstat_gist_page ,fcinfo );
225212case GIN_AM_OID :
226213err = "gin index" ;
227214break ;
@@ -321,36 +308,13 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
321308}
322309
323310/*
324- * pgstat_btree -- returns live/dead tuples info in a btree index
325- */
326- static Datum
327- pgstat_btree (Relation rel ,FunctionCallInfo fcinfo )
328- {
329- pgstat_btree_type stat = { {0 } };
330- Datum datum ;
331-
332- datum = pgstat_index ((pgstattuple_type * )& stat ,rel ,
333- BTREE_METAPAGE + 1 ,pgstat_btree_page ,fcinfo );
334-
335- ereport (NOTICE ,
336- (errmsg ("%.2f%% fragmented" ,
337- 100.0 * (stat .forward + stat .backward ) /
338- (stat .continuous + stat .forward + stat .backward )),
339- errhint ("continuous=%llu, forward=%llu, backward=%llu" ,
340- stat .continuous ,stat .forward ,stat .backward )));
341-
342- return datum ;
343- }
344-
345- /*
346- * pgstat_btree_page
311+ * pgstat_btree_page -- check tuples in a btree page
347312 */
348313static void
349314pgstat_btree_page (pgstattuple_type * stat ,Relation rel ,BlockNumber blkno )
350315{
351316Buffer buf ;
352317Page page ;
353- pgstat_btree_type * btstat = (pgstat_btree_type * )stat ;
354318
355319buf = ReadBuffer (rel ,blkno );
356320LockBuffer (buf ,BT_READ );
@@ -373,16 +337,6 @@ pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
373337}
374338else if (P_ISLEAF (opaque ))
375339{
376- /* check fragmentation */
377- if (P_RIGHTMOST (opaque ))
378- btstat -> continuous ++ ;
379- else if (opaque -> btpo_next < blkno )
380- btstat -> backward ++ ;
381- else if (opaque -> btpo_next > blkno + 1 )
382- btstat -> forward ++ ;
383- else
384- btstat -> continuous ++ ;
385-
386340pgstat_index_page (stat ,page ,P_FIRSTDATAKEY (opaque ),
387341PageGetMaxOffsetNumber (page ));
388342}
@@ -396,17 +350,7 @@ pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
396350}
397351
398352/*
399- * pgstat_hash -- returns live/dead tuples info in a hash index
400- */
401- static Datum
402- pgstat_hash (Relation rel ,FunctionCallInfo fcinfo )
403- {
404- pgstattuple_type stat = {0 };
405- return pgstat_index (& stat ,rel ,HASH_METAPAGE + 1 ,pgstat_hash_page ,fcinfo );
406- }
407-
408- /*
409- * pgstat_hash_page
353+ * pgstat_hash_page -- check tuples in a hash page
410354 */
411355static void
412356pgstat_hash_page (pgstattuple_type * stat ,Relation rel ,BlockNumber blkno )
@@ -448,17 +392,7 @@ pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
448392}
449393
450394/*
451- * pgstat_gist -- returns live/dead tuples info in a gist index
452- */
453- static Datum
454- pgstat_gist (Relation rel ,FunctionCallInfo fcinfo )
455- {
456- pgstattuple_type stat = {0 };
457- return pgstat_index (& stat ,rel ,GIST_ROOT_BLKNO + 1 ,pgstat_gist_page ,fcinfo );
458- }
459-
460- /*
461- * pgstat_gist_page
395+ * pgstat_gist_page -- check tuples in a gist page
462396 */
463397static void
464398pgstat_gist_page (pgstattuple_type * stat ,Relation rel ,BlockNumber blkno )
@@ -488,11 +422,12 @@ pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
488422 * pgstat_index -- returns live/dead tuples info in a generic index
489423 */
490424static Datum
491- pgstat_index (pgstattuple_type * stat , Relation rel ,BlockNumber start ,
492- pgstat_page pagefn , FunctionCallInfo fcinfo )
425+ pgstat_index (Relation rel ,BlockNumber start , pgstat_page pagefn ,
426+ FunctionCallInfo fcinfo )
493427{
494428BlockNumber nblocks ;
495429BlockNumber blkno ;
430+ pgstattuple_type stat = {0 };
496431
497432blkno = start ;
498433for (;;)
@@ -505,17 +440,17 @@ pgstat_index(pgstattuple_type *stat, Relation rel, BlockNumber start,
505440/* Quit if we've scanned the whole relation */
506441if (blkno >=nblocks )
507442{
508- stat -> table_len = (uint64 )nblocks * BLCKSZ ;
443+ stat . table_len = (uint64 )nblocks * BLCKSZ ;
509444break ;
510445}
511446
512447for (;blkno < nblocks ;blkno ++ )
513- pagefn (stat ,rel ,blkno );
448+ pagefn (& stat ,rel ,blkno );
514449}
515450
516451relation_close (rel ,AccessShareLock );
517452
518- return build_pgstattuple_type (stat ,fcinfo );
453+ return build_pgstattuple_type (& stat ,fcinfo );
519454}
520455
521456/*