# 如果容器未启动，如何运行sql查询？（重建失败）

**URL:** https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126
**Category:** Self-hosting
**Created:** [2023年一月18日 08:16 UTC](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126 "2023-01-18T08:16:14Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)
#### Post date: [2023年一月18日 08:59 UTC](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126/2 "2023-01-18T08:59:08Z")

</div>

你好 Ionut，

感谢你的 bug 报告。我会查看这个问题，但我认为你的数据库中存在一个索引问题，最近的一次迁移试图修复它，但有些问题需要人工干预才能解决。

我认为引入失败迁移的提交是这个：

> <https://github.com/discourse/discourse/commit/8ee71d439be094f777f19a8fca44c9c2a914d479>
>
> In Discourse, there are many migration files where we CREATE INDEX CONCURRENTLY …which requires us to set disable\_ddl\_transaction!. Setting disable\_ddl\_transaction! in a migration file runs the SQL statements outside of a transaction. The implication of this is that there is no ROLLBACK should any of the SQL statements fail.
> 
> We have seen lock timeouts occuring when running CREATE INDEX CONCURRENTLY. When that happens, the index would still have been created but marked as invalid by Postgres.
> 
> Per the postgres documentation:
> 
> \> If a problem arises while scanning the table, such as a deadlock or a uniqueness violation in a unique index, the CREATE INDEX command will fail but leave behind an “invalid” index. This index will be ignored for querying purposes because it might be incomplete; however it will still consume update overhead.
> \> The recommended recovery method in such cases is to drop the index and try again to perform CREATE INDEX CONCURRENTLY . (Another possibility is to rebuild the index with REINDEX INDEX CONCURRENTLY ).
> 
> When such scenarios happen, we are supposed to either drop and create the index again or run a REINDEX operation. However, I noticed today that we have not been doing so in Discourse. Instead, we’ve been incorrectly working around the problem by checking for the index existence before creating the index in order to make the migration idempotent. What this potentially mean is that we might have invalid indexes which are lying around in the database which PG will ignore for querying purposes.
> 
> This commits adds a migration which queries for all the
> invalid indexes in the \`public\` namespace and reindexes them.

你可以尝试将你的 Discourse 实例固定到之前的版本，即提交 `690e2f15ab9549486aaa6750e1093c1336bf17f2`。编辑你的 `app.yml` 文件，并在 [`params` 下设置 `version` 键](https://github.com/discourse/discourse_docker/blob/main/samples/standalone.yml#L37)。确保取消注释该键！

然后，一切都应该可以启动，你应该能够删除重复的 `vuejs` 标签，但这可能会产生一些不希望的效果，如果重复的标签正在使用中。我们会尝试找到一个更好的解决方案，但在此之前，这应该可以工作。

---

_[View the full topic](https://meta.discourse.org/t/how-does-one-run-sql-queries-if-the-container-doesnt-start-rebuild-failed/252126)._
