Querystring in connection string
See original GitHub issueThe problem
Postgres supports a bunch of stuff in connection strings: https://www.postgresql.org/docs/9.5/static/libpq-connect.html#LIBPQ-CONNSTRING
However, ConnectionParameters
, pg-connection-string
and pg.Pool
’s config seem to only be aware about certain limited subset (like ssl
, fallback_app_name
and others). Doesn’t seem practical to validate those in node, since server will already reject invalid config (I think).
Usecase
Instead of running a bunch of set X to Y
after connecting to postgres on every client, I’d like to specify some session settings on connection string. In particular, I’d like to reproduce the following psql
invocation from the node:
psql postgresql://?options=-c%20timezone%3DEurope/Moscow
Solution
Probably parse the whole query string and pass everything we found in it to the server. Not sure how connection flow works, and how to use those parsed params.
Looks like there are a bunch of places need to be change in order to achieve this. Not sure if these are all of them:
pg-connection-string
’sparse
function;pg.Client
;pg.ConnectionParameters
;pg.Pool
’s constructor- anything else?
I would be happy to PR this, but I’m not sure where to start. Any tips?
As a sidenote, would be neat to support this stuff: psql options="-c\ timezone=Europe/Moscow"
.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top GitHub Comments
Sure…so how connection works is
pg authenticates with the database
after authentication pg sends a bunch of settings to establish the session and configure the application name and other stuff…
It does so by first getting the bundle of settings to send: https://github.com/brianc/node-postgres/blob/master/lib/client.js#L212 And then sending these settings via the connection as key-value pairs https://github.com/brianc/node-postgres/blob/master/lib/client.js#L65 https://github.com/brianc/node-postgres/blob/master/lib/connection.js#L143
So modifying the
client.getStartupConf()
to be smarter is a good place to smart - and then you gotta walk back the various ways configs can be passed to the client/pool and make sure they all accept more parameters…once the client is aware of the object containing the parameters to send to the backend, actually sending them is pretty straight forward.As for the native connection we take the config object and turn it into a “keyword/value connection string” here:
https://github.com/brianc/node-postgres/blob/master/lib/connection-parameters.js#L74
That string is then passed unmodified to libpq where its converted into properties & sent to the postgres backend by libpq. The reason for doing a dns lookup in
connection-parameters
is because libpq is mostly non blocking but AFAIK it still does a blocking dns lookup if you connect with a hostname, which can cause big problems in node if you block the event loop in C, so we use node to lookup some DNS info before calling libpq to prevent this.I’d like to look into this, PR for
pg-connection-string
seems pretty straight-forward, but I am not sure what stuff to change insidepg
. The other problem is that parsing of query string is somethingpsql
(orlibpq
?) does on its own, and I don’t know how those parameters make its way to Postgres server.I’ll try to dig into this on my own some time, but would be nice if you (or someone else) could maybe explain connection flow here, if that’s not too much hassle.
That’s the word I was looking for! 😃