# 由于数据库关闭缓慢导致重建出现问题

**URL:** https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366
**Category:** Self-hosting
**Created:** [2023年四月19日 18:36 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366 "2023-04-19T18:36:27Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [2023年四月19日 18:36 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/1 "2023-04-19T18:36:27Z")

</div>

推荐的升级失败了，并且在中断后未能使我的论坛恢复正常。我正在运行 discourse-doctor 来尝试修复它，如果这也失败了，我将使用 VM 快照。

输出：

```plaintext
2023-04-19 18:28:31.298 UTC [42] LOG: received fast shutdown request
2023-04-19 18:28:33.651 UTC [65] LOG: shutting down
2023-04-19 18:28:33.974 UTC [42] LOG: database system is shut down

FAILED
--------------------
Pups::ExecError: su postgres -c 'psql discourse -c "alter schema public owner to discourse;"' failed with return #<Process::Status: pid 59 exit 2>
Location of failure: /usr/local/lib/ruby/gems/3.2.0/gems/pups-1.1.1/lib/pups/exec_command.rb:117:in `spawn'
exec failed with the params "su postgres -c 'psql $db_name -c \\\"alter schema public owner to $db_user;\\\"'"
bootstrap failed with exit code 2
**FAILED TO BOOTSTRAP** please scroll up and look for earlier error messages, there may be more than one.
./discourse-doctor may help diagnose the problem.
c13e1ba313de8fc84f6e2fb0f88197a908803c39791283effb8c82f55b56b6dc
Command exited with non-zero status 1
1.85user 1.84system 3:21.56elapsed 1%CPU (0avgtext+0avgdata 36996maxresident)k
197608inputs+368outputs (1133major+96509minor)pagefaults 0swaps

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2023年四月19日 18:38 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/2 "2023-04-19T18:38:17Z")

</div>

您是否在使用 beta 分支？

您可以尝试使用以下命令重启您的容器：

```
 ./launcher start app

```

但这应该是 `discourse-doctor` 的功能。

您需要提供更多输出，因为错误信息超出了您包含的内容范围。

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [2023年四月19日 18:40 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/3 "2023-04-19T18:40:19Z")

</div>

是的，我们在 beta 分支上。我一直在 nohup 中运行，所以我有完整的日志。

Discourse-doctor 仍在努力运行，但尚未失败，所以我抱有希望。

[https://pastebin.mozilla.org/iw2zc5zd](https://pastebin.mozilla.org/iw2zc5zd)

编辑：Discourse-doctor 使我们恢复了正常运行。

我基本上是自找的，在收到通知一小时后就进行了升级，并且是第一个这样做的。之前没有真正备份，所以我在这里为大家承担了风险。

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [2023年四月19日 19:12 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/4 "2023-04-19T19:12:24Z")

</div>

> - 2023-04-19 18:28:26.755 UTC [45] LOG: 数据库系统未正确关闭；正在进行自动恢复

如果您的数据库无法在 60 秒内安全停止，这在大数据库和较慢磁盘上会发生，它将进入此状态，并且如果在 5 秒内无法恢复（这种情况很少见，因为它很大/很慢），则重建会失败。

这与此处列出的更改无关，而是自 2016 年以来 Discourse 中一直存在的问题。

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [2023年四月19日 20:09 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/5 "2023-04-19T20:09:03Z")

</div>

啊，谢谢。也许对于我们这样的大型论坛，应该让它等待更长时间。如果你只是终止数据库进程，它在重新启动后需要回滚事务，这可能需要很长时间。

关于 beta 的术语有点令人困惑。管理员仪表板显示我们正在运行 beta 版，我们是否应该查看其他地方？我的理解是，基于发布公告中不建议使用稳定分支的说法，beta 版是推荐用于 discourse 的。

---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [2023年四月19日 22:13 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/6 "2023-04-19T22:13:19Z")

</div>

默认值实际上是 tests\_passed，这被认为是生产就绪的。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2023年四月20日 01:18 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/7 "2023-04-20T01:18:14Z")

</div>

> [@Wingtip](#):
>
> 如果只是终止数据库进程，它在重新启动后需要回滚事务，这可能需要很长时间

您的数据库有多大？它在固态硬盘上吗？您有多少内存？

拥有一个单独的数据容器将需要更少的数据库重启。

---

<div class="post-metadata">

### Author: ![Ed\_S](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ed_s/32/134015_2.png) [@Ed\_S](https://meta.discourse.org/u/Ed_S)
#### Post date: [2023年四月20日 05:18 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/8 "2023-04-20T05:18:10Z")

</div>

> [@pfaffman](#):
>
> 您的数据库有多大？它在固态硬盘上吗？您有多少内存？

何时决定在 60 秒内安全关闭？现在有多少安装比当时正常情况大得多？

理想情况下，这种 60 秒的等待应该更像是一种闭环等待，并有一个限制。听起来这个限制应该更高，因为现在有很多实例比当时正常情况大得多，而且存在漏洞。

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [2023年四月20日 13:19 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/9 "2023-04-20T13:19:32Z")

</div>

它有 105GB，在 SSD 上，16GB VM，我为 postgres 分配了 8GB 的缓冲区。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2023年四月20日 14:08 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/10 "2023-04-20T14:08:06Z")

</div>

> [@Ed\_S](#):
>
> 何时决定在 60 年代安全关停？

我认为我看到它至少可以追溯到 2016 年。_但情况已经改变了。_ 编辑：这是一个新的提交。

> <https://github.com/discourse/discourse_docker/commit/d54cc57f5fe7910db77cd92cfe40f2e011135bac>
>
> Over the years we had LOTS of cases of PostgreSQL instances that weren't stopped… properly. This gives PG more time to handle it's shutdown cleanly on the worst case, but will still shutdown fast in normal cases.

> [@Ed\_S](#):
>
> 现在有多少安装比当时正常的大得多？

我认为标准安装的并不多，因为这几乎从一开始就是这样了。

> [@Wingtip](#):
>
> 它是 105GB，在 SSD 上，16GB VM，我给了 postgres 一个 8GB 的缓冲区池。

呃，是的。这是一个大数据库。我怀疑很少有人拥有如此大的数据库，而不是在 RDS 上，或者至少是一个单独的容器。您可能应该考虑切换到 2 容器安装。

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [2023年四月20日 14:19 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/11 "2023-04-20T14:19:42Z")

</div>

我们会考虑的，切换方法有文档记录吗？增加 60 秒计时器还有其他无法提供的优势吗？

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [2023年四月20日 14:36 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/12 "2023-04-20T14:36:23Z")

</div>

> [@Wingtip](#):
>
> 增加60秒计时器不会提供什么？

我昨天将其增加到10分钟

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [2023年四月20日 14:37 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/13 "2023-04-20T14:37:59Z")

</div>

太好了，我还以为他是在发布 2016 年的原始提交。那么对我们来说有什么好处吗？

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2023年四月20日 14:38 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/14 "2023-04-20T14:38:31Z")

</div>

> [@Wingtip](#):
>
> 切换方法有文档记录吗？

你可以查看 [Move from standalone container to separate web and data containers](https://meta.discourse.org/t/move-from-standalone-container-to-separate-web-and-data-containers/29413)

> [@Wingtip](#):
>
> 还有其他增加 60 秒计时器无法提供的优势吗？

你可以在旧容器继续运行的同时构建一个新容器。你不需要关闭数据库来构建一个新容器。

> [@Wingtip](#):
>
> 所以对我们来说有什么好处吗？

现在有 10 分钟的时间关闭 postgres，这应该可以解决你目前的问题。一旦你再进行一次重建，你将拥有 10 分钟而不是一分钟。

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [2023年四月20日 14:40 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/15 "2023-04-20T14:40:34Z")

</div>

那位家伙刚刚构建了一个全新的两个容器实例，然后从备份恢复。我们绝对不会在没有充分理由的情况下这样做，我只是不得不在大约两个月前这样做，以避免 PG13 升级所需的磁盘空间。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2023年四月20日 14:41 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/16 "2023-04-20T14:41:41Z")

</div>

如果你不在 PG13 上，你应该修复它。

我会启动一个新服务器并迁移到那里。

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [2023年四月20日 14:42 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/17 "2023-04-20T14:42:20Z")

</div>

我们现在，_那个_最终是不可避免的！除了数据库，我们还需要从不再受支持的 18.04LTS 进行升级。

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2023年四月21日 22:04 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/19 "2023-04-21T22:04:40Z")

</div>

拥有如此庞大的数据库，您应该将其移至专用容器。

这将大大加快重建速度，并使一切变得更简单。

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [2023年四月25日 14:58 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/20 "2023-04-25T14:58:35Z")

</div>

如果能有关于如何操作的文档，而无需从头开始完全重建，那么恢复备份我们将肯定考虑。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2023年四月25日 14:59 UTC](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366/21 "2023-04-25T14:59:13Z")

</div>

所以您想[快速迁移到单独的 Web 和数据容器](https://meta.discourse.org/t/migrate-quickly-to-separate-web-and-data-containers/153355)

[下一頁](https://meta.discourse.org/t/problem-rebuilding-because-of-slow-database-shutdown/262366.md?page=2)
