Month: October 2020

  • Mastodon PostgreSQL upgrade fun

    Howdy friends!

    If you’re a mastodon user on tilde.zone (the tildeverse mastodon instance), you might’ve noticed some downtime recently.

    Here’s a quick recap of what went down during the upgrade process.

    We run the current stable version of PostgreSQL from the postgres apt repos. PostgreSQL 13 was released recently and the apt upgrades automatically created a new cluster running 13.

    The database for mastodon has gotten quite large (about 16gb) which complicates this upgrade a bit. This was my initial plan:

    1. drop the 13 cluster created by the apt package upgrades
    2. upgrade the 12-main cluster to 13
    3. drop the 12 cluster

    These steps appeared to work fine, but closer inspection afterwards led me to discover that the new cluster had ended up with SQL_ASCII encoding somehow. This is not a situation we want to be in. Time to fix it.

    Here’s the new plan:

    1. stop mastodon:
      for i in streaming sidekiq web; do systemctl stop mastodon-$i; done
    2. dump current database state:

      pg_dump mastodon_production > db.dump

    3. drop and recreate cluster with utf8 encoding:
      pg_dropcluster 13 main --stop pg_createcluster --locale=en_US.UTF8 13 main --start
    4. restore backup:
      sudo -u postgres psql -c "create user mastodon createdb;" sudo -u mastodon createdb -E utf8 mastodon_production
      sudo -u mastodon psql < db.dump

    I’m still not 100% sure how the encoding reverted to ASCII but it seems that the locale was not correctly set while running the apt upgrades…

    If this happens to you, hopefully this helps you wade out while keeping all your data :)