简体   繁体   English

Heroku Postgres 数据库随机崩溃,“错误:由于管理员命令而终止连接”

[英]Heroku Postgres database randomly crashing, “error: terminating connection due to administrator command”

Every once in a while Postgres will just error out (error message at the bottom).每隔一段时间,Postgres 就会出错(底部的错误消息)。 I can't force it to crash ever so I can't reproduce it to find the source of the error.我不能强迫它崩溃,所以我无法重现它以找到错误的来源。 I'm also on the free tier so there are no Postgres logs.我也在免费层,所以没有 Postgres 日志。

I'm running a Slack app using Bolt.js on a Heroku dyno.我在 Heroku 测功机上使用 Bolt.js 运行 Slack 应用程序。

In app.js I connect to the DB在 app.js 我连接到数据库

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

Then I have a generic query function that takes in the client, the query, and the values然后我有一个通用查询 function 接受客户端、查询和值

//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 };

I'm just realizing is my throw err;我只是意识到我的throw err; line above causing Postgres to crash if a query fails?如果查询失败,上面的行会导致 Postgres 崩溃吗? Should I just log that error instead?我应该只记录那个错误吗?

Do I need to be releasing my client any restarting it every query?我是否需要在每次查询时释放我的客户并重新启动它? Do I need to set up pooling or something?我需要设置池或其他东西吗? Heroku dynos restart every 24 hours so it's not like my connection is open for weeks. Heroku 测功机每 24 小时重新启动一次,所以我的连接不会持续数周。

Any help that points me in the right direction would be amazing.任何为我指明正确方向的帮助都会令人惊叹。 Thank you!谢谢!

The error message is错误信息是

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

That error means that somebody sent the signal SIGTERM to the database process.该错误意味着有人向数据库进程发送了信号SIGTERM

  • Perhaps a rogue piece of your code is killing sessions by calling the pg_terminate_backend function.也许您的代码的流氓部分正在通过调用pg_terminate_backend function 来终止会话。

  • The alternative is that the database is shut down in the default fast mode.另一种方法是在默认快速模式下关闭数据库。

Consult the database log to figure out which of the two things happened.查阅数据库日志以确定发生了两件事中的哪一件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM