【发布时间】:2020-05-04 21:37:31
【问题描述】:
一个使用 Sequelize.js ORM 的 Node.js 应用程序正在对在 Mac OSX 主机系统上的 Docker 容器内运行的 PostgreSQL 11.2 服务器执行批量插入。每个批量插入通常由大约 1000-4000 行组成,批量插入并发数为 30,因此任何时候最多有 30 个活动插入操作。
const bulkInsert = async (payload) => {
try {
await sequelizeModelInstance.bulkCreate(payload);
} catch (e) {
console.log(e);
}
}
pLimit = require('p-limit')(30);
(function() => {
const promises = data.map(d => pLimit(() => bulkInsert(d))) // pLimit() controls Promise concurrency
const result = await Promise.all(promises)
})();
一段时间后,PostgreSQL 服务器将开始报错Connection terminated unexpectedly,然后是the database system is in recovery mode。
在重复多次并检查我的日志后,似乎在执行一批 30 个批量插入时通常会发生此错误,其中几个批量插入每个包含超过 100,000 行。例如,当尝试进行 190000、650000 和 150000 行的 3 次批量插入以及每次 1000-4000 行的 27 次插入时,会发生一次特定的崩溃。
系统内存未满,CPU负载正常,磁盘空间充足。
问题: PostgreSQL 在这种情况下崩溃是否正常?如果是这样,我们可以调整 PostgreSQL 设置以允许更大的批量插入吗?如果是大批量插入的原因,Sequelize.js 有没有为我们拆分批量插入的功能?
在 Docker 容器中的 PostgreSQL 11.2、TimescaleDB 1.5.1、节点 v12.6.0、sequelize 5.21.3、Mac Catalina 10.15.2 上运行
发生问题后立即记录 PostgreSQL 日志
2020-01-18 00:58:26.094 UTC [1] LOG: server process (PID 199) was terminated by signal 9
2020-01-18 00:58:26.094 UTC [1] DETAIL: Failed process was running: INSERT INTO "foo" ("id","opId","unix","side","price","amount","b","s","serverTimestamp") VALUES (89880,'5007564','1579219200961','front','0.0000784','35','undefined','undefined','2020-01-17 00:00:01.038 +00:00'),.........
2020-01-18 00:58:26.108 UTC [1] LOG: terminating any other active server processes
2020-01-18 00:58:26.110 UTC [220] WARNING: terminating connection because of crash of another server process
2020-01-18 00:58:26.110 UTC [220] DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2020-01-18 00:58:26.110 UTC [220] HINT: In a moment you should be able to reconnect to the database and repeat your command.
2020-01-18 00:58:26.148 UTC [214] WARNING: terminating connection because of crash of another server process
2020-01-18 00:58:26.148 UTC [214] DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2020-01-18 00:58:26.148 UTC [214] HINT: In a moment you should be able to reconnect to the database and repeat your command.
2020-01-18 00:58:26.149 UTC [203] WARNING: terminating connection because of crash of another server process
2020-01-18 00:58:26.149 UTC [203] DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
...
2020-01-18 00:58:30.098 UTC [1] LOG: all server processes terminated; reinitializing
2020-01-18 00:58:30.240 UTC [223] FATAL: the database system is in recovery mode
2020-01-18 00:58:30.241 UTC [222] LOG: database system was interrupted; last known up at 2020-01-18 00:50:13 UTC
2020-01-18 00:58:30.864 UTC [224] FATAL: the database system is in recovery mode
2020-01-18 00:58:31.604 UTC [225] FATAL: the database system is in recovery mode
2020-01-18 00:58:32.297 UTC [226] FATAL: the database system is in recovery mode
2020-01-18 00:58:32.894 UTC [227] FATAL: the database system is in recovery mode
2020-01-18 00:58:33.394 UTC [228] FATAL: the database system is in recovery mode
2020-01-18 01:00:55.911 UTC [222] LOG: database system was not properly shut down; automatic recovery in progress
2020-01-18 01:00:56.856 UTC [222] LOG: redo starts at 0/197C610
2020-01-18 01:01:55.662 UTC [229] FATAL: the database system is in recovery mode
【问题讨论】:
-
Postgresql 似乎被外力杀死了,可能是你操作系统的OOM Killer,你试过禁用它吗? (你提到了Docker,也可能是run镜像里面的OOMK)。
-
@NickLeBlanc 对于 Docker OOMK,我们应该使用
--oom-kill-disable=true标志还是使用--memory=4g将 Docker 容器内存限制从 2 GB 增加到 4 GB? -
@NickLeBlanc 似乎找不到任何关于 Mac 的 OOMK 的信息,仅适用于 Linux。
-
"系统内存未满" 你怎么知道的?你用了什么工具?它可能会很快从未满到满再到未满。在不到一个典型的监测间隔内。为什么用“mysql”标记?
-
@jjanes 你说得对,我无法确定应用/数据库的内存使用峰值是否会导致系统的内存使用被充分利用并导致OOM场景。我已将 mysql 标记替换为 docker.
标签: node.js postgresql docker sequelize.js timescaledb