Skip to content

Commit c800995

Browse files
committed
Fixed psql's Control-C handling when COPY in progress
1 parent fc8e6c7 commit c800995

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/bin/psql/common.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.16 2000/02/20 14:28:20 petere Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.17 2000/02/21 19:40:41 petere Exp $
77
*/
88
#include "postgres.h"
99
#include "common.h"
@@ -246,15 +246,18 @@ volatile bool cancel_pressed;
246246
void
247247
handle_sigint(SIGNAL_ARGS)
248248
{
249+
cancel_pressed = true;
250+
251+
if (copy_state)
252+
return;
253+
249254
if (cancelConn == NULL)
250255
#ifndef WIN32
251256
siglongjmp(main_loop_jmp, 1);
252257
#else
253258
return;
254259
#endif
255260

256-
cancel_pressed = true;
257-
258261
/* Try to send cancel request */
259262
if (PQrequestCancel(cancelConn))
260263
write_stderr("\nCancel request sent\n");
@@ -297,6 +300,9 @@ PSQLexec(const char *query)
297300

298301
cancelConn = pset.db;
299302
res = PQexec(pset.db, query);
303+
if (PQresultStatus(res) == PGRES_COPY_IN ||
304+
PQresultStatus(res) == PGRES_COPY_OUT)
305+
copy_state = true;
300306
cancelConn = NULL;
301307

302308
if (PQstatus(pset.db) == CONNECTION_BAD)
@@ -388,6 +394,9 @@ SendQuery(const char *query)
388394

389395
cancelConn = pset.db;
390396
results = PQexec(pset.db, query);
397+
if (PQresultStatus(results) == PGRES_COPY_IN ||
398+
PQresultStatus(results) == PGRES_COPY_OUT)
399+
copy_state = true;
391400
cancelConn = NULL;
392401

393402
if (results == NULL)

src/bin/psql/copy.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.10 2000/02/16 13:15:26 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.11 2000/02/21 19:40:41 petere Exp $
77
*/
88
#include "postgres.h"
99
#include "copy.h"
1010

1111
#include <errno.h>
1212
#include <assert.h>
13+
#include <signal.h>
1314
#ifndef WIN32
1415
#include <unistd.h> /* for isatty */
1516
#else
1617
#include <io.h> /* I think */
1718
#endif
1819

1920
#include "libpq-fe.h"
21+
#include "pqsignal.h"
2022

2123
#include "settings.h"
2224
#include "common.h"
@@ -26,6 +28,8 @@
2628
#define strcasecmp(x,y) stricmp(x,y)
2729
#endif
2830

31+
bool copy_state;
32+
2933
/*
3034
* parse_slash_copy
3135
* -- parses \copy command line
@@ -358,7 +362,9 @@ handleCopyOut(PGconn *conn, FILE *copystream)
358362
}
359363
}
360364
fflush(copystream);
361-
return !PQendcopy(conn);
365+
ret = !PQendcopy(conn);
366+
copy_state = false;
367+
return ret;
362368
}
363369

364370

@@ -386,6 +392,7 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt)
386392
char *s;
387393
int bufleft;
388394
int c = 0;
395+
int ret;
389396

390397
if (prompt) /* disable prompt if not interactive */
391398
{
@@ -435,5 +442,7 @@ handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt)
435442
}
436443
PQputline(conn, "\n");
437444
}
438-
return !PQendcopy(conn);
445+
ret = !PQendcopy(conn);
446+
copy_state = false;
447+
return ret;
439448
}

src/bin/psql/copy.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/copy.h,v 1.7 2000/02/16 13:15:26 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/copy.h,v 1.8 2000/02/21 19:40:42 petere Exp $
77
*/
88
#ifndef COPY_H
99
#define COPY_H
1010

1111
#include "libpq-fe.h"
1212

13+
extern bool copy_state;
14+
1315
/* handler for \copy */
1416
bool do_copy(const char *args);
1517

0 commit comments

Comments
 (0)