diff --git a/internal/patroni/config.go b/internal/patroni/config.go index 8f9948796..58dd5920b 100644 --- a/internal/patroni/config.go +++ b/internal/patroni/config.go @@ -484,8 +484,6 @@ func instanceYAML( } postgresql := map[string]any{ - // TODO(cbandy): "bin_dir" - // Missing here is "connect_address" which cannot be known until the // instance Pod is created. That value should be injected using the downward // API and the PATRONI_POSTGRESQL_CONNECT_ADDRESS environment variable. diff --git a/internal/patroni/reconcile.go b/internal/patroni/reconcile.go index a8de99f02..c52629549 100644 --- a/internal/patroni/reconcile.go +++ b/internal/patroni/reconcile.go @@ -105,7 +105,14 @@ func InstancePod(ctx context.Context, } } - container.Command = []string{"patroni", configDirectory} + // Start Patroni in an environment that prioritizes executables of the specified Postgres version. + // NOTE: Patroni ignores PATH when "postgresql.bin_dir" is set. + // + // https://patroni.readthedocs.io/en/latest/yaml_configuration.html#postgresql + container.Command = []string{ + "sh", "-c", "--", postgres.ShellPath(inCluster.Spec.PostgresVersion) + ` && exec "$@"`, "--", + "patroni", configDirectory, + } container.Env = append(container.Env, instanceEnvironment(inCluster, inClusterPodService, inPatroniLeaderService, diff --git a/internal/patroni/reconcile_test.go b/internal/patroni/reconcile_test.go index 729bd6573..a4727a324 100644 --- a/internal/patroni/reconcile_test.go +++ b/internal/patroni/reconcile_test.go @@ -140,6 +140,12 @@ func TestInstancePod(t *testing.T) { assert.Assert(t, cmp.MarshalMatches(template.Spec, ` containers: - command: + - sh + - -c + - -- + - PATH="/usr/lib/postgresql/11/bin:/usr/libexec/postgresql11:/usr/pgsql-11/bin${PATH+:${PATH}}" + && exec "$@" + - -- - patroni - /etc/patroni env: diff --git a/internal/postgres/config.go b/internal/postgres/config.go index ebefc9dd6..7cc96d8d0 100644 --- a/internal/postgres/config.go +++ b/internal/postgres/config.go @@ -405,7 +405,7 @@ func startupCommand( instance *v1beta1.PostgresInstanceSetSpec, parameters *ParameterSet, ) []string { - version := fmt.Sprint(cluster.Spec.PostgresVersion) + version := cluster.Spec.PostgresVersion dataDir := DataDirectory(cluster) logDir := parameters.Value("log_directory") walDir := WALDirectory(cluster, instance) @@ -463,7 +463,7 @@ chmod +x /tmp/pg_rewind_tde.sh ` } - args := []string{version, walDir} + args := []string{fmt.Sprint(version), walDir} script := strings.Join([]string{ `declare -r expected_major_version="$1" pgwal_directory="$2"`, @@ -485,6 +485,9 @@ chmod +x /tmp/pg_rewind_tde.sh // Function to change a directory symlink while keeping the directory contents. strings.TrimSpace(bashSafeLink), + // Prioritize executables for this major version of Postgres. + ShellPath(version), + // Log the effective user ID and all the group IDs. `echo Initializing ...`, `results 'uid' "$(id -u ||:)" 'gid' "$(id -G ||:)"`, diff --git a/internal/postgres/reconcile_test.go b/internal/postgres/reconcile_test.go index 73ac1125d..f219d6905 100644 --- a/internal/postgres/reconcile_test.go +++ b/internal/postgres/reconcile_test.go @@ -274,6 +274,7 @@ initContainers: set -x; mv --no-target-directory "${current}" "${desired}" ln --no-dereference --force --symbolic "${desired}" "${name}" ) + PATH="/usr/lib/postgresql/11/bin:/usr/libexec/postgresql11:/usr/pgsql-11/bin${PATH+:${PATH}}" echo Initializing ... results 'uid' "$(id -u ||:)" 'gid' "$(id -G ||:)" if [[ "${pgwal_directory}" == *"pgwal/"* ]] && [[ ! -d "/pgwal/pgbackrest-spool" ]];then rm -rf "/pgdata/pgbackrest-spool" && mkdir -p "/pgwal/pgbackrest-spool" && ln --force --symbolic "/pgwal/pgbackrest-spool" "/pgdata/pgbackrest-spool";fi