Skip to content

Commit daf7527

Browse files
committed
New func _vc_scanoneind: scan one index relation to update statistic
in pg_class if no one page was reapped by vacuum.
1 parent f3c3458 commit daf7527

File tree

1 file changed

+63
-13
lines changed

1 file changed

+63
-13
lines changed

src/backend/commands/vacuum.c

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.17 1997/01/25 19:29:36 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.18 1997/01/29 02:59:03 vadim Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -68,6 +68,7 @@ static void _vc_rpfheap (VRelList curvrl, Relation onerel, VPageList Vvpl, VPage
6868
static void _vc_vacheap (VRelList curvrl, Relation onerel, VPageList vpl);
6969
static void _vc_vacpage (Page page, VPageDescr vpd, Relation archrel);
7070
static void _vc_vaconeind (VPageList vpl, Relation indrel, int nhtups);
71+
static void _vc_scanoneind (Relation indrel, int nhtups);
7172
static void _vc_updstats(Oid relid, int npages, int ntuples, bool hasindex);
7273
static void _vc_setpagelock(Relation rel, BlockNumber blkno);
7374
static VPageDescr _vc_tidreapped (ItemPointer itemptr, VPageList curvrl);
@@ -400,34 +401,38 @@ _vc_vacone (VRelList curvrl)
400401
Vvpl.vpl_npages = Fvpl.vpl_npages = 0;
401402
_vc_scanheap(curvrl, onerel, &Vvpl, &Fvpl);
402403

403-
/* Now open/count indices */
404+
/* Now open indices */
404405
Irel = (Relation *) NULL;
405-
if ( Vvpl.vpl_npages > 0 )
406-
/* Open all indices of this relation */
407-
_vc_getindices(curvrl->vrl_relid, &nindices, &Irel);
408-
else
409-
/* Count indices only */
410-
_vc_getindices(curvrl->vrl_relid, &nindices, NULL);
406+
_vc_getindices(curvrl->vrl_relid, &nindices, &Irel);
411407

412408
if ( nindices > 0 )
413409
curvrl->vrl_hasindex = true;
414410
else
415411
curvrl->vrl_hasindex = false;
416412

417-
/* Clean index' relation(s) */
413+
/* Clean/scan index relation(s) */
418414
if ( Irel != (Relation*) NULL )
419415
{
420-
for (i = 0; i < nindices; i++)
421-
_vc_vaconeind (&Vvpl, Irel[i], curvrl->vrl_ntups);
416+
if ( Vvpl.vpl_npages > 0 )
417+
{
418+
for (i = 0; i < nindices; i++)
419+
_vc_vaconeind (&Vvpl, Irel[i], curvrl->vrl_ntups);
420+
}
421+
else /* just scan indices to update statistic */
422+
{
423+
for (i = 0; i < nindices; i++)
424+
_vc_scanoneind (Irel[i], curvrl->vrl_ntups);
425+
}
422426
}
423427

424428
if ( Fvpl.vpl_npages > 0 ) /* Try to shrink heap */
425429
_vc_rpfheap (curvrl, onerel, &Vvpl, &Fvpl, nindices, Irel);
426-
else if ( Vvpl.vpl_npages > 0 ) /* Clean pages from Vvpl list */
430+
else
427431
{
428432
if ( Irel != (Relation*) NULL )
429433
_vc_clsindices (nindices, Irel);
430-
_vc_vacheap (curvrl, onerel, &Vvpl);
434+
if ( Vvpl.vpl_npages > 0 ) /* Clean pages from Vvpl list */
435+
_vc_vacheap (curvrl, onerel, &Vvpl);
431436
}
432437

433438
/* ok - free Vvpl list of reapped pages */
@@ -1266,6 +1271,51 @@ _vc_vacpage (Page page, VPageDescr vpd, Relation archrel)
12661271

12671272
} /* _vc_vacpage */
12681273

1274+
/*
1275+
* _vc_scanoneind() -- scan one index relation to update statistic.
1276+
*
1277+
*/
1278+
static void
1279+
_vc_scanoneind (Relation indrel, int nhtups)
1280+
{
1281+
RetrieveIndexResult res;
1282+
IndexScanDesc iscan;
1283+
int nitups;
1284+
int nipages;
1285+
struct rusage ru0, ru1;
1286+
1287+
getrusage(RUSAGE_SELF, &ru0);
1288+
1289+
/* walk through the entire index */
1290+
iscan = index_beginscan(indrel, false, 0, (ScanKey) NULL);
1291+
nitups = 0;
1292+
1293+
while ((res = index_getnext(iscan, ForwardScanDirection))
1294+
!= (RetrieveIndexResult) NULL)
1295+
{
1296+
nitups++;
1297+
pfree(res);
1298+
}
1299+
1300+
index_endscan(iscan);
1301+
1302+
/* now update statistics in pg_class */
1303+
nipages = RelationGetNumberOfBlocks(indrel);
1304+
_vc_updstats(indrel->rd_id, nipages, nitups, false);
1305+
1306+
getrusage(RUSAGE_SELF, &ru1);
1307+
1308+
elog (MESSLEV, "Ind %.*s: Pages %u; Tuples %u. Elapsed %u/%u sec.",
1309+
NAMEDATALEN, indrel->rd_rel->relname.data, nipages, nitups,
1310+
ru1.ru_stime.tv_sec - ru0.ru_stime.tv_sec,
1311+
ru1.ru_utime.tv_sec - ru0.ru_utime.tv_sec);
1312+
1313+
if ( nitups != nhtups )
1314+
elog (NOTICE, "NUMBER OF INDEX' TUPLES (%u) IS NOT THE SAME AS HEAP' (%u)",
1315+
nitups, nhtups);
1316+
1317+
} /* _vc_scanoneind */
1318+
12691319
/*
12701320
* _vc_vaconeind() -- vacuum one index relation.
12711321
*

0 commit comments

Comments
 (0)