Skip to content

Commit 58f337a

Browse files
committed
Centralize implementation of delay code by creating a pg_usleep()
subroutine in src/port/pgsleep.c. Remove platform dependencies from miscadmin.h and put them in port.h where they belong. Extend recent vacuum cost-based-delay patch to apply to VACUUM FULL, ANALYZE, and non-btree index vacuuming. By the way, where is the documentation for the cost-based-delay patch?
1 parent 87bd956 commit 58f337a

File tree

16 files changed

+138
-154
lines changed

16 files changed

+138
-154
lines changed

src/Makefile.global.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*-makefile-*-
2-
# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.174 2004/02/02 00:11:30 momjian Exp $
2+
# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.175 2004/02/10 03:42:42 tgl Exp $
33

44
#------------------------------------------------------------------------------
55
# All PostgreSQL makefiles include this file and use the variables it sets,
@@ -339,7 +339,7 @@ endif
339339
#
340340
# substitute implementations of the C library
341341

342-
LIBOBJS = @LIBOBJS@ path.o sprompt.o thread.o
342+
LIBOBJS = @LIBOBJS@ path.o pgsleep.o sprompt.o thread.o
343343

344344
ifneq (,$(LIBOBJS))
345345
LIBS += -lpgport

src/backend/access/gist/gist.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/gist/gist.c,v 1.107 2004/01/07 18:56:23 neilc Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/gist/gist.c,v 1.108 2004/02/10 03:42:42 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -19,6 +19,7 @@
1919
#include "access/gistscan.h"
2020
#include "access/heapam.h"
2121
#include "catalog/index.h"
22+
#include "commands/vacuum.h"
2223
#include "miscadmin.h"
2324

2425

@@ -1614,6 +1615,8 @@ gistbulkdelete(PG_FUNCTION_ARGS)
16141615

16151616
while (index_getnext_indexitem(iscan, ForwardScanDirection))
16161617
{
1618+
vacuum_delay_point();
1619+
16171620
if (callback(&iscan->xs_ctup.t_self, callback_state))
16181621
{
16191622
ItemPointerData indextup = iscan->currentItemData;

src/backend/access/hash/hash.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/hash/hash.c,v 1.70 2004/01/07 18:56:23 neilc Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/hash/hash.c,v 1.71 2004/02/10 03:42:43 tgl Exp $
1212
*
1313
* NOTES
1414
* This file contains only the public interface routines.
@@ -23,6 +23,7 @@
2323
#include "access/heapam.h"
2424
#include "access/xlogutils.h"
2525
#include "catalog/index.h"
26+
#include "commands/vacuum.h"
2627
#include "executor/executor.h"
2728
#include "miscadmin.h"
2829

@@ -514,6 +515,8 @@ hashbulkdelete(PG_FUNCTION_ARGS)
514515
OffsetNumber maxoffno;
515516
bool page_dirty = false;
516517

518+
vacuum_delay_point();
519+
517520
buf = _hash_getbuf(rel, blkno, HASH_WRITE);
518521
page = BufferGetPage(buf);
519522
_hash_checkpage(rel, page, LH_BUCKET_PAGE | LH_OVERFLOW_PAGE);

src/backend/access/nbtree/nbtree.c

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.112 2004/02/10 01:55:24 tgl Exp $
15+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.113 2004/02/10 03:42:43 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -22,6 +22,7 @@
2222
#include "access/heapam.h"
2323
#include "access/nbtree.h"
2424
#include "catalog/index.h"
25+
#include "commands/vacuum.h"
2526
#include "miscadmin.h"
2627
#include "storage/freespace.h"
2728
#include "storage/smgr.h"
@@ -584,27 +585,7 @@ btbulkdelete(PG_FUNCTION_ARGS)
584585
maxoff;
585586
BlockNumber nextpage;
586587

587-
CHECK_FOR_INTERRUPTS();
588-
589-
/*
590-
* If we're called by a cost based vacuum, do the
591-
* napping in case the balance exceeded the limit.
592-
*/
593-
if (VacuumCostActive && !InterruptPending &&
594-
VacuumCostBalance >= VacuumCostLimit)
595-
{
596-
int msec;
597-
598-
msec = VacuumCostNaptime * VacuumCostBalance / VacuumCostLimit;
599-
if (msec < VacuumCostNaptime * 4)
600-
PG_MSLEEP(msec);
601-
else
602-
PG_MSLEEP(VacuumCostNaptime * 4);
603-
604-
VacuumCostBalance = 0;
605-
606-
CHECK_FOR_INTERRUPTS();
607-
}
588+
vacuum_delay_point();
608589

609590
ndeletable = 0;
610591
page = BufferGetPage(buf);

src/backend/access/rtree/rtree.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/rtree/rtree.c,v 1.82 2004/01/07 18:56:24 neilc Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/rtree/rtree.c,v 1.83 2004/02/10 03:42:43 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -20,6 +20,7 @@
2020
#include "access/rtree.h"
2121
#include "access/xlogutils.h"
2222
#include "catalog/index.h"
23+
#include "commands/vacuum.h"
2324
#include "executor/executor.h"
2425
#include "miscadmin.h"
2526

@@ -1219,6 +1220,8 @@ rtbulkdelete(PG_FUNCTION_ARGS)
12191220

12201221
while (index_getnext_indexitem(iscan, ForwardScanDirection))
12211222
{
1223+
vacuum_delay_point();
1224+
12221225
if (callback(&iscan->xs_ctup.t_self, callback_state))
12231226
{
12241227
ItemPointerData indextup = iscan->currentItemData;

src/backend/access/transam/xact.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.162 2004/02/10 01:55:24 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.163 2004/02/10 03:42:43 tgl Exp $
1212
*
1313
* NOTES
1414
* Transaction aborts can now occur two ways:
@@ -142,7 +142,6 @@
142142
#include "postgres.h"
143143

144144
#include <unistd.h>
145-
#include <sys/time.h>
146145

147146
#include "access/gistscan.h"
148147
#include "access/hash.h"
@@ -562,7 +561,7 @@ RecordTransactionCommit(void)
562561
*/
563562
if (CommitDelay > 0 && enableFsync &&
564563
CountActiveBackends() >= CommitSiblings)
565-
PG_USLEEP(CommitDelay);
564+
pg_usleep(CommitDelay);
566565

567566
XLogFlush(recptr);
568567
}

src/backend/commands/analyze.c

Lines changed: 5 additions & 5 deletions
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.66 2004/01/06 18:07:31 neilc Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.67 2004/02/10 03:42:43 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -529,7 +529,7 @@ acquire_sample_rows(Relation onerel, HeapTuple *rows, int targrows,
529529
rows[numrows++] = heap_copytuple(tuple);
530530
if (numrows >= targrows)
531531
break;
532-
CHECK_FOR_INTERRUPTS();
532+
vacuum_delay_point();
533533
}
534534
heap_endscan(scan);
535535

@@ -604,7 +604,7 @@ acquire_sample_rows(Relation onerel, HeapTuple *rows, int targrows,
604604
OffsetNumber targoffset,
605605
maxoffset;
606606

607-
CHECK_FOR_INTERRUPTS();
607+
vacuum_delay_point();
608608

609609
t = select_next_random_record(t, targrows, &rstate);
610610
/* Try to read the t'th record in the table */
@@ -912,7 +912,7 @@ compute_minimal_stats(VacAttrStats *stats,
912912
int firstcount1,
913913
j;
914914

915-
CHECK_FOR_INTERRUPTS();
915+
vacuum_delay_point();
916916

917917
value = heap_getattr(tuple, stats->attnum, tupDesc, &isnull);
918918

@@ -1214,7 +1214,7 @@ compute_scalar_stats(VacAttrStats *stats,
12141214
Datum value;
12151215
bool isnull;
12161216

1217-
CHECK_FOR_INTERRUPTS();
1217+
vacuum_delay_point();
12181218

12191219
value = heap_getattr(tuple, stats->attnum, tupDesc, &isnull);
12201220

src/backend/commands/vacuum.c

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*
1515
* IDENTIFICATION
16-
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.272 2004/02/10 01:55:25 tgl Exp $
16+
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.273 2004/02/10 03:42:43 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -181,6 +181,10 @@ vacuum(VacuumStmt *vacstmt)
181181
if (vacstmt->vacuum)
182182
PreventTransactionChain((void *) vacstmt, stmttype);
183183

184+
/* Turn vacuum cost accounting on or off */
185+
VacuumCostActive = (VacuumCostNaptime > 0);
186+
VacuumCostBalance = 0;
187+
184188
/*
185189
* Send info about dead objects to the statistics collector
186190
*/
@@ -374,6 +378,9 @@ vacuum(VacuumStmt *vacstmt)
374378

375379
if (anl_context)
376380
MemoryContextDelete(anl_context);
381+
382+
/* Turn off vacuum cost accounting */
383+
VacuumCostActive = false;
377384
}
378385

379386
/*
@@ -1082,7 +1089,7 @@ scan_heap(VRelStats *vacrelstats, Relation onerel,
10821089
bool do_reap,
10831090
do_frag;
10841091

1085-
CHECK_FOR_INTERRUPTS();
1092+
vacuum_delay_point();
10861093

10871094
buf = ReadBuffer(onerel, blkno);
10881095
page = BufferGetPage(buf);
@@ -1528,7 +1535,7 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
15281535
blkno > last_move_dest_block;
15291536
blkno--)
15301537
{
1531-
CHECK_FOR_INTERRUPTS();
1538+
vacuum_delay_point();
15321539

15331540
/*
15341541
* Forget fraged_pages pages at or after this one; they're no
@@ -2311,7 +2318,8 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
23112318
i < vacuumed_pages;
23122319
i++, curpage++)
23132320
{
2314-
CHECK_FOR_INTERRUPTS();
2321+
vacuum_delay_point();
2322+
23152323
Assert((*curpage)->blkno < blkno);
23162324
if ((*curpage)->offsets_used == 0)
23172325
{
@@ -2342,7 +2350,8 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
23422350
i < num_fraged_pages;
23432351
i++, curpage++)
23442352
{
2345-
CHECK_FOR_INTERRUPTS();
2353+
vacuum_delay_point();
2354+
23462355
Assert((*curpage)->blkno < blkno);
23472356
if ((*curpage)->blkno > last_move_dest_block)
23482357
break; /* no need to scan any further */
@@ -2553,7 +2562,8 @@ vacuum_heap(VRelStats *vacrelstats, Relation onerel, VacPageList vacuum_pages)
25532562

25542563
for (i = 0, vacpage = vacuum_pages->pagedesc; i < nblocks; i++, vacpage++)
25552564
{
2556-
CHECK_FOR_INTERRUPTS();
2565+
vacuum_delay_point();
2566+
25572567
if ((*vacpage)->offsets_free > 0)
25582568
{
25592569
buf = ReadBuffer(onerel, (*vacpage)->blkno);
@@ -3164,3 +3174,34 @@ vac_show_rusage(VacRUsage *ru0)
31643174

31653175
return result;
31663176
}
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+
int msec;
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy