aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2021-03-23 14:27:50 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2021-03-23 14:27:50 -0400
commitd75edab42c66cdc7b120d0d483ef73343d621eb4 (patch)
tree1330aa41608a16fd854c4d2f60f353d0436d21a0 /src
parent16568c0a8aee85052cfd3e465d21d1e1d1b442e1 (diff)
downloadpostgresql-d75edab42c66cdc7b120d0d483ef73343d621eb4.tar.gz
postgresql-d75edab42c66cdc7b120d0d483ef73343d621eb4.zip
Fix psql's \connect command some more.
Jasen Betts reported yet another unintended side effect of commit 85c54287a: 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 85c54287a, 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
Diffstat (limited to 'src')
-rw-r--r--src/bin/psql/command.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index d05d7eb981a..f22c0ee49c0 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -2969,6 +2969,25 @@ do_connect(enum trivalue reuse_previous_specification,
if (strcmp(replci->keyword, "password") == 0)
have_password = true;
}
+ else if (!reuse_previous)
+ {
+ /*
+ * When we have a connstring and are not re-using
+ * parameters, swap *all* entries, even those not set
+ * by the connstring. This avoids absorbing
+ * environment-dependent defaults from the result of
+ * PQconndefaults(). We don't want to do that because
+ * they'd override service-file entries if the
+ * connstring specifies a service parameter, whereas
+ * the priority should be the other way around. libpq
+ * can certainly recompute any defaults we don't pass
+ * here. (In this situation, it's a bit wasteful to
+ * have called PQconndefaults() at all, but not doing
+ * so would require yet another major code path here.)
+ */
+ replci->val = ci->val;
+ ci->val = NULL;
+ }
}
Assert(ci->keyword == NULL && replci->keyword == NULL);