Ron*_*ver 0 postgresql node.js pg-promise
我试图根据其他表中选定的数据插入数百/千个数据,但我发现错误“客户端太多”,这是错误
我使用 pgp (pg Promise) lib,这是我的代码片段
function call(){
for (let index = 0; index < 5; index++) {
getPendingData().then((result) => {
db.tx((t) => {
let counter = 0;
const queries = result.map((data) => {
counter++;
return db.none(`insert into test_data (id, isdeleted, parentid) values ('${uuidv1()}', 0, '${uuidv1()}x-${uuidv1()}' ) `);
});
return t.batch(queries);
});
});
}
}
let getPendingData = async () => {
return db.task('getPendingData', async (t) => {
return await t.any('select * from other_table');
});
}
(call())
Run Code Online (Sandbox Code Playgroud)
我设置 max pg conn 是 100,有什么线索如何在不添加 max conn 的情况下解决这个问题吗?
不幸的是,您的代码中有太多问题......
无论您如何使用 Promise,还是如何使用pg-promise.
承诺的问题在于您应该将它们链接起来,这意味着使用db.tx( return db.tx(...)) 的结果(您没有这样做),创建了一个松散的承诺,结果是与事务关联的松散连接。另外,return await也是一种反模式。
使用的问题pg-promise是您应该针对t您正在创建的事务/任务上下文执行查询(如此处所示)。db但是您正在针对根连接执行每个查询,这会产生无数的连接请求。
此外,创建一项只执行一个查询的任务没有任何意义。
如果这还不够糟糕,您还需要对值进行字符串连接,这在查询格式中是严格禁止的。
最后,多次插入应该作为多行查询执行,而不是作为单独的查询执行,这会浪费性能 - 请参阅多行插入。
| 归档时间: |
|
| 查看次数: |
857 次 |
| 最近记录: |