Remove data job removeDataCommand returns an entrypoint that removes certain directories. We currently target the `pgdata/pg{old_version}` and `pgdata/pg{old_version}_wal` directories for removal.
(upgrade *v1beta1.PGUpgrade)
| 267 | // We currently target the `pgdata/pg{old_version}` and `pgdata/pg{old_version}_wal` |
| 268 | // directories for removal. |
| 269 | func removeDataCommand(upgrade *v1beta1.PGUpgrade) []string { |
| 270 | oldVersion := upgrade.Spec.FromPostgresVersion |
| 271 | |
| 272 | // Before removing the directories (both data and wal), we check that |
| 273 | // the directory is not in use by running `pg_controldata` and making sure |
| 274 | // the server state is "shut down in recovery" |
| 275 | args := []string{fmt.Sprint(oldVersion)} |
| 276 | script := strings.Join([]string{ |
| 277 | // Exit immediately when a pipeline or subshell exits non-zero or when expanding an unset variable. |
| 278 | `shopt -so errexit nounset`, |
| 279 | |
| 280 | `declare -r data_volume='/pgdata' old_version="$1"`, |
| 281 | `printf 'Removing PostgreSQL %s data...\n\n' "$@"`, |
| 282 | `delete() (set -x && rm -rf -- "$@")`, |
| 283 | |
| 284 | `old_data="${data_volume}/pg${old_version}"`, |
| 285 | `control=$(` + postgres.ShellPath(oldVersion) + ` && LC_ALL=C pg_controldata "${old_data}")`, |
| 286 | `read -r state <<< "${control##*cluster state:}"`, |
| 287 | |
| 288 | // We expect exactly one state for a replica that has been stopped. |
| 289 | // |
| 290 | // https://git.postgresql.org/gitweb/?p=postgresql.git;hb=refs/tags/REL_10_0;f=src/bin/pg_controldata/pg_controldata.c#l55 |
| 291 | // https://git.postgresql.org/gitweb/?p=postgresql.git;hb=refs/tags/REL_17_0;f=src/bin/pg_controldata/pg_controldata.c#l58 |
| 292 | `[[ "${state}" == 'shut down in recovery' ]] || { printf >&2 'Unexpected state! %q\n' "${state}"; exit 1; }`, |
| 293 | |
| 294 | // "rm" does not follow symbolic links. |
| 295 | // Delete the old data directory after subdirectories that contain versioned data. |
| 296 | `delete "${old_data}/pg_wal/"`, |
| 297 | `delete "${old_data}" && echo 'Success!'`, |
| 298 | }, "\n") |
| 299 | |
| 300 | return append([]string{"bash", "-c", "--", script, "remove"}, args...) |
| 301 | } |
| 302 | |
| 303 | // generateRemoveDataJob returns a Job that can remove the data |
| 304 | // on the given replica StatefulSet |
no test coverage detected