简体   繁体   中英

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

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?

1

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

It is sometimes better to just use pool.query() to query the DB instead of using client.query(), you are not required to deal with releasing clients then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM