Skip to content

Commit 4f670c6

Browse files
committed
Fix psql's \connect command some more.
Jasen Betts reported yet another unintended side effect of commit 85c5428: reconnecting with "\c service=whatever" did not have the expected results. The reason is that starting from the output of PQconndefaults() effectively allows environment variables (such as PGPORT) to override entries in the service file, whereas the normal priority is the other way around. Not using PQconndefaults at all would require yet a third main code path in do_connect's parameter setup, so I don't really want to fix it that way. But we can have the logic effectively ignore all the default values for just a couple more lines of code. This patch doesn't change the behavior for "\c -reuse-previous=on service=whatever". That remains significantly different from before 85c5428, because many more parameters will be re-used, and thus not be possible for service entries to replace. But I think this is (mostly?) intentional. In any case, since libpq does not report where it got parameter values from, it's hard to do differently. Per bug #16936 from Jasen Betts. As with the previous patches, back-patch to all supported branches. (9.5 is unfortunately now out of support, so this won't get fixed there.) Discussion: https://postgr.es/m/16936-3f524322a53a29f0@postgresql.org
1 parent e73068b commit 4f670c6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/bin/psql/command.c

+19
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,25 @@ do_connect(enum trivalue reuse_previous_specification,
19091909
if (strcmp(replci->keyword, "password") == 0)
19101910
have_password = true;
19111911
}
1912+
else if (!reuse_previous)
1913+
{
1914+
/*
1915+
* When we have a connstring and are not re-using
1916+
* parameters, swap *all* entries, even those not set
1917+
* by the connstring. This avoids absorbing
1918+
* environment-dependent defaults from the result of
1919+
* PQconndefaults(). We don't want to do that because
1920+
* they'd override service-file entries if the
1921+
* connstring specifies a service parameter, whereas
1922+
* the priority should be the other way around. libpq
1923+
* can certainly recompute any defaults we don't pass
1924+
* here. (In this situation, it's a bit wasteful to
1925+
* have called PQconndefaults() at all, but not doing
1926+
* so would require yet another major code path here.)
1927+
*/
1928+
replci->val = ci->val;
1929+
ci->val = NULL;
1930+
}
19121931
}
19131932
Assert(ci->keyword == NULL && replci->keyword == NULL);
19141933

0 commit comments

Comments
 (0)