Skip to content

Commit 0de5e1f

Browse files
authored
Fix PostgreSQL upgrade to support multiple versions and upgrade TimescaleDB in all databases
2 parents 005cc69 + 3a4a498 commit 0de5e1f

File tree

3 files changed

+92
-42
lines changed

3 files changed

+92
-42
lines changed

.github/workflows/postgresql.yml

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ jobs:
6565
- name: Set tags
6666
run: |
6767
if [ -z "$TAG" ]; then
68-
echo "TAG=-t openremote/postgresql:develop" >> $GITHUB_ENV
68+
echo "TAG=-t openremote/postgresql:develop-amd64" >> $GITHUB_ENV
6969
else
70-
echo "TAG=-t openremote/postgresql:latest -t openremote/postgresql:$TAG" >> $GITHUB_ENV
70+
echo "TAG=-t openremote/postgresql:$TAG-amd64" >> $GITHUB_ENV
7171
fi
7272
env:
7373
TAG: ${{ github.event.release.tag_name }}
@@ -144,9 +144,9 @@ jobs:
144144
- name: Set tags
145145
run: |
146146
if [ -z "$TAG" ]; then
147-
echo "TAG=-t openremote/postgresql:develop" >> $GITHUB_ENV
147+
echo "TAG=-t openremote/postgresql:develop-arm64" >> $GITHUB_ENV
148148
else
149-
echo "TAG=-t openremote/postgresql:latest -t openremote/postgresql:$TAG" >> $GITHUB_ENV
149+
echo "TAG=-t openremote/postgresql:$TAG-arm64" >> $GITHUB_ENV
150150
fi
151151
env:
152152
TAG: ${{ github.event.release.tag_name }}
@@ -180,3 +180,36 @@ jobs:
180180
--no-cache-filter trimmed \
181181
--no-cache-filter trimmed-all \
182182
$TAG .
183+
184+
create_manifest:
185+
needs: [image_postgresql_amd64, image_postgresql_arm64]
186+
runs-on: ubuntu-latest
187+
188+
steps:
189+
- name: Set tags
190+
run: |
191+
if [ -z "$TAG" ]; then
192+
echo "TAG=openremote/postgresql:develop" >> $GITHUB_ENV
193+
echo "TAG_AMD64=openremote/postgresql:develop-amd64" >> $GITHUB_ENV
194+
echo "TAG_ARM64=openremote/postgresql:develop-arm64" >> $GITHUB_ENV
195+
else
196+
echo "TAG=openremote/postgresql:$TAG" >> $GITHUB_ENV
197+
echo "TAG_LATEST=openremote/postgresql:latest" >> $GITHUB_ENV
198+
echo "TAG_AMD64=openremote/postgresql:$TAG-amd64" >> $GITHUB_ENV
199+
echo "TAG_ARM64=openremote/postgresql:$TAG-arm64" >> $GITHUB_ENV
200+
fi
201+
env:
202+
TAG: ${{ github.event.release.tag_name }}
203+
204+
- name: Login to DockerHub
205+
uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef
206+
with:
207+
username: ${{ secrets._TEMP_DOCKERHUB_USER }}
208+
password: ${{ secrets._TEMP_DOCKERHUB_PASSWORD }}
209+
210+
- name: Create and push multi-arch manifest
211+
run: |
212+
docker buildx imagetools create -t $TAG $TAG_AMD64 $TAG_ARM64
213+
if [ ! -z "$TAG_LATEST" ]; then
214+
docker buildx imagetools create -t $TAG_LATEST $TAG_AMD64 $TAG_ARM64
215+
fi

Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
ARG PG_MAJOR_PREVIOUS=15
32
ARG PG_MAJOR=17
43
ARG TIMESCALE_VERSION=2.22
54

@@ -40,12 +39,13 @@ FROM timescale/timescaledb-ha:pg17-ts${TIMESCALE_VERSION}-all AS trimmed-all
4039
FROM scratch
4140
COPY --from=trimmed / /
4241

43-
ARG PG_MAJOR_PREVIOUS
4442
ARG PG_MAJOR
4543

46-
## Copy previous PG MAJOR version executable
47-
COPY --from=trimmed-all /usr/lib/postgresql/${PG_MAJOR_PREVIOUS} /usr/lib/postgresql/${PG_MAJOR_PREVIOUS}
48-
COPY --from=trimmed-all /usr/share/postgresql/${PG_MAJOR_PREVIOUS} /usr/share/postgresql/${PG_MAJOR_PREVIOUS}
44+
## Copy only PostgreSQL 14 and 15 for upgrade support
45+
COPY --from=trimmed-all /usr/lib/postgresql/14 /usr/lib/postgresql/14
46+
COPY --from=trimmed-all /usr/lib/postgresql/15 /usr/lib/postgresql/15
47+
COPY --from=trimmed-all /usr/share/postgresql/14 /usr/share/postgresql/14
48+
COPY --from=trimmed-all /usr/share/postgresql/15 /usr/share/postgresql/15
4949

5050
# Increment this to indicate that a re-index should be carried out on first startup with existing data; REINDEX can still be overidden
5151
# with OR_DISABLE_REINDEX=true

or-entrypoint.sh

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
9696
# Don't automatically abort on non-0 exit status, just in case timescaledb extension isn't installed
9797
set +e
9898

99-
# Get the latest TimescaleDB version available
99+
# Get the latest TimescaleDB version available for the OLD PostgreSQL version
100+
# We must use DB_VERSION here since we're running on the old server
100101
TS_VERSION_REGEX="\-\-([0-9|\.]+)\."
101-
TS_SCRIPT_NAME=$(find /usr/share/postgresql/$PG_MAJOR/extension/ -type f -name "timescaledb--*.sql" | sort | tail -n 1)
102+
TS_SCRIPT_NAME=$(find /usr/share/postgresql/${DB_VERSION}/extension/ -type f -name "timescaledb--*.sql" | sort | tail -n 1)
102103
if [ "$TS_SCRIPT_NAME" != "" ] && [[ $TS_SCRIPT_NAME =~ $TS_VERSION_REGEX ]]; then
103104
TARGET_TS_VERSION=${BASH_REMATCH[1]}
104105
echo "Target TimescaleDB version available: ${TARGET_TS_VERSION}"
@@ -387,23 +388,20 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
387388
fi
388389
fi
389390

390-
# Only do this upgrade if we're NOT in the middle of a PostgreSQL upgrade
391-
# (TimescaleDB was already upgraded in STEP 1 before pg_upgrade)
392-
if [ "$DB_VERSION" == "$PG_MAJOR" ]; then
393-
if [ "$DO_TS_UPGRADE" == "true" ] && [ "$OR_DISABLE_AUTO_UPGRADE" == "true" ]; then
394-
echo "----------------------------------------------------------------------------------"
395-
echo "TimescaleDB upgrade can be performed but OR_DISABLE_AUTO_UPGRADE=true so skipping!"
396-
echo "----------------------------------------------------------------------------------"
397-
fi
398-
399-
if [ "${OR_DISABLE_AUTO_UPGRADE}" == "true" ]; then
400-
DO_TS_UPGRADE=false
401-
fi
402-
else
403-
# PostgreSQL version mismatch means we're in upgrade mode
404-
# TimescaleDB upgrade already happened in STEP 1
391+
# Check if auto-upgrade is disabled
392+
if [ "$DO_TS_UPGRADE" == "true" ] && [ "$OR_DISABLE_AUTO_UPGRADE" == "true" ]; then
393+
echo "----------------------------------------------------------------------------------"
394+
echo "TimescaleDB upgrade can be performed but OR_DISABLE_AUTO_UPGRADE=true so skipping!"
395+
echo "----------------------------------------------------------------------------------"
405396
DO_TS_UPGRADE=false
406-
echo "Skipping TimescaleDB upgrade check - already upgraded in STEP 1 before pg_upgrade"
397+
fi
398+
399+
# If we just did a PostgreSQL upgrade, we MUST upgrade TimescaleDB on the new cluster
400+
# pg_upgrade copies extension metadata but doesn't upgrade extensions
401+
# STEP 1 upgraded TS on the OLD cluster, but pg_upgrade created a NEW cluster
402+
if [ "$DB_VERSION" != "$PG_MAJOR" ] && [ "$OR_DISABLE_AUTO_UPGRADE" != "true" ]; then
403+
echo "PostgreSQL was just upgraded - forcing TimescaleDB upgrade on new cluster"
404+
DO_TS_UPGRADE=true
407405
fi
408406

409407

@@ -442,23 +440,42 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
442440
echo "================================================================================="
443441
echo "STEP 3: Upgrading TimescaleDB on PostgreSQL ${PG_MAJOR}..."
444442
echo "================================================================================="
445-
446-
# Get current version
447-
CURRENT_TS_VERSION=$(docker_process_sql -X -c "SELECT extversion FROM pg_extension WHERE extname='timescaledb';" | grep -v extversion | grep -v row | tr -d ' ')
448-
echo "Current TimescaleDB version: ${CURRENT_TS_VERSION}"
449443
echo "Target TimescaleDB version: ${TS_VERSION}"
450444

451-
# Don't automatically abort on non-0 exit status, just in case timescaledb extension isn't installed on the DB
452-
set +e
453-
docker_process_sql -X -c "ALTER EXTENSION timescaledb UPDATE;"
454-
455-
if [ $? -eq 0 ]; then
456-
NEW_TS_VERSION=$(docker_process_sql -X -c "SELECT extversion FROM pg_extension WHERE extname='timescaledb';" | grep -v extversion | grep -v row | tr -d ' ')
457-
echo "TimescaleDB upgraded: ${CURRENT_TS_VERSION} -> ${NEW_TS_VERSION}"
458-
docker_process_sql -c "CREATE EXTENSION IF NOT EXISTS timescaledb_toolkit; ALTER EXTENSION timescaledb_toolkit UPDATE;"
459-
fi
460-
461-
# Return the error handling back to automatically aborting on non-0 exit status
445+
# Don't automatically abort on non-0 exit status, just in case timescaledb extension isn't installed
446+
set +e
447+
448+
# Upgrade TimescaleDB in ALL databases that have it installed
449+
# This is critical after pg_upgrade which copies extension metadata but doesn't upgrade
450+
echo "Finding all databases with TimescaleDB extension..."
451+
DATABASES=$(docker_process_sql -X -t -c "SELECT datname FROM pg_database WHERE datallowconn;" | grep -v "^$")
452+
453+
for DB in $DATABASES; do
454+
echo "Checking database: $DB"
455+
HAS_TS=$(docker_process_sql -X -d "$DB" -c "SELECT 1 FROM pg_extension WHERE extname='timescaledb';" | grep -v "^$" | wc -l)
456+
457+
if [ "$HAS_TS" -gt 0 ]; then
458+
CURRENT_TS_VERSION=$(docker_process_sql -X -d "$DB" -c "SELECT extversion FROM pg_extension WHERE extname='timescaledb';" | grep -v extversion | grep -v row | tr -d ' ')
459+
echo " Database $DB has TimescaleDB ${CURRENT_TS_VERSION}, upgrading..."
460+
docker_process_sql -X -d "$DB" -c "ALTER EXTENSION timescaledb UPDATE;"
461+
NEW_TS_VERSION=$(docker_process_sql -X -d "$DB" -c "SELECT extversion FROM pg_extension WHERE extname='timescaledb';" | grep -v extversion | grep -v row | tr -d ' ')
462+
echo " Upgraded: ${CURRENT_TS_VERSION} -> ${NEW_TS_VERSION}"
463+
464+
# Also upgrade toolkit if present
465+
HAS_TOOLKIT=$(docker_process_sql -X -d "$DB" -c "SELECT 1 FROM pg_extension WHERE extname='timescaledb_toolkit';" | grep -v "^$" | wc -l)
466+
if [ "$HAS_TOOLKIT" -gt 0 ]; then
467+
echo " Upgrading timescaledb_toolkit in $DB..."
468+
docker_process_sql -X -d "$DB" -c "ALTER EXTENSION timescaledb_toolkit UPDATE;"
469+
else
470+
echo " Creating timescaledb_toolkit in $DB..."
471+
docker_process_sql -d "$DB" -c "CREATE EXTENSION IF NOT EXISTS timescaledb_toolkit;"
472+
fi
473+
fi
474+
done
475+
476+
echo "TimescaleDB upgrade complete in all databases"
477+
478+
# Return the error handling back to automatically aborting on non-0 exit status
462479
set -e
463480

464481
echo "================================================================================="

0 commit comments

Comments
 (0)