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

Commita64ca63

Browse files
committed
Use WaitLatch, not pg_usleep, for delaying in pg_sleep().
This avoids platform-dependent behavior wherein pg_sleep() might fail to beinterrupted by statement timeout, query cancel, SIGTERM, etc. Also, sincethere's no reason to wake up once a second any more, we can reduce thepower consumption of a sleeping backend a tad.Back-patch to 9.3, since use of SA_RESTART for SIGALRM makes this a biggerissue than it used to be.
1 parentf69aece commita64ca63

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,16 +384,16 @@ pg_sleep(PG_FUNCTION_ARGS)
384384
float8endtime;
385385

386386
/*
387-
* Webreak the requested sleep into segments of no more than 1 second, to
388-
*put an upper bound on how long it will take us to respond to a cancel
389-
*or die interrupt. (Note that pg_usleepisinterruptible by signals on
390-
*some platforms but not others.)Also, this method avoids exposing
391-
*pg_usleep's upper bound on allowed delays.
387+
* Wesleep using WaitLatch, to ensure that we'll wake up promptly if an
388+
*important signal (such as SIGALRM or SIGINT) arrives. Because
389+
*WaitLatch's upper limit of delayisINT_MAX milliseconds, and the user
390+
*might ask for more than that, we sleep for at most 10 minutes and then
391+
*loop.
392392
*
393393
* By computing the intended stop time initially, we avoid accumulation of
394394
* extra delay across multiple sleeps.This also ensures we won't delay
395-
* less than the specified timeif pg_usleep isinterruptedbyother
396-
*signals such as SIGHUP.
395+
* less than the specified timewhen WaitLatch isterminated earlybya
396+
*non-query-cancelling signal such as SIGHUP.
397397
*/
398398

399399
#ifdefHAVE_INT64_TIMESTAMP
@@ -407,15 +407,22 @@ pg_sleep(PG_FUNCTION_ARGS)
407407
for (;;)
408408
{
409409
float8delay;
410+
longdelay_ms;
410411

411412
CHECK_FOR_INTERRUPTS();
413+
412414
delay=endtime-GetNowFloat();
413-
if (delay >=1.0)
414-
pg_usleep(1000000L);
415+
if (delay >=600.0)
416+
delay_ms=600000;
415417
elseif (delay>0.0)
416-
pg_usleep((long)ceil(delay*1000000.0));
418+
delay_ms= (long)ceil(delay*1000.0);
417419
else
418420
break;
421+
422+
(void)WaitLatch(&MyProc->procLatch,
423+
WL_LATCH_SET |WL_TIMEOUT,
424+
delay_ms);
425+
ResetLatch(&MyProc->procLatch);
419426
}
420427

421428
PG_RETURN_VOID();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp