36
36
*
37
37
*
38
38
* 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 $
40
40
*
41
41
*-------------------------------------------------------------------------
42
42
*/
47
47
#include "access/genam.h"
48
48
#include "access/heapam.h"
49
49
#include "access/transam.h"
50
+ #include "commands/dbcommands.h"
50
51
#include "commands/vacuum.h"
51
52
#include "miscadmin.h"
52
53
#include "pgstat.h"
54
+ #include "postmaster/autovacuum.h"
53
55
#include "storage/freespace.h"
54
56
#include "utils/lsyscache.h"
55
57
#include "utils/memutils.h"
@@ -90,6 +92,7 @@ typedef struct LVRelStats
90
92
int max_free_pages ; /* # slots allocated in array */
91
93
PageFreeSpaceInfo * free_pages ; /* array or heap of blkno/avail */
92
94
BlockNumber tot_free_pages ; /* total pages with >= threshold space */
95
+ int num_index_scans ;
93
96
} LVRelStats ;
94
97
95
98
@@ -141,6 +144,14 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
141
144
Relation * Irel ;
142
145
int nindexes ;
143
146
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 ();
144
155
145
156
if (vacstmt -> verbose )
146
157
elevel = INFO ;
@@ -156,6 +167,8 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
156
167
/* XXX should we scale it up or down? Adjust vacuum.c too, if so */
157
168
vacrelstats -> threshold = GetAvgFSMRequestSize (& onerel -> rd_node );
158
169
170
+ vacrelstats -> num_index_scans = 0 ;
171
+
159
172
/* Open all indexes of the relation */
160
173
vac_open_indexes (onerel , RowExclusiveLock , & nindexes , & Irel );
161
174
vacrelstats -> hasindex = (nindexes > 0 );
@@ -200,6 +213,40 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
200
213
/* report results to the stats collector, too */
201
214
pgstat_report_vacuum (RelationGetRelid (onerel ), onerel -> rd_rel -> relisshared ,
202
215
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
+ }
203
250
}
204
251
205
252
@@ -282,6 +329,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
282
329
lazy_vacuum_heap (onerel , vacrelstats );
283
330
/* Forget the now-vacuumed tuples, and press on */
284
331
vacrelstats -> num_dead_tuples = 0 ;
332
+ vacrelstats -> num_index_scans ++ ;
285
333
}
286
334
287
335
buf = ReadBuffer (onerel , blkno );
@@ -490,6 +538,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
490
538
vacrelstats );
491
539
/* Remove tuples from heap */
492
540
lazy_vacuum_heap (onerel , vacrelstats );
541
+ vacrelstats -> num_index_scans ++ ;
493
542
}
494
543
495
544
/* Do post-vacuum cleanup and statistics update for each index */
0 commit comments