由于另一个服务器进程崩溃而终止连接

jee*_*ace 4 postgresql jdbc

我有一个 java 抓取工具,它产生大约 1,500-2,100 个线程,每个线程通过 jdbc 连接到同一个数据库并进行插入。这些螺纹插入的频率很高。

当连接太多时,数据库似乎崩溃了。当所有线程都失去连接时,我总是会产生 210 个线程或连接。以下是在服务器端生成的日志消息。

2015-07-15 20:18:37 UTC [10825-21] LOG:  checkpointer process (PID 13435) was terminated by signal 9: Killed
2015-07-15 20:18:37 UTC [10825-22] LOG:  terminating any other active server processes
2015-07-15 20:18:37 UTC [16836-1] user@db WARNING:  terminating connection because of crash of another server process
2015-07-15 20:18:37 UTC [16836-2] user@db 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.
2015-07-15 20:18:37 UTC [16836-3] user@db HINT:  In a moment you should be able to reconnect to the database and repeat your command.
Run Code Online (Sandbox Code Playgroud)

以下是client(scraper)端显示的error msg。

SEVERE: An I/O error occurred while sending to the backend.
org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:283)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:420)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:366)
    at SocketBot.run(SocketBot.java:167)
Caused by: java.io.EOFException
    at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:284)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1803)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    ... 4 more
Run Code Online (Sandbox Code Playgroud)

在 postgresql.conf 文件中,我设置了最大连接数 = 2500。但我认为这里还有更多需要调整的地方。如果有人想知道,我的服务器有 512MB RAM。

Cra*_*ger 5

Linux 的内存不足杀手可能是终止进程。这意味着您的服务器配置错误。强烈建议您不要让 Linux 过度使用内存,因此它会以适当的内存不足错误响应应用程序,而不是几乎随机终止进程。PostgreSQL 旨在处理内存不足的情况,但如果 Linux 终止进程而不是报告内存不足,则无法处理。

要确认这就是这里发生的事情,请使用dmesg命令检查内核消息日志。

请参阅http://www.postgresql.org/docs/current/static/kernel-resources.html#LINUX-MEMORY-OVERCOMMIT