Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBS-13361 (mirror): Upgrade to PostgreSQL 16 #277

Merged
merged 8 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ If you use [UFW](https://help.ubuntu.com/community/UFW) to manage your firewall:

* Current MB Branch: [v-2024-04-09](build/musicbrainz/Dockerfile#L88)
* Current DB_SCHEMA_SEQUENCE: [28](build/musicbrainz/Dockerfile#L123)
* Postgres Version: [12](docker-compose.yml)
* Postgres Version: [16](docker-compose.yml)
(can be changed by setting the environment variable `POSTGRES_VERSION`)
* MB Solr search server: [3.4.2](docker-compose.yml#L88)
(can be changed by setting the environment variable `MB_SOLR_VERSION`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set -e
shopt -s extglob

PGDATA=/var/lib/postgresql/data
PGDATA_OLD="$PGDATA"/9.5
PGDATA_NEW="$PGDATA"/12
PGDATA_OLD="$PGDATA"/12
PGDATA_NEW="$PGDATA"/16
PGAMQP_DIR=/tmp/pg_amqp

function cleanup() {
Expand All @@ -15,8 +15,8 @@ function cleanup() {
fi

if [[ -d "$PGDATA_OLD" ]]; then
echo "Clean $PG_DATA_OLD off but 9.5 data"
rm -rf "$PGDATA"/!(9.5)
echo "Clean $PG_DATA_OLD off but 12 data"
rm -rf "$PGDATA"/!(12)
mv -v "$PGDATA_OLD"/* "$PGDATA"
rmdir "$PGDATA_OLD"
fi
Expand All @@ -28,35 +28,38 @@ function cleanup() {
}
trap cleanup EXIT

sudo -u postgres /usr/lib/postgresql/9.5/bin/pg_ctl stop -w -D "$PGDATA" \
sudo -u postgres /usr/lib/postgresql/12/bin/pg_ctl stop -w -D "$PGDATA" 2>/dev/null \
|| echo 'Assuming server is stopped...'

# We use the --link flag on pg_upgrade below to make hard links instead of
# copying files, drastically improving the speed of the upgrade. Hard links,
# of course, require the linked files to be on the same file system, but
# $PGDATA is the volume *root*. To work around that, we have to move the
# existing v9.5 cluster to a '9.5' subdir, and create the new v12 cluster in
# a '12' subdir. Once we're finished, we'll move the new cluster's files
# existing v12 cluster to a '12' subdir, and create the new v16 cluster in
# a '16' subdir. Once we're finished, we'll move the new cluster's files
# back into $PGDATA.
cd "$PGDATA"
sudo -u postgres mkdir -p 9.5 12
chmod 700 9.5 12
sudo -u postgres mv !(9.5|12) 9.5
sudo -u postgres mkdir -p 12 16
chmod 700 12 16
sudo -u postgres mv !(12|16) 12

sudo -u postgres /usr/lib/postgresql/12/bin/initdb \
sudo -u postgres /usr/lib/postgresql/16/bin/initdb \
--encoding utf8 \
--username musicbrainz \
"$PGDATA_NEW"

git clone -b "v0.4.1" --depth=1 https://github.com/omniti-labs/pg_amqp.git "$PGAMQP_DIR"
# There is no tag v0.4.2 (or 0.5.0) yet
PG_AMQP_GIT_REF="240d477d40c5e7a579b931c98eb29cef4edda164"
git clone https://github.com/omniti-labs/pg_amqp.git "$PGAMQP_DIR"
cd "$PGAMQP_DIR"
make PG_CONFIG=/usr/lib/postgresql/12/bin/pg_config
git checkout "$PG_AMQP_GIT_REF"
make PG_CONFIG=/usr/lib/postgresql/16/bin/pg_config PG_CPPFLAGS=-Wno-error=implicit-int
make install
cd "$PGDATA"

sudo -u postgres /usr/lib/postgresql/12/bin/pg_upgrade \
--old-bindir=/usr/lib/postgresql/9.5/bin/ \
--new-bindir=/usr/lib/postgresql/12/bin/ \
sudo -u postgres /usr/lib/postgresql/16/bin/pg_upgrade \
--old-bindir=/usr/lib/postgresql/12/bin/ \
--new-bindir=/usr/lib/postgresql/16/bin/ \
--old-datadir="$PGDATA_OLD" \
--new-datadir="$PGDATA_NEW" \
--jobs=3 \
Expand All @@ -72,9 +75,5 @@ mv "$PGDATA_NEW"/* "$PGDATA"/
rmdir "$PGDATA_NEW"
cp -a "$PGDATA_OLD"/{postgresql.conf,pg_hba.conf} .

# Start the new cluster in the background, so we can apply
# 20200518-pg12-after-upgrade.sql via the website container.
sudo -u postgres /usr/lib/postgresql/12/bin/pg_ctl start -w -D "$PGDATA"

./delete_old_cluster.sh
rm delete_old_cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ services:
db:
build:
args:
- POSTGRES_VERSION=9.5
image: musicbrainz-docker_db:9.5
- POSTGRES_VERSION=12
image: musicbrainz-docker_db:12
command: /bin/sleep infinity

musicbrainz:
Expand Down
79 changes: 0 additions & 79 deletions admin/upgrade-to-postgres12

This file was deleted.

81 changes: 81 additions & 0 deletions admin/upgrade-to-postgres16
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

set -e -o pipefail -u

# shellcheck source=admin/lib/common.inc.bash
source "$(dirname "${BASH_SOURCE[0]}")/lib/common.inc.bash"

HELP=$(cat <<EOH
Usage: $SCRIPT_NAME [--help]

Upgrade database from Postgres 12 to Postgres 16.
EOH
)

if [[ $# -ne 0 && $1 =~ -*h(elp)? ]]
then
echo "$HELP"
exit 0 # EX_OK
elif [[ $# -ne 0 ]]
then
echo >&2 "$SCRIPT_NAME: unrecognized argument: $1"
echo >&2 "Try '$SCRIPT_NAME help' for usage."
exit 64 # EX_USAGE
fi

$DOCKER_COMPOSE_CMD down
$DOCKER_COMPOSE_CMD \
-f docker-compose.yml \
-f admin/lib/upgrade-to-postgres16/pg-12-stopped.yml \
up -d
$DOCKER_COMPOSE_CMD stop indexer search mq musicbrainz redis

$DOCKER_COMPOSE_CMD exec db apt-get update
$DOCKER_COMPOSE_CMD exec db apt-get install \
--no-install-suggests \
--no-install-recommends \
-y \
postgresql-16 \
postgresql-client-16 \
postgresql-server-dev-16 \
sudo

PGDATA=/var/lib/postgresql/data
$DOCKER_COMPOSE_CMD exec db \
sudo -u postgres /usr/lib/postgresql/12/bin/pg_ctl start -w -D "$PGDATA"
$DOCKER_COMPOSE_CMD exec db \
sudo -u postgres /usr/lib/postgresql/12/bin/pg_isready -t 60

CURRENT_PG_VERSION=$($DOCKER_COMPOSE_CMD exec db psql -U musicbrainz -d musicbrainz_db -tAq -P pager=off -c 'SHOW server_version_num')
if echo "$CURRENT_PG_VERSION" | grep -v '^12'; then
echo "Error: Current postgres version should be < 13, not $CURRENT_PG_VERSON"
exit 1
fi

$DOCKER_COMPOSE_CMD stop db

$DOCKER_COMPOSE_CMD \
-f docker-compose.yml \
-f admin/lib/upgrade-to-postgres16/pg-12-stopped.yml \
up -d db

DB_CONTAINER_ID=$($DOCKER_COMPOSE_CMD ps -q db)
$DOCKER_CMD cp admin/lib/upgrade-to-postgres16/do-pg_upgrade.sh "$DB_CONTAINER_ID":/tmp/
$DOCKER_COMPOSE_CMD exec db /bin/bash /tmp/do-pg_upgrade.sh

$DOCKER_COMPOSE_CMD exec db \
sudo -u postgres /usr/lib/postgresql/16/bin/pg_ctl start -w -D "$PGDATA"
$DOCKER_COMPOSE_CMD exec db \
sudo -u postgres /usr/lib/postgresql/16/bin/pg_isready -t 60

$DOCKER_COMPOSE_CMD exec db psql -U musicbrainz -d musicbrainz_db -tA -P pager=off -f "$PGDATA"/update_extensions.sql
$DOCKER_COMPOSE_CMD exec db rm "$PGDATA"/update_extensions.sql

$DOCKER_COMPOSE_CMD exec db \
sudo -u postgres /usr/lib/postgresql/16/bin/vacuumdb -U musicbrainz --all --analyze-in-stages

$DOCKER_COMPOSE_CMD stop db
$DOCKER_COMPOSE_CMD rm --stop --force db
$DOCKER_COMPOSE_CMD up --build -d

echo 'Upgrade complete!'
2 changes: 1 addition & 1 deletion build/musicbrainz-dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ARG CPANMINUS_SRC_SUM=963e63c6e1a8725ff2f624e9086396ae150db51dd0a337c3781d09a994

COPY keyrings/* /etc/apt/keyrings/
ARG NODE_MAJOR_VERSION=20
ARG POSTGRES_VERSION=12
ARG POSTGRES_VERSION=16
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.asc] https://deb.nodesource.com/node_${NODE_MAJOR_VERSION}.x nodistro main" > /etc/apt/sources.list.d/nodesource.list && \
echo "deb [signed-by=/etc/apt/keyrings/pgdg.asc] http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list && \
Expand Down
2 changes: 1 addition & 1 deletion build/musicbrainz/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ARG CPANMINUS_SRC_SUM=963e63c6e1a8725ff2f624e9086396ae150db51dd0a337c3781d09a994

COPY keyrings/* /etc/apt/keyrings/
ARG NODE_MAJOR_VERSION=20
ARG POSTGRES_VERSION=12
ARG POSTGRES_VERSION=16
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.asc] https://deb.nodesource.com/node_${NODE_MAJOR_VERSION}.x nodistro main" > /etc/apt/sources.list.d/nodesource.list && \
echo "deb [signed-by=/etc/apt/keyrings/pgdg.asc] http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list && \
Expand Down
2 changes: 1 addition & 1 deletion build/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG POSTGRES_VERSION=12
ARG POSTGRES_VERSION=16
ARG POSTGRES_IMAGE_VARIANT=bookworm
ARG POSTGRES_IMAGE_TAG=${POSTGRES_VERSION}-${POSTGRES_IMAGE_VARIANT}
FROM postgres:${POSTGRES_IMAGE_TAG}
Expand Down
2 changes: 2 additions & 0 deletions compose/musicbrainz-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ services:
musicbrainz:
build:
context: build/musicbrainz-dev
args:
- POSTGRES_VERSION=${POSTGRES_VERSION:-16}
volumes:
- ${MUSICBRAINZ_SERVER_LOCAL_ROOT:?Missing path of MusicBrainz Server working copy}:/musicbrainz-server
- ./.musicbrainz-dev.bash_history.d/:/root/.bash_history.d/:z
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.alt.db-only-mirror.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ services:
build:
context: build/postgres
args:
- POSTGRES_VERSION=${POSTGRES_VERSION:-12}
image: musicbrainz-docker_db:${POSTGRES_VERSION:-12}
- POSTGRES_VERSION=${POSTGRES_VERSION:-16}
image: musicbrainz-docker_db:${POSTGRES_VERSION:-16}
logging:
driver: "json-file"
options:
Expand All @@ -34,7 +34,7 @@ services:
build:
context: build/musicbrainz
args:
- POSTGRES_VERSION=${POSTGRES_VERSION:-12}
- POSTGRES_VERSION=${POSTGRES_VERSION:-16}
logging:
driver: "json-file"
options:
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ services:
build:
context: build/postgres
args:
- POSTGRES_VERSION=${POSTGRES_VERSION:-12}
image: musicbrainz-docker_db:${POSTGRES_VERSION:-12}
- POSTGRES_VERSION=${POSTGRES_VERSION:-16}
image: musicbrainz-docker_db:${POSTGRES_VERSION:-16}
logging:
driver: "json-file"
options:
Expand All @@ -40,7 +40,7 @@ services:
build:
context: build/musicbrainz
args:
- POSTGRES_VERSION=${POSTGRES_VERSION:-12}
- POSTGRES_VERSION=${POSTGRES_VERSION:-16}
logging:
driver: "json-file"
options:
Expand Down