Skip to content

Commit 0e6d72d

Browse files
committed
Make PSQLexec's behavior on loss of connection more reasonable;
report original error before attempting reset, not after.
1 parent 19ff2e4 commit 0e6d72d

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

src/bin/psql/common.c

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -364,34 +364,43 @@ PSQLexec(PsqlSettings *pset, const char *query)
364364

365365
pqsignal(SIGINT, SIG_DFL); /* now control-C is back to normal */
366366

367-
if (PQstatus(pset->db) == CONNECTION_BAD)
368-
{
369-
fputs("The connection to the server was lost. Attempting reset: ", stderr);
370-
PQreset(pset->db);
371-
if (PQstatus(pset->db) == CONNECTION_BAD)
372-
{
373-
fputs("Failed.\n", stderr);
374-
PQfinish(pset->db);
375-
PQclear(res);
376-
pset->db = NULL;
377-
return NULL;
378-
}
379-
else
380-
fputs("Succeeded.\n", stderr);
381-
}
382-
383-
if (res && (PQresultStatus(res) == PGRES_COMMAND_OK ||
384-
PQresultStatus(res) == PGRES_TUPLES_OK ||
385-
PQresultStatus(res) == PGRES_COPY_IN ||
386-
PQresultStatus(res) == PGRES_COPY_OUT)
387-
)
388-
return res;
389-
else
367+
if (PQstatus(pset->db) == CONNECTION_OK)
390368
{
369+
if (res && (PQresultStatus(res) == PGRES_COMMAND_OK ||
370+
PQresultStatus(res) == PGRES_TUPLES_OK ||
371+
PQresultStatus(res) == PGRES_COPY_IN ||
372+
PQresultStatus(res) == PGRES_COPY_OUT)
373+
)
374+
return res; /* Normal success case... */
375+
/* Normal failure case --- display error and return NULL */
391376
fputs(PQerrorMessage(pset->db), pset->queryFout);
392377
PQclear(res);
393378
return NULL;
394379
}
380+
381+
/* Lost connection. Report whatever libpq has to say,
382+
* then consider recovery.
383+
*/
384+
fputs(PQerrorMessage(pset->db), pset->queryFout);
385+
PQclear(res);
386+
if (!pset->cur_cmd_interactive)
387+
{
388+
fprintf(stderr, "%s: connection to server was lost\n",
389+
pset->progname);
390+
exit(EXIT_BADCONN);
391+
}
392+
fputs("The connection to the server was lost. Attempting reset: ", stderr);
393+
fflush(stderr);
394+
PQreset(pset->db);
395+
if (PQstatus(pset->db) == CONNECTION_BAD)
396+
{
397+
fputs("Failed.\n", stderr);
398+
PQfinish(pset->db);
399+
pset->db = NULL;
400+
}
401+
else
402+
fputs("Succeeded.\n", stderr);
403+
return NULL;
395404
}
396405

397406

@@ -517,17 +526,19 @@ SendQuery(PsqlSettings *pset, const char *query)
517526
{
518527
if (!pset->cur_cmd_interactive)
519528
{
520-
fprintf(stderr, "%s: connection to server was lost", pset->progname);
529+
fprintf(stderr, "%s: connection to server was lost\n",
530+
pset->progname);
521531
exit(EXIT_BADCONN);
522532
}
523533
fputs("The connection to the server was lost. Attempting reset: ", stderr);
534+
fflush(stderr);
524535
PQreset(pset->db);
525536
if (PQstatus(pset->db) == CONNECTION_BAD)
526537
{
527538
fputs("Failed.\n", stderr);
528539
PQfinish(pset->db);
529-
PQclear(results);
530540
pset->db = NULL;
541+
PQclear(results);
531542
return false;
532543
}
533544
else

0 commit comments

Comments
 (0)