我在使用 NodeJS 和 PostgreSQL 时遇到了一个奇怪的错误,我希望你能帮助我。
我有大量的数据集,大约有 200 万个条目,我想将它们插入到数据库中。
一份数据由 4 列组成:
id: string,
points: float[][]
mid: float[]
occurences: json[]
我像这样插入数据:
let pgp = require('pg-promise')(options);
let connectionString = 'postgres://archiv:archiv@localhost:5432/fotoarchivDB';
let db = pgp(connectionString);
cityNet.forEach((arr) => {
db
.none(
"INSERT INTO currentcitynet(id,points,mid,occurences) VALUES $1",
Inserts("${id},${points}::double precision[],${mid}::double precision[],${occurences}::json[]",arr))
.then(data => {
//success
})
.catch(error => {
console.log(error);
//error
});
})
function Inserts(template, data) {
if (!(this instanceof Inserts)) {
return new Inserts(template, data);
}
this._rawDBType = true;
this.formatDBType = function() {
return data.map(d => "(" + pgp.as.format(template, d) + ")").join(",");
};
这对于前 309248 个数据块来说是正确的,然后突然间它会在尝试插入的每个下一个数据(看起来像这样)中出现错误:
{ error: syntax error at end of input
at Connection.parseE (/home/christian/Masterarbeit_reworked/projekt/server/node_modules/pg-promise/node_modules/pg/lib/connection.js:539:11)
at Connection.parseMessage (/home/christian/Masterarbeit_reworked/projekt/server/node_modules/pg-promise/node_modules/pg/lib/connection.js:366:17)
at Socket.<anonymous> (/home/christian/Masterarbeit_reworked/projekt/server/node_modules/pg-promise/node_modules/pg/lib/connection.js:105:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:548:20)
name: 'error',
length: 88,
severity: 'ERROR',
code: '42601',
detail: undefined,
hint: undefined,
position: '326824',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'scan.l',
line: '1074',
routine: 'scanner_yyerror' }
每个迭代错误消息的“位置”条目都会发生变化。
我可以重做,但在 309248 个条目之后它总是会出错。
当我尝试插入更少的条目(例如 1000 个条目)时,不会发生错误。
这真的让我很困惑。我认为 PostgreSQL 没有最大行数。此外,错误消息对我根本没有帮助。
SOLVED发现错误了。在我的数据中,存在“空”条目。过滤掉空数据已成功。
我将尝试其他插入数据的建议,因为当前的方式有效,但性能非常糟糕。