Files
cozystack/hack
Andrey Kolkov 98866c9b49 fix(postgres): rebuild indexes whose collation changed version
An operand image built on another Debian release ships another glibc and
ICU. An index ordered by a collation of either library is then ordered by
rules the server no longer applies: lookups through it miss rows, and a
unique index stops seeing duplicates. PostgreSQL only logs a warning.

The init Job compares the library version each collation was created with
against the one the server reports, rebuilds the indexes that depend on a
collation whose version changed, and refreshes the recorded versions once
every rebuild in the database succeeded. A database on the C or POSIX
collation with no explicitly collated index, which is what the chart
creates by default, has nothing to rebuild and is not delayed.

The pass has to hold on a hook that starts while the instances are still
rolling, runs as the superuser in databases tenants own, and is retried
after a failure:

- It runs after the passes that manage roles and databases, so that a
  rebuild that fails costs none of them, and it waits for the server to
  report the PostgreSQL version and Debian release of its own image when
  an index depends on a versioned collation. The rebuild counter of the
  package is left out of that comparison, because a tag is rebuilt in
  place.
- Every session pins search_path to pg_catalog, so that a function or an
  operator a tenant defined in public cannot stand in for a built-in.
- A retry continues where the last run stopped. REINDEX CONCURRENTLY gives
  an index a new OID, so the highest OID seen when a repair begins marks
  what is left; an index rebuilt in place keeps its OID and is told apart
  by its storage file. Both live in a setting of the database.
- A rebuild that cannot finish is not started for ever: after two
  attempts the index is left to an operator, the pass says how to clear
  it, and the release stays failed until then. Lock waits and in-place
  rebuilds are bounded by what is left of the Job.
- A collation that a range partition key depends on keeps its recorded
  version, because rebuilding an index moves no row between partitions.

PostgreSQL records a collation for an index only where it orders a key,
so the collations of the columns an index reads in its expressions and
predicate count as well. Before PostgreSQL 15 the server records no
version for a database's default collation, and the libc collation of the
same locale stands in for it.

The e2e suite creates a collation with a stale version and checks that an
upgrade rebuilds a unique index and an exclusion constraint's index on it,
refreshes the version and removes the repair setting.

Assisted-by: LLM
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
(cherry picked from commit a10e449a56)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
2026-11-25 10:49:47 +00:00
..