Skip to content

Commit 12992ab

Browse files
committed
Separate out the VacRUsage stuff as an independent module, in preparation
for using it for other things besides VACUUM.
1 parent 9c87382 commit 12992ab

File tree

6 files changed

+155
-112
lines changed

6 files changed

+155
-112
lines changed

src/backend/commands/vacuum.c

Lines changed: 18 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*
1515
* IDENTIFICATION
16-
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.315 2005/09/22 17:32:58 tgl Exp $
16+
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.316 2005/10/03 22:52:21 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -46,6 +46,7 @@
4646
#include "utils/inval.h"
4747
#include "utils/lsyscache.h"
4848
#include "utils/memutils.h"
49+
#include "utils/pg_rusage.h"
4950
#include "utils/relcache.h"
5051
#include "utils/syscache.h"
5152
#include "pgstat.h"
@@ -1233,9 +1234,9 @@ scan_heap(VRelStats *vacrelstats, Relation onerel,
12331234
VTupleLink vtlinks = (VTupleLink) palloc(100 * sizeof(VTupleLinkData));
12341235
int num_vtlinks = 0;
12351236
int free_vtlinks = 100;
1236-
VacRUsage ru0;
1237+
PGRUsage ru0;
12371238

1238-
vac_init_rusage(&ru0);
1239+
pg_rusage_init(&ru0);
12391240

12401241
relname = RelationGetRelationName(onerel);
12411242
ereport(elevel,
@@ -1592,14 +1593,14 @@ scan_heap(VRelStats *vacrelstats, Relation onerel,
15921593
"Total free space (including removable row versions) is %.0f bytes.\n"
15931594
"%u pages are or will become empty, including %u at the end of the table.\n"
15941595
"%u pages containing %.0f free bytes are potential move destinations.\n"
1595-
"%s",
1596+
"%s.",
15961597
nkeep,
15971598
(unsigned long) min_tlen, (unsigned long) max_tlen,
15981599
nunused,
15991600
free_space,
16001601
empty_pages, empty_end_pages,
16011602
fraged_pages->num_pages, usable_free_space,
1602-
vac_show_rusage(&ru0))));
1603+
pg_rusage_show(&ru0))));
16031604
}
16041605

16051606

@@ -1636,9 +1637,9 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
16361637
num_fraged_pages,
16371638
vacuumed_pages;
16381639
int keep_tuples = 0;
1639-
VacRUsage ru0;
1640+
PGRUsage ru0;
16401641

1641-
vac_init_rusage(&ru0);
1642+
pg_rusage_init(&ru0);
16421643

16431644
ExecContext_Init(&ec, onerel);
16441645

@@ -2362,8 +2363,8 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
23622363
(errmsg("\"%s\": moved %u row versions, truncated %u to %u pages",
23632364
RelationGetRelationName(onerel),
23642365
num_moved, nblocks, blkno),
2365-
errdetail("%s",
2366-
vac_show_rusage(&ru0))));
2366+
errdetail("%s.",
2367+
pg_rusage_show(&ru0))));
23672368

23682369
/*
23692370
* Reflect the motion of system tuples to catalog cache here.
@@ -2950,9 +2951,9 @@ scan_index(Relation indrel, double num_tuples)
29502951
{
29512952
IndexBulkDeleteResult *stats;
29522953
IndexVacuumCleanupInfo vcinfo;
2953-
VacRUsage ru0;
2954+
PGRUsage ru0;
29542955

2955-
vac_init_rusage(&ru0);
2956+
pg_rusage_init(&ru0);
29562957

29572958
/*
29582959
* Even though we're not planning to delete anything, we use the
@@ -2982,9 +2983,9 @@ scan_index(Relation indrel, double num_tuples)
29822983
stats->num_index_tuples,
29832984
stats->num_pages),
29842985
errdetail("%u index pages have been deleted, %u are currently reusable.\n"
2985-
"%s",
2986+
"%s.",
29862987
stats->pages_deleted, stats->pages_free,
2987-
vac_show_rusage(&ru0))));
2988+
pg_rusage_show(&ru0))));
29882989

29892990
/*
29902991
* Check for tuple count mismatch. If the index is partial, then it's
@@ -3022,9 +3023,9 @@ vacuum_index(VacPageList vacpagelist, Relation indrel,
30223023
{
30233024
IndexBulkDeleteResult *stats;
30243025
IndexVacuumCleanupInfo vcinfo;
3025-
VacRUsage ru0;
3026+
PGRUsage ru0;
30263027

3027-
vac_init_rusage(&ru0);
3028+
pg_rusage_init(&ru0);
30283029

30293030
/* Do bulk deletion */
30303031
stats = index_bulk_delete(indrel, tid_reaped, (void *) vacpagelist);
@@ -3050,10 +3051,10 @@ vacuum_index(VacPageList vacpagelist, Relation indrel,
30503051
stats->num_pages),
30513052
errdetail("%.0f index row versions were removed.\n"
30523053
"%u index pages have been deleted, %u are currently reusable.\n"
3053-
"%s",
3054+
"%s.",
30543055
stats->tuples_removed,
30553056
stats->pages_deleted, stats->pages_free,
3056-
vac_show_rusage(&ru0))));
3057+
pg_rusage_show(&ru0))));
30573058

30583059
/*
30593060
* Check for tuple count mismatch. If the index is partial, then it's
@@ -3429,60 +3430,6 @@ enough_space(VacPage vacpage, Size len)
34293430
}
34303431

34313432

3432-
/*
3433-
* Initialize usage snapshot.
3434-
*/
3435-
void
3436-
vac_init_rusage(VacRUsage *ru0)
3437-
{
3438-
struct timezone tz;
3439-
3440-
getrusage(RUSAGE_SELF, &ru0->ru);
3441-
gettimeofday(&ru0->tv, &tz);
3442-
}
3443-
3444-
/*
3445-
* Compute elapsed time since ru0 usage snapshot, and format into
3446-
* a displayable string. Result is in a static string, which is
3447-
* tacky, but no one ever claimed that the Postgres backend is
3448-
* threadable...
3449-
*/
3450-
const char *
3451-
vac_show_rusage(VacRUsage *ru0)
3452-
{
3453-
static char result[100];
3454-
VacRUsage ru1;
3455-
3456-
vac_init_rusage(&ru1);
3457-
3458-
if (ru1.tv.tv_usec < ru0->tv.tv_usec)
3459-
{
3460-
ru1.tv.tv_sec--;
3461-
ru1.tv.tv_usec += 1000000;
3462-
}
3463-
if (ru1.ru.ru_stime.tv_usec < ru0->ru.ru_stime.tv_usec)
3464-
{
3465-
ru1.ru.ru_stime.tv_sec--;
3466-
ru1.ru.ru_stime.tv_usec += 1000000;
3467-
}
3468-
if (ru1.ru.ru_utime.tv_usec < ru0->ru.ru_utime.tv_usec)
3469-
{
3470-
ru1.ru.ru_utime.tv_sec--;
3471-
ru1.ru.ru_utime.tv_usec += 1000000;
3472-
}
3473-
3474-
snprintf(result, sizeof(result),
3475-
"CPU %d.%02ds/%d.%02du sec elapsed %d.%02d sec.",
3476-
(int) (ru1.ru.ru_stime.tv_sec - ru0->ru.ru_stime.tv_sec),
3477-
(int) (ru1.ru.ru_stime.tv_usec - ru0->ru.ru_stime.tv_usec) / 10000,
3478-
(int) (ru1.ru.ru_utime.tv_sec - ru0->ru.ru_utime.tv_sec),
3479-
(int) (ru1.ru.ru_utime.tv_usec - ru0->ru.ru_utime.tv_usec) / 10000,
3480-
(int) (ru1.tv.tv_sec - ru0->tv.tv_sec),
3481-
(int) (ru1.tv.tv_usec - ru0->tv.tv_usec) / 10000);
3482-
3483-
return result;
3484-
}
3485-
34863433
/*
34873434
* vacuum_delay_point --- check for interrupts and cost-based delay.
34883435
*

src/backend/commands/vacuumlazy.c

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
*
3333
* IDENTIFICATION
34-
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.59 2005/09/22 17:32:58 tgl Exp $
34+
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.60 2005/10/03 22:52:22 tgl Exp $
3535
*
3636
*-------------------------------------------------------------------------
3737
*/
@@ -48,6 +48,7 @@
4848
#include "storage/freespace.h"
4949
#include "storage/smgr.h"
5050
#include "utils/lsyscache.h"
51+
#include "utils/pg_rusage.h"
5152

5253

5354
/*
@@ -209,9 +210,9 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
209210
BlockNumber *index_pages_removed;
210211
bool did_vacuum_index = false;
211212
int i;
212-
VacRUsage ru0;
213+
PGRUsage ru0;
213214

214-
vac_init_rusage(&ru0);
215+
pg_rusage_init(&ru0);
215216

216217
relname = RelationGetRelationName(onerel);
217218
ereport(elevel,
@@ -478,11 +479,11 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
478479
errdetail("%.0f dead row versions cannot be removed yet.\n"
479480
"There were %.0f unused item pointers.\n"
480481
"%u pages are entirely empty.\n"
481-
"%s",
482+
"%s.",
482483
nkeep,
483484
nunused,
484485
empty_pages,
485-
vac_show_rusage(&ru0))));
486+
pg_rusage_show(&ru0))));
486487
}
487488

488489

@@ -502,9 +503,9 @@ lazy_vacuum_heap(Relation onerel, LVRelStats *vacrelstats)
502503
{
503504
int tupindex;
504505
int npages;
505-
VacRUsage ru0;
506+
PGRUsage ru0;
506507

507-
vac_init_rusage(&ru0);
508+
pg_rusage_init(&ru0);
508509
npages = 0;
509510

510511
tupindex = 0;
@@ -533,8 +534,8 @@ lazy_vacuum_heap(Relation onerel, LVRelStats *vacrelstats)
533534
(errmsg("\"%s\": removed %d row versions in %d pages",
534535
RelationGetRelationName(onerel),
535536
tupindex, npages),
536-
errdetail("%s",
537-
vac_show_rusage(&ru0))));
537+
errdetail("%s.",
538+
pg_rusage_show(&ru0))));
538539
}
539540

540541
/*
@@ -602,9 +603,9 @@ lazy_scan_index(Relation indrel, LVRelStats *vacrelstats)
602603
{
603604
IndexBulkDeleteResult *stats;
604605
IndexVacuumCleanupInfo vcinfo;
605-
VacRUsage ru0;
606+
PGRUsage ru0;
606607

607-
vac_init_rusage(&ru0);
608+
pg_rusage_init(&ru0);
608609

609610
/*
610611
* Acquire appropriate type of lock on index: must be exclusive if
@@ -652,9 +653,9 @@ lazy_scan_index(Relation indrel, LVRelStats *vacrelstats)
652653
stats->num_index_tuples,
653654
stats->num_pages),
654655
errdetail("%u index pages have been deleted, %u are currently reusable.\n"
655-
"%s",
656+
"%s.",
656657
stats->pages_deleted, stats->pages_free,
657-
vac_show_rusage(&ru0))));
658+
pg_rusage_show(&ru0))));
658659

659660
pfree(stats);
660661
}
@@ -679,9 +680,9 @@ lazy_vacuum_index(Relation indrel,
679680
{
680681
IndexBulkDeleteResult *stats;
681682
IndexVacuumCleanupInfo vcinfo;
682-
VacRUsage ru0;
683+
PGRUsage ru0;
683684

684-
vac_init_rusage(&ru0);
685+
pg_rusage_init(&ru0);
685686

686687
/*
687688
* Acquire appropriate type of lock on index: must be exclusive if
@@ -729,10 +730,10 @@ lazy_vacuum_index(Relation indrel,
729730
stats->num_pages),
730731
errdetail("%.0f index row versions were removed.\n"
731732
"%u index pages have been deleted, %u are currently reusable.\n"
732-
"%s",
733+
"%s.",
733734
stats->tuples_removed,
734735
stats->pages_deleted, stats->pages_free,
735-
vac_show_rusage(&ru0))));
736+
pg_rusage_show(&ru0))));
736737

737738
pfree(stats);
738739
}
@@ -749,9 +750,9 @@ lazy_truncate_heap(Relation onerel, LVRelStats *vacrelstats)
749750
int n;
750751
int i,
751752
j;
752-
VacRUsage ru0;
753+
PGRUsage ru0;
753754

754-
vac_init_rusage(&ru0);
755+
pg_rusage_init(&ru0);
755756

756757
/*
757758
* We need full exclusive lock on the relation in order to do
@@ -828,8 +829,8 @@ lazy_truncate_heap(Relation onerel, LVRelStats *vacrelstats)
828829
(errmsg("\"%s\": truncated %u to %u pages",
829830
RelationGetRelationName(onerel),
830831
old_rel_pages, new_rel_pages),
831-
errdetail("%s",
832-
vac_show_rusage(&ru0))));
832+
errdetail("%s.",
833+
pg_rusage_show(&ru0))));
833834
}
834835

835836
/*

src/backend/utils/misc/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Makefile for utils/misc
55
#
66
# IDENTIFICATION
7-
# $PostgreSQL: pgsql/src/backend/utils/misc/Makefile,v 1.23 2005/02/26 18:43:33 tgl Exp $
7+
# $PostgreSQL: pgsql/src/backend/utils/misc/Makefile,v 1.24 2005/10/03 22:52:23 tgl Exp $
88
#
99
#-------------------------------------------------------------------------
1010

@@ -14,7 +14,7 @@ include $(top_builddir)/src/Makefile.global
1414

1515
override CPPFLAGS := -I$(srcdir) $(CPPFLAGS)
1616

17-
OBJS = guc.o help_config.o ps_status.o superuser.o
17+
OBJS = guc.o help_config.o pg_rusage.o ps_status.o superuser.o
1818

1919
# This location might depend on the installation directories. Therefore
2020
# we can't subsitute it into pg_config.h.

0 commit comments

Comments
 (0)