@@ -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