Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ updates:
ignore:
- dependency-name: postgres
versions:
- ">=15"
- ">=19"
- dependency-name: valkey/valkey
versions:
- ">=9"
Expand Down
4 changes: 2 additions & 2 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ runs:
id: restore_cache_sentry
uses: BYK/docker-volume-cache-action/restore@0efa5cf5178c9906cb46ed8d1a357df8fd6b1a06
with:
key: db-volumes-sentry-v3-${{ steps.cache_key.outputs.ARCH }}-${{ inputs.compose_profiles }}-${{ steps.cache_key.outputs.SENTRY_MIGRATIONS_MD5 }}
key: db-volumes-sentry-v4-${{ steps.cache_key.outputs.ARCH }}-${{ inputs.compose_profiles }}-${{ steps.cache_key.outputs.SENTRY_MIGRATIONS_MD5 }}
restore-keys: |
key: db-volumes-sentry-v3-${{ steps.cache_key.outputs.ARCH }}-${{ inputs.compose_profiles }}
key: db-volumes-sentry-v4-${{ steps.cache_key.outputs.ARCH }}-${{ inputs.compose_profiles }}
volumes: |
sentry-postgres

Expand Down
5 changes: 2 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ services:
command: ["valkey-server", "/usr/local/etc/redis/redis.conf"]
postgres:
<<: *restart_policy
# Using the same postgres version as Sentry dev for consistency purposes
image: "postgres:14.24-trixie"
image: "postgres:18.6-trixie"
healthcheck:
<<: *healthcheck_defaults
# Using default user "postgres" from sentry/sentry.conf.example.py or value of POSTGRES_USER if provided
Expand All @@ -172,7 +171,7 @@ services:
environment:
POSTGRES_HOST_AUTH_METHOD: "trust"
volumes:
- "sentry-postgres:/var/lib/postgresql/data"
- "sentry-postgres:/var/lib/postgresql"
shm_size: 256m
pgbouncer:
<<: *restart_policy
Expand Down
45 changes: 36 additions & 9 deletions install/upgrade-postgres.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
echo "${_group}Ensuring proper PostgreSQL version ..."

postgres_version=$($CONTAINER_ENGINE run --rm -v sentry-postgres:/db busybox sh -c 'if [ -f /db/PG_VERSION ]; then cat /db/PG_VERSION; fi')
postgres_version=$($CONTAINER_ENGINE run --rm -v sentry-postgres:/db busybox sh -c 'for data in /db /db/18/docker; do if [ -f "$data/PG_VERSION" ]; then cat "$data/PG_VERSION"; break; fi; done')
Comment thread
aldy505 marked this conversation as resolved.

if [[ -n "$($CONTAINER_ENGINE volume ls -q --filter name=sentry-postgres)" && "$($CONTAINER_ENGINE run --rm -v sentry-postgres:/db busybox cat /db/PG_VERSION 2>/dev/null)" == "9.6" ]]; then
$CONTAINER_ENGINE volume rm sentry-postgres-new || true
Expand Down Expand Up @@ -43,16 +43,43 @@ if [[ -n "$($CONTAINER_ENGINE volume ls -q --filter name=sentry-postgres)" && "$
$dc stop postgres
fi

# Reindex existing PostgreSQL 14 data once for the glibc 2.36 (Bookworm) -> 2.41 (Trixie) change.
if [[ "$postgres_version" == "14" || -z "$postgres_version" ]]; then
needs_reindex=$($CONTAINER_ENGINE run --rm -v sentry-postgres:/db busybox sh -c 'if [ -f /db/PG_VERSION ] && [ ! -f /db/14-trixie-reindexed ]; then echo yes; fi')
if $CONTAINER_ENGINE volume inspect sentry-postgres-new >/dev/null 2>&1; then
echo "Found sentry-postgres-new from an interrupted PostgreSQL upgrade. Recover the database before removing this volume and rerunning install.sh."

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put the big warning sign here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain what do you mean by "big warning sign"? This should not happen for normal users if they do not interrupt their ./install.sh or that does not break.

This is unrelated to reindex issues which users reported on different issues.

exit 1
fi

start_service_and_wait_ready postgres
if [[ "$needs_reindex" == "yes" ]]; then
echo "Re-indexing due to glibc change, this may take a while..."
$dc exec postgres psql -U postgres -v ON_ERROR_STOP=1 -c "REINDEX DATABASE postgres;"
if [[ "$postgres_version" == "14" ]]; then
# Reindex once under PostgreSQL 14 Trixie before upgrading the major version.
if ! $CONTAINER_ENGINE run --rm -v sentry-postgres:/db:ro busybox test -f /db/14-trixie-reindexed; then
echo "[1/2] PostgreSQL 14 Bookworm -> 14 Trixie: reindexing for the glibc change, this may take a while..."
$CONTAINER_ENGINE run --rm --user postgres --network none --shm-size=256m \
-v sentry-postgres:/var/lib/postgresql/data \
postgres:14.24-trixie bash -ec '
trap "pg_ctl -m fast -w stop" EXIT
pg_ctl -w start
psql -U postgres -v ON_ERROR_STOP=1 -c "REINDEX DATABASE postgres;"
touch "$PGDATA/14-trixie-reindexed"
'
Comment on lines +54 to +62

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice.

echo "[1/2] PostgreSQL 14 Trixie reindex completed and database stopped."
else
echo "[1/2] PostgreSQL 14 Trixie reindex already completed; skipping."
fi
$dc exec postgres sh -c 'touch "$PGDATA/14-trixie-reindexed"'

echo "[2/2] PostgreSQL 14 Trixie -> 18 Trixie: upgrading..."
$CONTAINER_ENGINE run --rm \
-e POSTGRES_INITDB_ARGS=--no-data-checksums \
-v sentry-postgres:/var/lib/postgresql/14/data \
-v sentry-postgres-new:/var/lib/postgresql/18/docker \
tianon/postgres-upgrade:14-to-18

echo "[2/2] pg_upgrade completed. Replacing the PostgreSQL 14 volume with PostgreSQL 18 data..."
$CONTAINER_ENGINE volume rm sentry-postgres
$CONTAINER_ENGINE volume create --name sentry-postgres
$CONTAINER_ENGINE run --rm -v sentry-postgres-new:/from -v sentry-postgres:/to alpine ash -ec \
"mkdir -p /to/18/docker; cp -av /from/. /to/18/docker; echo 'host all all all trust' >> /to/18/docker/pg_hba.conf"
$CONTAINER_ENGINE volume rm sentry-postgres-new
Comment on lines +68 to +80

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'd need a third step, from @kostirez1 on Discord:

FYI: POSTGRES_INITDB_ARGS=--no-data-checksums

Starting with PG18 (26.10.0+), new Sentry installs will have data checksums enabled, because that's now the PostgreSQL default. Existing clusters (PG14) will keep running without them.

It's a minor point, but the next major upgrade (PG18 -> 19) has to account for this setting. pg_upgrade fails if the old and new clusters have different checksum settings.

To fix this, enable checksums with pg_checksums --enable right after the upgrade, before PG18 takes traffic. It needs the cluster offline and rewrites every data file, so it fits best inside the upgrade's downtime window.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuation and summary of my messages on Discord:

Enabling pg_checksums is something unrelated to this PR, we can decide whether to activate that or not, as that would need a complete read of all databases / tables / indexes, it could happen in another release or this release but another PR, either way I intend to keep this PR just for upgrading from 14 to 18.

Let's decide whether to enable pg_checksums or keep it disabled later.

postgres_version=18
echo "[2/2] PostgreSQL 18 Trixie upgrade completed."
fi

echo "${_endgroup}"
Loading