Skip to content

Commit 4f7064c

Browse files
committed
Cleanup of entrypoint logic and fix slim image missing tune binary
1 parent b7e67c0 commit 4f7064c

2 files changed

Lines changed: 134 additions & 171 deletions

File tree

or-entrypoint.sh

Lines changed: 133 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,14 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
7070
if [ -s "${PGDATA}/PG_VERSION" ]; then
7171
DB_VERSION=$(cat "${PGDATA}/PG_VERSION")
7272
fi
73+
74+
# Get the current version of the timescaleDB extension
75+
TS_VERSION=""
76+
if [ -s "${PGDATA}/OR_TS_VERSION" ]; then
77+
TS_VERSION=$(cat "${PGDATA}/OR_TS_VERSION")
78+
fi
7379

74-
if [ "$DB_VERSION" != "$PG_MAJOR" ] && [ "$OR_DISABLE_AUTO_UPGRADE" == "true" ]; then
80+
if [ "$DB_VERSION" != "$PG_MAJOR" ] && [ "$OR_DISABLE_AUTO_UPGRADE" == "true" ]; then
7581
echo "---------------------------------------------------------------------------------"
7682
echo "Postgres major version has changed but OR_DISABLE_AUTO_UPGRADE=true so container will likely fail to start!"
7783
echo "---------------------------------------------------------------------------------"
@@ -98,33 +104,43 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
98104
fi
99105
fi
100106

101-
# STEP 1: Upgrade TimescaleDB on OLD PostgreSQL version (if needed)
102-
# This must happen BEFORE pg_upgrade so both old and new PG have the same TS version
103-
if [ "$DB_VERSION" != "$PG_MAJOR" ] && [ "$OR_DISABLE_AUTO_UPGRADE" != "true" ]; then
104-
echo "================================================================================="
105-
echo "STEP 1: Upgrading TimescaleDB on PostgreSQL ${DB_VERSION} before PG upgrade..."
106-
echo "================================================================================="
107-
108-
# Start temporary server on OLD PostgreSQL version
109-
echo "Starting temporary PostgreSQL ${DB_VERSION} server..."
110-
111-
# Temporarily update PATH to use old PostgreSQL version
112-
OLD_PATH=$PATH
113-
export PATH="/usr/lib/postgresql/${DB_VERSION}/bin:$PATH"
114-
115-
docker_temp_server_start "$@"
116-
117-
# Don't automatically abort on non-0 exit status, just in case timescaledb extension isn't installed
118-
set +e
119-
120-
# Get the latest TimescaleDB version available for the OLD PostgreSQL version
121-
# We must use DB_VERSION here since we're running on the old server
122-
TS_VERSION_REGEX="\-\-([0-9|\.]+)\."
123-
TS_SCRIPT_NAME=$(find /usr/share/postgresql/${DB_VERSION}/extension/ -type f -name "timescaledb--*.sql" | sort | tail -n 1)
124-
if [ "$TS_SCRIPT_NAME" != "" ] && [[ $TS_SCRIPT_NAME =~ $TS_VERSION_REGEX ]]; then
125-
TARGET_TS_VERSION=${BASH_REMATCH[1]}
126-
echo "Target TimescaleDB version available: ${TARGET_TS_VERSION}"
107+
# STEP 1: Upgrade TimescaleDB on existing PostgreSQL version (if needed)
108+
# This must happen BEFORE and AFTER pg_upgrade to ensure both are on the latest versions for each PG version
109+
echo "---------------------------------------------------------------------------------"
110+
echo "STEP 1: Looking for Timescale DB latest extension version for current PG $DB_VERSION"
111+
echo "---------------------------------------------------------------------------------"
112+
if [ -z "$TS_VERSION" ]; then
113+
echo "OR_TS_VERSION doesn't exist so assuming TS latest version should be installed"
114+
NEW_TS_VERSION=$(ls /usr/share/postgresql/$DB_VERSION/extension/timescaledb--*.sql | grep -v '[0-9]--[0-9]' | sed 's/.*timescaledb--//;s/\.sql//' | sort -V | tail -n 1)
115+
else
116+
echo "Current TS version is $TS_VERSION looking for latest upgradable version..."
117+
NEW_TS_VERSION=$(ls /usr/share/postgresql/$DB_VERSION/extension/timescaledb--$TS_VERSION*.sql | sort -V | tail -n 1 | sed 's/.*--//;s/\.sql$//')
118+
fi
119+
120+
if [ -z "$NEW_TS_VERSION" ]; then
121+
echo "Cannot determine the latest available version of TimescaleDB"
122+
exit 13
123+
fi
124+
125+
if [ "$NEW_TS_VERSION" != "$TS_VERSION" ]; then
126+
if [ "$OR_DISABLE_AUTO_UPGRADE" == "true" ]; then
127+
echo "WARNING: A newer TS version is available but auto upgrade is disabled"
128+
else
129+
echo "Upgrading Timescale DB to version $NEW_TS_VERSION..."
130+
131+
# Start temporary server
132+
echo "Starting temporary PostgreSQL ${DB_VERSION} server..."
133+
134+
# Temporarily update PATH to use old PostgreSQL version
135+
OLD_PATH=$PATH
136+
export PATH="/usr/lib/postgresql/${DB_VERSION}/bin:$PATH"
137+
138+
docker_temp_server_start "$@"
139+
140+
# Don't automatically abort on non-0 exit status, just in case timescaledb extension isn't installed
141+
set +e
127142

143+
INSTALLED_TS_VERSION="$NEW_TS_VERSION"
128144
# Upgrade TimescaleDB in ALL databases that have it installed
129145
# This is critical because template1, postgres, and user databases may all have TimescaleDB
130146
# We must include template databases because template1 often has TimescaleDB installed
@@ -136,14 +152,14 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
136152
HAS_TS=$(docker_process_sql -X -d "$DB" -c "SELECT 1 FROM pg_extension WHERE extname='timescaledb';" | grep -v "^$" | wc -l)
137153

138154
if [ "$HAS_TS" -gt 0 ]; then
139-
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 ' ')
155+
CURRENT_TS_VERSION=$(docker_process_sql -t -d "$DB" -c "SELECT extversion FROM pg_extension WHERE extname = 'timescaledb';" | tr -d ' ')
140156
echo " Database $DB has TimescaleDB ${CURRENT_TS_VERSION}, upgrading..."
141157
docker_process_sql -X -d "$DB" -c "ALTER EXTENSION timescaledb UPDATE;"
142-
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 ' ')
143-
echo " Upgraded: ${CURRENT_TS_VERSION} -> ${NEW_TS_VERSION}"
158+
INSTALLED_TS_VERSION=$(docker_process_sql -t -d "$DB" -c "SELECT extversion FROM pg_extension WHERE extname = 'timescaledb';" | tr -d ' ')
159+
echo " Upgraded: ${CURRENT_TS_VERSION} -> ${INSTALLED_TS_VERSION}"
144160

145161
# Also upgrade toolkit if present
146-
HAS_TOOLKIT=$(docker_process_sql -X -d "$DB" -c "SELECT 1 FROM pg_extension WHERE extname='timescaledb_toolkit';" | grep -v "^$" | wc -l)
162+
HAS_TOOLKIT=$(docker_process_sql -t -d "$DB" -c "SELECT 1 FROM pg_extension WHERE extname = 'timescaledb_toolkit';" | tr -d ' ')
147163
if [ "$HAS_TOOLKIT" -gt 0 ]; then
148164
echo " Upgrading timescaledb_toolkit in $DB..."
149165
docker_process_sql -X -d "$DB" -c "ALTER EXTENSION timescaledb_toolkit UPDATE;"
@@ -152,19 +168,26 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
152168
done
153169

154170
echo "TimescaleDB upgrade complete in all databases"
171+
172+
# Return error handling back to automatically aborting on non-0 exit status
173+
set -e
174+
175+
echo "Stopping temporary server..."
176+
docker_temp_server_stop
177+
178+
# Restore PATH
179+
export PATH=$OLD_PATH
180+
181+
if [ "$INSTALLED_TS_VERSION" != "$NEW_TS_VERSION" ]; then
182+
echo "ERROR Upgraded TS version = $INSTALLED_TS_VERSION but expected $NEW_TS_VERSION"
183+
exit 14
184+
fi
185+
186+
echo "================================================================================="
187+
echo "STEP 1 Complete: TimescaleDB upgraded to $INSTALLED_TS_VERSION on PostgreSQL ${DB_VERSION}"
188+
echo "================================================================================="
189+
echo "$INSTALLED_TS_VERSION" > "${PGDATA}/OR_TS_VERSION"
155190
fi
156-
157-
# Return error handling back to automatically aborting on non-0 exit status
158-
set -e
159-
160-
docker_temp_server_stop
161-
162-
# Restore PATH
163-
export PATH=$OLD_PATH
164-
165-
echo "================================================================================="
166-
echo "STEP 1 Complete: TimescaleDB upgraded on PostgreSQL ${DB_VERSION}"
167-
echo "================================================================================="
168191
fi
169192

170193
# STEP 2: Upgrade PostgreSQL if needed
@@ -359,74 +382,69 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
359382
set -e
360383
fi
361384

362-
# STEP 3: Upgrade TimescaleDB on NEW PostgreSQL version (if needed)
363-
# Do timescale upgrade if needed - First look for latest extension version number in extension files
364-
echo "----------------------------------------------------------"
365-
echo "Checking latest available TimescaleDB extension version..."
366-
echo "----------------------------------------------------------"
367-
TS_VERSION_REGEX="\-\-([0-9|\.]+)\."
368-
TS_SCRIPT_NAME=$(find /usr/share/postgresql/$PG_MAJOR/extension/ -type f -name "timescaledb--*.sql" | sort | tail -n 1)
369-
TS_VERSION=""
370-
TS_VERSION_FILE="${PGDATA}/OR_TS_VERSION"
371-
372-
if [ "$TS_SCRIPT_NAME" == "" ] || ! [[ $TS_SCRIPT_NAME =~ $TS_VERSION_REGEX ]]; then
373-
echo "------------------------------------------------------"
374-
echo "Cannot determine current TimescaleDB extension version"
375-
echo "------------------------------------------------------"
376-
exit 15
377-
else
378-
TS_VERSION=${BASH_REMATCH[1]}
379-
fi
380-
381-
if [ "$TS_VERSION" == "" ]; then
382-
echo "------------------------------------------------------"
383-
echo "Cannot determine current TimescaleDB extension version"
384-
echo "------------------------------------------------------"
385-
exit 15
386-
fi
387-
388-
DO_TS_UPGRADE=false
389-
echo "Checking whether Timescale needs upgrading..."
390-
if [ ! -f "${TS_VERSION_FILE}" ]; then
391-
echo "-----------------------------------------------------"
392-
echo "No OR_TS_VERSION file so assuming upgrade is required"
393-
echo "-----------------------------------------------------"
394-
DO_TS_UPGRADE=true
395-
else
396-
echo "-------------------------------------------------------"
397-
echo "Getting version number from existing OR_TS_VERSION file"
398-
echo "-------------------------------------------------------"
399-
PREVIOUS_TS_VERSION=$(cat "$TS_VERSION_FILE")
400-
if [ "${PREVIOUS_TS_VERSION}" != "${TS_VERSION}" ]; then
401-
echo "------------------------------------------------------------------------------"
402-
echo "TimescaleDB extension upgrade required ${PREVIOUS_TS_VERSION} -> ${TS_VERSION}"
403-
echo "------------------------------------------------------------------------------"
404-
DO_TS_UPGRADE=true
405-
else
406-
echo "----------------------------------------------------"
407-
echo "TimescaleDB extension is up to date at: ${TS_VERSION}"
408-
echo "----------------------------------------------------"
409-
fi
410-
fi
411-
412-
# Check if auto-upgrade is disabled
413-
if [ "$DO_TS_UPGRADE" == "true" ] && [ "$OR_DISABLE_AUTO_UPGRADE" == "true" ]; then
414-
echo "----------------------------------------------------------------------------------"
415-
echo "TimescaleDB upgrade can be performed but OR_DISABLE_AUTO_UPGRADE=true so skipping!"
416-
echo "----------------------------------------------------------------------------------"
417-
DO_TS_UPGRADE=false
418-
fi
419-
420385
# If we just did a PostgreSQL upgrade, we MUST upgrade TimescaleDB on the new cluster
421386
# pg_upgrade copies extension metadata but doesn't upgrade extensions
422387
# STEP 1 upgraded TS on the OLD cluster, but pg_upgrade created a NEW cluster
423388
if [ "$DB_VERSION" != "$PG_MAJOR" ] && [ "$OR_DISABLE_AUTO_UPGRADE" != "true" ]; then
424-
echo "PostgreSQL was just upgraded - forcing TimescaleDB upgrade on new cluster"
425-
DO_TS_UPGRADE=true
426-
fi
389+
echo "---------------------------------------------------------------------------------"
390+
echo "STEP 3: Running Timescale DB upgrade for PG $DB_VERSION"
391+
echo "---------------------------------------------------------------------------------"
392+
393+
# Start temporary server
394+
echo "Starting temporary server..."
395+
docker_temp_server_start "$@"
396+
397+
# Don't automatically abort on non-0 exit status, just in case timescaledb extension isn't installed
398+
set +e
427399

400+
INSTALLED_TS_VERSION=
401+
# Upgrade TimescaleDB in ALL databases that have it installed
402+
# This is critical because template1, postgres, and user databases may all have TimescaleDB
403+
# We must include template databases because template1 often has TimescaleDB installed
404+
echo "Finding all databases with TimescaleDB extension..."
405+
DATABASES=$(docker_process_sql -X -t -c "SELECT datname FROM pg_database WHERE datallowconn;" | grep -v "^$")
406+
407+
for DB in $DATABASES; do
408+
echo "Checking database: $DB"
409+
HAS_TS=$(docker_process_sql -X -d "$DB" -c "SELECT 1 FROM pg_extension WHERE extname='timescaledb';" | grep -v "^$" | wc -l)
410+
411+
if [ "$HAS_TS" -gt 0 ]; then
412+
CURRENT_TS_VERSION=$(docker_process_sql -t -d "$DB" -c "SELECT extversion FROM pg_extension WHERE extname = 'timescaledb';" | tr -d ' ')
413+
echo " Database $DB has TimescaleDB ${CURRENT_TS_VERSION}, upgrading..."
414+
docker_process_sql -X -d "$DB" -c "ALTER EXTENSION timescaledb UPDATE;"
415+
INSTALLED_TS_VERSION=$(docker_process_sql -t -d "$DB" -c "SELECT extversion FROM pg_extension WHERE extname = 'timescaledb';" | tr -d ' ')
416+
echo " Upgraded: ${CURRENT_TS_VERSION} -> ${INSTALLED_TS_VERSION}"
417+
418+
# Also upgrade toolkit if present
419+
HAS_TOOLKIT=$(docker_process_sql -t -d "$DB" -c "SELECT 1 FROM pg_extension WHERE extname = 'timescaledb_toolkit';" | tr -d ' ')
420+
if [ "$HAS_TOOLKIT" -gt 0 ]; then
421+
echo " Upgrading timescaledb_toolkit in $DB..."
422+
docker_process_sql -X -d "$DB" -c "ALTER EXTENSION timescaledb_toolkit UPDATE;"
423+
fi
424+
fi
425+
done
426+
427+
echo "TimescaleDB upgrade complete in all databases"
428+
429+
# Return error handling back to automatically aborting on non-0 exit status
430+
set -e
428431

429-
# Do re-indexing check
432+
echo "Stopping temporary server..."
433+
docker_temp_server_stop
434+
435+
if [ -z "$INSTALLED_TS_VERSION" ]; then
436+
echo "================================================================================="
437+
echo "STEP 3 Error: Cannot determine latest installed version of TimescaleDB"
438+
echo "================================================================================="
439+
else
440+
echo "================================================================================="
441+
echo "STEP 3 Complete: TimescaleDB upgraded to $INSTALLED_TS_VERSION on PostgreSQL ${DB_VERSION}"
442+
echo "================================================================================="
443+
echo "$INSTALLED_TS_VERSION" > "${PGDATA}/OR_TS_VERSION"
444+
fi
445+
fi
446+
447+
# STEP 4: Do re-indexing check
430448
DO_REINDEX=false
431449
if [ "$OR_DISABLE_REINDEX" == 'true' ] || [ -z "$OR_REINDEX_COUNTER" ]; then
432450
echo "----------------------------"
@@ -449,77 +467,21 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
449467
fi
450468
fi
451469

452-
if [ "$DO_REINDEX" == "true" ] || [ "$DO_TS_UPGRADE" == "true" ]; then
453-
echo "-------------------------"
454-
echo "Starting temporary server"
455-
echo "-------------------------"
470+
if [ "$DO_REINDEX" == "true" ]; then
471+
echo "Starting temporary server..."
456472
docker_temp_server_start "$@"
457473

458-
# STEP 3: Upgrade TimescaleDB on new PostgreSQL version (if needed)
459-
# Cannot do this on a running DB as the extension is configured to preload
460-
if [ "$DO_TS_UPGRADE" == "true" ]; then
461-
echo "================================================================================="
462-
echo "STEP 3: Upgrading TimescaleDB on PostgreSQL ${PG_MAJOR}..."
463-
echo "================================================================================="
464-
echo "Target TimescaleDB version: ${TS_VERSION}"
465-
466-
# Don't automatically abort on non-0 exit status, just in case timescaledb extension isn't installed
467-
set +e
468-
469-
# Upgrade TimescaleDB in ALL databases that have it installed
470-
# This is critical after pg_upgrade which copies extension metadata but doesn't upgrade
471-
echo "Finding all databases with TimescaleDB extension..."
472-
DATABASES=$(docker_process_sql -X -t -c "SELECT datname FROM pg_database WHERE datallowconn;" | grep -v "^$")
473-
474-
for DB in $DATABASES; do
475-
echo "Checking database: $DB"
476-
HAS_TS=$(docker_process_sql -X -d "$DB" -c "SELECT 1 FROM pg_extension WHERE extname='timescaledb';" | grep -v "^$" | wc -l)
474+
echo "Running timescaledb tune script..."
475+
/docker-entrypoint-initdb.d/001_timescaledb_tune.sh
477476

478-
if [ "$HAS_TS" -gt 0 ]; then
479-
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 ' ')
480-
echo " Database $DB has TimescaleDB ${CURRENT_TS_VERSION}, upgrading..."
481-
docker_process_sql -X -d "$DB" -c "ALTER EXTENSION timescaledb UPDATE;"
482-
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 ' ')
483-
echo " Upgraded: ${CURRENT_TS_VERSION} -> ${NEW_TS_VERSION}"
484-
485-
# Also upgrade toolkit if present
486-
HAS_TOOLKIT=$(docker_process_sql -X -d "$DB" -c "SELECT 1 FROM pg_extension WHERE extname='timescaledb_toolkit';" | grep -v "^$" | wc -l)
487-
if [ "$HAS_TOOLKIT" -gt 0 ]; then
488-
echo " Upgrading timescaledb_toolkit in $DB..."
489-
docker_process_sql -X -d "$DB" -c "ALTER EXTENSION timescaledb_toolkit UPDATE;"
490-
else
491-
echo " Creating timescaledb_toolkit in $DB..."
492-
docker_process_sql -d "$DB" -c "CREATE EXTENSION IF NOT EXISTS timescaledb_toolkit;"
493-
fi
494-
fi
495-
done
496-
497-
echo "TimescaleDB upgrade complete in all databases"
498-
499-
# Return the error handling back to automatically aborting on non-0 exit status
500-
set -e
501-
502-
echo "================================================================================="
503-
echo "STEP 3 Complete: TimescaleDB upgraded on PostgreSQL ${PG_MAJOR}"
504-
echo "================================================================================="
505-
echo "$TS_VERSION" > "${PGDATA}/OR_TS_VERSION"
506-
fi
477+
echo "Re-indexing the DB..."
478+
docker_process_sql -c "REINDEX database $POSTGRES_DB;"
479+
507480

508-
if [ "$DO_REINDEX" == "true" ]; then
509-
echo "----------------------------------"
510-
echo "Running timescaledb tune script..."
511-
echo "----------------------------------"
512-
/docker-entrypoint-initdb.d/001_timescaledb_tune.sh
513-
echo "---------------------"
514-
echo "Re-indexing the DB..."
515-
echo "---------------------"
516-
docker_process_sql -c "REINDEX database $POSTGRES_DB;"
517-
echo "------------------"
518-
echo "REINDEX completed!"
519-
echo "------------------"
520-
touch "$REINDEX_FILE"
521-
fi
481+
echo "REINDEX completed!"
482+
touch "$REINDEX_FILE"
522483

484+
echo "Stoppig temporary server..."
523485
docker_temp_server_stop
524486
fi
525487
fi

slim-image.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ slim build --target "$SOURCE_IMAGE" \
5959
--preserve-path=/etc/postgresql \
6060
--preserve-path=/etc/ssl \
6161
--include-shell \
62+
--include-bin=/usr/local/bin/timescaledb-tune
6263
--include-bin=/usr/bin/sort \
6364
--include-bin=/usr/bin/find \
6465
--include-bin=/usr/bin/xargs \

0 commit comments

Comments
 (0)