Node-postgres: Server version detection

Created on 6 Nov 2019  Â·  9Comments  Â·  Source: brianc/node-postgres

A question for the library's gurus...


Is there anything within the PostgreSQL connection protocol that would provide in indication as to the server's version and/or features, without having to execute SELECT version()?

Here's the thing: I need to auto-replace every SELECT * FROM proc_name() with CALL proc_name() - the new syntax added in PostgreSQL v11, so I need to know what server version we are connected to, without having to execute a separate SELECT version() for it.

question

Most helpful comment

I don’t think any part of pg uses the ParameterStatus startup messages yet, but they are parsed and should be emitted on the connection as a 'parameterStatus' event. server_version is one.

All 9 comments

I have opened a related question on StackOverflow.

@brianc @charmander Guys, we are in the age of too many new versions of the server that bring new features, and knowing what server version we use on the protocol level is becoming imperative.

I don’t think any part of pg uses the ParameterStatus startup messages yet, but they are parsed and should be emitted on the connection as a 'parameterStatus' event. server_version is one.

should be emitted

Please, yes, need those badly. How can we do that?

Basically, I only care about the server version to be provided with every fresh connection.

function findParameterStatus(msg) {
    if (msg.parameterName === 'server_version') {
        this.off('parameterStatus', findParameterStatus);
        console.log('server version is:', msg.parameterValue);
    }
}

client.connection.on('parameterStatus', findParameterStatus);

something like that

@charmander The way you describe it works. But I'm using connections through the Pool only, and the library does not forward event parameterStatus to the pool.

Any idea how to work-around it?

A custom Client type that extends pg.Client is one way (maybe not the simplest) – you can add the listener in the constructor.

Ok, so I declared my own Client type like this:

class MyClient extends Client {
    constructor(config) {
        super(config);
        this.connection.on('parameterStatus', msg => {
            if (msg.parameterName === 'server_version') {
                console.log('Version:', msg.parameterValue);
            }
        });
    }
}

And then specified it within the connection options as custom Client for the pool:

const config = {
    database: 'bla',
    user: 'postgres',
    password: 'bla',
    Client: MyClient
};

This seems to work fine :smile:

@charmander Does this look right to you?

@charmander

How do you do the same with the Native Bindings? The client does not have connection property.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AhmedBHameed picture AhmedBHameed  Â·  3Comments

spollack picture spollack  Â·  4Comments

lucasmrl picture lucasmrl  Â·  3Comments

frmoded picture frmoded  Â·  3Comments

dindurthy picture dindurthy  Â·  4Comments