Skip to content

Commit 89535db

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 bea87c0 commit 89535db

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
@@ -1793,8 +1793,14 @@ do_connect(enum trivalue reuse_previous_specification,
17931793
psql_error("\\connect: %s", PQerrorMessage(n_conn));
17941794
if (o_conn)
17951795
{
1796+
/*
1797+
* Transition to having no connection. Keep this bit in sync
1798+
* with CheckConnection().
1799+
*/
17961800
PQfinish(o_conn);
17971801
pset.db = NULL;
1802+
ResetCancelConn();
1803+
UnsyncVariables();
17981804
}
17991805
}
18001806

@@ -1808,7 +1814,8 @@ do_connect(enum trivalue reuse_previous_specification,
18081814

18091815
/*
18101816
* Replace the old connection with the new one, and update
1811-
* connection-dependent variables.
1817+
* connection-dependent variables. Keep the resynchronization logic in
1818+
* sync with CheckConnection().
18121819
*/
18131820
PQsetNoticeProcessor(n_conn, NoticeProcessor, NULL);
18141821
pset.db = n_conn;
@@ -1885,7 +1892,8 @@ connection_warnings(bool in_startup)
18851892
sverbuf, sizeof(sverbuf)));
18861893

18871894
#ifdef WIN32
1888-
checkWin32Codepage();
1895+
if (in_startup)
1896+
checkWin32Codepage();
18891897
#endif
18901898
printSSLInfo();
18911899
}

src/bin/psql/common.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,27 @@ CheckConnection(void)
299299
if (!OK)
300300
{
301301
psql_error("Failed.\n");
302+
303+
/*
304+
* Transition to having no connection. Keep this bit in sync with
305+
* do_connect().
306+
*/
302307
PQfinish(pset.db);
303308
pset.db = NULL;
304309
ResetCancelConn();
305310
UnsyncVariables();
306311
}
307312
else
313+
{
308314
psql_error("Succeeded.\n");
315+
316+
/*
317+
* Re-sync, just in case anything changed. Keep this in sync with
318+
* do_connect().
319+
*/
320+
SyncVariables();
321+
connection_warnings(false); /* Must be after SyncVariables */
322+
}
309323
}
310324

311325
return OK;

0 commit comments

Comments
 (0)