3131 *
3232 *
3333 * IDENTIFICATION
34- * $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.34 2004/02/03 17:34:02 tgl Exp $
34+ * $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.35 2004/02/06 19:36:17 wieck Exp $
3535 *
3636 *-------------------------------------------------------------------------
3737 */
@@ -148,6 +148,11 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
148148vac_open_indexes (onerel ,& nindexes ,& Irel );
149149hasindex = (nindexes > 0 );
150150
151+ /* Turn on vacuum cost accounting */
152+ if (VacuumCostNaptime > 0 )
153+ VacuumCostActive = true;
154+ VacuumCostBalance = 0 ;
155+
151156/* Do the vacuuming */
152157lazy_scan_heap (onerel ,vacrelstats ,Irel ,nindexes );
153158
@@ -168,6 +173,9 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
168173/* Update shared free space map with final free space info */
169174lazy_update_fsm (onerel ,vacrelstats );
170175
176+ /* Turn off vacuum cost accounting */
177+ VacuumCostActive = false;
178+
171179/* Update statistics in pg_class */
172180vac_update_relstats (RelationGetRelid (onerel ),vacrelstats -> rel_pages ,
173181vacrelstats -> rel_tuples ,hasindex );
@@ -228,6 +236,25 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
228236
229237CHECK_FOR_INTERRUPTS ();
230238
239+ /*
240+ * Do the napping in a cost based vacuum.
241+ */
242+ if (VacuumCostActive && !InterruptPending &&
243+ VacuumCostBalance >=VacuumCostLimit )
244+ {
245+ int msec ;
246+
247+ msec = VacuumCostNaptime * VacuumCostBalance /VacuumCostLimit ;
248+ if (msec < VacuumCostNaptime * 4 )
249+ PG_MSLEEP (msec );
250+ else
251+ PG_MSLEEP (VacuumCostNaptime * 4 );
252+
253+ VacuumCostBalance = 0 ;
254+
255+ CHECK_FOR_INTERRUPTS ();
256+ }
257+
231258/*
232259 * If we are close to overrunning the available space for
233260 * dead-tuple TIDs, pause and do a cycle of vacuuming before we
@@ -469,6 +496,25 @@ lazy_vacuum_heap(Relation onerel, LVRelStats *vacrelstats)
469496
470497CHECK_FOR_INTERRUPTS ();
471498
499+ /*
500+ * Do the napping in a cost based vacuum.
501+ */
502+ if (VacuumCostActive && !InterruptPending &&
503+ VacuumCostBalance >=VacuumCostLimit )
504+ {
505+ int msec ;
506+
507+ msec = VacuumCostNaptime * VacuumCostBalance /VacuumCostLimit ;
508+ if (msec < VacuumCostNaptime * 4 )
509+ PG_MSLEEP (msec );
510+ else
511+ PG_MSLEEP (VacuumCostNaptime * 4 );
512+
513+ VacuumCostBalance = 0 ;
514+
515+ CHECK_FOR_INTERRUPTS ();
516+ }
517+
472518tblk = ItemPointerGetBlockNumber (& vacrelstats -> dead_tuples [tupindex ]);
473519buf = ReadBuffer (onerel ,tblk );
474520LockBufferForCleanup (buf );
@@ -800,6 +846,25 @@ count_nondeletable_pages(Relation onerel, LVRelStats *vacrelstats)
800846
801847CHECK_FOR_INTERRUPTS ();
802848
849+ /*
850+ * Do the napping in a cost based vacuum.
851+ */
852+ if (VacuumCostActive && !InterruptPending &&
853+ VacuumCostBalance >=VacuumCostLimit )
854+ {
855+ int msec ;
856+
857+ msec = VacuumCostNaptime * VacuumCostBalance /VacuumCostLimit ;
858+ if (msec < VacuumCostNaptime * 4 )
859+ PG_MSLEEP (msec );
860+ else
861+ PG_MSLEEP (VacuumCostNaptime * 4 );
862+
863+ VacuumCostBalance = 0 ;
864+
865+ CHECK_FOR_INTERRUPTS ();
866+ }
867+
803868blkno -- ;
804869
805870buf = ReadBuffer (onerel ,blkno );