简体   繁体   English

Postgres:何时在查询中使用.end() vs.release()(这在 util.promisify 中是否发生了变化)? 缺少这些功能是否会增加 memory 的使用量?

[英]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 度量标准的线性增加部分所示?

1

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.

相关问题 NodeJs util.promisify 不是函数 - NodeJs util.promisify is not a function TypeError:util.promisify不是函数吗? - TypeError: util.promisify is not a function? 使用AJAX会增加PHP的内存使用吗? - Does the use of AJAX increase memory usage of PHP? 如何通过writeFile fs函数使用节点js util.promisify并等待 - How to use node js util.promisify with the writeFile fs function and await 在构造函数中使用'this'关键字是否会整体上增加内存使用量? - Does the use of 'this' keyword within a constructor increase memory usage overall? 节点:util.promisify() 没有回调作为最终参数 - Node: util.promisify() without callback as final parameter 有没有办法在打字稿上使用 util.promisify 继承方法签名? - Is there any way to inherit method signature with util.promisify on typescript? 模块“util”已被外部化以实现浏览器兼容性。 无法在客户端代码中访问“util.promisify” - Module "util" has been externalized for browser compatibility. Cannot access "util.promisify" in client code 'util.promisify(setTimeout)' 和 'ms => new Promise(resolve => setTimeout(resolve, ms))' 之间的区别 - difference between 'util.promisify(setTimeout)' and 'ms => new Promise(resolve => setTimeout(resolve, ms))' Continue.map 循环后 util.promisify 请求迭代失败(异步等待) - Continue .map loop after util.promisify request fails in iteration (async await)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM