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 ,
  IDLE_SESSION_TIMEOUT , IDLE_STATS_UPDATE_TIMEOUT , CLIENT_CONNECTION_CHECK_TIMEOUT , STARTUP_PROGRESS_TIMEOUT ,
  USER_TIMEOUT , MAX_TIMEOUTS = USER_TIMEOUT + 10
}
 
enum  TimeoutType { TMPARAM_AFTER , TMPARAM_AT , TMPARAM_EVERY }
 

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_every (TimeoutId id, TimestampTz fin_time, 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 45 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 
IDLE_SESSION_TIMEOUT 
IDLE_STATS_UPDATE_TIMEOUT 
CLIENT_CONNECTION_CHECK_TIMEOUT 
STARTUP_PROGRESS_TIMEOUT 
USER_TIMEOUT 
MAX_TIMEOUTS 

Definition at line 23 of file timeout.h.

24 {
25  /* Predefined timeout reasons */
38  /* First user-definable timeout reason */
40  /* Maximum number of timeout reasons */
42 } TimeoutId;
TimeoutId
Definition: timeout.h:24
@ STARTUP_PROGRESS_TIMEOUT
Definition: timeout.h:37
@ STANDBY_LOCK_TIMEOUT
Definition: timeout.h:32
@ IDLE_SESSION_TIMEOUT
Definition: timeout.h:34
@ STARTUP_PACKET_TIMEOUT
Definition: timeout.h:26
@ IDLE_IN_TRANSACTION_SESSION_TIMEOUT
Definition: timeout.h:33
@ LOCK_TIMEOUT
Definition: timeout.h:28
@ STANDBY_DEADLOCK_TIMEOUT
Definition: timeout.h:30
@ STATEMENT_TIMEOUT
Definition: timeout.h:29
@ DEADLOCK_TIMEOUT
Definition: timeout.h:27
@ MAX_TIMEOUTS
Definition: timeout.h:41
@ USER_TIMEOUT
Definition: timeout.h:39
@ IDLE_STATS_UPDATE_TIMEOUT
Definition: timeout.h:35
@ CLIENT_CONNECTION_CHECK_TIMEOUT
Definition: timeout.h:36
@ STANDBY_TIMEOUT
Definition: timeout.h:31

◆ TimeoutType

Enumerator
TMPARAM_AFTER 
TMPARAM_AT 
TMPARAM_EVERY 

Definition at line 50 of file timeout.h.

51 {
53  TMPARAM_AT,
55 } TimeoutType;
TimeoutType
Definition: timeout.h:51
@ TMPARAM_AT
Definition: timeout.h:53
@ TMPARAM_EVERY
Definition: timeout.h:54
@ TMPARAM_AFTER
Definition: timeout.h:52

Function Documentation

◆ disable_all_timeouts()

void disable_all_timeouts ( bool  keep_indicators)

Definition at line 755 of file timeout.c.

756 {
757  int i;
758 
759  disable_alarm();
760 
761  /*
762  * We used to disable the timer interrupt here, but in common usage
763  * patterns it's cheaper to leave it enabled; that may save us from having
764  * to enable it again shortly. See comments in schedule_alarm().
765  */
766 
768 
769  for (i = 0; i < MAX_TIMEOUTS; i++)
770  {
771  all_timeouts[i].active = false;
772  if (!keep_indicators)
773  all_timeouts[i].indicator = false;
774  }
775 }
int i
Definition: isn.c:73
volatile bool active
Definition: timeout.c:31
volatile bool indicator
Definition: timeout.c:32
#define disable_alarm()
Definition: timeout.c:69
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:45
static volatile int num_active_timeouts
Definition: timeout.c:52

References timeout_params::active, all_timeouts, disable_alarm, i, timeout_params::indicator, MAX_TIMEOUTS, and num_active_timeouts.

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

◆ disable_timeout()

void disable_timeout ( TimeoutId  id,
bool  keep_indicator 
)

Definition at line 689 of file timeout.c.

690 {
691  /* Assert request is sane */
693  Assert(all_timeouts[id].timeout_handler != NULL);
694 
695  /* Disable timeout interrupts for safety. */
696  disable_alarm();
697 
698  /* Find the timeout and remove it from the active list. */
699  if (all_timeouts[id].active)
701 
702  /* Mark it inactive, whether it was active or not. */
703  if (!keep_indicator)
704  all_timeouts[id].indicator = false;
705 
706  /* Reschedule the interrupt, if any timeouts remain active. */
707  if (num_active_timeouts > 0)
709 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
Assert(fmt[strlen(fmt) - 1] !='\n')
static int find_active_timeout(TimeoutId id)
Definition: timeout.c:96
static bool all_timeouts_initialized
Definition: timeout.c:46
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:210
static void remove_timeout_index(int index)
Definition: timeout.c:137

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

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

◆ disable_timeouts()

void disable_timeouts ( const DisableTimeoutParams timeouts,
int  count 
)

Definition at line 722 of file timeout.c.

723 {
724  int i;
725 
727 
728  /* Disable timeout interrupts for safety. */
729  disable_alarm();
730 
731  /* Cancel the timeout(s). */
732  for (i = 0; i < count; i++)
733  {
734  TimeoutId id = timeouts[i].id;
735 
736  Assert(all_timeouts[id].timeout_handler != NULL);
737 
738  if (all_timeouts[id].active)
740 
741  if (!timeouts[i].keep_indicator)
742  all_timeouts[id].indicator = false;
743  }
744 
745  /* Reschedule the interrupt, if any timeouts remain active. */
746  if (num_active_timeouts > 0)
748 }
TimeoutId id
Definition: timeout.h:70

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

Referenced by LockErrorCleanup(), and ProcSleep().

◆ enable_timeout_after()

void enable_timeout_after ( TimeoutId  id,
int  delay_ms 
)

Definition at line 564 of file timeout.c.

565 {
567  TimestampTz fin_time;
568 
569  /* Disable timeout interrupts for safety. */
570  disable_alarm();
571 
572  /* Queue the timeout at the appropriate time. */
574  fin_time = TimestampTzPlusMilliseconds(now, delay_ms);
575  enable_timeout(id, now, fin_time, 0);
576 
577  /* Set the timer interrupt. */
579 }
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1538
int64 TimestampTz
Definition: timestamp.h:39
static void enable_timeout(TimeoutId id, TimestampTz now, TimestampTz fin_time, int interval_in_ms)
Definition: timeout.c:158
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56

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

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

◆ enable_timeout_at()

void enable_timeout_at ( TimeoutId  id,
TimestampTz  fin_time 
)

Definition at line 611 of file timeout.c.

612 {
614 
615  /* Disable timeout interrupts for safety. */
616  disable_alarm();
617 
618  /* Queue the timeout at the appropriate time. */
620  enable_timeout(id, now, fin_time, 0);
621 
622  /* Set the timer interrupt. */
624 }

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

◆ enable_timeout_every()

void enable_timeout_every ( TimeoutId  id,
TimestampTz  fin_time,
int  delay_ms 
)

Definition at line 588 of file timeout.c.

589 {
591 
592  /* Disable timeout interrupts for safety. */
593  disable_alarm();
594 
595  /* Queue the timeout at the appropriate time. */
597  enable_timeout(id, now, fin_time, delay_ms);
598 
599  /* Set the timer interrupt. */
601 }

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

Referenced by begin_startup_progress_phase().

◆ enable_timeouts()

void enable_timeouts ( const EnableTimeoutParams timeouts,
int  count 
)

Definition at line 634 of file timeout.c.

635 {
637  int i;
638 
639  /* Disable timeout interrupts for safety. */
640  disable_alarm();
641 
642  /* Queue the timeout(s) at the appropriate times. */
644 
645  for (i = 0; i < count; i++)
646  {
647  TimeoutId id = timeouts[i].id;
648  TimestampTz fin_time;
649 
650  switch (timeouts[i].type)
651  {
652  case TMPARAM_AFTER:
653  fin_time = TimestampTzPlusMilliseconds(now,
654  timeouts[i].delay_ms);
655  enable_timeout(id, now, fin_time, 0);
656  break;
657 
658  case TMPARAM_AT:
659  enable_timeout(id, now, timeouts[i].fin_time, 0);
660  break;
661 
662  case TMPARAM_EVERY:
663  fin_time = TimestampTzPlusMilliseconds(now,
664  timeouts[i].delay_ms);
665  enable_timeout(id, now, fin_time, timeouts[i].delay_ms);
666  break;
667 
668  default:
669  elog(ERROR, "unrecognized timeout type %d",
670  (int) timeouts[i].type);
671  break;
672  }
673  }
674 
675  /* Set the timer interrupt. */
677 }
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
TimeoutId id
Definition: timeout.h:59

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

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

◆ get_timeout_active()

bool get_timeout_active ( TimeoutId  id)

Definition at line 784 of file timeout.c.

785 {
786  return all_timeouts[id].active;
787 }

References timeout_params::active, and all_timeouts.

Referenced by disable_statement_timeout(), enable_statement_timeout(), and start_xact_command().

◆ get_timeout_finish_time()

TimestampTz get_timeout_finish_time ( TimeoutId  id)

Definition at line 831 of file timeout.c.

832 {
833  return all_timeouts[id].fin_time;
834 }
TimestampTz fin_time
Definition: timeout.c:38

References all_timeouts, and timeout_params::fin_time.

Referenced by ProcessInterrupts().

◆ get_timeout_indicator()

bool get_timeout_indicator ( TimeoutId  id,
bool  reset_indicator 
)

Definition at line 797 of file timeout.c.

798 {
799  if (all_timeouts[id].indicator)
800  {
801  if (reset_indicator)
802  all_timeouts[id].indicator = false;
803  return true;
804  }
805  return false;
806 }

References all_timeouts, and timeout_params::indicator.

Referenced by ProcessInterrupts().

◆ get_timeout_start_time()

TimestampTz get_timeout_start_time ( TimeoutId  id)

Definition at line 817 of file timeout.c.

818 {
819  return all_timeouts[id].start_time;
820 }
TimestampTz start_time
Definition: timeout.c:37

References all_timeouts, and timeout_params::start_time.

Referenced by ProcSleep().

◆ InitializeTimeouts()

void InitializeTimeouts ( void  )

Definition at line 474 of file timeout.c.

475 {
476  int i;
477 
478  /* Initialize, or re-initialize, all local state */
479  disable_alarm();
480 
482 
483  for (i = 0; i < MAX_TIMEOUTS; i++)
484  {
485  all_timeouts[i].index = i;
486  all_timeouts[i].active = false;
487  all_timeouts[i].indicator = false;
490  all_timeouts[i].fin_time = 0;
492  }
493 
495 
496  /* Now establish the signal handler */
498 }
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:180
int interval_in_ms
Definition: timeout.c:39
timeout_handler_proc timeout_handler
Definition: timeout.c:35
TimeoutId index
Definition: timeout.c:28
static void handle_sig_alarm(SIGNAL_ARGS)
Definition: timeout.c:364
#define SIGALRM
Definition: win32_port.h:173

References timeout_params::active, all_timeouts, all_timeouts_initialized, disable_alarm, timeout_params::fin_time, handle_sig_alarm(), i, timeout_params::index, timeout_params::indicator, timeout_params::interval_in_ms, 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().

◆ RegisterTimeout()

TimeoutId RegisterTimeout ( TimeoutId  id,
timeout_handler_proc  handler 
)

Definition at line 509 of file timeout.c.

510 {
512 
513  /* There's no need to disable the signal handler here. */
514 
515  if (id >= USER_TIMEOUT)
516  {
517  /* Allocate a user-defined timeout reason */
518  for (id = USER_TIMEOUT; id < MAX_TIMEOUTS; id++)
519  if (all_timeouts[id].timeout_handler == NULL)
520  break;
521  if (id >= MAX_TIMEOUTS)
522  ereport(FATAL,
523  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
524  errmsg("cannot add more timeout reasons")));
525  }
526 
527  Assert(all_timeouts[id].timeout_handler == NULL);
528 
529  all_timeouts[id].timeout_handler = handler;
530 
531  return id;
532 }
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define FATAL
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:143

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

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

◆ reschedule_timeouts()

void reschedule_timeouts ( void  )

Definition at line 544 of file timeout.c.

545 {
546  /* For flexibility, allow this to be called before we're initialized. */
548  return;
549 
550  /* Disable timeout interrupts for safety. */
551  disable_alarm();
552 
553  /* Reschedule the interrupt, if any timeouts remain active. */
554  if (num_active_timeouts > 0)
556 }

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

Referenced by AbortSubTransaction(), and AbortTransaction().