Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef23a77

Browse files
committed
Enable configurable log of autovacuum actions. Initial patch from Simon
Riggs, additional code and docs by me. Per discussion.
1 parent 432ea3c commit ef23a77

File tree

7 files changed

+134
-15
lines changed

7 files changed

+134
-15
lines changed

doc/src/sgml/config.sgml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.120 2007/04/16 18:29:50 alvherre Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.121 2007/04/18 16:44:17 alvherre Exp $ -->
22

33
<chapter Id="runtime-config">
44
<title>Server Configuration</title>
@@ -3190,6 +3190,25 @@ SELECT * FROM parent WHERE key = 2400;
31903190
</listitem>
31913191
</varlistentry>
31923192

3193+
<varlistentry id="guc-log-autovacuum" xreflabel="log_autovacuum">
3194+
<term><varname>log_autovacuum</varname> (<type>integer</type>)</term>
3195+
<indexterm>
3196+
<primary><varname>log_autovacuum</> configuration parameter</primary>
3197+
</indexterm>
3198+
<listitem>
3199+
<para>
3200+
Causes actions executed by autovacuum to be logged if it ran for at
3201+
least the specified number of milliseconds. Setting this to zero prints
3202+
all action durations. Minus-one (the default) disables logging
3203+
autovacuum action durations. For example, if you set it to
3204+
<literal>250ms</literal> then all vacuums and analyzes that run
3205+
250ms or longer will be logged. Enabling this parameter can be helpful
3206+
in tracking autovacuum activity. This setting can only be set in
3207+
the <filename>postgresql.conf</> file or on the server command line.
3208+
</para>
3209+
</listitem>
3210+
</varlistentry>
3211+
31933212
<varlistentry id="guc-autovacuum-naptime" xreflabel="autovacuum_naptime">
31943213
<term><varname>autovacuum_naptime</varname> (<type>integer</type>)</term>
31953214
<indexterm>

src/backend/commands/analyze.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.104 2007/04/06 04:21:42 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.105 2007/04/18 16:44:17 alvherre Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -22,17 +22,20 @@
2222
#include "catalog/index.h"
2323
#include "catalog/indexing.h"
2424
#include "catalog/namespace.h"
25+
#include "commands/dbcommands.h"
2526
#include "commands/vacuum.h"
2627
#include "executor/executor.h"
2728
#include "miscadmin.h"
2829
#include "parser/parse_expr.h"
2930
#include "parser/parse_oper.h"
3031
#include "parser/parse_relation.h"
3132
#include "pgstat.h"
33+
#include "postmaster/autovacuum.h"
3234
#include "utils/acl.h"
3335
#include "utils/datum.h"
3436
#include "utils/lsyscache.h"
3537
#include "utils/memutils.h"
38+
#include "utils/pg_rusage.h"
3639
#include "utils/syscache.h"
3740
#include "utils/tuplesort.h"
3841

@@ -109,6 +112,8 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt)
109112
double totalrows,
110113
totaldeadrows;
111114
HeapTuple *rows;
115+
PGRUsage ru0;
116+
TimestampTz starttime = 0;
112117

113118
if (vacstmt->verbose)
114119
elevel = INFO;
@@ -190,6 +195,14 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt)
190195
return;
191196
}
192197

198+
/* measure elapsed time iff autovacuum logging requires it */
199+
if (IsAutoVacuumWorkerProcess() && Log_autovacuum >= 0)
200+
{
201+
pg_rusage_init(&ru0);
202+
if (Log_autovacuum > 0)
203+
starttime = GetCurrentTimestamp();
204+
}
205+
193206
ereport(elevel,
194207
(errmsg("analyzing \"%s.%s\"",
195208
get_namespace_name(RelationGetNamespace(onerel)),
@@ -451,6 +464,34 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt)
451464
* expose us to concurrent-update failures in update_attstats.)
452465
*/
453466
relation_close(onerel, NoLock);
467+
468+
/* Log the action if appropriate */
469+
if (IsAutoVacuumWorkerProcess() && Log_autovacuum >= 0)
470+
{
471+
long diff;
472+
473+
if (Log_autovacuum > 0)
474+
{
475+
TimestampTz endtime;
476+
int usecs;
477+
long secs;
478+
479+
endtime = GetCurrentTimestamp();
480+
TimestampDifference(starttime, endtime, &secs, &usecs);
481+
482+
diff = secs * 1000 + usecs / 1000;
483+
}
484+
485+
if (Log_autovacuum == 0 || diff >= Log_autovacuum)
486+
{
487+
ereport(LOG,
488+
(errmsg("automatic analyze of table \"%s.%s.%s\" system usage: %s",
489+
get_database_name(MyDatabaseId),
490+
get_namespace_name(RelationGetNamespace(onerel)),
491+
RelationGetRelationName(onerel),
492+
pg_rusage_show(&ru0))));
493+
}
494+
}
454495
}
455496

456497
/*

src/backend/commands/vacuumlazy.c

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*
3737
*
3838
* IDENTIFICATION
39-
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.85 2007/02/21 22:47:45 momjian Exp $
39+
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.86 2007/04/18 16:44:18 alvherre Exp $
4040
*
4141
*-------------------------------------------------------------------------
4242
*/
@@ -47,9 +47,11 @@
4747
#include "access/genam.h"
4848
#include "access/heapam.h"
4949
#include "access/transam.h"
50+
#include "commands/dbcommands.h"
5051
#include "commands/vacuum.h"
5152
#include "miscadmin.h"
5253
#include "pgstat.h"
54+
#include "postmaster/autovacuum.h"
5355
#include "storage/freespace.h"
5456
#include "utils/lsyscache.h"
5557
#include "utils/memutils.h"
@@ -90,6 +92,7 @@ typedef struct LVRelStats
9092
int max_free_pages; /* # slots allocated in array */
9193
PageFreeSpaceInfo *free_pages; /* array or heap of blkno/avail */
9294
BlockNumber tot_free_pages; /* total pages with >= threshold space */
95+
int num_index_scans;
9396
} LVRelStats;
9497

9598

@@ -141,6 +144,14 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
141144
Relation *Irel;
142145
int nindexes;
143146
BlockNumber possibly_freeable;
147+
PGRUsage ru0;
148+
TimestampTz starttime = 0;
149+
150+
pg_rusage_init(&ru0);
151+
152+
/* measure elapsed time iff autovacuum logging requires it */
153+
if (IsAutoVacuumWorkerProcess() && Log_autovacuum > 0)
154+
starttime = GetCurrentTimestamp();
144155

145156
if (vacstmt->verbose)
146157
elevel = INFO;
@@ -156,6 +167,8 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
156167
/* XXX should we scale it up or down? Adjust vacuum.c too, if so */
157168
vacrelstats->threshold = GetAvgFSMRequestSize(&onerel->rd_node);
158169

170+
vacrelstats->num_index_scans = 0;
171+
159172
/* Open all indexes of the relation */
160173
vac_open_indexes(onerel, RowExclusiveLock, &nindexes, &Irel);
161174
vacrelstats->hasindex = (nindexes > 0);
@@ -200,6 +213,40 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
200213
/* report results to the stats collector, too */
201214
pgstat_report_vacuum(RelationGetRelid(onerel), onerel->rd_rel->relisshared,
202215
vacstmt->analyze, vacrelstats->rel_tuples);
216+
217+
/* and log the action if appropriate */
218+
if (IsAutoVacuumWorkerProcess() && Log_autovacuum >= 0)
219+
{
220+
long diff;
221+
222+
if (Log_autovacuum > 0)
223+
{
224+
TimestampTz endtime;
225+
int usecs;
226+
long secs;
227+
228+
endtime = GetCurrentTimestamp();
229+
TimestampDifference(starttime, endtime, &secs, &usecs);
230+
231+
diff = secs * 1000 + usecs / 1000;
232+
}
233+
234+
if (Log_autovacuum == 0 || diff >= Log_autovacuum)
235+
{
236+
ereport(LOG,
237+
(errmsg("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n"
238+
"pages: %d removed, %d remain\n"
239+
"tuples: %.0f removed, %.0f remain\n"
240+
"system usage: %s",
241+
get_database_name(MyDatabaseId),
242+
get_namespace_name(RelationGetNamespace(onerel)),
243+
RelationGetRelationName(onerel),
244+
vacrelstats->num_index_scans,
245+
vacrelstats->pages_removed, vacrelstats->rel_pages,
246+
vacrelstats->tuples_deleted, vacrelstats->rel_tuples,
247+
pg_rusage_show(&ru0))));
248+
}
249+
}
203250
}
204251

205252

@@ -282,6 +329,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
282329
lazy_vacuum_heap(onerel, vacrelstats);
283330
/* Forget the now-vacuumed tuples, and press on */
284331
vacrelstats->num_dead_tuples = 0;
332+
vacrelstats->num_index_scans++;
285333
}
286334

287335
buf = ReadBuffer(onerel, blkno);
@@ -490,6 +538,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
490538
vacrelstats);
491539
/* Remove tuples from heap */
492540
lazy_vacuum_heap(onerel, vacrelstats);
541+
vacrelstats->num_index_scans++;
493542
}
494543

495544
/* Do post-vacuum cleanup and statistics update for each index */

src/backend/postmaster/autovacuum.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.41 2007/04/16 18:29:52 alvherre Exp $
13+
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.42 2007/04/18 16:44:18 alvherre Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -71,6 +71,8 @@ int autovacuum_freeze_max_age;
7171
int autovacuum_vac_cost_delay;
7272
int autovacuum_vac_cost_limit;
7373

74+
int Log_autovacuum = -1;
75+
7476
/* Flags to tell if we are in an autovacuum process */
7577
static bool am_autovacuum_launcher = false;
7678
static bool am_autovacuum_worker = false;
@@ -1814,7 +1816,6 @@ do_autovacuum(void)
18141816
{
18151817
Oid relid = lfirst_oid(cell);
18161818
autovac_table *tab;
1817-
char *relname;
18181819
WorkerInfo worker;
18191820
bool skipit;
18201821

@@ -1891,12 +1892,6 @@ do_autovacuum(void)
18911892
VacuumCostDelay = tab->at_vacuum_cost_delay;
18921893
VacuumCostLimit = tab->at_vacuum_cost_limit;
18931894

1894-
relname = get_rel_name(relid);
1895-
elog(DEBUG2, "autovac: will%s%s %s",
1896-
(tab->at_dovacuum ? " VACUUM" : ""),
1897-
(tab->at_doanalyze ? " ANALYZE" : ""),
1898-
relname);
1899-
19001895
/*
19011896
* Advertise my cost delay parameters for the balancing algorithm, and
19021897
* do a balance
@@ -1915,7 +1910,6 @@ do_autovacuum(void)
19151910
tab->at_freeze_min_age);
19161911
/* be tidy */
19171912
pfree(tab);
1918-
pfree(relname);
19191913
}
19201914

19211915
/*

src/backend/utils/misc/guc.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Written by Peter Eisentraut <[email protected]>.
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.385 2007/04/16 18:29:55 alvherre Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.386 2007/04/18 16:44:18 alvherre Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -1505,6 +1505,17 @@ static struct config_int ConfigureNamesInt[] =
15051505
-1, -1, INT_MAX / 1000, NULL, NULL
15061506
},
15071507

1508+
{
1509+
{"log_autovacuum", PGC_BACKEND, LOGGING_WHAT,
1510+
gettext_noop("Sets the minimum execution time above which autovacuum actions "
1511+
"will be logged."),
1512+
gettext_noop("Zero prints all actions. The default is -1 (turning this feature off)."),
1513+
GUC_UNIT_MS
1514+
},
1515+
&Log_autovacuum,
1516+
-1, -1, INT_MAX / 1000, NULL, NULL
1517+
},
1518+
15081519
{
15091520
{"bgwriter_delay", PGC_SIGHUP, RESOURCES,
15101521
gettext_noop("Background writer sleep time between rounds."),

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,11 @@
376376
#autovacuum = on # enable autovacuum subprocess?
377377
# 'on' requires stats_start_collector
378378
# and stats_row_level to also be on
379-
#autovacuum_max_workers = 3 # max # of autovacuum subprocesses
379+
#autovacuum_max_workers = 3 # max # of autovacuum subprocesses
380380
#autovacuum_naptime = 1min # time between autovacuum runs
381+
#log_autovacuum = -1 # -1 is disabled, 0 logs all actions
382+
# and their durations, > 0 logs only
383+
# actions running at least N msec.
381384
#autovacuum_vacuum_threshold = 500 # min # of tuple updates before
382385
# vacuum
383386
#autovacuum_analyze_threshold = 250 # min # of tuple updates before

src/include/postmaster/autovacuum.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/postmaster/autovacuum.h,v 1.9 2007/04/16 18:30:03 alvherre Exp $
10+
* $PostgreSQL: pgsql/src/include/postmaster/autovacuum.h,v 1.10 2007/04/18 16:44:18 alvherre Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -31,6 +31,8 @@ extern int autovacuum_vac_cost_limit;
3131
/* autovacuum launcher PID, only valid when worker is shutting down */
3232
extern int AutovacuumLauncherPid;
3333

34+
extern int Log_autovacuum;
35+
3436
/* Status inquiry functions */
3537
extern bool AutoVacuumingActive(void);
3638
extern bool IsAutoVacuumLauncherProcess(void);

0 commit comments

Comments
 (0)