# 502 errors when renaming large (by topic count) categories

**URL:** https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107
**Category:** Bug
**Created:** [September 15, 2020, 4:32pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107 "2020-09-15T16:32:24Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 15, 2020, 4:32pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/1 "2020-09-15T16:32:24Z")

</div>

Hi.  
I’ve also posted here: [Moving posts returns 502 bad gateway - #63 by Canapin](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/63)  
but it seems that the error occurs in other contexts. I can’t rename a category:

 ![image](https://global.discourse-cdn.com/meta/original/3X/3/f/3f1a72040018956158af01539f87b0850d76505c.png)

Js console:

 ![image](https://global.discourse-cdn.com/meta/original/3X/d/8/d826613805901d586ea4061155b8e136823963bb.png)

Same in safe-mode (I don’t have custom themes or plugins anyway, except data-explorer), version 2.6.0.beta2  
My server is on Ubuntu 18, has 4 vCPU, 8 GB ram, 160 GB disk space.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [September 15, 2020, 4:37pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/2 "2020-09-15T16:37:11Z")

</div>

Does it take a long time for the error to surface? I think you’re running into a timeout because of a heavy query.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 15, 2020, 4:39pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/3 "2020-09-15T16:39:37Z")

</div>

20-30 seconds maybe? Also, it doesn’t happen when renaming other categories. 🤔

edit: also happens on my development forum (fairly strong computer with a ubuntu subsystem).

re-edit:

1. splits by smaller batches seem to work. So Discourse doesn’t like to move more than 3000 topics, even of a more powerful server (I upscaled to a 16 vCPU / 32 GB). So I guess that this issue is different from renaming my categories. I still couldn’t rename the category.

2. I created a new category B (which I can rename at will), moved all the topics from the category A (which I can’t rename) to the category B: then I can’t rename category B anymore, and I can rename the category A.  
I tried to rename my categories, choosing a category with more posts in it each time. Category with more content (more posts, larger topics) seems to take more time to be renamed.

There are 2 categories that I can’t rename (seems to take too much time indeed…):

 ![image](https://global.discourse-cdn.com/meta/original/3X/c/0/c097b87028410325546bbcc60fa301c1f8248d11.png)

I can rename Public profile messages, which contain only topics without replies.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [September 15, 2020, 9:24pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/4 "2020-09-15T21:24:00Z")

</div>

> [@Canapin](#):
>
> 20-30 seconds maybe?

30 seconds is the nginx timeout, so that makes sense. It just needs more time than that.  
If I were you I would try this from a rails console.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 15, 2020, 9:26pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/5 "2020-09-15T21:26:08Z")

</div>

I thought doing it this way, but I wasn’t sure it would be safe: I wonder why it would take that much time just to rename a category title in Discourse 🤔  
Does it need to check stuff I don’t think about?

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [September 15, 2020, 9:31pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/6 "2020-09-15T21:31:07Z")

</div>

> [@Canapin](#):
>
> Does it need to check stuff I don’t think about?

Apparently 🙂  
I guess it might be recalculating some stats. What you could do is connect to Postgres and run something like

```plaintext
SELECT pid, age(query_start, clock_timestamp()), usename, query 
FROM pg_stat_activity 
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%' 
ORDER BY query_start desc;

```

to see what it is what is taking so long.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 15, 2020, 9:42pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/7 "2020-09-15T21:42:08Z")

</div>

Thank you!  
I’m not familiar with that though. Should I run this query while Discourse is trying to change the category name?

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [September 15, 2020, 9:52pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/8 "2020-09-15T21:52:13Z")

</div>

Yes, it will show you the currently running queries, so you should run it while waiting. You have 30 seconds 😉

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 16, 2020, 11:46am UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/9 "2020-09-16T11:46:35Z")

</div>

I ran the query about 10 seconds after trying to change the category name, and here’s what it returns:

 ![image](https://global.discourse-cdn.com/meta/original/3X/3/f/3f434b8d1cec091bff5237c8efcd609943cc07cb.png)

I see nothing weird? 🤔

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [September 16, 2020, 11:57am UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/10 "2020-09-16T11:57:19Z")

</div>

I see a very trivial query (`UPDATE categories...`) that looks like it is taking up 13 seconds already…  
What happens if you run it manually?

EDIT if this is an imported forum, it might help to backup and then restore it.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 16, 2020, 1:59pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/11 "2020-09-16T13:59:48Z")

</div>

Since I have the same issue on my development install on my home computer, I tried on my dev forum.

I tried to change the category name through Discourse interface. The error I’ve shown in my first post occured and my ram was filled up:  
 ![image](https://global.discourse-cdn.com/meta/original/3X/a/a/aa8b19b5c906a85fd77fedd4e873382dfecd0b73.png)

Your query shows:

 ![image](https://global.discourse-cdn.com/meta/original/3X/1/6/166298d20fe7e3fa4b8c84b74f2a17ece2a3391f.png)

I tried updating from the rails console,  
Tried these two commands:

```ruby
ActiveRecord::Base.connection.execute("UPDATE categories SET name = 'General Discussion', topic_template = '', sort_order = '', default_view = '', read_only_banner = '', name_lower = 'general_discussion', updated_at = '2020-09-16 13:36:50.014192' WHERE categories.id = 21")

```

```ruby
Category.where(id: 21).update(name: "General")

```

Both worked like a charm.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [September 16, 2020, 6:17pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/12 "2020-09-16T18:17:38Z")

</div>

That’s… interesting… 🤔

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 19, 2020, 11:09am UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/13 "2020-09-19T11:09:20Z")

</div>

Update: I tried to rename a category on my other, live forum (different data/server/os/etc, but same Discourse version).

Changing a category name is slow (~10s). I tried multiple times and also encountered a 502 error once.

I feel like this is an issue from Discourse maybe related to a recent update? 🤔  
Both forums use 2.6.0.beta2.

Can someone try to change a category name (a category with thousands, or tens of thousands topics) on their own forum, with the same Discours version to see if it is slow or generates a 502?

---

<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: [September 21, 2020, 12:13am UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/14 "2020-09-21T00:13:08Z")

</div>

> [@Canapin](#):
>
> I feel like this is an issue from Discourse maybe related to a recent update?

Did renaming categories with a large number of topics work fast for you in previous Discourse version?

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 21, 2020, 12:24am UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/15 "2020-09-21T00:24:06Z")

</div>

I don’t remember having a 502 error on my previous forum (which was also imported), but I haven’t renamed a category since the import 3 years ago. I don’t remember if renaming a category was slow.  
As for my new, larger forum, it was imported on the Discourse’s version I’m mentioning so I can’t compare with an older version.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [September 25, 2020, 5:17pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/16 "2020-09-25T17:17:24Z")

</div>

> [@Canapin](#):
>
> I tried updating from the rails console,  
> Tried these two commands:
> 
> ```plaintext
> ActiveRecord::Base.connection.execute("UPDATE categories SET name = 'General Discussion', topic_template = '', sort_order = '', default_view = '', read_only_banner = '', name_lower = 'general_discussion', updated_at = '2020-09-16 13:36:50.014192' WHERE categories.id = 21")
> 
> ```
> 
> ```plaintext
> Category.where(id: 21).update(name: "General")
> 
> ```
> 
> Both worked like a charm.

Just a correction about this: I think I previously did `Category.where(id: 21).update(name: "General")` for a category from which I moved the topics out or something like that, because I had to use this command multiple times on various categories right now, and it appears that the command duration is proportional to the number of posts in the target category.  
So basically, it’s the same issue as when I update the category from the interface, except that it won’t timeout in the command line.

It took more maybe one minute for a category with more than 30000 topics, more than, 30 seconds on a topic with more than 15000 topics, and it was fast for categories with few topics.

---

<div class="post-metadata">

### Author: ![SimeonGriggs](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@SimeonGriggs](https://meta.discourse.org/u/SimeonGriggs)
#### Post date: [November 3, 2020, 8:18pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/17 "2020-11-03T20:18:17Z")

</div>

Our forum has been getting increasingly error-prone over the last 6 months. I’ve just doubled our server resources (16 CPU’s, 64GB RAM) and while it runs much better, I’ve found a reproducible error.

Just trying to **rename one category** gives a 502 error every time.

---

<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: [November 3, 2020, 10:01pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/18 "2020-11-03T22:01:46Z")

</div>

> [@SimeonGriggs](#):
>
> I’ve just doubled our server resources (16 CPU’s, 64GB RAM)

Did you adjust the settings in the app.yml accordingly?

> [@SimeonGriggs](#):
>
> Just trying to **rename one category** gives a 502 error every time.

You should look in the logs to see if you get a hint. Is there something special about that category? Perhaps you have a database index corruption?

---

<div class="post-metadata">

### Author: ![SimeonGriggs](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@SimeonGriggs](https://meta.discourse.org/u/SimeonGriggs)
#### Post date: [November 3, 2020, 10:26pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/19 "2020-11-03T22:26:54Z")

</div>

> [@pfaffman](#):
>
> Did you adjust the settings in the app.yml accordingly?

Yep!

> [@pfaffman](#):
>
> You should look in the logs to see if you get a hint. Is there something special about that category? Perhaps you have a database index corruption?

Is that just looking at `forumwebsite.com/logs`? I don’t see errors that specifically relate to the action of renaming a category.

I see _lots_ of things in Info, but again nothing specific to the action.

Should I be trying to clear _any_ errors?

---

<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: [November 3, 2020, 10:36pm UTC](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107/20 "2020-11-03T22:36:19Z")

</div>

you can look at /admin/logs (or something like that) in the web interface and something like

```
tail -f /var/discourse/shared/standalone/logs/rails/production.log

```

That’s from memory and typed with fingers, so I’d recommend using some tab-completion. 🙂

[Next page](https://meta.discourse.org/t/502-errors-when-renaming-large-by-topic-count-categories/164107.md?page=2)
