Skip to content

Commit 3080f8f

Browse files
committed
Handle corner cases correctly in psql's reconnection logic.
After an unexpected connection loss and successful reconnection, psql neglected to resynchronize its internal state about the server, such as server version. Ordinarily we'd be reconnecting to the same server and so this isn't really necessary, but there are scenarios where we do need to update --- one example is where we have a list of possible connection targets and they're not all alike. Define "resynchronize" as including connection_warnings(), so that this case acts the same as \connect. This seems useful; for example, if the server version did change, the user might wish to know that. An attuned user might also notice that the new connection isn't SSL-encrypted, for example, though this approach isn't especially in-your-face about such changes. Although this part is a behavioral change, it only affects interactive sessions, so it should not break any applications. Also, in do_connect, make sure that we desynchronize correctly when abandoning an old connection in non-interactive mode. These problems evidently are the result of people patching only one of the two places where psql deals with connection changes, so insert some cross-referencing comments in hopes of forestalling future bugs of the same ilk. Lastly, in Windows builds, issue codepage mismatch warnings only at startup, not during reconnections. psql's codepage can't change during a reconnect, so complaining about it again seems like useless noise. Peter Billen and Tom Lane. Back-patch to all supported branches. Discussion: https://postgr.es/m/CAMTXbE8e6U=EBQfNSe01Ej17CBStGiudMAGSOPaw-ALxM-5jXg@mail.gmail.com
1 parent 9230cc7 commit 3080f8f

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/bin/psql/command.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3157,8 +3157,14 @@ do_connect(enum trivalue reuse_previous_specification,
31573157
psql_error("\\connect: %s", PQerrorMessage(n_conn));
31583158
if (o_conn)
31593159
{
3160+
/*
3161+
* Transition to having no connection. Keep this bit in sync
3162+
* with CheckConnection().
3163+
*/
31603164
PQfinish(o_conn);
31613165
pset.db = NULL;
3166+
ResetCancelConn();
3167+
UnsyncVariables();
31623168
}
31633169
}
31643170

@@ -3172,7 +3178,8 @@ do_connect(enum trivalue reuse_previous_specification,
31723178

31733179
/*
31743180
* Replace the old connection with the new one, and update
3175-
* connection-dependent variables.
3181+
* connection-dependent variables. Keep the resynchronization logic in
3182+
* sync with CheckConnection().
31763183
*/
31773184
PQsetNoticeProcessor(n_conn, NoticeProcessor, NULL);
31783185
pset.db = n_conn;
@@ -3247,7 +3254,8 @@ connection_warnings(bool in_startup)
32473254
sverbuf, sizeof(sverbuf)));
32483255

32493256
#ifdef WIN32
3250-
checkWin32Codepage();
3257+
if (in_startup)
3258+
checkWin32Codepage();
32513259
#endif
32523260
printSSLInfo();
32533261
}

src/bin/psql/common.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,27 @@ CheckConnection(void)
422422
if (!OK)
423423
{
424424
psql_error("Failed.\n");
425+
426+
/*
427+
* Transition to having no connection. Keep this bit in sync with
428+
* do_connect().
429+
*/
425430
PQfinish(pset.db);
426431
pset.db = NULL;
427432
ResetCancelConn();
428433
UnsyncVariables();
429434
}
430435
else
436+
{
431437
psql_error("Succeeded.\n");
438+
439+
/*
440+
* Re-sync, just in case anything changed. Keep this in sync with
441+
* do_connect().
442+
*/
443+
SyncVariables();
444+
connection_warnings(false); /* Must be after SyncVariables */
445+
}
432446
}
433447

434448
return OK;

0 commit comments

Comments
 (0)