Re: BUG #5304: psql using conninfo fails in connecting to the server - Mailing list pgsql-bugs

From Joe Conway
Subject Re: BUG #5304: psql using conninfo fails in connecting to the server
Date
Msg-id 4B691129.9000008@joeconway.com
Whole thread Raw
In response to Re: BUG #5304: psql using conninfo fails in connecting to the server  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: BUG #5304: psql using conninfo fails in connecting to the server  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
On 02/02/2010 06:40 PM, Tom Lane wrote:
> The difference with PQconnectdbParams is that the dbname might not be
> the first thing in the parameter array.  I think that a straightforward
> implementation would have the effect of the expanded dbname overriding
> parameters given before it, but not those given after it.  Consider
>
>     keyword[0] = "port";
>     values[0] = "5678";
>     keyword[1] = "dbname";
>     values[1] = "dbname = db user = foo port = 9999";
>     keyword[2] = "user";
>     values[2] = "uu";
>
> What I'm imagining is that this would end up equivalent to
> dbname = db user = uu port = 9999.  That's probably reasonable,
> and maybe even useful, as long as it's documented.

No doc changes yet, and I still have not corrected the earlier mentioned
issue,

> While I'm looking at this, there's another bogosity in the original
> patch: it neglected the PQsetdbLogin call in psql/command.c, meaning
> that doing \c would result in losing the application_name setting.

but I wanted to get feedback before going further.

This patch implements Tom's idea above. Note that I also rearranged the
parameters for the call from psql so that dbname is last, therefore
allowing a conninfo to override all other settings. The result looks like:

    keywords[0]    = "host";
    values[0]    = options.host;
    keywords[1]    = "port";
    values[1]    = options.port;
    keywords[2]    = "user";
    values[2]    = options.username;
    keywords[3]    = "password";
    values[3]    = password;
    keywords[4]    = "application_name";
    values[4]    = pset.progname;
    keywords[5]    = "dbname";
    values[5]    = (options.action == ACT_LIST_DB &&
            options.dbname == NULL) ?
            "postgres" : options.dbname;
    keywords[6]    = NULL;
    values[6]    = NULL;

Which produces:

# psql -U postgres "dbname=regression user=fred application_name='joe\'s
app'"
psql (8.5devel)
Type "help" for help.

regression=> select backend_start, application_name from pg_stat_activity ;
         backend_start         | application_name
-------------------------------+------------------
 2010-02-02 21:44:55.969202-08 | joe's app
(1 row)

regression=> select current_user;
 current_user
--------------
 fred
(1 row)

Are there any of the psql parameters that we do not want to allow to be
overridden by the conninfo string?


Joe

Attachment

pgsql-bugs by date:

Previous
From: Jasen Betts
Date:
Subject: Re: whole-row functional index?
Next
From: Tom Lane
Date:
Subject: Re: BUG #5304: psql using conninfo fails in connecting to the server