fix(postgres): run the collation repair outside the Helm hook

The collation repair ran at the end of the post-upgrade init-job and,
when the image moved, waited for the primary to run it. Helm starts that
hook as soon as the Cluster is patched; CNPG then rolls the replicas
first and the primary last, and every instance whose clients hold
sessions sits in smart shutdown for smartShutdownTimeout (180s on
existing clusters). With three instances the wait took about eight of
the Job's nine minutes, the Job printed "the init-job is out of time,
the next run of it carries on" and the HelmRelease went UpgradeFailed
until Flux retried. When the Job's psql met the primary restart instead,
it failed with "FATAL: the database system is shutting down" and failed
the upgrade the same way. A rebuild of a large index does not fit the
hook budget either.

Moving the repair into an ordinary manifest does not help:
helm-controller waits for every Job of a release to complete within the
same upgrade timeout. So a short post-upgrade hook, run after the
init-job, creates the repair Job through the API, outside the release,
and returns. The repair Job has an hour, keeps the wait for the new
image and the resume marks it had, and is owned by the Cluster. It has a
fixed name, so two repairs never run against one Cluster: the hook
leaves one still running on the same image alone and deletes any other,
waiting for its pod to go, before creating the new one, which also
retries a repair that failed. The hook's Role reaches only that Job and
the release's Cluster by name, apart from create, which RBAC cannot
narrow, and is deleted once the hooks succeed.

psql exits with status 2 both when an instance refuses connections while
shutting down and when it drops a session, so both Jobs now rerun their
script after a growing pause on that status instead of failing; the
scripts are idempotent and their elapsed-time bounds keep counting
across attempts. A connection lost during an index rebuild is reported
the same way. smartShutdownTimeout stays as it is: the repair no longer
waits inside the hook, so the rollout's length does not matter.

The PostGIS extension update waits for the same rollout, so it moves
into the repair Job with the collation pass, and the messages of both
name the next upgrade of the release as what starts them again. A lost
connection in the PostGIS pass is reported with the same status as in
the collation pass, so the Job reconnects instead of giving up.

The repair runs after install as well. The init-job used to record there
which Debian release each database started on, and without that record
the first image move is detected only if the repair reaches the primary
before it restarts. The Job sets PGCONNECT_TIMEOUT, so a primary that
does not answer is retried instead of using up the Job's deadline.

A failed index rebuild no longer stops the PostGIS update that follows
it: a unique index that duplicates keep from being rebuilt would
otherwise hold the extensions back until an operator removes them. The
Job still fails at its end.

With an hour to spend, the repair now waits for the new image whenever
the image changes, not only when an index already depends on a versioned
collation. Otherwise it planned on the previous primary, missed an index
created meanwhile, and the release it recorded at the end claimed that
index repaired.

The repair Job carries the WorkloadMonitor's selector labels, opts in to
having its failure reported with workloads.cozystack.io/report-failure,
and has no ttlSecondsAfterFinished, so a failed repair stays visible until the next
upgrade replaces it instead of vanishing while the indexes stay wrong.

Refs: #5459
Assisted-By: LLM
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
This commit is contained in:
Andrey Kolkov committed 2026-11-30 17:58:17 +07:00
1 parent 222c31fcf8
commit 66bba3bf1e
10 files changed
+574 -79

No files matched your search

+1 -1
View File
@@ -47,7 +47,7 @@ type ConfigSpec struct {
// PostgreSQL image flavor, fixed when the cluster is created. `postgresql` uses the CloudNativePG PostgreSQL image. `postgis` uses the CloudNativePG PostGIS image, which adds postgis, postgis_raster, postgis_topology, postgis_sfcgal, postgis_tiger_geocoder and address_standardizer; enable them per database in `databases.<name>.extensions`. Changing the flavor of an existing cluster is refused, and a backup restores only into a cluster of the flavor it was taken from: to change flavor, create a new cluster and move the data with a logical dump (pg_dump / pg_restore).
// +kubebuilder:default:="postgresql"
Flavor Flavor `json:"flavor"`
// Allow a cluster created on a Debian bullseye image (before the move to standard-trixie) to move to the new image without a clean locale check. Such a cluster is checked before each upgrade, except while `bootstrap.enabled` is set (set it back to false once a restore set up by hand has finished), and stays on its current image until the check finds nothing that depends on a collation other than C, POSIX or builtin: while this is false, a cluster the check found affected (an ICU or libc locale, or C.UTF-8) or could not finish on stays put, because glibc and ICU on the new image sort text differently and every index on such text must be rebuilt. A held cluster runs the bare bullseye tag it has, gets no further PostgreSQL minors and so none of their security fixes, and refuses a change of `version`, since new majors are built only on the new images. To leave that state, either remove the dependency (move such columns and indexes to C, or to the builtin `pg_c_utf8` on PostgreSQL 17 and later) and let the next upgrade check again, or set this to true in a maintenance window: the cluster moves on the next upgrade, replicas restart on the new image first and can return wrong results through `-ro`, and unique indexes can accept duplicates, until the indexes are rebuilt on the primary. The init Job rebuilds the indexes on such text, those on C.UTF-8 and other C.* locales included; check range partitions, CHECK constraints and materialized views on it, which a rebuild does not fix. A held cluster shows the hold only as an unchanged image; the result and the objects found are in the `postgres.cozystack.io/locale-data` and `postgres.cozystack.io/locale-data-objects` annotations of the cnpg.io Cluster, which an administrator can read. A restore of a backup taken on one of those bullseye images comes back on that image and is checked by the next upgrade.
// Allow a cluster created on a Debian bullseye image (before the move to standard-trixie) to move to the new image without a clean locale check. Such a cluster is checked before each upgrade, except while `bootstrap.enabled` is set (set it back to false once a restore set up by hand has finished), and stays on its current image until the check finds nothing that depends on a collation other than C, POSIX or builtin: while this is false, a cluster the check found affected (an ICU or libc locale, or C.UTF-8) or could not finish on stays put, because glibc and ICU on the new image sort text differently and every index on such text must be rebuilt. A held cluster runs the bare bullseye tag it has, gets no further PostgreSQL minors and so none of their security fixes, and refuses a change of `version`, since new majors are built only on the new images. To leave that state, either remove the dependency (move such columns and indexes to C, or to the builtin `pg_c_utf8` on PostgreSQL 17 and later) and let the next upgrade check again, or set this to true in a maintenance window: the cluster moves on the next upgrade, replicas restart on the new image first and can return wrong results through `-ro`, and unique indexes can accept duplicates, until the indexes are rebuilt on the primary. The collation repair Job, which a post-upgrade hook starts once the init Job is done, rebuilds the indexes on such text after the rollout, those on C.UTF-8 and other C.* locales included, and its log reports a rebuild that failed; check range partitions, CHECK constraints and materialized views on it, which a rebuild does not fix. A held cluster shows the hold only as an unchanged image; the result and the objects found are in the `postgres.cozystack.io/locale-data` and `postgres.cozystack.io/locale-data-objects` annotations of the cnpg.io Cluster, which an administrator can read. A restore of a backup taken on one of those bullseye images comes back on that image and is checked by the next upgrade.
// +kubebuilder:default:=false
AllowCollationChange bool `json:"allowCollationChange"`
// TLS configuration for server connections.