|
13 | 13 | *
|
14 | 14 | *
|
15 | 15 | * IDENTIFICATION
|
16 |
| - * $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.272 2004/02/1001:55:25 tgl Exp $ |
| 16 | + * $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.273 2004/02/1003:42:43 tgl Exp $ |
17 | 17 | *
|
18 | 18 | *-------------------------------------------------------------------------
|
19 | 19 | */
|
@@ -181,6 +181,10 @@ vacuum(VacuumStmt *vacstmt)
|
181 | 181 | if (vacstmt->vacuum)
|
182 | 182 | PreventTransactionChain((void*)vacstmt,stmttype);
|
183 | 183 |
|
| 184 | +/* Turn vacuum cost accounting on or off */ |
| 185 | +VacuumCostActive= (VacuumCostNaptime>0); |
| 186 | +VacuumCostBalance=0; |
| 187 | + |
184 | 188 | /*
|
185 | 189 | * Send info about dead objects to the statistics collector
|
186 | 190 | */
|
@@ -374,6 +378,9 @@ vacuum(VacuumStmt *vacstmt)
|
374 | 378 |
|
375 | 379 | if (anl_context)
|
376 | 380 | MemoryContextDelete(anl_context);
|
| 381 | + |
| 382 | +/* Turn off vacuum cost accounting */ |
| 383 | +VacuumCostActive= false; |
377 | 384 | }
|
378 | 385 |
|
379 | 386 | /*
|
@@ -1082,7 +1089,7 @@ scan_heap(VRelStats *vacrelstats, Relation onerel,
|
1082 | 1089 | booldo_reap,
|
1083 | 1090 | do_frag;
|
1084 | 1091 |
|
1085 |
| -CHECK_FOR_INTERRUPTS(); |
| 1092 | +vacuum_delay_point(); |
1086 | 1093 |
|
1087 | 1094 | buf=ReadBuffer(onerel,blkno);
|
1088 | 1095 | page=BufferGetPage(buf);
|
@@ -1528,7 +1535,7 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
|
1528 | 1535 | blkno>last_move_dest_block;
|
1529 | 1536 | blkno--)
|
1530 | 1537 | {
|
1531 |
| -CHECK_FOR_INTERRUPTS(); |
| 1538 | +vacuum_delay_point(); |
1532 | 1539 |
|
1533 | 1540 | /*
|
1534 | 1541 | * Forget fraged_pages pages at or after this one; they're no
|
@@ -2311,7 +2318,8 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
|
2311 | 2318 | i<vacuumed_pages;
|
2312 | 2319 | i++,curpage++)
|
2313 | 2320 | {
|
2314 |
| -CHECK_FOR_INTERRUPTS(); |
| 2321 | +vacuum_delay_point(); |
| 2322 | + |
2315 | 2323 | Assert((*curpage)->blkno<blkno);
|
2316 | 2324 | if ((*curpage)->offsets_used==0)
|
2317 | 2325 | {
|
@@ -2342,7 +2350,8 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
|
2342 | 2350 | i<num_fraged_pages;
|
2343 | 2351 | i++,curpage++)
|
2344 | 2352 | {
|
2345 |
| -CHECK_FOR_INTERRUPTS(); |
| 2353 | +vacuum_delay_point(); |
| 2354 | + |
2346 | 2355 | Assert((*curpage)->blkno<blkno);
|
2347 | 2356 | if ((*curpage)->blkno>last_move_dest_block)
|
2348 | 2357 | break;/* no need to scan any further */
|
@@ -2553,7 +2562,8 @@ vacuum_heap(VRelStats *vacrelstats, Relation onerel, VacPageList vacuum_pages)
|
2553 | 2562 |
|
2554 | 2563 | for (i=0,vacpage=vacuum_pages->pagedesc;i<nblocks;i++,vacpage++)
|
2555 | 2564 | {
|
2556 |
| -CHECK_FOR_INTERRUPTS(); |
| 2565 | +vacuum_delay_point(); |
| 2566 | + |
2557 | 2567 | if ((*vacpage)->offsets_free>0)
|
2558 | 2568 | {
|
2559 | 2569 | buf=ReadBuffer(onerel, (*vacpage)->blkno);
|
@@ -3164,3 +3174,34 @@ vac_show_rusage(VacRUsage *ru0)
|
3164 | 3174 |
|
3165 | 3175 | returnresult;
|
3166 | 3176 | }
|
| 3177 | + |
| 3178 | +/* |
| 3179 | + * vacuum_delay_point --- check for interrupts and cost-based delay. |
| 3180 | + * |
| 3181 | + * This should be called in each major loop of VACUUM processing, |
| 3182 | + * typically once per page processed. |
| 3183 | + */ |
| 3184 | +void |
| 3185 | +vacuum_delay_point(void) |
| 3186 | +{ |
| 3187 | +/* Always check for interrupts */ |
| 3188 | +CHECK_FOR_INTERRUPTS(); |
| 3189 | + |
| 3190 | +/* Nap if appropriate */ |
| 3191 | +if (VacuumCostActive&& !InterruptPending&& |
| 3192 | +VacuumCostBalance >=VacuumCostLimit) |
| 3193 | +{ |
| 3194 | +intmsec; |
| 3195 | + |
| 3196 | +msec=VacuumCostNaptime*VacuumCostBalance /VacuumCostLimit; |
| 3197 | +if (msec>VacuumCostNaptime*4) |
| 3198 | +msec=VacuumCostNaptime*4; |
| 3199 | + |
| 3200 | +pg_usleep(msec*1000L); |
| 3201 | + |
| 3202 | +VacuumCostBalance=0; |
| 3203 | + |
| 3204 | +/* Might have gotten an interrupt while sleeping */ |
| 3205 | +CHECK_FOR_INTERRUPTS(); |
| 3206 | +} |
| 3207 | +} |