mediacloud / backend

Media Cloud is an open source, open data platform that allows researchers to answer quantitative questions about the content of online media.
http://www.mediacloud.org
GNU Affero General Public License v3.0
281 stars 87 forks source link

Upgrade to PostgreSQL 11 #530

Closed pypt closed 5 years ago

pypt commented 5 years ago

PostgreSQL 11 (released ~2 months ago), among other features, has some nice partitioning improvements that I'd like to use for partitioning downloads and download_texts (#514):

It will also be nice to try out all the performance improvements, e.g. JIT query compilation.

As always, I'll do a test upgrade first and the actual production upgrade afterwards. Production upgrade will involve up to 1 hour of downtime.

Plan for test upgrade:

Plan for production upgrade:

pypt commented 5 years ago

Test upgrade done, everything should take less than an hour.

Can we plan 1 hour of downtime for 4 AM EST on January 3rd?

pypt commented 5 years ago

The whole process for self-reference:

#
# IN CASE PG_UPGRADE FAILS:
#
rm -rf /var/lib/postgresql/11/main/
sudo -H -u postgres /usr/lib/postgresql/11/bin/initdb -D /var/lib/postgresql/11/main/ -E UTF8

#
# START ONE OF THE SERVERS FOR DEBUGGING:
#
sudo -H -u postgres /usr/lib/postgresql/11/bin/postgres -D /var/lib/postgresql/11/main -c config_file=/etc/postgresql/11/main/postgresql.conf
sudo -H -u postgres /usr/lib/postgresql/10/bin/postgres -D /var/lib/postgresql/10/main -c config_file=/etc/postgresql/10/main/postgresql.conf

#
# ---
#

# Install new PostgreSQL
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
apt install postgresql-11 postgresql-client-11 postgresql-contrib-11 postgresql-plperl-11 postgresql-server-dev-11

echo "include_dir = 'conf.d'" >> /etc/postgresql/11/main/postgresql.conf
cp /etc/postgresql/10/main/conf.d/01-mediacloud.conf /etc/postgresql/11/main/conf.d/
# Enable jit

mkdir -p /var/run/postgresql/11-main.pg_stat_tmp
chown postgres:postgres /var/run/postgresql/11-main.pg_stat_tmp

# Change port from 5432 to 5433
vim /etc/postgresql/11/main/postgresql.conf

# Remove cruft
rm /var/lib/postgresql/pg_*.log
rm /var/lib/postgresql/pg_*.custom
rm /var/lib/postgresql/pg_upgrade_dump_globals.sql

# Test if clusters are compatible (~1 min)
cd /var/lib/postgresql/
sudo -H -u postgres time \
    /usr/lib/postgresql/11/bin/pg_upgrade \
    --jobs=`nproc --all` \
    --old-bindir=/usr/lib/postgresql/10/bin/ \
    --new-bindir=/usr/lib/postgresql/11/bin/ \
    --old-datadir=/var/lib/postgresql/10/main/ \
    --new-datadir=/var/lib/postgresql/11/main/ \
    --old-port=5432 \
    --new-port=5433 \
    --old-options=' -c config_file=/etc/postgresql/10/main/postgresql.conf' \
    --new-options=' -c config_file=/etc/postgresql/11/main/postgresql.conf' \
    --link \
    --check \
    --verbose

# Run the actual upgrade (~1 min)
cd /var/lib/postgresql/
sudo -H -u postgres time \
    /usr/lib/postgresql/11/bin/pg_upgrade \
    --jobs=`nproc --all` \
    --old-bindir=/usr/lib/postgresql/10/bin/ \
    --new-bindir=/usr/lib/postgresql/11/bin/ \
    --old-datadir=/var/lib/postgresql/10/main/ \
    --new-datadir=/var/lib/postgresql/11/main/ \
    --old-port=5432 \
    --new-port=5433 \
    --old-options=' -c config_file=/etc/postgresql/10/main/postgresql.conf' \
    --new-options=' -c config_file=/etc/postgresql/11/main/postgresql.conf' \
    --link \
    --verbose

# Change port from 5433 to 5432
vim /etc/postgresql/11/main/postgresql.conf

# Change maintenance_work_mem to 16GB
vim /etc/postgresql/11/main/conf.d/01-mediacloud.conf

# Remove old PostgreSQL
apt remove postgresql-10 postgresql-client-10 postgresql-contrib-10 postgresql-plperl-10 postgresql-server-dev-10

# Start and enable PostgreSQL
service postgresql start
systemctl enable postgresql

# Rebuild statistics (~40 mins)
# (monitor locks while running that because PostgreSQL might decide to do autovacuum)
sudo -H -u postgres time \
    /usr/lib/postgresql/11/bin/vacuumdb \
    --all \
    --analyze-in-stages \
    --verbose \
    --jobs=`nproc --all`

# Remove maintenance_work_mem exception (set back to 256MB)
vim /etc/postgresql/11/main/conf.d/01-mediacloud.conf
service postgresql restart

# Remove old cruft
rm -rf /var/lib/postgresql/10/
rm /var/lib/postgresql/pg_upgrade.log
rm /var/lib/postgresql/analyze_new_cluster.sh
rm /var/lib/postgresql/delete_old_cluster.sh
pypt commented 5 years ago

Started backup dump to Faith.

pypt commented 5 years ago

Upgrade done, currently running another post-upgrade backup to Faith.

pypt commented 5 years ago

First attempt to make a post-upgrade backup to Faith failed due to server upgrades, making a new one again.

pypt commented 5 years ago

Post-upgrade backup done.