Files
cozystack/packages/system/postgres-rd
Andrey Kolkov 66bba3bf1e 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>
2026-11-30 17:58:17 +07:00
..