Skip to content

Commit a6d46a9

Browse files
Upgrade Postgres to 14 (#2074)
* add postgres upgrade script, this is included in the install script and automatically upgrades users from PG 9.6 to 14
1 parent 688c65f commit a6d46a9

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ services:
126126
hard: 10032
127127
postgres:
128128
<<: *restart_policy
129-
image: "postgres:9.6"
129+
# Using the same postgres version as Sentry dev for consistency purposes
130+
image: "postgres:14.5"
130131
healthcheck:
131132
<<: *healthcheck_defaults
132133
# Using default user "postgres" from sentry/sentry.conf.example.py or value of POSTGRES_USER if provided

install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ source install/build-docker-images.sh
3030
source install/install-wal2json.sh
3131
source install/bootstrap-snuba.sh
3232
source install/create-kafka-topics.sh
33+
source install/upgrade-postgres.sh
3334
source install/set-up-and-migrate-database.sh
3435
source install/geoip.sh
3536
source install/wrap-up.sh

install/upgrade-postgres.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
echo "${_group}Ensuring proper PostgreSQL version ..."
2+
3+
if [[ -n "$(docker volume ls -q --filter name=sentry-postgres)" && "$(docker run --rm -v sentry-postgres:/db busybox cat /db/PG_VERSION 2>/dev/null)" == "9.6" ]]; then
4+
docker volume rm sentry-postgres-new || true
5+
# If this is Postgres 9.6 data, start upgrading it to 14.0 in a new volume
6+
docker run --rm \
7+
-v sentry-postgres:/var/lib/postgresql/9.6/data \
8+
-v sentry-postgres-new:/var/lib/postgresql/14/data \
9+
tianon/postgres-upgrade:9.6-to-14
10+
11+
# Get rid of the old volume as we'll rename the new one to that
12+
docker volume rm sentry-postgres
13+
docker volume create --name sentry-postgres
14+
# There's no rename volume in Docker so copy the contents from old to new name
15+
# Also append the `host all all all trust` line as `tianon/postgres-upgrade:9.6-to-14`
16+
# doesn't do that automatically.
17+
docker run --rm -v sentry-postgres-new:/from -v sentry-postgres:/to alpine ash -c \
18+
"cd /from ; cp -av . /to ; echo 'host all all all trust' >> /to/pg_hba.conf"
19+
# Finally, remove the new old volume as we are all in sentry-postgres now.
20+
docker volume rm sentry-postgres-new
21+
echo "Re-indexing due to glibc change, this may take a while..."
22+
echo "Starting up new PostgreSQL version"
23+
$dc up -d postgres
24+
25+
# Wait for postgres
26+
RETRIES=5
27+
until docker exec sentry-self-hosted-postgres-1 psql -U postgres -c "select 1" >/dev/null 2>&1 || [ $RETRIES -eq 0 ]; do
28+
echo "Waiting for postgres server, $((RETRIES--)) remaining attempts..."
29+
sleep 1
30+
done
31+
32+
# VOLUME_NAME is the same as container name
33+
# Reindex all databases and their system catalogs which are not templates
34+
DBS=$(docker exec sentry-self-hosted-postgres-1 psql -qAt -U postgres -c "SELECT datname FROM pg_database WHERE datistemplate = false;")
35+
for db in ${DBS}; do
36+
echo "Re-indexing database: ${db}"
37+
docker exec sentry-self-hosted-postgres-1 psql -qAt -U postgres -d ${db} -c "reindex system ${db}"
38+
docker exec sentry-self-hosted-postgres-1 psql -qAt -U postgres -d ${db} -c "reindex database ${db};"
39+
done
40+
41+
$dc stop postgres
42+
fi
43+
44+
echo "${_endgroup}"

0 commit comments

Comments
 (0)