diff options
author | Michael Paquier <michael@paquier.xyz> | 2019-12-02 11:18:56 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2019-12-02 11:18:56 +0900 |
commit | a4fd3aa719e8f97299dfcf1a8f79b3017e2b8d8b (patch) | |
tree | 5e2abc311e6f09a3788dd2989e8ed56555975291 /src/bin/scripts/common.c | |
parent | c01ac6dcba0aa65ad237c3af4a67bc70da8e4b0e (diff) | |
download | postgresql-a4fd3aa719e8f97299dfcf1a8f79b3017e2b8d8b.tar.gz postgresql-a4fd3aa719e8f97299dfcf1a8f79b3017e2b8d8b.zip |
Refactor query cancellation code into src/fe_utils/
Originally, this code was duplicated in src/bin/psql/ and
src/bin/scripts/, but it can be useful for other frontend applications,
like pgbench. This refactoring offers the possibility to setup a custom
callback which would get called in the signal handler for SIGINT or when
the interruption console events happen on Windows.
Author: Fabien Coelho, with contributions from Michael Paquier
Reviewed-by: Álvaro Herrera, Ibrar Ahmed
Discussion: https://postgr.es/m/alpine.DEB.2.21.1910311939430.27369@lancre
Diffstat (limited to 'src/bin/scripts/common.c')
-rw-r--r-- | src/bin/scripts/common.c | 147 |
1 files changed, 0 insertions, 147 deletions
diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c index 1b38a1da494..d2a7547441a 100644 --- a/src/bin/scripts/common.c +++ b/src/bin/scripts/common.c @@ -24,14 +24,6 @@ #define ERRCODE_UNDEFINED_TABLE "42P01" - -static PGcancel *volatile cancelConn = NULL; -bool CancelRequested = false; - -#ifdef WIN32 -static CRITICAL_SECTION cancelConnLock; -#endif - /* * Provide strictly harmonized handling of --help and --version * options. @@ -465,142 +457,3 @@ yesno_prompt(const char *question) _(PG_YESLETTER), _(PG_NOLETTER)); } } - -/* - * SetCancelConn - * - * Set cancelConn to point to the current database connection. - */ -void -SetCancelConn(PGconn *conn) -{ - PGcancel *oldCancelConn; - -#ifdef WIN32 - EnterCriticalSection(&cancelConnLock); -#endif - - /* Free the old one if we have one */ - oldCancelConn = cancelConn; - - /* be sure handle_sigint doesn't use pointer while freeing */ - cancelConn = NULL; - - if (oldCancelConn != NULL) - PQfreeCancel(oldCancelConn); - - cancelConn = PQgetCancel(conn); - -#ifdef WIN32 - LeaveCriticalSection(&cancelConnLock); -#endif -} - -/* - * ResetCancelConn - * - * Free the current cancel connection, if any, and set to NULL. - */ -void -ResetCancelConn(void) -{ - PGcancel *oldCancelConn; - -#ifdef WIN32 - EnterCriticalSection(&cancelConnLock); -#endif - - oldCancelConn = cancelConn; - - /* be sure handle_sigint doesn't use pointer while freeing */ - cancelConn = NULL; - - if (oldCancelConn != NULL) - PQfreeCancel(oldCancelConn); - -#ifdef WIN32 - LeaveCriticalSection(&cancelConnLock); -#endif -} - -#ifndef WIN32 -/* - * Handle interrupt signals by canceling the current command, if a cancelConn - * is set. - */ -static void -handle_sigint(SIGNAL_ARGS) -{ - int save_errno = errno; - char errbuf[256]; - - /* Send QueryCancel if we are processing a database query */ - if (cancelConn != NULL) - { - if (PQcancel(cancelConn, errbuf, sizeof(errbuf))) - { - CancelRequested = true; - fprintf(stderr, _("Cancel request sent\n")); - } - else - fprintf(stderr, _("Could not send cancel request: %s"), errbuf); - } - else - CancelRequested = true; - - errno = save_errno; /* just in case the write changed it */ -} - -void -setup_cancel_handler(void) -{ - pqsignal(SIGINT, handle_sigint); -} -#else /* WIN32 */ - -/* - * Console control handler for Win32. Note that the control handler will - * execute on a *different thread* than the main one, so we need to do - * proper locking around those structures. - */ -static BOOL WINAPI -consoleHandler(DWORD dwCtrlType) -{ - char errbuf[256]; - - if (dwCtrlType == CTRL_C_EVENT || - dwCtrlType == CTRL_BREAK_EVENT) - { - /* Send QueryCancel if we are processing a database query */ - EnterCriticalSection(&cancelConnLock); - if (cancelConn != NULL) - { - if (PQcancel(cancelConn, errbuf, sizeof(errbuf))) - { - fprintf(stderr, _("Cancel request sent\n")); - CancelRequested = true; - } - else - fprintf(stderr, _("Could not send cancel request: %s"), errbuf); - } - else - CancelRequested = true; - - LeaveCriticalSection(&cancelConnLock); - - return TRUE; - } - else - /* Return FALSE for any signals not being handled */ - return FALSE; -} - -void -setup_cancel_handler(void) -{ - InitializeCriticalSection(&cancelConnLock); - - SetConsoleCtrlHandler(consoleHandler, TRUE); -} - -#endif /* WIN32 */ |