PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 ,
  TRANSACTION_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 46 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 
TRANSACTION_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 */
39 /* First user-definable timeout reason */
41 /* Maximum number of timeout reasons */
43} TimeoutId;
TimeoutId
Definition: timeout.h:24
@ STARTUP_PROGRESS_TIMEOUT
Definition: timeout.h:38
@ STANDBY_LOCK_TIMEOUT
Definition: timeout.h:32
@ IDLE_SESSION_TIMEOUT
Definition: timeout.h:35
@ 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:42
@ TRANSACTION_TIMEOUT
Definition: timeout.h:34
@ USER_TIMEOUT
Definition: timeout.h:40
@ IDLE_STATS_UPDATE_TIMEOUT
Definition: timeout.h:36
@ CLIENT_CONNECTION_CHECK_TIMEOUT
Definition: timeout.h:37
@ STANDBY_TIMEOUT
Definition: timeout.h:31

◆ TimeoutType

Enumerator
TMPARAM_AFTER 
TMPARAM_AT 
TMPARAM_EVERY 

Definition at line 51 of file timeout.h.

52{
TimeoutType
Definition: timeout.h:52
@ TMPARAM_AT
Definition: timeout.h:54
@ TMPARAM_EVERY
Definition: timeout.h:55
@ TMPARAM_AFTER
Definition: timeout.h:53

Function Documentation

◆ disable_all_timeouts()

void disable_all_timeouts ( bool  keep_indicators)

Definition at line 751 of file timeout.c.

752{
753 int i;
754
756
757 /*
758 * We used to disable the timer interrupt here, but in common usage
759 * patterns it's cheaper to leave it enabled; that may save us from having
760 * to enable it again shortly. See comments in schedule_alarm().
761 */
762
764
765 for (i = 0; i < MAX_TIMEOUTS; i++)
766 {
767 all_timeouts[i].active = false;
768 if (!keep_indicators)
769 all_timeouts[i].indicator = false;
770 }
771}
int i
Definition: isn.c:72
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 pgarch_archiveXlog(), PostgresMain(), ResolveRecoveryConflictWithBufferPin(), and ResolveRecoveryConflictWithLock().

◆ disable_timeout()

void disable_timeout ( TimeoutId  id,
bool  keep_indicator 
)

Definition at line 685 of file timeout.c.

686{
687 /* Assert request is sane */
689 Assert(all_timeouts[id].timeout_handler != NULL);
690
691 /* Disable timeout interrupts for safety. */
693
694 /* Find the timeout and remove it from the active list. */
695 if (all_timeouts[id].active)
697
698 /* Mark it inactive, whether it was active or not. */
699 if (!keep_indicator)
700 all_timeouts[id].indicator = false;
701
702 /* Reschedule the interrupt, if any timeouts remain active. */
703 if (num_active_timeouts > 0)
705}
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1644
#define Assert(condition)
Definition: c.h:812
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 AbortTransaction(), assign_transaction_timeout(), BackendInitialize(), CommitTransaction(), disable_startup_progress_timeout(), disable_statement_timeout(), enable_statement_timeout(), PerformAuthentication(), PostgresMain(), PrepareTransaction(), and ProcSleep().

◆ disable_timeouts()

void disable_timeouts ( const DisableTimeoutParams timeouts,
int  count 
)

Definition at line 718 of file timeout.c.

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

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 560 of file timeout.c.

561{
563 TimestampTz fin_time;
564
565 /* Disable timeout interrupts for safety. */
567
568 /* Queue the timeout at the appropriate time. */
570 fin_time = TimestampTzPlusMilliseconds(now, delay_ms);
571 enable_timeout(id, now, fin_time, 0);
572
573 /* Set the timer interrupt. */
575}
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1608
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:85

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

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

◆ enable_timeout_at()

void enable_timeout_at ( TimeoutId  id,
TimestampTz  fin_time 
)

Definition at line 607 of file timeout.c.

608{
610
611 /* Disable timeout interrupts for safety. */
613
614 /* Queue the timeout at the appropriate time. */
616 enable_timeout(id, now, fin_time, 0);
617
618 /* Set the timer interrupt. */
620}

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 584 of file timeout.c.

585{
587
588 /* Disable timeout interrupts for safety. */
590
591 /* Queue the timeout at the appropriate time. */
593 enable_timeout(id, now, fin_time, delay_ms);
594
595 /* Set the timer interrupt. */
597}

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

Referenced by enable_startup_progress_timeout().

◆ enable_timeouts()

void enable_timeouts ( const EnableTimeoutParams timeouts,
int  count 
)

Definition at line 630 of file timeout.c.

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

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

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

◆ get_timeout_active()

bool get_timeout_active ( TimeoutId  id)

◆ get_timeout_finish_time()

TimestampTz get_timeout_finish_time ( TimeoutId  id)

Definition at line 827 of file timeout.c.

828{
829 return all_timeouts[id].fin_time;
830}
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 793 of file timeout.c.

794{
795 if (all_timeouts[id].indicator)
796 {
797 if (reset_indicator)
798 all_timeouts[id].indicator = false;
799 return true;
800 }
801 return false;
802}

References all_timeouts, and timeout_params::indicator.

Referenced by ProcessInterrupts().

◆ get_timeout_start_time()

TimestampTz get_timeout_start_time ( TimeoutId  id)

Definition at line 813 of file timeout.c.

814{
815 return all_timeouts[id].start_time;
816}
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 470 of file timeout.c.

471{
472 int i;
473
474 /* Initialize, or re-initialize, all local state */
476
478
479 for (i = 0; i < MAX_TIMEOUTS; i++)
480 {
482 all_timeouts[i].active = false;
483 all_timeouts[i].indicator = false;
488 }
489
491
492 /* Now establish the signal handler */
494}
pqsigfunc pqsignal(int signo, pqsigfunc func)
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:174

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 AutoVacWorkerMain(), BackendInitialize(), BackgroundWorkerMain(), PostgresMain(), ReplSlotSyncWorkerMain(), StartupProcessMain(), and WalSndSignals().

◆ RegisterTimeout()

TimeoutId RegisterTimeout ( TimeoutId  id,
timeout_handler_proc  handler 
)

Definition at line 505 of file timeout.c.

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

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 540 of file timeout.c.

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

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

Referenced by AbortSubTransaction(), and AbortTransaction().