1
1
/*
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 $
3
3
*
4
4
* Copyright (c) 2001,2002Tatsuo Ishii
5
5
*
@@ -59,35 +59,19 @@ typedef struct pgstattuple_type
59
59
uint64 free_space ;/* free/reusable space in bytes */
60
60
}pgstattuple_type ;
61
61
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
-
74
62
typedef void (* pgstat_page )(pgstattuple_type * ,Relation ,BlockNumber );
75
63
76
64
static Datum build_pgstattuple_type (pgstattuple_type * stat ,
77
65
FunctionCallInfo fcinfo );
78
66
static Datum pgstat_relation (Relation rel ,FunctionCallInfo fcinfo );
79
67
static Datum pgstat_heap (Relation rel ,FunctionCallInfo fcinfo );
80
- static Datum pgstat_btree (Relation rel ,FunctionCallInfo fcinfo );
81
68
static void pgstat_btree_page (pgstattuple_type * stat ,
82
69
Relation rel ,BlockNumber blkno );
83
- static Datum pgstat_hash (Relation rel ,FunctionCallInfo fcinfo );
84
70
static void pgstat_hash_page (pgstattuple_type * stat ,
85
71
Relation rel ,BlockNumber blkno );
86
- static Datum pgstat_gist (Relation rel ,FunctionCallInfo fcinfo );
87
72
static void pgstat_gist_page (pgstattuple_type * stat ,
88
73
Relation 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 ,
91
75
pgstat_page pagefn ,FunctionCallInfo fcinfo );
92
76
static void pgstat_index_page (pgstattuple_type * stat ,Page page ,
93
77
OffsetNumber minoff ,OffsetNumber maxoff );
@@ -217,11 +201,14 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
217
201
switch (rel -> rd_rel -> relam )
218
202
{
219
203
case BTREE_AM_OID :
220
- return pgstat_btree (rel ,fcinfo );
204
+ return pgstat_index (rel ,BTREE_METAPAGE + 1 ,
205
+ pgstat_btree_page ,fcinfo );
221
206
case HASH_AM_OID :
222
- return pgstat_hash (rel ,fcinfo );
207
+ return pgstat_index (rel ,HASH_METAPAGE + 1 ,
208
+ pgstat_hash_page ,fcinfo );
223
209
case GIST_AM_OID :
224
- return pgstat_gist (rel ,fcinfo );
210
+ return pgstat_index (rel ,GIST_ROOT_BLKNO + 1 ,
211
+ pgstat_gist_page ,fcinfo );
225
212
case GIN_AM_OID :
226
213
err = "gin index" ;
227
214
break ;
@@ -321,36 +308,13 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
321
308
}
322
309
323
310
/*
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
347
312
*/
348
313
static void
349
314
pgstat_btree_page (pgstattuple_type * stat ,Relation rel ,BlockNumber blkno )
350
315
{
351
316
Buffer buf ;
352
317
Page page ;
353
- pgstat_btree_type * btstat = (pgstat_btree_type * )stat ;
354
318
355
319
buf = ReadBuffer (rel ,blkno );
356
320
LockBuffer (buf ,BT_READ );
@@ -373,16 +337,6 @@ pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
373
337
}
374
338
else if (P_ISLEAF (opaque ))
375
339
{
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
-
386
340
pgstat_index_page (stat ,page ,P_FIRSTDATAKEY (opaque ),
387
341
PageGetMaxOffsetNumber (page ));
388
342
}
@@ -396,17 +350,7 @@ pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
396
350
}
397
351
398
352
/*
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
410
354
*/
411
355
static void
412
356
pgstat_hash_page (pgstattuple_type * stat ,Relation rel ,BlockNumber blkno )
@@ -448,17 +392,7 @@ pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
448
392
}
449
393
450
394
/*
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
462
396
*/
463
397
static void
464
398
pgstat_gist_page (pgstattuple_type * stat ,Relation rel ,BlockNumber blkno )
@@ -488,11 +422,12 @@ pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
488
422
* pgstat_index -- returns live/dead tuples info in a generic index
489
423
*/
490
424
static 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 )
493
427
{
494
428
BlockNumber nblocks ;
495
429
BlockNumber blkno ;
430
+ pgstattuple_type stat = {0 };
496
431
497
432
blkno = start ;
498
433
for (;;)
@@ -505,17 +440,17 @@ pgstat_index(pgstattuple_type *stat, Relation rel, BlockNumber start,
505
440
/* Quit if we've scanned the whole relation */
506
441
if (blkno >=nblocks )
507
442
{
508
- stat -> table_len = (uint64 )nblocks * BLCKSZ ;
443
+ stat . table_len = (uint64 )nblocks * BLCKSZ ;
509
444
break ;
510
445
}
511
446
512
447
for (;blkno < nblocks ;blkno ++ )
513
- pagefn (stat ,rel ,blkno );
448
+ pagefn (& stat ,rel ,blkno );
514
449
}
515
450
516
451
relation_close (rel ,AccessShareLock );
517
452
518
- return build_pgstattuple_type (stat ,fcinfo );
453
+ return build_pgstattuple_type (& stat ,fcinfo );
519
454
}
520
455
521
456
/*