@@ -121,11 +121,33 @@ for version in $DEB_PG_SUPPORTED_VERSIONS; do
121121 " postgresql-${version} -pg-stat-kcache" \
122122 " ${EXTRAS[@]} "
123123
124- # Clean up timescaledb versions except the last 5 minor versions
124+ # Clean up timescaledb versions - keep at least 5 minor versions, but ensure compatibility with the lowest/oldest PG version (where possible)
125125 # To ensure backward compatibility with our production system i.e 2.11.2
126+
126127 exclude_patterns=()
127128 versions=$( find " /usr/lib/postgresql/$version /lib/" -name ' timescaledb-2.*.so' | sed -rn ' s/.*timescaledb-([1-9]+\.[0-9]+\.[0-9]+)\.so$/\1/p' | sort -rV)
128- latest_minor_versions=$( echo " $versions " | awk -F. ' {print $1"."$2}' | uniq | head -n 5)
129+
130+ # Calculate the number of versions dynamically based on the lowest PG version's latest minor
131+ num_versions=5
132+ if [ -n " $first_latest_minor " ]; then
133+ minor_versions=$( echo " $versions " | awk -F. ' {print $1"."$2}' | uniq)
134+ position=0
135+ found=0
136+ while IFS= read -r minor; do
137+ position=$(( position + 1 ))
138+ if [ " $minor " = " $first_latest_minor " ]; then
139+ found=1
140+ break
141+ fi
142+ done <<< " $minor_versions"
143+
144+ # if found, keep max(5, position) versions (so all versions have at least 1 version in common with lowest PG version)
145+ if [ $found -eq 1 ] && [ $position -gt $num_versions ]; then
146+ num_versions=$position
147+ fi
148+ fi
149+
150+ latest_minor_versions=$( echo " $versions " | awk -F. ' {print $1"."$2}' | uniq | head -n " $num_versions " )
129151 for minor in $latest_minor_versions ; do
130152 for full_version in $( echo " $versions " | grep " ^$minor " ) ; do
131153 exclude_patterns+=(! -name timescaledb-" ${full_version} " .so)
@@ -134,6 +156,11 @@ for version in $DEB_PG_SUPPORTED_VERSIONS; do
134156 done
135157 # find "/usr/lib/postgresql/$version/lib/" \( -name 'timescaledb-2.*.so' -o -name 'timescaledb-tsl-2.*.so' \) "${exclude_patterns[@]}" -delete
136158
159+ # Save the latest minor version from the first PG version
160+ if [ -z " $first_latest_minor " ]; then
161+ first_latest_minor=$( echo " $latest_minor_versions " | head -n 1)
162+ fi
163+
137164 # Install 3rd party stuff
138165
139166 if [ " ${TIMESCALEDB_APACHE_ONLY} " != " true" ] && [ " ${TIMESCALEDB_TOOLKIT} " = " true" ]; then
0 commit comments