Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita4fd3aa

Browse files
committed
Refactor query cancellation code into src/fe_utils/
Originally, this code was duplicated in src/bin/psql/ andsrc/bin/scripts/, but it can be useful for other frontend applications,like pgbench. This refactoring offers the possibility to setup a customcallback which would get called in the signal handler for SIGINT or whenthe interruption console events happen on Windows.Author: Fabien Coelho, with contributions from Michael PaquierReviewed-by: Álvaro Herrera, Ibrar AhmedDiscussion:https://postgr.es/m/alpine.DEB.2.21.1910311939430.27369@lancre
1 parentc01ac6d commita4fd3aa

File tree

14 files changed

+294
-326
lines changed

14 files changed

+294
-326
lines changed

‎src/bin/psql/command.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include"copy.h"
3030
#include"crosstabview.h"
3131
#include"describe.h"
32+
#include"fe_utils/cancel.h"
3233
#include"fe_utils/print.h"
3334
#include"fe_utils/string_utils.h"
3435
#include"help.h"

‎src/bin/psql/common.c

Lines changed: 26 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include"common/logging.h"
2424
#include"copy.h"
2525
#include"crosstabview.h"
26+
#include"fe_utils/cancel.h"
2627
#include"fe_utils/mbprint.h"
2728
#include"fe_utils/string_utils.h"
2829
#include"portability/instr_time.h"
@@ -228,58 +229,28 @@ NoticeProcessor(void *arg, const char *message)
228229
* Code to support query cancellation
229230
*
230231
* Before we start a query, we enable the SIGINT signal catcher to send a
231-
* cancel request to the backend. Note that sending the cancel directly from
232-
* the signal handler is safe because PQcancel() is written to make it
233-
* so. We use write() to report to stderr because it's better to use simple
234-
* facilities in a signal handler.
235-
*
236-
* On win32, the signal canceling happens on a separate thread, because
237-
* that's how SetConsoleCtrlHandler works. The PQcancel function is safe
238-
* for this (unlike PQrequestCancel). However, a CRITICAL_SECTION is required
239-
* to protect the PGcancel structure against being changed while the signal
240-
* thread is using it.
232+
* cancel request to the backend.
241233
*
242234
* SIGINT is supposed to abort all long-running psql operations, not only
243235
* database queries. In most places, this is accomplished by checking
244-
*cancel_pressed during long-running loops. However, that won't work when
236+
*CancelRequested during long-running loops. However, that won't work when
245237
* blocked on user input (in readline() or fgets()). In those places, we
246238
* set sigint_interrupt_enabled true while blocked, instructing the signal
247239
* catcher to longjmp through sigint_interrupt_jmp. We assume readline and
248-
* fgets are coded to handle possible interruption. (XXX currently this does
249-
* not work on win32, so control-C is less useful there)
240+
* fgets are coded to handle possible interruption.
241+
*
242+
* On Windows, currently this does not work, so control-C is less useful
243+
* there, and the callback is just a no-op.
250244
*/
251245
volatileboolsigint_interrupt_enabled= false;
252246

253247
sigjmp_bufsigint_interrupt_jmp;
254248

255-
staticPGcancel*volatilecancelConn=NULL;
256-
257-
#ifdefWIN32
258-
staticCRITICAL_SECTIONcancelConnLock;
259-
#endif
260-
261-
/*
262-
* Write a simple string to stderr --- must be safe in a signal handler.
263-
* We ignore the write() result since there's not much we could do about it.
264-
* Certain compilers make that harder than it ought to be.
265-
*/
266-
#definewrite_stderr(str) \
267-
do { \
268-
const char *str_ = (str); \
269-
intrc_; \
270-
rc_ = write(fileno(stderr), str_, strlen(str_)); \
271-
(void) rc_; \
272-
} while (0)
273-
274-
275249
#ifndefWIN32
276250

277251
staticvoid
278-
handle_sigint(SIGNAL_ARGS)
252+
psql_cancel_callback(void)
279253
{
280-
intsave_errno=errno;
281-
charerrbuf[256];
282-
283254
/* if we are waiting for input, longjmp out of it */
284255
if (sigint_interrupt_enabled)
285256
{
@@ -288,74 +259,24 @@ handle_sigint(SIGNAL_ARGS)
288259
}
289260

290261
/* else, set cancel flag to stop any long-running loops */
291-
cancel_pressed= true;
292-
293-
/* and send QueryCancel if we are processing a database query */
294-
if (cancelConn!=NULL)
295-
{
296-
if (PQcancel(cancelConn,errbuf,sizeof(errbuf)))
297-
write_stderr("Cancel request sent\n");
298-
else
299-
{
300-
write_stderr("Could not send cancel request: ");
301-
write_stderr(errbuf);
302-
}
303-
}
304-
305-
errno=save_errno;/* just in case the write changed it */
262+
CancelRequested= true;
306263
}
307264

308-
void
309-
setup_cancel_handler(void)
310-
{
311-
pqsignal(SIGINT,handle_sigint);
312-
}
313-
#else/* WIN32 */
265+
#else
314266

315-
staticBOOLWINAPI
316-
consoleHandler(DWORDdwCtrlType)
267+
staticvoid
268+
psql_cancel_callback(void)
317269
{
318-
charerrbuf[256];
319-
320-
if (dwCtrlType==CTRL_C_EVENT||
321-
dwCtrlType==CTRL_BREAK_EVENT)
322-
{
323-
/*
324-
* Can't longjmp here, because we are in wrong thread :-(
325-
*/
326-
327-
/* set cancel flag to stop any long-running loops */
328-
cancel_pressed= true;
329-
330-
/* and send QueryCancel if we are processing a database query */
331-
EnterCriticalSection(&cancelConnLock);
332-
if (cancelConn!=NULL)
333-
{
334-
if (PQcancel(cancelConn,errbuf,sizeof(errbuf)))
335-
write_stderr("Cancel request sent\n");
336-
else
337-
{
338-
write_stderr("Could not send cancel request: ");
339-
write_stderr(errbuf);
340-
}
341-
}
342-
LeaveCriticalSection(&cancelConnLock);
343-
344-
return TRUE;
345-
}
346-
else
347-
/* Return FALSE for any signals not being handled */
348-
return FALSE;
270+
/* nothing to do here */
349271
}
350272

273+
#endif/* !WIN32 */
274+
351275
void
352-
setup_cancel_handler(void)
276+
psql_setup_cancel_handler(void)
353277
{
354-
InitializeCriticalSection(&cancelConnLock);
355-
356-
SetConsoleCtrlHandler(consoleHandler, TRUE);
278+
setup_cancel_handler(psql_cancel_callback);
357279
}
358-
#endif/* WIN32 */
359280

360281

361282
/* ConnectionUp
@@ -428,62 +349,6 @@ CheckConnection(void)
428349

429350

430351

431-
/*
432-
* SetCancelConn
433-
*
434-
* Set cancelConn to point to the current database connection.
435-
*/
436-
void
437-
SetCancelConn(void)
438-
{
439-
PGcancel*oldCancelConn;
440-
441-
#ifdefWIN32
442-
EnterCriticalSection(&cancelConnLock);
443-
#endif
444-
445-
/* Free the old one if we have one */
446-
oldCancelConn=cancelConn;
447-
/* be sure handle_sigint doesn't use pointer while freeing */
448-
cancelConn=NULL;
449-
450-
if (oldCancelConn!=NULL)
451-
PQfreeCancel(oldCancelConn);
452-
453-
cancelConn=PQgetCancel(pset.db);
454-
455-
#ifdefWIN32
456-
LeaveCriticalSection(&cancelConnLock);
457-
#endif
458-
}
459-
460-
461-
/*
462-
* ResetCancelConn
463-
*
464-
* Free the current cancel connection, if any, and set to NULL.
465-
*/
466-
void
467-
ResetCancelConn(void)
468-
{
469-
PGcancel*oldCancelConn;
470-
471-
#ifdefWIN32
472-
EnterCriticalSection(&cancelConnLock);
473-
#endif
474-
475-
oldCancelConn=cancelConn;
476-
/* be sure handle_sigint doesn't use pointer while freeing */
477-
cancelConn=NULL;
478-
479-
if (oldCancelConn!=NULL)
480-
PQfreeCancel(oldCancelConn);
481-
482-
#ifdefWIN32
483-
LeaveCriticalSection(&cancelConnLock);
484-
#endif
485-
}
486-
487352

488353
/*
489354
* AcceptResult
@@ -707,7 +572,7 @@ PSQLexec(const char *query)
707572
returnNULL;
708573
}
709574

710-
SetCancelConn();
575+
SetCancelConn(pset.db);
711576

712577
res=PQexec(pset.db,query);
713578

@@ -746,7 +611,7 @@ PSQLexecWatch(const char *query, const printQueryOpt *opt)
746611
return0;
747612
}
748613

749-
SetCancelConn();
614+
SetCancelConn(pset.db);
750615

751616
if (pset.timing)
752617
INSTR_TIME_SET_CURRENT(before);
@@ -773,7 +638,7 @@ PSQLexecWatch(const char *query, const printQueryOpt *opt)
773638
* consumed. The user's intention, though, is to cancel the entire watch
774639
* process, so detect a sent cancellation request and exit in this case.
775640
*/
776-
if (cancel_pressed)
641+
if (CancelRequested)
777642
{
778643
PQclear(res);
779644
return0;
@@ -973,8 +838,8 @@ ExecQueryTuples(const PGresult *result)
973838
{
974839
constchar*query=PQgetvalue(result,r,c);
975840

976-
/* Abandon execution ifcancel_pressed */
977-
if (cancel_pressed)
841+
/* Abandon execution ifCancelRequested */
842+
if (CancelRequested)
978843
gotoloop_exit;
979844

980845
/*
@@ -1091,7 +956,7 @@ ProcessResult(PGresult **results)
1091956
FILE*copystream;
1092957
PGresult*copy_result;
1093958

1094-
SetCancelConn();
959+
SetCancelConn(pset.db);
1095960
if (result_status==PGRES_COPY_OUT)
1096961
{
1097962
boolneed_close= false;
@@ -1342,7 +1207,7 @@ SendQuery(const char *query)
13421207
if (fgets(buf,sizeof(buf),stdin)!=NULL)
13431208
if (buf[0]=='x')
13441209
gotosendquery_cleanup;
1345-
if (cancel_pressed)
1210+
if (CancelRequested)
13461211
gotosendquery_cleanup;
13471212
}
13481213
elseif (pset.echo==PSQL_ECHO_QUERIES)
@@ -1360,7 +1225,7 @@ SendQuery(const char *query)
13601225
fflush(pset.logfile);
13611226
}
13621227

1363-
SetCancelConn();
1228+
SetCancelConn(pset.db);
13641229

13651230
transaction_status=PQtransactionStatus(pset.db);
13661231

@@ -1886,7 +1751,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
18861751
* writing things to the stream, we presume $PAGER has disappeared and
18871752
* stop bothering to pull down more data.
18881753
*/
1889-
if (ntuples<fetch_count||cancel_pressed||flush_error||
1754+
if (ntuples<fetch_count||CancelRequested||flush_error||
18901755
ferror(fout))
18911756
break;
18921757
}

‎src/bin/psql/common.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ extern volatile bool sigint_interrupt_enabled;
2626

2727
externsigjmp_bufsigint_interrupt_jmp;
2828

29-
externvoidsetup_cancel_handler(void);
30-
31-
externvoidSetCancelConn(void);
32-
externvoidResetCancelConn(void);
29+
externvoidpsql_setup_cancel_handler(void);
3330

3431
externPGresult*PSQLexec(constchar*query);
3532
externintPSQLexecWatch(constchar*query,constprintQueryOpt*opt);

‎src/bin/psql/large_obj.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include"common.h"
1111
#include"common/logging.h"
12+
#include"fe_utils/cancel.h"
1213
#include"large_obj.h"
1314
#include"settings.h"
1415

@@ -146,7 +147,7 @@ do_lo_export(const char *loid_arg, const char *filename_arg)
146147
if (!start_lo_xact("\\lo_export",&own_transaction))
147148
return false;
148149

149-
SetCancelConn();
150+
SetCancelConn(NULL);
150151
status=lo_export(pset.db,atooid(loid_arg),filename_arg);
151152
ResetCancelConn();
152153

@@ -182,7 +183,7 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
182183
if (!start_lo_xact("\\lo_import",&own_transaction))
183184
return false;
184185

185-
SetCancelConn();
186+
SetCancelConn(NULL);
186187
loid=lo_import(pset.db,filename_arg);
187188
ResetCancelConn();
188189

@@ -244,7 +245,7 @@ do_lo_unlink(const char *loid_arg)
244245
if (!start_lo_xact("\\lo_unlink",&own_transaction))
245246
return false;
246247

247-
SetCancelConn();
248+
SetCancelConn(NULL);
248249
status=lo_unlink(pset.db,loid);
249250
ResetCancelConn();
250251

‎src/bin/psql/startup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ main(int argc, char *argv[])
301301
exit(EXIT_BADCONN);
302302
}
303303

304-
setup_cancel_handler();
304+
psql_setup_cancel_handler();
305305

306306
PQsetNoticeProcessor(pset.db,NoticeProcessor,NULL);
307307

‎src/bin/scripts/clusterdb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ main(int argc, char *argv[])
133133
exit(1);
134134
}
135135

136-
setup_cancel_handler();
136+
setup_cancel_handler(NULL);
137137

138138
if (alldb)
139139
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp