Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita0a26c4

Browse files
committed
Avoid integer overflow issues in autovacuum.
1 parente976fd4 commita0a26c4

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-26
lines changed

‎src/backend/postmaster/autovacuum.c

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.49 2007/06/08 21:21:28 alvherre Exp $
13+
* $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.50 2007/06/13 21:24:55 alvherre Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
1717
#include"postgres.h"
1818

1919
#include<signal.h>
2020
#include<sys/types.h>
21+
#include<sys/time.h>
2122
#include<time.h>
2223
#include<unistd.h>
2324

@@ -73,6 +74,10 @@ intautovacuum_vac_cost_limit;
7374

7475
intLog_autovacuum=-1;
7576

77+
78+
/* maximum sleep duration in the launcher, in seconds */
79+
#defineAV_SLEEP_QUANTUM 10
80+
7681
/* Flags to tell if we are in an autovacuum process */
7782
staticboolam_autovacuum_launcher= false;
7883
staticboolam_autovacuum_worker= false;
@@ -197,7 +202,8 @@ NON_EXEC_STATIC void AutoVacWorkerMain(int argc, char *argv[]);
197202
NON_EXEC_STATICvoidAutoVacLauncherMain(intargc,char*argv[]);
198203

199204
staticOiddo_start_worker(void);
200-
staticuint64launcher_determine_sleep(boolcanlaunch,boolrecursing);
205+
staticvoidlauncher_determine_sleep(boolcanlaunch,boolrecursing,
206+
structtimeval*nap);
201207
staticvoidlaunch_worker(TimestampTznow);
202208
staticList*get_database_list(void);
203209
staticvoidrebuild_database_list(Oidnewdb);
@@ -487,7 +493,7 @@ AutoVacLauncherMain(int argc, char *argv[])
487493

488494
for (;;)
489495
{
490-
uint64micros;
496+
structtimevalnap;
491497
boolcan_launch;
492498
TimestampTzcurrent_time=0;
493499

@@ -498,11 +504,39 @@ AutoVacLauncherMain(int argc, char *argv[])
498504
if (!PostmasterIsAlive(true))
499505
exit(1);
500506

501-
micros=launcher_determine_sleep(AutoVacuumShmem->av_freeWorkers!=
502-
INVALID_OFFSET, false);
507+
launcher_determine_sleep(AutoVacuumShmem->av_freeWorkers!=
508+
INVALID_OFFSET, false,&nap);
509+
510+
/*
511+
* Sleep for a while according to schedule. We only sleep in
512+
* AV_SLEEP_QUANTUM second intervals, in order to promptly notice
513+
* postmaster death.
514+
*/
515+
while (nap.tv_sec>0||nap.tv_usec>0)
516+
{
517+
uint32sleeptime;
518+
519+
sleeptime=nap.tv_usec;
520+
nap.tv_usec=0;
503521

504-
/* Sleep for a while according to schedule */
505-
pg_usleep(micros);
522+
if (nap.tv_sec>0)
523+
{
524+
sleeptime+=Min(nap.tv_sec,AV_SLEEP_QUANTUM)*1000000;
525+
nap.tv_sec-=Min(nap.tv_sec,AV_SLEEP_QUANTUM);
526+
}
527+
528+
pg_usleep(sleeptime);
529+
530+
/*
531+
* Emergency bailout if postmaster has died. This is to avoid the
532+
* necessity for manual cleanup of all postmaster children.
533+
*/
534+
if (!PostmasterIsAlive(true))
535+
exit(1);
536+
537+
if (avlauncher_shutdown_request||got_SIGHUP||got_SIGUSR1)
538+
break;
539+
}
506540

507541
/* the normal shutdown case */
508542
if (avlauncher_shutdown_request)
@@ -647,16 +681,15 @@ AutoVacLauncherMain(int argc, char *argv[])
647681
}
648682

649683
/*
650-
* Determine the time to sleep,in microseconds,based on the database list.
684+
* Determine the time to sleep, based on the database list.
651685
*
652686
* The "canlaunch" parameter indicates whether we can start a worker right now,
653-
* for example due to the workers being all busy.
687+
* for example due to the workers being all busy. If this is false, we will
688+
* cause a long sleep, which will be interrupted when a worker exits.
654689
*/
655-
staticuint64
656-
launcher_determine_sleep(boolcanlaunch,boolrecursing)
690+
staticvoid
691+
launcher_determine_sleep(boolcanlaunch,boolrecursing,structtimeval*nap)
657692
{
658-
longsecs;
659-
intusecs;
660693
Dlelem*elem;
661694

662695
/*
@@ -667,23 +700,28 @@ launcher_determine_sleep(bool canlaunch, bool recursing)
667700
*/
668701
if (!canlaunch)
669702
{
670-
secs=autovacuum_naptime;
671-
usecs=0;
703+
nap->tv_sec=autovacuum_naptime;
704+
nap->tv_usec=0;
672705
}
673706
elseif ((elem=DLGetTail(DatabaseList))!=NULL)
674707
{
675708
avl_dbase*avdb=DLE_VAL(elem);
676709
TimestampTzcurrent_time=GetCurrentTimestamp();
677710
TimestampTznext_wakeup;
711+
longsecs;
712+
intusecs;
678713

679714
next_wakeup=avdb->adl_next_worker;
680715
TimestampDifference(current_time,next_wakeup,&secs,&usecs);
716+
717+
nap->tv_sec=secs;
718+
nap->tv_usec=usecs;
681719
}
682720
else
683721
{
684722
/* list is empty, sleep for whole autovacuum_naptime seconds */
685-
secs=autovacuum_naptime;
686-
usecs=0;
723+
nap->tv_sec=autovacuum_naptime;
724+
nap->tv_usec=0;
687725
}
688726

689727
/*
@@ -696,20 +734,19 @@ launcher_determine_sleep(bool canlaunch, bool recursing)
696734
* We only recurse once. rebuild_database_list should always return times
697735
* in the future, but it seems best not to trust too much on that.
698736
*/
699-
if (secs==0L&&usecs==0&& !recursing)
737+
if (nap->tv_sec==0L&&nap->tv_usec==0&& !recursing)
700738
{
701739
rebuild_database_list(InvalidOid);
702-
returnlauncher_determine_sleep(canlaunch, true);
740+
launcher_determine_sleep(canlaunch, true,nap);
741+
return;
703742
}
704743

705744
/* 100ms is the smallest time we'll allow the launcher to sleep */
706-
if (secs <=0L&&usecs <=100000)
745+
if (nap->tv_sec <=0L&&nap->tv_usec <=100000)
707746
{
708-
secs=0L;
709-
usecs=100000;/* 100 ms */
747+
nap->tv_sec=0L;
748+
nap->tv_usec=100000;/* 100 ms */
710749
}
711-
712-
returnsecs*1000000+usecs;
713750
}
714751

715752
/*

‎src/backend/utils/misc/guc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Written by Peter Eisentraut <peter_e@gmx.net>.
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.396 2007/06/08 18:23:52 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.397 2007/06/13 21:24:56 alvherre Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -1645,7 +1645,7 @@ static struct config_int ConfigureNamesInt[] =
16451645
GUC_UNIT_S
16461646
},
16471647
&autovacuum_naptime,
1648-
60,1,INT_MAX,NULL,NULL
1648+
60,1,INT_MAX /1000,NULL,NULL
16491649
},
16501650
{
16511651
{"autovacuum_vacuum_threshold",PGC_SIGHUP,AUTOVACUUM,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp