# 升级失败 PG::UniqueViolation -- 无法创建唯一索引 "unique\_post\_links\_ccnew"

**URL:** <https://meta.discourse.org/t/upgrade-failed-pg-uniqueviolation-could-not-create-unique-index-unique-post-links-ccnew/195040>\
**Category:** Self-hosting\
**Created:** [2021年六月25日 18:32 UTC](https://meta.discourse.org/t/upgrade-failed-pg-uniqueviolation-could-not-create-unique-index-unique-post-links-ccnew/195040 "2021-06-25T18:32:46Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [2021年六月25日 18:32 UTC](https://meta.discourse.org/t/upgrade-failed-pg-uniqueviolation-could-not-create-unique-index-unique-post-links-ccnew/195040/1 "2021-06-25T18:32:46Z")

</div>

我的标准安装升级至 `2.8.0.beta2` 时失败，报错如下：

```
PG::UniqueViolation: ERROR: could not create unique index "unique_post_links_ccnew"

```

该错误来自：

```
I, [2021-06-25T18:35:47.756688 #1] INFO -- : == 20210525112226 RemoveLengthConstrainFromTopicLinkUrl: migrating ============

```

看起来并非索引损坏。

这有帮助吗？

```plaintext
[6] pry(main)> TopicLink.where(url: bad_url).pluck(:id, :topic_id, :post_id, :user_id)
=> [[15733, 14351, 63616, 911], [16131, 14505, 63986, 911], [27141, 14505, 63986, 1152]]

```

我最终只是给重复的 URL 追加了 `badX`，之后便成功了。不过我还不太清楚这可能会在未来引发哪些问题……

---

<div class="post-metadata">

**Author:** ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)\
**Post date:** [2022年一月17日 20:13 UTC](https://meta.discourse.org/t/upgrade-failed-pg-uniqueviolation-could-not-create-unique-index-unique-post-links-ccnew/195040/2 "2022-01-17T20:13:23Z")

</div>

根据我们的经验，早期版本的 Postgres 中存在大量所谓的“不可能”的索引损坏错误。您是否仍然遇到这种情况？

---

<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:** [2022年一月18日 03:05 UTC](https://meta.discourse.org/t/upgrade-failed-pg-uniqueviolation-could-not-create-unique-index-unique-post-links-ccnew/195040/3 "2022-01-18T03:05:10Z")

</div>

我好像自从升级到 pg12 后就没见过它了。

---

<div class="post-metadata">

**Author:** ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)\
**Post date:** [2022年一月18日 06:32 UTC](https://meta.discourse.org/t/upgrade-failed-pg-uniqueviolation-could-not-create-unique-index-unique-post-links-ccnew/195040/4 "2022-01-18T06:32:28Z")

</div>

我们一直到 PG13，我认为这（损坏的索引）基本上为我们解决了问题，对吗 @sam？

---

<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:** [2022年一月18日 06:53 UTC](https://meta.discourse.org/t/upgrade-failed-pg-uniqueviolation-could-not-create-unique-index-unique-post-links-ccnew/195040/5 "2022-01-18T06:53:31Z")

</div>

@saj 已解决此问题。我们已在基础架构中解决了它。大多数自托管者已设法解决。

我们过去用于升级开源映像中 PG 的机制，由于 PG 版本之间底层 libc 的更改，存在一些固有的安全问题。

我认为这个问题现在在更广泛的生态系统中已基本消失，尽管一些旧的升级可能仍会遇到它。修复我们现有的开源升级脚本并处理庞大的 PG 升级矩阵将是一场噩梦。

尽管如此，至少我们现在非常清楚问题的根源，并且能够更好地应对它。

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2022年二月17日 06:54 UTC](https://meta.discourse.org/t/upgrade-failed-pg-uniqueviolation-could-not-create-unique-index-unique-post-links-ccnew/195040/6 "2022-02-17T06:54:28Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
