From 3eae5e5d91084e0882a286bac464782701e17d21 Mon Sep 17 00:00:00 2001 From: Oleksandr Shulgin Date: Thu, 20 Oct 2016 12:24:48 +0200 Subject: [PATCH] psql: stop sending commands after connection reset Previsouly an input line such as "BEGIN; UPDATE something..." could result in UPDATE running outside of transaction if the first statement happen to trigger connection reset. NB: this does *not* protect from blocks of commands pasted into the terminal. --- src/bin/psql/common.c | 2 ++ src/bin/psql/mainloop.c | 5 ++++- src/bin/psql/settings.h | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index a7789df..34a4507 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -386,6 +386,8 @@ CheckConnection(void) } else psql_error("Succeeded.\n"); + + pset.conn_was_reset = true; } return OK; diff --git a/src/bin/psql/mainloop.c b/src/bin/psql/mainloop.c index 37dfa4d..6d39ce8 100644 --- a/src/bin/psql/mainloop.c +++ b/src/bin/psql/mainloop.c @@ -391,11 +391,14 @@ MainLoop(FILE *source) } /* fall out of loop if lexer reached EOL */ - if (scan_result == PSCAN_INCOMPLETE || + if (pset.conn_was_reset || + scan_result == PSCAN_INCOMPLETE || scan_result == PSCAN_EOL) break; } + pset.conn_was_reset = false; + /* Add line to pending history if we didn't execute anything yet */ if (pset.cur_cmd_interactive && !line_saved_in_history) pg_append_history(line, history_buf); diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 8cfe9d2..39a4be0 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -102,6 +102,9 @@ typedef struct _psqlSettings FILE *cur_cmd_source; /* describe the status of the current main * loop */ bool cur_cmd_interactive; + bool conn_was_reset; /* indicates that the connection was reset + * during the last attempt to execute an + * interactive command */ int sversion; /* backend server version */ const char *progname; /* in case you renamed psql */ char *inputfile; /* file being currently processed, if any */ -- 2.7.4