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, CLIENT_CONNECTION_CHECK_TIMEOUT, USER_TIMEOUT, MAX_TIMEOUTS = USER_TIMEOUT + 10
}
 
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 43 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 
CLIENT_CONNECTION_CHECK_TIMEOUT 
USER_TIMEOUT 
MAX_TIMEOUTS 

Definition at line 23 of file timeout.h.

◆ TimeoutType

Enumerator
TMPARAM_AFTER 
TMPARAM_AT 

Definition at line 48 of file timeout.h.

49 {
52 } TimeoutType;
TimeoutType
Definition: timeout.h:48

Function Documentation

◆ disable_all_timeouts()

void disable_all_timeouts ( bool  keep_indicators)

Definition at line 687 of file timeout.c.

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

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

688 {
689  int i;
690 
691  disable_alarm();
692 
693  /*
694  * We used to disable the timer interrupt here, but in common usage
695  * patterns it's cheaper to leave it enabled; that may save us from having
696  * to enable it again shortly. See comments in schedule_alarm().
697  */
698 
700 
701  for (i = 0; i < MAX_TIMEOUTS; i++)
702  {
703  all_timeouts[i].active = false;
704  if (!keep_indicators)
705  all_timeouts[i].indicator = false;
706  }
707 }
#define disable_alarm()
Definition: timeout.c:68
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
int i
static volatile int num_active_timeouts
Definition: timeout.c:51

◆ disable_timeout()

void disable_timeout ( TimeoutId  id,
bool  keep_indicator 
)

Definition at line 621 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().

622 {
623  /* Assert request is sane */
625  Assert(all_timeouts[id].timeout_handler != NULL);
626 
627  /* Disable timeout interrupts for safety. */
628  disable_alarm();
629 
630  /* Find the timeout and remove it from the active list. */
631  if (all_timeouts[id].active)
633 
634  /* Mark it inactive, whether it was active or not. */
635  if (!keep_indicator)
636  all_timeouts[id].indicator = false;
637 
638  /* Reschedule the interrupt, if any timeouts remain active. */
639  if (num_active_timeouts > 0)
641 }
static bool all_timeouts_initialized
Definition: timeout.c:45
static void remove_timeout_index(int index)
Definition: timeout.c:135
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
#define disable_alarm()
Definition: timeout.c:68
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:206
#define Assert(condition)
Definition: c.h:804
static int find_active_timeout(TimeoutId id)
Definition: timeout.c:94
static volatile int num_active_timeouts
Definition: timeout.c:51

◆ disable_timeouts()

void disable_timeouts ( const DisableTimeoutParams timeouts,
int  count 
)

Definition at line 654 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().

655 {
656  int i;
657 
659 
660  /* Disable timeout interrupts for safety. */
661  disable_alarm();
662 
663  /* Cancel the timeout(s). */
664  for (i = 0; i < count; i++)
665  {
666  TimeoutId id = timeouts[i].id;
667 
668  Assert(all_timeouts[id].timeout_handler != NULL);
669 
670  if (all_timeouts[id].active)
672 
673  if (!timeouts[i].keep_indicator)
674  all_timeouts[id].indicator = false;
675  }
676 
677  /* Reschedule the interrupt, if any timeouts remain active. */
678  if (num_active_timeouts > 0)
680 }
static bool all_timeouts_initialized
Definition: timeout.c:45
static void remove_timeout_index(int index)
Definition: timeout.c:135
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
#define disable_alarm()
Definition: timeout.c:68
volatile bool indicator
Definition: timeout.c:32
TimeoutId id
Definition: timeout.h:67
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:44
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:206
#define Assert(condition)
Definition: c.h:804
TimeoutId
Definition: timeout.h:23
static int find_active_timeout(TimeoutId id)
Definition: timeout.c:94
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 524 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(), ProcessInterrupts(), ProcSleep(), and start_xact_command().

525 {
527  TimestampTz fin_time;
528 
529  /* Disable timeout interrupts for safety. */
530  disable_alarm();
531 
532  /* Queue the timeout at the appropriate time. */
533  now = GetCurrentTimestamp();
534  fin_time = TimestampTzPlusMilliseconds(now, delay_ms);
535  enable_timeout(id, now, fin_time);
536 
537  /* Set the timer interrupt. */
538  schedule_alarm(now);
539 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
int64 TimestampTz
Definition: timestamp.h:39
#define disable_alarm()
Definition: timeout.c:68
static void enable_timeout(TimeoutId id, TimestampTz now, TimestampTz fin_time)
Definition: timeout.c:156
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:206
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544

◆ enable_timeout_at()

void enable_timeout_at ( TimeoutId  id,
TimestampTz  fin_time 
)

Definition at line 549 of file timeout.c.

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

550 {
552 
553  /* Disable timeout interrupts for safety. */
554  disable_alarm();
555 
556  /* Queue the timeout at the appropriate time. */
557  now = GetCurrentTimestamp();
558  enable_timeout(id, now, fin_time);
559 
560  /* Set the timer interrupt. */
561  schedule_alarm(now);
562 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
int64 TimestampTz
Definition: timestamp.h:39
#define disable_alarm()
Definition: timeout.c:68
static void enable_timeout(TimeoutId id, TimestampTz now, TimestampTz fin_time)
Definition: timeout.c:156
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:206
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544

◆ enable_timeouts()

void enable_timeouts ( const EnableTimeoutParams timeouts,
int  count 
)

Definition at line 572 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().

573 {
575  int i;
576 
577  /* Disable timeout interrupts for safety. */
578  disable_alarm();
579 
580  /* Queue the timeout(s) at the appropriate times. */
581  now = GetCurrentTimestamp();
582 
583  for (i = 0; i < count; i++)
584  {
585  TimeoutId id = timeouts[i].id;
586  TimestampTz fin_time;
587 
588  switch (timeouts[i].type)
589  {
590  case TMPARAM_AFTER:
591  fin_time = TimestampTzPlusMilliseconds(now,
592  timeouts[i].delay_ms);
593  enable_timeout(id, now, fin_time);
594  break;
595 
596  case TMPARAM_AT:
597  enable_timeout(id, now, timeouts[i].fin_time);
598  break;
599 
600  default:
601  elog(ERROR, "unrecognized timeout type %d",
602  (int) timeouts[i].type);
603  break;
604  }
605  }
606 
607  /* Set the timer interrupt. */
608  schedule_alarm(now);
609 }
TimeoutId id
Definition: timeout.h:56
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
int64 TimestampTz
Definition: timestamp.h:39
#define disable_alarm()
Definition: timeout.c:68
#define ERROR
Definition: elog.h:46
static void enable_timeout(TimeoutId id, TimestampTz now, TimestampTz fin_time)
Definition: timeout.c:156
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:206
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56
TimeoutId
Definition: timeout.h:23
#define elog(elevel,...)
Definition: elog.h:232
int i
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544

◆ get_timeout_active()

bool get_timeout_active ( TimeoutId  id)

Definition at line 716 of file timeout.c.

References timeout_params::active.

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

717 {
718  return all_timeouts[id].active;
719 }
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 763 of file timeout.c.

References timeout_params::fin_time.

Referenced by ProcessInterrupts().

764 {
765  return all_timeouts[id].fin_time;
766 }
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 729 of file timeout.c.

References timeout_params::indicator.

Referenced by ProcessInterrupts().

730 {
731  if (all_timeouts[id].indicator)
732  {
733  if (reset_indicator)
734  all_timeouts[id].indicator = false;
735  return true;
736  }
737  return false;
738 }
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 749 of file timeout.c.

References timeout_params::start_time.

Referenced by ProcSleep().

750 {
751  return all_timeouts[id].start_time;
752 }
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 435 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().

436 {
437  int i;
438 
439  /* Initialize, or re-initialize, all local state */
440  disable_alarm();
441 
443 
444  for (i = 0; i < MAX_TIMEOUTS; i++)
445  {
446  all_timeouts[i].index = i;
447  all_timeouts[i].active = false;
448  all_timeouts[i].indicator = false;
451  all_timeouts[i].fin_time = 0;
452  }
453 
455 
456  /* Now establish the signal handler */
458 }
static bool all_timeouts_initialized
Definition: timeout.c:45
TimestampTz fin_time
Definition: timeout.c:38
#define disable_alarm()
Definition: timeout.c:68
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:165
int i
static volatile int num_active_timeouts
Definition: timeout.c:51
static void handle_sig_alarm(SIGNAL_ARGS)
Definition: timeout.c:348

◆ RegisterTimeout()

TimeoutId RegisterTimeout ( TimeoutId  id,
timeout_handler_proc  handler 
)

Definition at line 469 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().

470 {
472 
473  /* There's no need to disable the signal handler here. */
474 
475  if (id >= USER_TIMEOUT)
476  {
477  /* Allocate a user-defined timeout reason */
478  for (id = USER_TIMEOUT; id < MAX_TIMEOUTS; id++)
479  if (all_timeouts[id].timeout_handler == NULL)
480  break;
481  if (id >= MAX_TIMEOUTS)
482  ereport(FATAL,
483  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
484  errmsg("cannot add more timeout reasons")));
485  }
486 
487  Assert(all_timeouts[id].timeout_handler == NULL);
488 
489  all_timeouts[id].timeout_handler = handler;
490 
491  return id;
492 }
static bool all_timeouts_initialized
Definition: timeout.c:45
int errcode(int sqlerrcode)
Definition: elog.c:698
timeout_handler_proc timeout_handler
Definition: timeout.c:35
#define FATAL
Definition: elog.h:49
static timeout_params all_timeouts[MAX_TIMEOUTS]
Definition: timeout.c:44
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ reschedule_timeouts()

void reschedule_timeouts ( void  )

Definition at line 504 of file timeout.c.

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

Referenced by AbortSubTransaction(), and AbortTransaction().

505 {
506  /* For flexibility, allow this to be called before we're initialized. */
508  return;
509 
510  /* Disable timeout interrupts for safety. */
511  disable_alarm();
512 
513  /* Reschedule the interrupt, if any timeouts remain active. */
514  if (num_active_timeouts > 0)
516 }
static bool all_timeouts_initialized
Definition: timeout.c:45
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
#define disable_alarm()
Definition: timeout.c:68
static void schedule_alarm(TimestampTz now)
Definition: timeout.c:206
static volatile int num_active_timeouts
Definition: timeout.c:51