Postgres: When to use .end() vs .release() in queries (and does this change in util.promisify)? Does lack of these functions increase memory usage?

  heroku, javascript, node.js, postgresql, promise

So in my application, I’m using a Postgres DB in a Node.js environment that’s deployed to Heroku. I realized that in all of my queries, I don’t have a db.release() or db.end(), and I think I need to have those at the end of my functions. I’ve tried looking around stack and other articles to determine the difference between them, where to use them in the functions, and whether not using them could be causing my Heroku memory capacity to be reached, but I haven’t been able to find resources detailing the issues.

First question–When do I use .end() and when do I use .release()? What’s the difference, and is there a different function I should use to ‘disconnect’ from the database?

Second question–in a basic query such as the following, where do I put the .end() or .release()? (AKA before or after the return)

db.query(sql, (err, res) => {
  if (err) {
    return console.log(err.message);
  } else {
    return res;
  }    
})

Third question–if I promisify db.query, how do I .end() or .release()?

const util = require('util')
const queryP = util.promisify(db.query)
await queryP.call(db, sqlStatement);

Final question–could the lack of using .end() or .release() be causing my Heroku memory to be reached, as seen in the linear increasing portion of the memory Metric in the dashboard?

https://i.stack.imgur.com/fO3Bz.png

Just a little note, I require(‘pg’) and run db.connect() early in the Node.js file. Thank you for everything!

Source: Ask Javascript Questions

LEAVE A COMMENT