[英]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.所以在我的应用程序中,我在部署到 Heroku 的 Node.js 环境中使用 Postgres DB。 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.我意识到在我的所有查询中,我没有 db.release() 或 db.end(),我认为我需要在我的函数末尾有这些。 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.我尝试查看堆栈和其他文章以确定它们之间的区别,在函数中使用它们的位置,以及不使用它们是否会导致我的 Heroku memory 容量达到,但我无法找到详细说明问题的资源。
First question--When do I use.end() and when do I use.release()?第一个问题——什么时候使用.end(),什么时候使用.release()? What's the difference, and is there a different function I should use to 'disconnect' from the database?有什么区别,我应该使用不同的 function 从数据库“断开”连接吗?
Second question--in a basic query such as the following, where do I put the.end() or.release()?第二个问题——在如下的基本查询中,我应该把 .end() 或 .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()?第三个问题——如果我承诺 db.query,I.end() 或 .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?最后一个问题——缺少 using.end() 或 .release() 是否会导致我的 Heroku memory 达到,如仪表板中 ZCD69B4957F06CD818D7BF3D61980E29 度量标准的线性增加部分所示?
Just a little note, I require('pg') and run db.connect() early in the Node.js file.请注意,我需要('pg')并在 Node.js 文件的早期运行 db.connect()。 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.有时最好只使用 pool.query() 来查询数据库,而不是使用 client.query(),那时您不需要处理释放客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.