@@ -21,6 +21,7 @@ import (
2121 "github.com/crunchydata/postgres-operator/internal/feature"
2222 "github.com/crunchydata/postgres-operator/internal/initialize"
2323 "github.com/crunchydata/postgres-operator/internal/naming"
24+ "github.com/crunchydata/postgres-operator/internal/postgres"
2425 "github.com/crunchydata/postgres-operator/internal/shell"
2526 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2627)
@@ -86,13 +87,25 @@ func upgradeCommand(spec *v1beta1.PGUpgradeSettings, fetchKeyCommand string) []s
8687 `id; [[ "$(id -nu)" == 'postgres' && "$(id -ng)" == 'postgres' ]]` ,
8788
8889 `section 'Step 2 of 7: Finding data and tools...'` ,
89-
90- // Expect Postgres executables at the Red Hat paths.
91- `old_bin="/usr/pgsql-${old_version}/bin" && [[ -x "${old_bin}/postgres" ]]` ,
92- `new_bin="/usr/pgsql-${new_version}/bin" && [[ -x "${new_bin}/initdb" ]]` ,
9390 `old_data="${data_volume}/pg${old_version}" && [[ -d "${old_data}" ]]` ,
9491 `new_data="${data_volume}/pg${new_version}"` ,
9592
93+ // Search for Postgres executables matching the old and new versions.
94+ // Use `command -v` to look through all of PATH, then trim the executable name from the absolute path.
95+ `old_bin=$(` + postgres .ShellPath (oldVersion ) + ` && command -v postgres)` ,
96+ `old_bin="${old_bin%/postgres}"` ,
97+ `new_bin=$(` + postgres .ShellPath (newVersion ) + ` && command -v pg_upgrade)` ,
98+ `new_bin="${new_bin%/pg_upgrade}"` ,
99+
100+ // The executables found might not be the versions we need, so do a cursory check before writing to disk.
101+ // pg_upgrade checks every executable thoroughly since PostgreSQL v14.
102+ //
103+ // https://git.postgresql.org/gitweb/?p=postgresql.git;hb=refs/tags/REL_10_0;f=src/bin/pg_upgrade/exec.c#l355
104+ // https://git.postgresql.org/gitweb/?p=postgresql.git;hb=refs/tags/REL_14_0;f=src/bin/pg_upgrade/exec.c#l358
105+ // https://git.postgresql.org/gitweb/?p=postgresql.git;hb=refs/tags/REL_18_0;f=src/bin/pg_upgrade/exec.c#l370
106+ `(set -x && [[ "$("${old_bin}/postgres" --version)" =~ ") ${old_version}"($|[^0-9]) ]])` ,
107+ `(set -x && [[ "$("${new_bin}/initdb" --version)" =~ ") ${new_version}"($|[^0-9]) ]])` ,
108+
96109 // pg_upgrade writes its files in "${new_data}/pg_upgrade_output.d" since PostgreSQL v15.
97110 // Change to a writable working directory to be compatible with PostgreSQL v14 and earlier.
98111 //
@@ -115,6 +128,9 @@ func upgradeCommand(spec *v1beta1.PGUpgradeSettings, fetchKeyCommand string) []s
115128 `value=$(LC_ALL=C PGDATA="${old_data}" "${old_bin}/postgres" -C shared_preload_libraries)` ,
116129 `echo >> "${new_data}/postgresql.conf" "shared_preload_libraries = '${value//$'\''/$'\'\''}'"` ,
117130
131+ // NOTE: The default for --new-bindir is the directory of pg_upgrade since PostgreSQL v13.
132+ //
133+ // https://www.postgresql.org/docs/release/13#id-1.11.6.28.5.11
118134 `section 'Step 5 of 7: Checking for potential issues...'` ,
119135 `"${new_bin}/pg_upgrade" --check` + argMethod + argJobs + ` \` ,
120136 `--old-bindir="${old_bin}" --old-datadir="${old_data}" \` ,
@@ -253,7 +269,7 @@ func removeDataCommand(upgrade *v1beta1.PGUpgrade) []string {
253269 `delete() (set -x && rm -rf -- "$@")` ,
254270
255271 `old_data="${data_volume}/pg${old_version}"` ,
256- `control=$(LC_ALL=C /usr/pgsql-${old_version}/bin/ pg_controldata "${old_data}")` ,
272+ `control=$(` + postgres . ShellPath ( oldVersion ) + ` && LC_ALL=C pg_controldata "${old_data}")` ,
257273 `read -r state <<< "${control##*cluster state:}"` ,
258274
259275 // We expect exactly one state for a replica that has been stopped.
0 commit comments