# Database cleanup before PostgreSQL 18 upgrade: recommended retention period for log/statistics tables?

**URL:** https://meta.discourse.org/t/database-cleanup-before-postgresql-18-upgrade-recommended-retention-period-for-log-statistics-tables/409130
**Category:** Support
**Created:** [August 3, 2026, 6:55am UTC](https://meta.discourse.org/t/database-cleanup-before-postgresql-18-upgrade-recommended-retention-period-for-log-statistics-tables/409130 "2026-08-03T06:55:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![scavin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/scavin/32/115938_2.png) [@scavin](https://meta.discourse.org/u/scavin)
#### Post date: [August 3, 2026, 6:55am UTC](https://meta.discourse.org/t/database-cleanup-before-postgresql-18-upgrade-recommended-retention-period-for-log-statistics-tables/409130/1 "2026-08-03T06:55:29Z")

</div>

Hi everyone,

I saw the PostgreSQL 18 upgrade guide for self-hosted Discourse:

> [@PostgreSQL 18 update for self-hosters](https://meta.discourse.org/t/postgresql-18-update-for-self-hosters/406194):
>
> warning WARNING! If your database is very large, you will need a lot of extra disk space (3x database size) and should be very careful with this upgrade! We’ve just landed changes to upgrade our Docker image to PostgreSQL 18. Any site admins rebuilding Discourse from the command line will be upgraded to PostgreSQL 18 from the previous PostgreSQL 15. Note that if you held back from upgrading when the PostgreSQL 15 update [happened back in 2025](https://meta.discourse.org/t/postgresql-15-update/349515), you can skip that upgrade and go straight to P…

It mentions that the upgrade process requires a significant amount of additional disk space, so I started checking my disk usage and database size before attempting the migration.

My current disk usage:

```plaintext
/dev/sda1 97G 44G 54G 45% /

```

My Discourse database size is about 21 GB. I found that most of the space is not used by posts, but by several statistics and log tables.

The largest tables are:

```plaintext
topic_views 5.2 GB
post_timings 1.9 GB
browser_pageview_events 1.8 GB
ai_api_audit_logs 1.6 GB
incoming_links 1.5 GB
user_auth_token_logs 1.2 GB

```

For comparison:

```plaintext
posts 844 MB

```

I checked the data ranges:

```plaintext
topic_views:
2015-04-03 ~ 2026-08-03

incoming_links:
2015-04-09 ~ 2026-08-03

user_auth_token_logs:
2021-08-15 ~ 2026-08-03

ai_api_audit_logs:
2026-02-04 ~ 2026-08-03

browser_pageview_events:
2026-05-28 ~ 2026-08-03

```

I understand that these tables have different purposes, but I am not sure what retention periods are considered reasonable for a production Discourse instance.

My questions:

1. For `user_auth_token_logs`, how long do you usually keep the records?

2. For tables like `incoming_links`, `topic_views`, and `post_timings`, do you normally keep all historical data, or periodically remove older records?

3. Is there any recommended cleanup or maintenance procedure before a PostgreSQL major version upgrade?

So far, I have not deleted anything. I only ran:

```bash
vacuumdb --analyze discourse

```

My goal is to free unnecessary disk space before upgrading to PostgreSQL 18, while keeping normal Discourse functionality and useful audit information.

I would appreciate any recommendations or real-world experience from people running self-hosted Discourse sites.

Thanks!

---

<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: [August 3, 2026, 2:40pm UTC](https://meta.discourse.org/t/database-cleanup-before-postgresql-18-upgrade-recommended-retention-period-for-log-statistics-tables/409130/2 "2026-08-03T14:40:57Z")

</div>

It’s a good question, I think. But note that the linked topic has been updated and now says you need 2X database size as free disk space - which, it seems, you do already have.

In my own case, I see I have many unpruned docker images, a large swap file, all my backups, and in one case 4G of journal files too.

So, as a complement to reducing the size of the database on disk, one can look at other things on disk which can be reduced.

---

<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: [August 3, 2026, 3:27pm UTC](https://meta.discourse.org/t/database-cleanup-before-postgresql-18-upgrade-recommended-retention-period-for-log-statistics-tables/409130/3 "2026-08-03T15:27:09Z")

</div>

> [@scavin](#):
>
> `ai_api_audit_logs 1.6 GB`

You can control the retention period of this one using site settings.

> [@scavin](#):
>
> 1. For tables like `incoming_links`, `topic_views`, and `post_timings`, do you normally keep all historical data, or periodically remove older records?

Those are meant to be kept forever, and they are usually the largest tables in most instances, so I wouldn’t worry about that.
