【发布时间】:2017-08-19 18:17:18
【问题描述】:
我正在尝试使用 NodeJs 在 Postgres 表中插入超过 100 万行 问题是当我启动脚本时,内存不断增加,直到达到 1.5 GB 的 RAM,然后我得到错误: 致命错误:CALL_AND_RETRY_LAST 分配失败 - 进程内存不足
结果始终相同 - 大约插入了 7000 行而不是 100 万行
这里是代码
var pg = require('pg');
var fs = require('fs');
var config = require('./config.js');
var PgClient = new pg.Client(config.pg);
PgClient.connect();
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('resources/database.csv') //file contains over 1 million lines
});
var n=0;
lineReader.on('line', function(line) {
n++;
var insert={"firstname":"John","lastname":"Conor"};
//No matter what data we insert, the point is that the number of inserted rows much less than it should be
PgClient.query('INSERT INTO HUMANS (firstname,lastname) values ($1,$2)', [insert.firstname,insert.lastname]);
});
lineReader.on('close',function() {
console.log('end '+n);
});
【问题讨论】:
-
您是否尝试过在收到一行后暂停并在调用查询的回调后恢复?我认为排队的查询太多,这可能会耗尽您的进程内存。
-
我添加了 lineReader.pause();在查询和 lineReader.resume() 之前;查询后,但看起来这不起作用。同样的错误
-
考虑改为进行批量插入。逐行插入太昂贵了。
-
@m3n1at 你的意思是你在
PgClient.query()调用中添加了一个回调,里面有你调用lineReader.resume()的地方? -
@mscdex 是的,我做到了。问题相同 - FATAL ERROR: CALL_AND_RETRY_LAST 分配失败 - 进程内存不足
标签: node.js postgresql