【问题标题】:Heroku Postgres database randomly crashing, "error: terminating connection due to administrator command"Heroku Postgres 数据库随机崩溃,“错误:由于管理员命令而终止连接”
【发布时间】:2021-06-13 05:16:09
【问题描述】:

每隔一段时间,Postgres 就会出错(底部的错误消息)。我不能强迫它崩溃,所以我无法重现它以找到错误的来源。我也在免费层,所以没有 Postgres 日志。

我正在 Heroku 测功机上使用 Bolt.js 运行 Slack 应用程序。

在 app.js 中我连接到数据库

const pg_client = new Client({
  connectionString: process.env.DATABASE_URL,
  ssl: {
    rejectUnauthorized: false,
  },
});
pg_client.connect();

然后我有一个通用查询函数,它接收客户端、查询和值

//Generic query function
    let res;
    try {
      await pg_client.query("BEGIN");
      try {
        res = await pg_client.query(q, v);
        await pg_client.query("COMMIT");
      } catch (err) {
        await pg_client.query("ROLLBACK");
        throw err;
      }
    } finally {
      //client.release()
    }
    return res;
}

module.exports = { query };

我刚刚意识到如果查询失败,我的 throw err; 行是否会导致 Postgres 崩溃?我应该只记录那个错误吗?

我是否需要在每次查询时释放我的客户端并重新启动它?我需要设置池或其他东西吗? Heroku dynos 每 24 小时重启一次,所以我的连接不会持续数周。

任何能指引我正确方向的帮助都会令人惊叹。谢谢!

错误信息是

2021-03-15T21:04:15.932529+00:00 app[web.1]: events.js:292
2021-03-15T21:04:15.932548+00:00 app[web.1]: throw er; // Unhandled 'error' event
2021-03-15T21:04:15.932549+00:00 app[web.1]: ^
2021-03-15T21:04:15.932549+00:00 app[web.1]:
2021-03-15T21:04:15.932552+00:00 app[web.1]: error: terminating connection due to administrator command
2021-03-15T21:04:15.932554+00:00 app[web.1]: at Parser.parseErrorMessage (/app/node_modules/pg-protocol/dist/parser.js:278:15)
2021-03-15T21:04:15.932554+00:00 app[web.1]: at Parser.handlePacket (/app/node_modules/pg-protocol/dist/parser.js:126:29)
2021-03-15T21:04:15.932555+00:00 app[web.1]: at Parser.parse (/app/node_modules/pg-protocol/dist/parser.js:39:38)
2021-03-15T21:04:15.932556+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/pg-protocol/dist/index.js:10:42)
2021-03-15T21:04:15.932556+00:00 app[web.1]: at TLSSocket.emit (events.js:315:20)
2021-03-15T21:04:15.932557+00:00 app[web.1]: at addChunk (internal/streams/readable.js:309:12)
2021-03-15T21:04:15.932557+00:00 app[web.1]: at readableAddChunk (internal/streams/readable.js:284:9)
2021-03-15T21:04:15.932558+00:00 app[web.1]: at TLSSocket.Readable.push (internal/streams/readable.js:223:10)
2021-03-15T21:04:15.932558+00:00 app[web.1]: at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
2021-03-15T21:04:15.932559+00:00 app[web.1]: Emitted 'error' event on Client instance at:
2021-03-15T21:04:15.932559+00:00 app[web.1]: at Client._handleErrorEvent (/app/node_modules/pg/lib/client.js:319:10)
2021-03-15T21:04:15.932559+00:00 app[web.1]: at Client._handleErrorMessage (/app/node_modules/pg/lib/client.js:330:12)
2021-03-15T21:04:15.932560+00:00 app[web.1]: at Connection.emit (events.js:315:20)
2021-03-15T21:04:15.932560+00:00 app[web.1]: at /app/node_modules/pg/lib/connection.js:115:12
2021-03-15T21:04:15.932561+00:00 app[web.1]: at Parser.parse (/app/node_modules/pg-protocol/dist/parser.js:40:17)
2021-03-15T21:04:15.932561+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/pg-protocol/dist/index.js:10:42)
2021-03-15T21:04:15.932562+00:00 app[web.1]: [... lines matching original stack trace ...]
2021-03-15T21:04:15.932562+00:00 app[web.1]: at TLSSocket.Readable.push (internal/streams/readable.js:223:10) {
2021-03-15T21:04:15.932563+00:00 app[web.1]: length: 116,
2021-03-15T21:04:15.932563+00:00 app[web.1]: severity: 'FATAL',
2021-03-15T21:04:15.932564+00:00 app[web.1]: code: '57P01',
2021-03-15T21:04:15.932564+00:00 app[web.1]: detail: undefined,
2021-03-15T21:04:15.932565+00:00 app[web.1]: hint: undefined,
2021-03-15T21:04:15.932565+00:00 app[web.1]: position: undefined,
2021-03-15T21:04:15.932566+00:00 app[web.1]: internalPosition: undefined,
2021-03-15T21:04:15.932566+00:00 app[web.1]: internalQuery: undefined,
2021-03-15T21:04:15.932566+00:00 app[web.1]: where: undefined,
2021-03-15T21:04:15.932567+00:00 app[web.1]: schema: undefined,
2021-03-15T21:04:15.932567+00:00 app[web.1]: table: undefined,
2021-03-15T21:04:15.932567+00:00 app[web.1]: column: undefined,
2021-03-15T21:04:15.932568+00:00 app[web.1]: dataType: undefined,
2021-03-15T21:04:15.932568+00:00 app[web.1]: constraint: undefined,
2021-03-15T21:04:15.932569+00:00 app[web.1]: file: 'postgres.c',
2021-03-15T21:04:15.932569+00:00 app[web.1]: line: '3023',
2021-03-15T21:04:15.932570+00:00 app[web.1]: routine: 'ProcessInterrupts'
2021-03-15T21:04:15.932570+00:00 app[web.1]: }
2021-03-15T21:04:16.359776+00:00 heroku[web.1]: Process exited with status 1
2021-03-15T21:04:16.449742+00:00 heroku[web.1]: State changed from up to crashed
2021-03-15T21:04:16.501439+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-15T21:04:20.383409+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-03-15T21:04:23.522205+00:00 app[web.1]: ⚡️ Bolt app is running!
2021-03-15T21:04:24.872264+00:00 heroku[web.1]: State changed from starting to up
2021-03-15T21:06:30.856589+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT 10.152.19.239:5432
2021-03-15T21:06:30.856610+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
2021-03-15T21:06:30.856611+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2021-03-15T21:06:30.857096+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning

【问题讨论】:

    标签: node.js postgresql heroku


    【解决方案1】:

    那个错误意味着有人向数据库进程发送了信号SIGTERM

    • 也许您的一段恶意代码正在通过调用pg_terminate_backend 函数来杀死会话。

    • 另一种方法是在默认快速模式下关闭数据库。

    查阅数据库日志以确定发生了这两件事中的哪一件。

    【讨论】:

    • 感谢您的意见!确定我的代码没有调用pg_terminate_backend 在默认快速模式下数据库如何/为什么会关闭?不幸的是,我在 Heroku 上的免费 Postgres 层,所以没有日志:/
    • 我不知道 Heroku 何时选择关闭其数据库。如果您无权访问日志,那么您将迷失方向。也许问你的服务提供商。我只能告诉你是什么导致了这个消息。
    • 错误日志的第一行是even.tsjs:292 throw er; //unhandled 'error' event。因此,似乎一个未捕获的错误使整个事情崩溃了。但是 Postgres 错误说 terminating connection due to admin. 这两件事有冲突吗?
    • 那不是 PostgreSQL 日志。如果发生数据库崩溃,您会收到不同的错误消息。
    猜你喜欢
    • 1970-01-01
    • 2019-01-20
    • 2016-06-11
    • 2011-08-12
    • 2021-12-28
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多