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 */
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
9092int max_free_pages ;/* # slots allocated in array */
9193PageFreeSpaceInfo * free_pages ;/* array or heap of blkno/avail */
9294BlockNumber 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)
141144Relation * Irel ;
142145int nindexes ;
143146BlockNumber 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
145156if (vacstmt -> verbose )
146157elevel = 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 */
157168vacrelstats -> threshold = GetAvgFSMRequestSize (& onerel -> rd_node );
158169
170+ vacrelstats -> num_index_scans = 0 ;
171+
159172/* Open all indexes of the relation */
160173vac_open_indexes (onerel ,RowExclusiveLock ,& nindexes ,& Irel );
161174vacrelstats -> hasindex = (nindexes > 0 );
@@ -200,6 +213,40 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
200213/* report results to the stats collector, too */
201214pgstat_report_vacuum (RelationGetRelid (onerel ),onerel -> rd_rel -> relisshared ,
202215vacstmt -> 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,
282329lazy_vacuum_heap (onerel ,vacrelstats );
283330/* Forget the now-vacuumed tuples, and press on */
284331vacrelstats -> num_dead_tuples = 0 ;
332+ vacrelstats -> num_index_scans ++ ;
285333}
286334
287335buf = ReadBuffer (onerel ,blkno );
@@ -490,6 +538,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
490538vacrelstats );
491539/* Remove tuples from heap */
492540lazy_vacuum_heap (onerel ,vacrelstats );
541+ vacrelstats -> num_index_scans ++ ;
493542}
494543
495544/* Do post-vacuum cleanup and statistics update for each index */