3

I am building a json API using express (well, maybe will move to koa). I store my data in PostgreSQL database, and i use pg-promise to fetch data from it (async/await via babel).

I am totally new to node.js and i can't find any information about performance measurement in that environment.

To be specific:

module.exports.get_hierarchy = async function () {
    const rows = await postgres.any('SELECT id, parent, title, permission FROM heading');

    var result = [];
    // some black magic goes here...

    return result;
}

I want to know (programmatically if possible) how much time SELECT consumes. (Not the time promise lives from constructing to resolving, which can be achieved by taking two timestamps, but actual time consumed by the DB server to process query).

Can this be achieved? If so, how?

1
  • What you are looking for in PostgreSQL is called EXPLAIN that you need to execute directly, best is via pgAdmin UI ;) Commented Nov 23, 2016 at 9:28

1 Answer 1

3

As you mentioned it yourself, there are two ways to profile the execution time in your case:

  • the time from you calling a method of pg-promise to getting the result back
  • the time it takes to execute the query by the server

The first one is the easiest to do by using method result, which resolves with Result object that has property duration.

And the second one requires direct execution of EXPLAIN, typically via the pgAdmin tool that comes with every PostgreSQL installation, or you can use psql terminal.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for pointing to result method. How could i miss it? The downside is incompatibility with methods like one and oneOrNone, but it's bearable. Also duration property seems to be undocumented (github.com/brianc/node-postgres/wiki/Query#result-object); i hope it isn't going to dissapear in future releases:)
Hey, and thank you for bringing such a great library to mankind:)
Property duration is documented in the API: resolves with the original Result object, extended with property duration - query duration in milliseconds. ;) And no, I'm not removing it in the future releases ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.