summaryrefslogtreecommitdiff
path: root/src/backend/tcop/postgres.c
diff options
context:
space:
mode:
authorAlvaro Herrera2012-07-16 22:43:21 +0000
committerAlvaro Herrera2012-07-17 02:55:33 +0000
commitf34c68f09671c4566854c7e20e9253d4f335c0b0 (patch)
treedcfea396da6622644aca0cf35799e6cc13fa1137 /src/backend/tcop/postgres.c
parentdd16f9480ac67ab0c6b0102d110cd5121ed9ab46 (diff)
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
Diffstat (limited to 'src/backend/tcop/postgres.c')
-rw-r--r--src/backend/tcop/postgres.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index f696375cabc..37dfa18c1d0 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -72,6 +72,7 @@
#include "utils/memutils.h"
#include "utils/ps_status.h"
#include "utils/snapmgr.h"
+#include "utils/timeout.h"
#include "utils/timestamp.h"
#include "mb/pg_wchar.h"
@@ -2396,9 +2397,9 @@ start_xact_command(void)
/* Set statement timeout running, if any */
/* NB: this mustn't be enabled until we are within an xact */
if (StatementTimeout > 0)
- enable_sig_alarm(StatementTimeout, true);
+ enable_timeout_after(STATEMENT_TIMEOUT, StatementTimeout);
else
- cancel_from_timeout = false;
+ disable_timeout(STATEMENT_TIMEOUT, false);
xact_started = true;
}
@@ -2410,7 +2411,7 @@ finish_xact_command(void)
if (xact_started)
{
/* Cancel any active statement timeout before committing */
- disable_sig_alarm(true);
+ disable_timeout(STATEMENT_TIMEOUT, false);
/* Now commit the command */
ereport(DEBUG3,
@@ -2891,7 +2892,7 @@ ProcessInterrupts(void)
(errcode(ERRCODE_QUERY_CANCELED),
errmsg("canceling authentication due to timeout")));
}
- if (cancel_from_timeout)
+ if (get_timeout_indicator(STATEMENT_TIMEOUT))
{
ImmediateInterruptOK = false; /* not idle anymore */
DisableNotifyInterrupt();
@@ -3614,7 +3615,7 @@ PostgresMain(int argc, char *argv[], const char *username)
pqsignal(SIGQUIT, quickdie); /* hard crash time */
else
pqsignal(SIGQUIT, die); /* cancel current query and exit */
- pqsignal(SIGALRM, handle_sig_alarm); /* timeout conditions */
+ InitializeTimeouts(); /* establishes SIGALRM handler */
/*
* Ignore failure to write to frontend. Note: if frontend closes
@@ -3802,10 +3803,10 @@ PostgresMain(int argc, char *argv[], const char *username)
/*
* Forget any pending QueryCancel request, since we're returning to
- * the idle loop anyway, and cancel the statement timer if running.
+ * the idle loop anyway, and cancel any active timeout requests.
*/
QueryCancelPending = false;
- disable_sig_alarm(true);
+ disable_all_timeouts(false);
QueryCancelPending = false; /* again in case timeout occurred */
/*