mirror of
https://github.com/postgres/postgres.git
synced 2025-08-28 18:48:04 +03:00
Introduce timeout handling framework
Management of timeouts was getting a little cumbersome; what we
originally had was more than enough back when we were only concerned
about deadlocks and query cancel; however, when we added timeouts for
standby processes, the code got considerably messier. Since there are
plans to add more complex timeouts, this seems a good time to introduce
a central timeout handling module.
External modules register their timeout handlers during process
initialization, and later enable and disable them as they see fit using
a simple API; timeout.c is in charge of keeping track of which timeouts
are in effect at any time, installing a common SIGALRM signal handler,
and calling setitimer() as appropriate to ensure timely firing of
external handlers.
timeout.c additionally supports pluggable modules to add their own
timeouts, though this capability isn't exercised anywhere yet.
Additionally, as of this commit, walsender processes are aware of
timeouts; we had a preexisting bug there that made those ignore SIGALRM,
thus being subject to unhandled deadlocks, particularly during the
authentication phase. This has already been fixed in back branches in
commit 0bf8eb2a
, which see for more details.
Main author: Zoltán Böszörményi
Some review and cleanup by Álvaro Herrera
Extensive reworking by Tom Lane
This commit is contained in:
54
src/include/utils/timeout.h
Normal file
54
src/include/utils/timeout.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* timeout.h
|
||||
* Routines to multiplex SIGALRM interrupts for multiple timeout reasons.
|
||||
*
|
||||
*
|
||||
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* src/include/utils/timeout.h
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#ifndef TIMEOUT_H
|
||||
#define TIMEOUT_H
|
||||
|
||||
#include "datatype/timestamp.h"
|
||||
|
||||
/*
|
||||
* Identifiers for timeout reasons. Note that in case multiple timeouts
|
||||
* trigger at the same time, they are serviced in the order of this enum.
|
||||
*/
|
||||
typedef enum TimeoutId
|
||||
{
|
||||
/* Predefined timeout reasons */
|
||||
STARTUP_PACKET_TIMEOUT,
|
||||
DEADLOCK_TIMEOUT,
|
||||
STATEMENT_TIMEOUT,
|
||||
STANDBY_DEADLOCK_TIMEOUT,
|
||||
STANDBY_TIMEOUT,
|
||||
/* First user-definable timeout reason */
|
||||
USER_TIMEOUT,
|
||||
/* Maximum number of timeout reasons */
|
||||
MAX_TIMEOUTS = 16
|
||||
} TimeoutId;
|
||||
|
||||
/* callback function signature */
|
||||
typedef void (*timeout_handler) (void);
|
||||
|
||||
/* timeout setup */
|
||||
extern void InitializeTimeouts(void);
|
||||
extern TimeoutId RegisterTimeout(TimeoutId id, timeout_handler handler);
|
||||
|
||||
/* timeout operation */
|
||||
extern void enable_timeout_after(TimeoutId id, int delay_ms);
|
||||
extern void enable_timeout_at(TimeoutId id, TimestampTz fin_time);
|
||||
extern void disable_timeout(TimeoutId id, bool keep_indicator);
|
||||
extern void disable_all_timeouts(bool keep_indicators);
|
||||
|
||||
/* accessors */
|
||||
extern bool get_timeout_indicator(TimeoutId id);
|
||||
extern TimestampTz get_timeout_start_time(TimeoutId id);
|
||||
|
||||
#endif /* TIMEOUT_H */
|
Reference in New Issue
Block a user