PostgreSQL 13 update

:warning: WARNING! If your database is very large, you will need a lot of extra disk space (2x database size) and should be very careful with this upgrade!

We’ve just landed changes to upgrade our Docker image to PostgreSQL 13. Any site admins rebuilding Discourse from the command line will be upgraded to PostgreSQL 13 from the previous PostgreSQL 12. Note that if you held back from upgrading when the PostgreSQL 12 update happened back in May, you can skip that upgrade and go straight to PostgreSQL 13.

If you had held back the upgrade previously, change the PostgreSQL template in app.yml from templates/postgres.10.template.yml to templates/postgres.template.yml.

As with any upgrade, it is strongly advised to take a backup before doing anything.

Updating

Official Install Guide (single container)

On you next rebuild, you will see this message at the end:

-------------------------------------------------------------------------------------
UPGRADE OF POSTGRES COMPLETE

Old 12 database is stored at /shared/postgres_data_old

To complete the upgrade, rebuild again using:

./launcher rebuild app
-------------------------------------------------------------------------------------

That means everything went well in the upgrade! You just need to issue a new rebuild to get your site back and running.

Data Container Install

If you are running a setup with a dedicated data container based in the sample supplied in our discourse_docker repository, you want to be sure you are shutting down PostgreSQL in a safe and clean way.

Nowadays, we have background jobs running queries spanning several minutes, so shutting down the web container will help the data container be shutdown safely.

./launcher stop web_only
./launcher stop data
./launcher rebuild data
./launcher rebuild data
./launcher rebuild web_only

Before issuing the first rebuild to the data container, you can tail the PostgreSQL log to see if it was shutdown properly.

Running a tail -f shared/data/log/var-log/postgres/current should give you the following log if it was clean:

2020-05-13 18:33:33.457 UTC [36] LOG:  received smart shutdown request
2020-05-13 18:33:33.464 UTC [36] LOG:  worker process: logical replication launcher (PID 52) exited with exit code 1
2020-05-13 18:33:33.465 UTC [47] LOG:  shutting down
2020-05-13 18:33:33.479 UTC [36] LOG:  database system is shut down

Doing a manual update / space constrained environments

:warning::warning::warning:
YOU MUST BACKUP THE POSTGRES_DATA BEFORE TRYING THIS
:warning::warning::warning:

If you are in a constrained space environment without any way to get more space you can try the following:

./launcher stop app #(or both web_only and data if that is your case)
mkdir -p /var/discourse/shared/standalone/postgres_data_new
docker run --rm \
	-v /var/discourse/shared/standalone/postgres_data:/var/lib/postgresql/12/data \
	-v /var/discourse/shared/standalone/postgres_data_new:/var/lib/postgresql/13/data \
	tianon/postgres-upgrade:12-to-13
mv /var/discourse/shared/standalone/postgres_data /var/discourse/shared/standalone/postgres_data_old
mv /var/discourse/shared/standalone/postgres_data_new /var/discourse/shared/standalone/postgres_data
./launcher rebuild app #(or first data and then web_only if that is your case)

On my tests this procedure requires less than 1x your current database size in free space.

Postponing the update

If you need to postpone the update during your next rebuild, you can swap the PostgreSQL template on your app.yml file by changing "templates/postgres.template.yml" to "templates/postgres.12.template.yml".

This is not recommended, as some site admins will forget to revert the change afterwards.

Post update optional tasks

Optimizing PostgreSQL statistics

After the update, the new PostgreSQL won’t have table statistics on hand. You can generate those using:

cd /var/discourse
./launcher enter app
su postgres
psql
\connect discourse
VACUUM VERBOSE ANALYZE;
\q
exit
exit

Or this one-line version of the above:

/var/discourse/launcher run app "echo 'vacuum verbose analyze;' | su postgres -c 'psql discourse'"

Re-creating the indexes

This upgrade main feature is great file savings in our largest table in every instance, the post_timings table and it’s indexes. After doing a successful update you will need to run a command to re-build the indexes and reap the benefits.

cd /var/discourse
./launcher enter app
su postgres
psql
\connect discourse
REINDEX SCHEMA CONCURRENTLY public;
\q
exit
exit

If you can check the post_timings size before and after the REINDEX that would be a cool stat to share here!

You can use the below query to check the 20 largest data objects, run it before the reindex and after:

WITH RECURSIVE pg_inherit(inhrelid, inhparent) AS
    (select inhrelid, inhparent
    FROM pg_inherits
    UNION
    SELECT child.inhrelid, parent.inhparent
    FROM pg_inherit child, pg_inherits parent
    WHERE child.inhparent = parent.inhrelid),
pg_inherit_short AS (SELECT * FROM pg_inherit WHERE inhparent NOT IN (SELECT inhrelid FROM pg_inherit))
SELECT table_schema
    , TABLE_NAME
    , row_estimate
    , pg_size_pretty(total_bytes) AS total
    , pg_size_pretty(index_bytes) AS INDEX
    , pg_size_pretty(toast_bytes) AS toast
    , pg_size_pretty(table_bytes) AS TABLE
  FROM (
    SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes
    FROM (
         SELECT c.oid
              , nspname AS table_schema
              , relname AS TABLE_NAME
              , SUM(c.reltuples) OVER (partition BY parent) AS row_estimate
              , SUM(pg_total_relation_size(c.oid)) OVER (partition BY parent) AS total_bytes
              , SUM(pg_indexes_size(c.oid)) OVER (partition BY parent) AS index_bytes
              , SUM(pg_total_relation_size(reltoastrelid)) OVER (partition BY parent) AS toast_bytes
              , parent
          FROM (
                SELECT pg_class.oid
                    , reltuples
                    , relname
                    , relnamespace
                    , pg_class.reltoastrelid
                    , COALESCE(inhparent, pg_class.oid) parent
                FROM pg_class
                    LEFT JOIN pg_inherit_short ON inhrelid = oid
                WHERE relkind IN ('r', 'p')
             ) c
             LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
  ) a
  WHERE oid = parent
) a
ORDER BY total_bytes DESC LIMIT 20;

Cleaning up old data

For a standard install, you can delete the old data in PG12 format with the following command:

cd /var/discourse
./launcher cleanup

If you have a separate data container, you’ll need to remove the backup copy like this:

rm -fr /var/discourse/shared/data/postgres_data_old/

FAQ

The source cluster was not shut down cleanly

If you get a upgrade failed with the above message, you can try a simpler approach to get it back into a better state.

Restart the old container with ./launcher start app. Wait a few minutes until it is back up.

Now shut it down again with ./launcher stop app. After that tail the logs to see if it was a clean one:

tail -f shared/data/log/var-log/postgres/current
2020-05-13 18:33:33.457 UTC [36] LOG:  received smart shutdown request
2020-05-13 18:33:33.464 UTC [36] LOG:  worker process: logical replication launcher (PID 52) exited with exit code 1
2020-05-13 18:33:33.465 UTC [47] LOG:  shutting down
2020-05-13 18:33:33.479 UTC [36] LOG:  database system is shut down

If the logs look like above, you can now try to upgrade again using ./launcher rebuild app.

lc_collate values for database “postgres” do not match

This errors happens if you are using non-default locales for your database. It was reported that you need 3 variables for it to succeed. Ensure that the env: section of you app.yml file has the 3 lines:

  LC_ALL: en_US.UTF-8
  LANG: en_US.UTF-8
  LANGUAGE: en_US.UTF-8

Changing en_US.UTF-8 to your locale.

Every rebuild does the upgrade again aka upgrade loop

When this happens your upgrade logs will contain

mv: cannot move '/shared/postgres_data' to '/shared/postgres_data_old/postgres_data': Directory not empty
mv: cannot move '/shared/postgres_data_new' to '/shared/postgres_data/postgres_data_new': Directory not empty

This means that there are still files from the last upgrade lingering around. Move those elsewhere before continuing.

Upgrade Complete suggestion scripts - do I need to do anything?

Once the upgrade completes, you’ll see output from the pg_upgrade message saying:

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
    ./analyze_new_cluster.sh

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh

You can safely ignore this message.

I did skip the PostgreSQL 12 update, what to do now?

You can follow the standard instructions at the top of this guide and they will upgrade from your version to 13 without issues.

If you are following the space constrained instructions, adapt the version numbers accordingly.

38 Likes

This is a bit older already but I was just doing a Reindex as I feel it was needed with indexes growing far too big.

For one I got the message that these two indexes couldn’t be reindexed in concurrent mode…

WARNING:  cannot reindex invalid index "public.index_post_timings_on_user_id_ccnew" concurrently, skipping
WARNING:  cannot reindex invalid index "public.post_timings_unique_ccnew" concurrently, skipping

I’m not sure if it would be necessary to reindex them anyways… but apart from that I went from this

table_name                           | row_estimate | table_size | index_size | total_size
-----------------------------------------------------------------------------------
post_timings                         | 838056320    | 39 GB      | 91 GB      | 131 GB
user_auth_token_logs                 | 55448312     | 13 GB      | 2753 MB    | 16 GB
user_visits                          | 6297707      | 364 MB     | 13 GB      | 14 GB
topic_views                          | 68461928     | 3154 MB    | 10 GB      | 13 GB
posts                                | 3709657      | 4994 MB    | 1014 MB    | 6008 MB
user_actions                         | 12126485     | 902 MB     | 3212 MB    | 4114 MB
post_search_data                     | 3655090      | 3250 MB    | 838 MB     | 4088 MB
topic_users                          | 19241922     | 1758 MB    | 1765 MB    | 3523 MB
incoming_links                       | 17313212     | 1108 MB    | 1027 MB    | 2135 MB
notifications                        | 3412445      | 1248 MB    | 461 MB     | 1709 MB
topic_links                          | 1040165      | 219 MB     | 592 MB     | 811 MB
user_profile_views                   | 4272621      | 234 MB     | 552 MB     | 786 MB
users                                | 58888        | 45 MB      | 734 MB     | 780 MB
email_logs                           | 1647494      | 470 MB     | 155 MB     | 625 MB
top_topics                           | 80524        | 69 MB      | 497 MB     | 566 MB
topic_link_clicks                    | 4687408      | 285 MB     | 243 MB     | 528 MB
post_actions                         | 3304748      | 244 MB     | 278 MB     | 522 MB
optimized_images                     | 1214201      | 361 MB     | 146 MB     | 506 MB
unsubscribe_keys                     | 1116854      | 191 MB     | 283 MB     | 474 MB
post_stats                           | 3703477      | 242 MB     | 159 MB     | 400 MB
post_revisions                       | 211684       | 376 MB     | 15 MB      | 391 MB
uploads                              | 321893       | 113 MB     | 224 MB     | 337 MB
topics                               | 301795       | 198 MB     | 71 MB      | 268 MB
post_custom_fields                   | 1175950      | 134 MB     | 107 MB     | 240 MB
topic_search_data                    | 294171       | 171 MB     | 64 MB      | 236 MB
search_logs                          | 711491       | 78 MB      | 104 MB     | 182 MB
post_replies                         | 1445383      | 84 MB      | 62 MB      | 146 MB
upload_references                    | 618769       | 47 MB      | 98 MB      | 145 MB
permalinks                           | 1003118      | 81 MB      | 60 MB      | 141 MB
sidebar_section_links                | 957929       | 71 MB      | 66 MB      | 137 MB
skipped_email_logs                   | 182522       | 32 MB      | 96 MB      | 128 MB
user_stats                           | 58888        | 110 MB     | 8648 kB    | 118 MB
post_uploads                         | 407543       | 26 MB      | 92 MB      | 118 MB
user_badges                          | 371916       | 39 MB      | 65 MB      | 105 MB
user_histories                       | 196073       | 62 MB      | 41 MB      | 102 MB
draft_sequences                      | 728706       | 46 MB      | 42 MB      | 88 MB
post_hotlinked_media                 | 413007       | 52 MB      | 35 MB      | 87 MB
directory_items                      | 350825       | 32 MB      | 54 MB      | 86 MB
given_daily_likes                    | 935113       | 42 MB      | 27 MB      | 69 MB
user_auth_tokens                     | 49808        | 27 MB      | 34 MB      | 60 MB
quoted_posts                         | 380076       | 23 MB      | 33 MB      | 57 MB
topic_allowed_users                  | 416505       | 25 MB      | 30 MB      | 56 MB
scheduler_stats                      | 263695       | 40 MB      | 9448 kB    | 49 MB
user_uploads                         | 337637       | 19 MB      | 28 MB      | 47 MB
user_options                         | 87346        | 27 MB      | 6152 kB    | 33 MB
email_tokens                         | 77848        | 13 MB      | 10 MB      | 24 MB
reviewables                          | 31052        | 6368 kB    | 17 MB      | 23 MB
topic_custom_fields                  | 130005       | 10008 kB   | 13 MB      | 22 MB
poll_votes                           | 173402       | 12 MB      | 11 MB      | 22 MB
group_users                          | 133293       | 12 MB      | 8888 kB    | 21 MB
user_emails                          | 58330        | 6136 kB    | 14 MB      | 20 MB
dismissed_topic_users                | 168660       | 11 MB      | 8240 kB    | 19 MB
shelved_notifications                | 212444       | 9360 kB    | 9392 kB    | 18 MB
user_custom_fields                   | 105860       | 8848 kB    | 6784 kB    | 15 MB
bookmarks                            | 42609        | 5192 kB    | 9872 kB    | 15 MB
user_avatars                         | 58646        | 3864 kB    | 7504 kB    | 11 MB
user_search_data                     | 58873        | 5704 kB    | 5216 kB    | 11 MB
plugin_store_rows                    | 40561        | 7792 kB    | 2904 kB    | 10 MB
reviewable_histories                 | 61304        | 4768 kB    | 5400 kB    | 10168 kB
user_profiles                        | 58882        | 6224 kB    | 3320 kB    | 9544 kB
reviewable_scores                    | 34792        | 4352 kB    | 2888 kB    | 7240 kB
user_archived_messages               | 57104        | 3464 kB    | 2648 kB    | 6112 kB
stylesheet_cache                     | 98           | 5304 kB    | 192 kB     | 5496 kB
topic_thumbnails                     | 23356        | 1424 kB    | 3096 kB    | 4520 kB
web_crawler_requests                 | 3888         | 648 kB     | 3224 kB    | 3872 kB
incoming_referers                    | 12252        | 1656 kB    | 1896 kB    | 3552 kB
drafts                               | 2574         | 2720 kB    | 216 kB     | 2936 kB
poll_options                         | 11424        | 1600 kB    | 1224 kB    | 2824 kB
application_requests                 | 12631        | 896 kB     | 744 kB     | 1640 kB
group_histories                      | 7844         | 632 kB     | 600 kB     | 1232 kB
user_api_keys                        | 321          | 904 kB     | 312 kB     | 1216 kB
ignored_users                        | 5121         | 424 kB     | 384 kB     | 808 kB
topic_groups                         | 5745         | 448 kB     | 288 kB     | 736 kB
topic_allowed_groups                 | 5728         | 296 kB     | 440 kB     | 736 kB
polls                                | 3505         | 408 kB     | 320 kB     | 728 kB
push_subscriptions                   | 1359         | 664 kB     | 56 kB      | 720 kB
group_archived_messages              | 5698         | 376 kB     | 288 kB     | 664 kB
category_users                       | 3958         | 256 kB     | 392 kB     | 648 kB
categories                           | 101          | 536 kB     | 112 kB     | 648 kB
incoming_domains                     | 3109         | 216 kB     | 240 kB     | 456 kB
group_mentions                       | 2817         | 208 kB     | 240 kB     | 448 kB
topic_timers                         | 1714         | 224 kB     | 176 kB     | 400 kB
user_second_factors                  | 1062         | 240 kB     | 144 kB     | 384 kB
category_featured_topics             | 748          | 104 kB     | 272 kB     | 376 kB
schema_migration_details             | 1415         | 264 kB     | 112 kB     | 376 kB
user_notification_schedules          | 1055         | 144 kB     | 96 kB      | 240 kB
user_api_key_scopes                  | 933          | 112 kB     | 80 kB      | 192 kB
javascript_caches                    | 8            | 128 kB     | 64 kB      | 192 kB
theme_fields                         | 17           | 152 kB     | 32 kB      | 184 kB
user_security_keys                   | 40           | 56 kB      | 112 kB     | 168 kB
schema_migrations                    | 1413         | 104 kB     | 64 kB      | 168 kB
user_warnings                        | 431          | 64 kB      | 96 kB      | 160 kB
muted_users                          | 432          | 64 kB      | 96 kB      | 160 kB
badges                               | 54           | 104 kB     | 48 kB      | 152 kB
chat_channels                        | 1            | 48 kB      | 96 kB      | 144 kB
invites                              | 126          | 64 kB      | 80 kB      | 144 kB
do_not_disturb_timings               | 312          | 56 kB      | 80 kB      | 136 kB
groups                               | 12           | 64 kB      | 48 kB      | 112 kB
data_explorer_query_groups           | 6            | 40 kB      | 64 kB      | 104 kB
site_settings                        | 161          | 64 kB      | 32 kB      | 96 kB
screened_urls                        | 2            | 48 kB      | 48 kB      | 96 kB
screened_emails                      | 10           | 48 kB      | 48 kB      | 96 kB
email_change_requests                | 22           | 48 kB      | 48 kB      | 96 kB
data_explorer_queries                | 19           | 80 kB      | 16 kB      | 96 kB
linked_topics                        | 183          | 48 kB      | 48 kB      | 96 kB
screened_ip_addresses                | 16           | 48 kB      | 48 kB      | 96 kB
user_chat_channel_memberships        | 18           | 40 kB      | 48 kB      | 88 kB
invited_users                        | 34           | 40 kB      | 48 kB      | 88 kB
child_themes                         | 2            | 40 kB      | 48 kB      | 88 kB
category_search_data                 | 101          | 56 kB      | 32 kB      | 88 kB
topic_invites                        | 17           | 40 kB      | 48 kB      | 88 kB
sitemaps                             | 10           | 48 kB      | 32 kB      | 80 kB
category_groups                      | 142          | 48 kB      | 32 kB      | 80 kB
directory_columns                    | 10           | 48 kB      | 32 kB      | 80 kB
translation_overrides                | 27           | 48 kB      | 32 kB      | 80 kB
theme_modifier_sets                  | 6            | 48 kB      | 32 kB      | 80 kB
onceoff_logs                         | 36           | 48 kB      | 32 kB      | 80 kB
color_scheme_colors                  | 40           | 48 kB      | 32 kB      | 80 kB
badge_types                          | 3            | 48 kB      | 32 kB      | 80 kB
themes                               | 6            | 48 kB      | 32 kB      | 80 kB
category_custom_fields               | 78           | 48 kB      | 32 kB      | 80 kB
incoming_emails                      | 0            | 8192 bytes | 64 kB      | 72 kB
ar_internal_metadata                 | 1            | 48 kB      | 16 kB      | 64 kB
backup_metadata                      | 6            | 48 kB      | 16 kB      | 64 kB
badge_groupings                      | 5            | 48 kB      | 16 kB      | 64 kB
color_schemes                        | 4            | 48 kB      | 16 kB      | 64 kB
web_hook_event_types                 | 14           | 48 kB      | 16 kB      | 64 kB
user_fields                          | 2            | 48 kB      | 16 kB      | 64 kB
theme_settings                       | 10           | 48 kB      | 16 kB      | 64 kB
user_field_options                   | 11           | 48 kB      | 16 kB      | 64 kB
user_exports                         | 61           | 48 kB      | 16 kB      | 64 kB
remote_themes                        | 4            | 48 kB      | 16 kB      | 64 kB
chat_threads                         | 0            | 8192 bytes | 48 kB      | 56 kB
watched_words                        | 0            | 24 kB      | 32 kB      | 56 kB
post_action_types                    | 7            | 40 kB      | 16 kB      | 56 kB
chat_messages                        | 0            | 8192 bytes | 48 kB      | 56 kB
external_upload_stubs                | 0            | 8192 bytes | 40 kB      | 48 kB
group_requests                       | 0            | 8192 bytes | 32 kB      | 40 kB
category_tag_stats                   | 0            | 0 bytes    | 40 kB      | 40 kB
user_associated_accounts             | 0            | 8192 bytes | 24 kB      | 32 kB
published_pages                      | 0            | 8192 bytes | 24 kB      | 32 kB
single_sign_on_records               | 0            | 8192 bytes | 24 kB      | 32 kB
tag_search_data                      | 0            | 8192 bytes | 24 kB      | 32 kB
tags                                 | 0            | 8192 bytes | 24 kB      | 32 kB
theme_translation_overrides          | 0            | 8192 bytes | 24 kB      | 32 kB
oauth2_user_infos                    | 0            | 8192 bytes | 24 kB      | 32 kB
api_keys                             | 0            | 8192 bytes | 24 kB      | 32 kB
backup_draft_posts                   | 0            | 8192 bytes | 24 kB      | 32 kB
group_associated_groups              | 0            | 0 bytes    | 32 kB      | 32 kB
user_associated_groups               | 0            | 0 bytes    | 32 kB      | 32 kB
imap_sync_logs                       | 0            | 8192 bytes | 24 kB      | 32 kB
chat_message_revisions               | 0            | 8192 bytes | 24 kB      | 32 kB
tag_group_permissions                | 0            | 0 bytes    | 24 kB      | 24 kB
topic_embeds                         | 0            | 8192 bytes | 16 kB      | 24 kB
custom_emojis                        | 0            | 8192 bytes | 16 kB      | 24 kB
api_key_scopes                       | 0            | 8192 bytes | 16 kB      | 24 kB
user_ip_address_histories            | 0            | 8192 bytes | 16 kB      | 24 kB
user_open_ids                        | 0            | 8192 bytes | 16 kB      | 24 kB
group_custom_fields                  | 0            | 8192 bytes | 16 kB      | 24 kB
web_hook_events                      | 0            | 8192 bytes | 16 kB      | 24 kB
form_templates                       | 0            | 8192 bytes | 16 kB      | 24 kB
associated_groups                    | 0            | 8192 bytes | 16 kB      | 24 kB
post_reply_keys                      | 0            | 0 bytes    | 24 kB      | 24 kB
chat_channel_archives                | 0            | 8192 bytes | 16 kB      | 24 kB
tag_users                            | 0            | 0 bytes    | 24 kB      | 24 kB
chat_message_reactions               | 0            | 8192 bytes | 16 kB      | 24 kB
backup_draft_topics                  | 0            | 0 bytes    | 24 kB      | 24 kB
shared_drafts                        | 0            | 0 bytes    | 24 kB      | 24 kB
anonymous_users                      | 0            | 0 bytes    | 24 kB      | 24 kB
category_tags                        | 0            | 0 bytes    | 24 kB      | 24 kB
incoming_chat_webhooks               | 0            | 8192 bytes | 16 kB      | 24 kB
allowed_pm_users                     | 0            | 0 bytes    | 24 kB      | 24 kB
message_bus                          | 0            | 8192 bytes | 16 kB      | 24 kB
post_details                         | 0            | 8192 bytes | 16 kB      | 24 kB
group_category_notification_defaults | 0            | 0 bytes    | 16 kB      | 16 kB
category_settings                    | 0            | 0 bytes    | 16 kB      | 16 kB
chat_uploads                         | 0            | 0 bytes    | 16 kB      | 16 kB
embeddable_hosts                     | 0            | 8192 bytes | 8192 bytes | 16 kB
chat_webhook_events                  | 0            | 0 bytes    | 16 kB      | 16 kB
developers                           | 0            | 0 bytes    | 16 kB      | 16 kB
sidebar_sections                     | 0            | 0 bytes    | 16 kB      | 16 kB
user_statuses                        | 0            | 8192 bytes | 8192 bytes | 16 kB
category_required_tag_groups         | 0            | 0 bytes    | 16 kB      | 16 kB
tag_groups                           | 0            | 8192 bytes | 8192 bytes | 16 kB
tag_group_memberships                | 0            | 0 bytes    | 16 kB      | 16 kB
invited_groups                       | 0            | 0 bytes    | 16 kB      | 16 kB
chat_mentions                        | 0            | 0 bytes    | 16 kB      | 16 kB
chat_drafts                          | 0            | 8192 bytes | 8192 bytes | 16 kB
web_hooks                            | 0            | 8192 bytes | 8192 bytes | 16 kB
group_tag_notification_defaults      | 0            | 0 bytes    | 16 kB      | 16 kB
reviewable_claimed_topics            | 0            | 0 bytes    | 16 kB      | 16 kB
category_tag_groups                  | 0            | 0 bytes    | 16 kB      | 16 kB
direct_message_users                 | 0            | 0 bytes    | 16 kB      | 16 kB
topic_tags                           | 0            | 0 bytes    | 16 kB      | 16 kB
direct_message_channels              | 0            | 0 bytes    | 8192 bytes | 8192 bytes
sidebar_urls                         | 0            | 0 bytes    | 8192 bytes | 8192 bytes
web_hook_event_types_hooks           | 0            | 0 bytes    | 8192 bytes | 8192 bytes
groups_web_hooks                     | 0            | 0 bytes    | 8192 bytes | 8192 bytes
tags_web_hooks                       | 0            | 0 bytes    | 8192 bytes | 8192 bytes
categories_web_hooks                 | 0            | 0 bytes    | 8192 bytes | 8192 bytes
badge_posts                          | 0            | 0 bytes    | 0 bytes    | 0 bytes

to this

table_name                           | row_estimate | table_size | index_size | total_size
-----------------------------------------------------------------------------------
post_timings                         | 839165632    | 39 GB      | 60 GB      | 99 GB
user_auth_token_logs                 | 55837296     | 13 GB      | 1571 MB    | 14 GB
topic_views                          | 69342912     | 3155 MB    | 4833 MB    | 7988 MB
posts                                | 3703619      | 4994 MB    | 1111 MB    | 6105 MB
post_search_data                     | 3655554      | 3294 MB    | 854 MB     | 4148 MB
topic_users                          | 19252818     | 1760 MB    | 1377 MB    | 3137 MB
user_actions                         | 12332480     | 902 MB     | 1911 MB    | 2813 MB
incoming_links                       | 17359070     | 1108 MB    | 1028 MB    | 2136 MB
notifications                        | 3265898      | 1248 MB    | 451 MB     | 1699 MB
user_visits                          | 6301336      | 364 MB     | 503 MB     | 867 MB
email_logs                           | 1642194      | 470 MB     | 150 MB     | 620 MB
user_profile_views                   | 4383830      | 234 MB     | 355 MB     | 589 MB
post_actions                         | 3310096      | 244 MB     | 278 MB     | 522 MB
optimized_images                     | 1219705      | 361 MB     | 147 MB     | 508 MB
topic_link_clicks                    | 4726663      | 285 MB     | 136 MB     | 422 MB
topic_links                          | 1050293      | 219 MB     | 201 MB     | 420 MB
post_stats                           | 3706837      | 242 MB     | 159 MB     | 400 MB
post_revisions                       | 211988       | 376 MB     | 15 MB      | 391 MB
unsubscribe_keys                     | 1122585      | 191 MB     | 126 MB     | 317 MB
topics                               | 301847       | 198 MB     | 69 MB      | 267 MB
uploads                              | 324756       | 113 MB     | 153 MB     | 267 MB
sidebar_section_links                | 966652       | 149 MB     | 94 MB      | 242 MB
post_custom_fields                   | 1176075      | 134 MB     | 107 MB     | 240 MB
topic_search_data                    | 296501       | 177 MB     | 58 MB      | 235 MB
post_replies                         | 1447174      | 84 MB      | 62 MB      | 146 MB
permalinks                           | 1003118      | 81 MB      | 60 MB      | 141 MB
search_logs                          | 599529       | 78 MB      | 37 MB      | 115 MB
user_stats                           | 58903        | 110 MB     | 1304 kB    | 111 MB
top_topics                           | 90583        | 69 MB      | 30 MB      | 99 MB
user_histories                       | 197285       | 62 MB      | 31 MB      | 92 MB
upload_references                    | 406557       | 47 MB      | 42 MB      | 89 MB
draft_sequences                      | 729650       | 46 MB      | 42 MB      | 88 MB
post_hotlinked_media                 | 413007       | 52 MB      | 35 MB      | 87 MB
user_badges                          | 373573       | 39 MB      | 30 MB      | 69 MB
given_daily_likes                    | 936258       | 42 MB      | 27 MB      | 69 MB
directory_items                      | 350832       | 32 MB      | 32 MB      | 65 MB
users                                | 58903        | 45 MB      | 18 MB      | 63 MB
topic_allowed_users                  | 431955       | 25 MB      | 28 MB      | 53 MB
quoted_posts                         | 386420       | 23 MB      | 25 MB      | 48 MB
scheduler_stats                      | 264749       | 40 MB      | 5848 kB    | 46 MB
post_uploads                         | 249805       | 26 MB      | 20 MB      | 46 MB
skipped_email_logs                   | 183171       | 32 MB      | 13 MB      | 45 MB
user_uploads                         | 363722       | 19 MB      | 23 MB      | 42 MB
user_auth_tokens                     | 49923        | 27 MB      | 7656 kB    | 34 MB
user_options                         | 58894        | 27 MB      | 3024 kB    | 29 MB
email_tokens                         | 77921        | 13 MB      | 10 MB      | 24 MB
poll_votes                           | 173471       | 12 MB      | 11 MB      | 22 MB
topic_custom_fields                  | 131252       | 10008 kB   | 12 MB      | 22 MB
group_users                          | 133321       | 12 MB      | 8856 kB    | 21 MB
shelved_notifications                | 212779       | 9360 kB    | 9392 kB    | 18 MB
dismissed_topic_users                | 167180       | 11 MB      | 7440 kB    | 18 MB
user_custom_fields                   | 106316       | 8848 kB    | 6376 kB    | 15 MB
user_emails                          | 59131        | 6136 kB    | 8776 kB    | 15 MB
reviewables                          | 31330        | 6736 kB    | 7048 kB    | 13 MB
user_search_data                     | 58894        | 5704 kB    | 5832 kB    | 11 MB
plugin_store_rows                    | 40617        | 7800 kB    | 2896 kB    | 10 MB
user_profiles                        | 58894        | 6224 kB    | 2992 kB    | 9216 kB
bookmarks                            | 38626        | 5192 kB    | 3952 kB    | 9144 kB
reviewable_histories                 | 68163        | 4768 kB    | 3344 kB    | 8112 kB
user_avatars                         | 58894        | 3864 kB    | 3624 kB    | 7488 kB
reviewable_scores                    | 35693        | 4352 kB    | 1880 kB    | 6232 kB
user_archived_messages               | 57929        | 3464 kB    | 2576 kB    | 6040 kB
stylesheet_cache                     | 137          | 4872 kB    | 48 kB      | 4920 kB
topic_thumbnails                     | 24120        | 1424 kB    | 2328 kB    | 3752 kB
incoming_referers                    | 12269        | 1656 kB    | 1896 kB    | 3552 kB
drafts                               | 2581         | 2720 kB    | 208 kB     | 2928 kB
poll_options                         | 11426        | 1600 kB    | 1224 kB    | 2824 kB
application_requests                 | 12632        | 896 kB     | 592 kB     | 1488 kB
group_histories                      | 7859         | 632 kB     | 600 kB     | 1232 kB
web_crawler_requests                 | 3909         | 648 kB     | 496 kB     | 1144 kB
user_api_keys                        | 321          | 904 kB     | 128 kB     | 1032 kB
ignored_users                        | 5135         | 424 kB     | 408 kB     | 832 kB
topic_groups                         | 5746         | 448 kB     | 288 kB     | 736 kB
topic_allowed_groups                 | 5776         | 296 kB     | 432 kB     | 728 kB
polls                                | 3506         | 408 kB     | 320 kB     | 728 kB
push_subscriptions                   | 1381         | 664 kB     | 48 kB      | 712 kB
category_users                       | 3959         | 256 kB     | 448 kB     | 704 kB
group_archived_messages              | 5711         | 376 kB     | 288 kB     | 664 kB
categories                           | 101          | 536 kB     | 112 kB     | 648 kB
incoming_domains                     | 3113         | 216 kB     | 240 kB     | 456 kB
group_mentions                       | 2821         | 208 kB     | 240 kB     | 448 kB
schema_migration_details             | 1477         | 272 kB     | 120 kB     | 392 kB
topic_timers                         | 1741         | 224 kB     | 120 kB     | 344 kB
user_second_factors                  | 1084         | 240 kB     | 88 kB      | 328 kB
category_featured_topics             | 748          | 104 kB     | 160 kB     | 264 kB
user_notification_schedules          | 1057         | 144 kB     | 96 kB      | 240 kB
javascript_caches                    | 8            | 128 kB     | 64 kB      | 192 kB
user_api_key_scopes                  | 936          | 112 kB     | 72 kB      | 184 kB
theme_fields                         | 17           | 152 kB     | 32 kB      | 184 kB
schema_migrations                    | 1475         | 104 kB     | 64 kB      | 168 kB
user_security_keys                   | 41           | 56 kB      | 112 kB     | 168 kB
chat_channels                        | 1            | 48 kB      | 112 kB     | 160 kB
muted_users                          | 431          | 64 kB      | 96 kB      | 160 kB
user_warnings                        | 437          | 64 kB      | 96 kB      | 160 kB
badges                               | 54           | 104 kB     | 48 kB      | 152 kB
invites                              | 126          | 64 kB      | 80 kB      | 144 kB
do_not_disturb_timings               | 240          | 56 kB      | 80 kB      | 136 kB
groups                               | 12           | 64 kB      | 48 kB      | 112 kB
data_explorer_query_groups           | 6            | 40 kB      | 64 kB      | 104 kB
data_explorer_queries                | 19           | 80 kB      | 16 kB      | 96 kB
email_change_requests                | 22           | 48 kB      | 48 kB      | 96 kB
linked_topics                        | 183          | 48 kB      | 48 kB      | 96 kB
screened_ip_addresses                | 15           | 48 kB      | 48 kB      | 96 kB
screened_emails                      | 9            | 48 kB      | 48 kB      | 96 kB
screened_urls                        | 2            | 48 kB      | 48 kB      | 96 kB
site_settings                        | 161          | 64 kB      | 32 kB      | 96 kB
user_chat_channel_memberships        | 18           | 40 kB      | 48 kB      | 88 kB
invited_users                        | 34           | 40 kB      | 48 kB      | 88 kB
category_search_data                 | 101          | 56 kB      | 32 kB      | 88 kB
topic_invites                        | 17           | 40 kB      | 48 kB      | 88 kB
child_themes                         | 2            | 40 kB      | 48 kB      | 88 kB
color_scheme_colors                  | 40           | 48 kB      | 32 kB      | 80 kB
category_groups                      | 142          | 48 kB      | 32 kB      | 80 kB
onceoff_logs                         | 36           | 48 kB      | 32 kB      | 80 kB
directory_columns                    | 10           | 48 kB      | 32 kB      | 80 kB
theme_modifier_sets                  | 6            | 48 kB      | 32 kB      | 80 kB
sitemaps                             | 10           | 48 kB      | 32 kB      | 80 kB
themes                               | 6            | 48 kB      | 32 kB      | 80 kB
category_custom_fields               | 78           | 48 kB      | 32 kB      | 80 kB
badge_types                          | 3            | 48 kB      | 32 kB      | 80 kB
translation_overrides                | 27           | 48 kB      | 32 kB      | 80 kB
chat_threads                         | 0            | 8192 bytes | 64 kB      | 72 kB
incoming_emails                      | 0            | 8192 bytes | 64 kB      | 72 kB
user_exports                         | 61           | 48 kB      | 16 kB      | 64 kB
chat_messages                        | 0            | 8192 bytes | 56 kB      | 64 kB
web_hook_event_types                 | 14           | 48 kB      | 16 kB      | 64 kB
ar_internal_metadata                 | 1            | 48 kB      | 16 kB      | 64 kB
badge_groupings                      | 5            | 48 kB      | 16 kB      | 64 kB
user_fields                          | 2            | 48 kB      | 16 kB      | 64 kB
color_schemes                        | 4            | 48 kB      | 16 kB      | 64 kB
remote_themes                        | 4            | 48 kB      | 16 kB      | 64 kB
user_field_options                   | 11           | 48 kB      | 16 kB      | 64 kB
backup_metadata                      | 6            | 48 kB      | 16 kB      | 64 kB
theme_settings                       | 10           | 48 kB      | 16 kB      | 64 kB
post_action_types                    | 7            | 40 kB      | 16 kB      | 56 kB
sidebar_sections                     | 1            | 8192 bytes | 48 kB      | 56 kB
external_upload_stubs                | 0            | 8192 bytes | 40 kB      | 48 kB
group_requests                       | 0            | 8192 bytes | 32 kB      | 40 kB
category_settings                    | 101          | 8192 bytes | 32 kB      | 40 kB
watched_words                        | 0            | 24 kB      | 16 kB      | 40 kB
category_tag_stats                   | 0            | 0 bytes    | 40 kB      | 40 kB
tags                                 | 0            | 8192 bytes | 24 kB      | 32 kB
group_associated_groups              | 0            | 0 bytes    | 32 kB      | 32 kB
imap_sync_logs                       | 0            | 8192 bytes | 24 kB      | 32 kB
tag_search_data                      | 0            | 8192 bytes | 24 kB      | 32 kB
user_associated_groups               | 0            | 0 bytes    | 32 kB      | 32 kB
published_pages                      | 0            | 8192 bytes | 24 kB      | 32 kB
api_keys                             | 0            | 8192 bytes | 24 kB      | 32 kB
backup_draft_posts                   | 0            | 8192 bytes | 24 kB      | 32 kB
oauth2_user_infos                    | 0            | 8192 bytes | 24 kB      | 32 kB
user_associated_accounts             | 0            | 8192 bytes | 24 kB      | 32 kB
chat_message_revisions               | 0            | 8192 bytes | 24 kB      | 32 kB
single_sign_on_records               | 0            | 8192 bytes | 24 kB      | 32 kB
theme_translation_overrides          | 0            | 8192 bytes | 24 kB      | 32 kB
category_form_templates              | 0            | 0 bytes    | 24 kB      | 24 kB
message_bus                          | 0            | 8192 bytes | 16 kB      | 24 kB
chat_message_reactions               | 0            | 8192 bytes | 16 kB      | 24 kB
shared_drafts                        | 0            | 0 bytes    | 24 kB      | 24 kB
chat_channel_archives                | 0            | 8192 bytes | 16 kB      | 24 kB
group_custom_fields                  | 0            | 8192 bytes | 16 kB      | 24 kB
tag_group_permissions                | 0            | 0 bytes    | 24 kB      | 24 kB
tag_users                            | 0            | 0 bytes    | 24 kB      | 24 kB
custom_emojis                        | 0            | 8192 bytes | 16 kB      | 24 kB
topic_embeds                         | 0            | 8192 bytes | 16 kB      | 24 kB
category_tags                        | 0            | 0 bytes    | 24 kB      | 24 kB
backup_draft_topics                  | 0            | 0 bytes    | 24 kB      | 24 kB
anonymous_users                      | 0            | 0 bytes    | 24 kB      | 24 kB
allowed_pm_users                     | 0            | 0 bytes    | 24 kB      | 24 kB
sidebar_urls                         | 9            | 8192 bytes | 16 kB      | 24 kB
user_ip_address_histories            | 0            | 8192 bytes | 16 kB      | 24 kB
user_open_ids                        | 0            | 8192 bytes | 16 kB      | 24 kB
api_key_scopes                       | 0            | 8192 bytes | 16 kB      | 24 kB
form_templates                       | 0            | 8192 bytes | 16 kB      | 24 kB
web_hook_events                      | 0            | 8192 bytes | 16 kB      | 24 kB
associated_groups                    | 0            | 8192 bytes | 16 kB      | 24 kB
theme_svg_sprites                    | 0            | 8192 bytes | 16 kB      | 24 kB
incoming_chat_webhooks               | 0            | 8192 bytes | 16 kB      | 24 kB
post_details                         | 0            | 8192 bytes | 16 kB      | 24 kB
post_reply_keys                      | 0            | 0 bytes    | 24 kB      | 24 kB
developers                           | 0            | 0 bytes    | 16 kB      | 16 kB
embeddable_hosts                     | 0            | 8192 bytes | 8192 bytes | 16 kB
group_category_notification_defaults | 0            | 0 bytes    | 16 kB      | 16 kB
invited_groups                       | 0            | 0 bytes    | 16 kB      | 16 kB
chat_drafts                          | 0            | 8192 bytes | 8192 bytes | 16 kB
topic_tags                           | 0            | 0 bytes    | 16 kB      | 16 kB
user_statuses                        | 0            | 8192 bytes | 8192 bytes | 16 kB
category_tag_groups                  | 0            | 0 bytes    | 16 kB      | 16 kB
category_required_tag_groups         | 0            | 0 bytes    | 16 kB      | 16 kB
tag_groups                           | 0            | 8192 bytes | 8192 bytes | 16 kB
chat_mentions                        | 0            | 0 bytes    | 16 kB      | 16 kB
web_hooks                            | 0            | 8192 bytes | 8192 bytes | 16 kB
chat_webhook_events                  | 0            | 0 bytes    | 16 kB      | 16 kB
summary_sections                     | 0            | 8192 bytes | 8192 bytes | 16 kB
user_chat_thread_memberships         | 0            | 0 bytes    | 16 kB      | 16 kB
reviewable_claimed_topics            | 0            | 0 bytes    | 16 kB      | 16 kB
tag_group_memberships                | 0            | 0 bytes    | 16 kB      | 16 kB
direct_message_users                 | 0            | 0 bytes    | 16 kB      | 16 kB
group_tag_notification_defaults      | 0            | 0 bytes    | 16 kB      | 16 kB
tags_web_hooks                       | 0            | 0 bytes    | 8192 bytes | 8192 bytes
categories_web_hooks                 | 0            | 0 bytes    | 8192 bytes | 8192 bytes
web_hook_event_types_hooks           | 0            | 0 bytes    | 8192 bytes | 8192 bytes
direct_message_channels              | 0            | 0 bytes    | 8192 bytes | 8192 bytes
groups_web_hooks                     | 0            | 0 bytes    | 8192 bytes | 8192 bytes
badge_posts                          | 0            | 0 bytes    | 0 bytes    | 0 bytes

which is quite a bit already. What do you think about the two indexes above?

3 Likes

These indexes are from when a concurrent reindex has failed previously.

When Postgres is performing a concurrent reindex, the new index is created with a _ccnew suffix. Once the new index is rebuilt, the old one is replaced with the new one. If a concurrent reindex fails, the temporary _ccnew index is not deleted.

It is safe to drop these temporary indexes manually with DROP INDEX IF EXISTS ...

As for the index sizes, the 30-50% reduction on the top four is impressive!

If I’m reading correctly, the user_visits index dropped from 13GB down to 503MB, <5% of the original size :exploding_head:

4 Likes