PostgreSQL Source Code  git master
timeout.h File Reference
Include dependency graph for timeout.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  EnableTimeoutParams
 
struct  DisableTimeoutParams
 

Typedefs

typedef enum TimeoutId TimeoutId
 
typedef void(* timeout_handler_proc) (void)
 
typedef enum TimeoutType TimeoutType
 

Enumerations

enum  TimeoutId {
  STARTUP_PACKET_TIMEOUT, DEADLOCK_TIMEOUT, LOCK_TIMEOUT, STATEMENT_TIMEOUT,
  STANDBY_DEADLOCK_TIMEOUT, STANDBY_TIMEOUT, STANDBY_LOCK_TIMEOUT, IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
  USER_TIMEOUT, MAX_TIMEOUTS = 16
}
 
enum  TimeoutType { TMPARAM_AFTER, TMPARAM_AT }
 

Functions

void InitializeTimeouts (void)
 
TimeoutId RegisterTimeout (TimeoutId id, timeout_handler_proc handler)
 
void reschedule_timeouts (void)
 
void enable_timeout_after (TimeoutId id, int delay_ms)
 
void enable_timeout_at (TimeoutId id, TimestampTz fin_time)
 
void enable_timeouts (const EnableTimeoutParams *timeouts, int count)
 
void disable_timeout (TimeoutId id, bool keep_indicator)
 
void disable_timeouts (const DisableTimeoutParams *timeouts, int count)
 
void disable_all_timeouts (bool keep_indicators)
 
bool get_timeout_active (TimeoutId id)
 
bool get_timeout_indicator (TimeoutId id, bool reset_indicator)
 
TimestampTz get_timeout_start_time (TimeoutId id)
 
TimestampTz get_timeout_finish_time (TimeoutId id)
 

Typedef Documentation

◆ timeout_handler_proc

typedef void(* timeout_handler_proc) (void)

Definition at line 41 of file timeout.h.

◆ TimeoutId

typedef enum TimeoutId TimeoutId

◆ TimeoutType

typedef enum TimeoutType TimeoutType

Enumeration Type Documentation

◆ TimeoutId

enum TimeoutId
Enumerator
STARTUP_PACKET_TIMEOUT 
DEADLOCK_TIMEOUT 
LOCK_TIMEOUT 
STATEMENT_TIMEOUT 
STANDBY_DEADLOCK_TIMEOUT 
STANDBY_TIMEOUT 
STANDBY_LOCK_TIMEOUT 
IDLE_IN_TRANSACTION_SESSION_TIMEOUT 
USER_TIMEOUT 
MAX_TIMEOUTS 

Definition at line 23 of file timeout.h.

24 {
25  /* Predefined timeout reasons */
34  /* First user-definable timeout reason */
36  /* Maximum number of timeout reasons */
37  MAX_TIMEOUTS = 16
38 } TimeoutId;
TimeoutId
Definition: timeout.h:23

◆ TimeoutType

Enumerator
TMPARAM_AFTER 
TMPARAM_AT 

Definition at line 46 of file timeout.h.

47 {
50 } TimeoutType;
TimeoutType
Definition: timeout.h:46

Function Documentation

◆ disable_all_timeouts()

void disable_all_timeouts ( bool  keep_indicators)

Definition at line 598 of file timeout.c.

References timeout_params::active, disable_alarm, elog, FATAL, i, timeout_params::indicator, ITIMER_REAL, MAX_TIMEOUTS, MemSet, num_active_timeouts, and setitimer().

Referenced by AutoVacLauncherMain(), PostgresMain(), ResolveRecoveryConflictWithBufferPin(), and ResolveRecoveryConflictWithLock().

599 {
600  int i;
601 
602  disable_alarm();
603 
604  /*
605  * Only bother to reset the timer if we think it's active. We could just
606  * let the interrupt happen anyway, but it's probably a bit cheaper to do
607  * setitimer() than to let the useless interrupt happen.
608  */
609  if (num_active_timeouts > 0)
610  {
611  struct itimerval timeval;
612 
613  MemSet(&timeval, 0, sizeof(struct itimerval));
614  if (setitimer(ITIMER_REAL, &timeval, NULL) != 0)
615  elog(FATAL, "could not disable SIGALRM timer: %m");
616  }
617 
619 
620  for (i = 0; i < MAX_TIMEOUTS; i++)
621  {
622  all_timeouts[i].active = false;
623  if (!keep_indicators)
624  all_timeouts[i].indicator = false;
625  }
626 }
#define MemSet(start, val, len)
Definition: c.h:962
#define disable_alarm()
Definition: timeout.c:65
volatile bool indicator
Definition: timeout.c:32
volatile bool active
Definition: timeout.c:31
#define FATAL
Definition: elog.h:52
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:44
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
Definition: timer.c:86
#define elog(elevel,...)
Definition: elog.h:228
int i
static volatile int num_active_timeouts
Definition: timeout.c:51
#define ITIMER_REAL
Definition: win32_port.h:182

◆ disable_timeout()

void disable_timeout ( TimeoutId  id,
bool  keep_indicator 
)

Definition at line 532 of file timeout.c.

References timeout_params::active, all_timeouts_initialized, Assert, disable_alarm, find_active_timeout(), GetCurrentTimestamp(), timeout_params::indicator, num_active_timeouts, remove_timeout_index(), schedule_alarm(), and timeout_params::timeout_handler.

Referenced by BackendInitialize(), disable_statement_timeout(), enable_statement_timeout(), PerformAuthentication(), PostgresMain(), ProcSleep(), and StandbyTimeoutHandler().

533 {
534  /* Assert request is sane */
536  Assert(all_timeouts[id].timeout_handler != NULL);
537 
538  /* Disable timeout interrupts for safety. */
539  disable_alarm();
540 
541  /* Find the timeout and remove it from the active list. */
542  if (all_timeouts[id].active)
544 
545  /* Mark it inactive, whether it was active or not. */
546  if (!keep_indicator)
547  all_timeouts[id].indicator = false;
548 
549  /* Reschedule the interrupt, if any timeouts remain active. */
550  if (num_active_timeouts > 0)
552 }
static bool all_timeouts_initialized
Definition: timeout.c:45
static void remove_timeout_index(int index)
Definition: timeout.c:124
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
#define disable_alarm()
Definition: timeout.c:65
volatile bool indicator
Definition: timeout.c:32
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:44
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:191
#define Assert(condition)
Definition: c.h:739
static int find_active_timeout(TimeoutId id)
Definition: timeout.c:83
static volatile int num_active_timeouts
Definition: timeout.c:51

◆ disable_timeouts()

void disable_timeouts ( const DisableTimeoutParams timeouts,
int  count 
)

Definition at line 565 of file timeout.c.

References timeout_params::active, all_timeouts_initialized, Assert, disable_alarm, find_active_timeout(), GetCurrentTimestamp(), i, DisableTimeoutParams::id, timeout_params::indicator, num_active_timeouts, remove_timeout_index(), schedule_alarm(), and timeout_params::timeout_handler.

Referenced by LockErrorCleanup(), and ProcSleep().

566 {
567  int i;
568 
570 
571  /* Disable timeout interrupts for safety. */
572  disable_alarm();
573 
574  /* Cancel the timeout(s). */
575  for (i = 0; i < count; i++)
576  {
577  TimeoutId id = timeouts[i].id;
578 
579  Assert(all_timeouts[id].timeout_handler != NULL);
580 
581  if (all_timeouts[id].active)
583 
584  if (!timeouts[i].keep_indicator)
585  all_timeouts[id].indicator = false;
586  }
587 
588  /* Reschedule the interrupt, if any timeouts remain active. */
589  if (num_active_timeouts > 0)
591 }
static bool all_timeouts_initialized
Definition: timeout.c:45
static void remove_timeout_index(int index)
Definition: timeout.c:124
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
#define disable_alarm()
Definition: timeout.c:65
volatile bool indicator
Definition: timeout.c:32
TimeoutId id
Definition: timeout.h:65
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:44
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:191
#define Assert(condition)
Definition: c.h:739
TimeoutId
Definition: timeout.h:23
static int find_active_timeout(TimeoutId id)
Definition: timeout.c:83
int i
static volatile int num_active_timeouts
Definition: timeout.c:51

◆ enable_timeout_after()

void enable_timeout_after ( TimeoutId  id,
int  delay_ms 
)

Definition at line 435 of file timeout.c.

References disable_alarm, enable_timeout(), timeout_params::fin_time, GetCurrentTimestamp(), now(), schedule_alarm(), and TimestampTzPlusMilliseconds.

Referenced by BackendInitialize(), enable_statement_timeout(), PerformAuthentication(), PostgresMain(), ProcSleep(), and ResolveRecoveryConflictWithBufferPin().

436 {
438  TimestampTz fin_time;
439 
440  /* Disable timeout interrupts for safety. */
441  disable_alarm();
442 
443  /* Queue the timeout at the appropriate time. */
444  now = GetCurrentTimestamp();
445  fin_time = TimestampTzPlusMilliseconds(now, delay_ms);
446  enable_timeout(id, now, fin_time);
447 
448  /* Set the timer interrupt. */
449  schedule_alarm(now);
450 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
int64 TimestampTz
Definition: timestamp.h:39
#define disable_alarm()
Definition: timeout.c:65
static void enable_timeout(TimeoutId id, TimestampTz now, TimestampTz fin_time)
Definition: timeout.c:145
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:191
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1547

◆ enable_timeout_at()

void enable_timeout_at ( TimeoutId  id,
TimestampTz  fin_time 
)

Definition at line 460 of file timeout.c.

References disable_alarm, enable_timeout(), GetCurrentTimestamp(), now(), and schedule_alarm().

461 {
463 
464  /* Disable timeout interrupts for safety. */
465  disable_alarm();
466 
467  /* Queue the timeout at the appropriate time. */
468  now = GetCurrentTimestamp();
469  enable_timeout(id, now, fin_time);
470 
471  /* Set the timer interrupt. */
472  schedule_alarm(now);
473 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
int64 TimestampTz
Definition: timestamp.h:39
#define disable_alarm()
Definition: timeout.c:65
static void enable_timeout(TimeoutId id, TimestampTz now, TimestampTz fin_time)
Definition: timeout.c:145
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:191
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1547

◆ enable_timeouts()

void enable_timeouts ( const EnableTimeoutParams timeouts,
int  count 
)

Definition at line 483 of file timeout.c.

References disable_alarm, elog, enable_timeout(), ERROR, timeout_params::fin_time, GetCurrentTimestamp(), i, EnableTimeoutParams::id, now(), schedule_alarm(), TimestampTzPlusMilliseconds, TMPARAM_AFTER, TMPARAM_AT, and generate_unaccent_rules::type.

Referenced by ProcSleep(), ResolveRecoveryConflictWithBufferPin(), and ResolveRecoveryConflictWithLock().

484 {
486  int i;
487 
488  /* Disable timeout interrupts for safety. */
489  disable_alarm();
490 
491  /* Queue the timeout(s) at the appropriate times. */
492  now = GetCurrentTimestamp();
493 
494  for (i = 0; i < count; i++)
495  {
496  TimeoutId id = timeouts[i].id;
497  TimestampTz fin_time;
498 
499  switch (timeouts[i].type)
500  {
501  case TMPARAM_AFTER:
502  fin_time = TimestampTzPlusMilliseconds(now,
503  timeouts[i].delay_ms);
504  enable_timeout(id, now, fin_time);
505  break;
506 
507  case TMPARAM_AT:
508  enable_timeout(id, now, timeouts[i].fin_time);
509  break;
510 
511  default:
512  elog(ERROR, "unrecognized timeout type %d",
513  (int) timeouts[i].type);
514  break;
515  }
516  }
517 
518  /* Set the timer interrupt. */
519  schedule_alarm(now);
520 }
TimeoutId id
Definition: timeout.h:54
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
int64 TimestampTz
Definition: timestamp.h:39
#define disable_alarm()
Definition: timeout.c:65
#define ERROR
Definition: elog.h:43
static void enable_timeout(TimeoutId id, TimestampTz now, TimestampTz fin_time)
Definition: timeout.c:145
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:191
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56
TimeoutId
Definition: timeout.h:23
#define elog(elevel,...)
Definition: elog.h:228
int i
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1547

◆ get_timeout_active()

bool get_timeout_active ( TimeoutId  id)

Definition at line 635 of file timeout.c.

References timeout_params::active.

Referenced by disable_statement_timeout(), and enable_statement_timeout().

636 {
637  return all_timeouts[id].active;
638 }
volatile bool active
Definition: timeout.c:31
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:44

◆ get_timeout_finish_time()

TimestampTz get_timeout_finish_time ( TimeoutId  id)

Definition at line 682 of file timeout.c.

References timeout_params::fin_time.

Referenced by ProcessInterrupts().

683 {
684  return all_timeouts[id].fin_time;
685 }
TimestampTz fin_time
Definition: timeout.c:38
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:44

◆ get_timeout_indicator()

bool get_timeout_indicator ( TimeoutId  id,
bool  reset_indicator 
)

Definition at line 648 of file timeout.c.

References timeout_params::indicator.

Referenced by ProcessInterrupts().

649 {
650  if (all_timeouts[id].indicator)
651  {
652  if (reset_indicator)
653  all_timeouts[id].indicator = false;
654  return true;
655  }
656  return false;
657 }
volatile bool indicator
Definition: timeout.c:32
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:44

◆ get_timeout_start_time()

TimestampTz get_timeout_start_time ( TimeoutId  id)

Definition at line 668 of file timeout.c.

References timeout_params::start_time.

Referenced by ProcSleep().

669 {
670  return all_timeouts[id].start_time;
671 }
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:44
TimestampTz start_time
Definition: timeout.c:37

◆ InitializeTimeouts()

void InitializeTimeouts ( void  )

Definition at line 346 of file timeout.c.

References timeout_params::active, all_timeouts_initialized, disable_alarm, timeout_params::fin_time, handle_sig_alarm(), i, timeout_params::index, timeout_params::indicator, MAX_TIMEOUTS, num_active_timeouts, pqsignal(), SIGALRM, timeout_params::start_time, and timeout_params::timeout_handler.

Referenced by AutoVacLauncherMain(), AutoVacWorkerMain(), BackendInitialize(), PostgresMain(), StartBackgroundWorker(), StartupProcessMain(), and WalSndSignals().

347 {
348  int i;
349 
350  /* Initialize, or re-initialize, all local state */
351  disable_alarm();
352 
354 
355  for (i = 0; i < MAX_TIMEOUTS; i++)
356  {
357  all_timeouts[i].index = i;
358  all_timeouts[i].active = false;
359  all_timeouts[i].indicator = false;
362  all_timeouts[i].fin_time = 0;
363  }
364 
366 
367  /* Now establish the signal handler */
369 }
static bool all_timeouts_initialized
Definition: timeout.c:45
TimestampTz fin_time
Definition: timeout.c:38
#define disable_alarm()
Definition: timeout.c:65
timeout_handler_proc timeout_handler
Definition: timeout.c:35
volatile bool indicator
Definition: timeout.c:32
volatile bool active
Definition: timeout.c:31
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:44
TimestampTz start_time
Definition: timeout.c:37
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
TimeoutId index
Definition: timeout.c:28
#define SIGALRM
Definition: win32_port.h:160
int i
static volatile int num_active_timeouts
Definition: timeout.c:51
static void handle_sig_alarm(SIGNAL_ARGS)
Definition: timeout.c:265

◆ RegisterTimeout()

TimeoutId RegisterTimeout ( TimeoutId  id,
timeout_handler_proc  handler 
)

Definition at line 380 of file timeout.c.

References all_timeouts_initialized, Assert, ereport, errcode(), errmsg(), FATAL, MAX_TIMEOUTS, timeout_params::timeout_handler, and USER_TIMEOUT.

Referenced by BackendInitialize(), InitPostgres(), and StartupProcessMain().

381 {
383 
384  /* There's no need to disable the signal handler here. */
385 
386  if (id >= USER_TIMEOUT)
387  {
388  /* Allocate a user-defined timeout reason */
389  for (id = USER_TIMEOUT; id < MAX_TIMEOUTS; id++)
390  if (all_timeouts[id].timeout_handler == NULL)
391  break;
392  if (id >= MAX_TIMEOUTS)
393  ereport(FATAL,
394  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
395  errmsg("cannot add more timeout reasons")));
396  }
397 
398  Assert(all_timeouts[id].timeout_handler == NULL);
399 
400  all_timeouts[id].timeout_handler = handler;
401 
402  return id;
403 }
static bool all_timeouts_initialized
Definition: timeout.c:45
int errcode(int sqlerrcode)
Definition: elog.c:608
timeout_handler_proc timeout_handler
Definition: timeout.c:35
#define FATAL
Definition: elog.h:52
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:44
#define ereport(elevel, rest)
Definition: elog.h:141
#define Assert(condition)
Definition: c.h:739
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ reschedule_timeouts()

void reschedule_timeouts ( void  )

Definition at line 415 of file timeout.c.

References all_timeouts_initialized, disable_alarm, GetCurrentTimestamp(), num_active_timeouts, and schedule_alarm().

Referenced by AbortSubTransaction(), and AbortTransaction().

416 {
417  /* For flexibility, allow this to be called before we're initialized. */
419  return;
420 
421  /* Disable timeout interrupts for safety. */
422  disable_alarm();
423 
424  /* Reschedule the interrupt, if any timeouts remain active. */
425  if (num_active_timeouts > 0)
427 }
static bool all_timeouts_initialized
Definition: timeout.c:45
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
#define disable_alarm()
Definition: timeout.c:65
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:191
static volatile int num_active_timeouts
Definition: timeout.c:51