Aleksei Sviridkin 8e5600d5e3 fix(postgres): update PostGIS extensions after an image upgrade (#5477)
## What this PR does

Refs #5459 (item 2)

After an upgrade that moves a `postgis` flavor Postgres to a new PostGIS
release (3.6.1 to 3.7.0 for v14-v18 on the current `main`), the PostGIS
library is new but the SQL objects in every database stay on the old
release: `postgis_full_version()` reports `core procs from "3.6.1 ..."
need upgrade`. The release notes tell users to run `ALTER EXTENSION
postgis UPDATE`, but the init Job created the extensions as the
superuser, so the tenant gets `must be owner of extension postgis` and
has no way to run the update.

The init Job now runs that update. It covers `postgis`,
`postgis_raster`, `postgis_topology`, `postgis_sfcgal`,
`postgis_tiger_geocoder`, `address_standardizer` and
`address_standardizer_data_us` in every connectable database where they
are installed:

- The target is the `default_version` the server offers, so the pass
only runs once the server is on the new image. If the server is already
up to date, it does nothing.
- Helm starts the hook while CNPG is still rolling the instances. The
pass waits until the server offers the PostGIS version that the Job's
own image ships, and stops waiting 120 s before the Job deadline (the
same bound the collation pass uses).
- If the rollout is slower than that, the Job fails. The init Job is a
hook, so a run that ends well does not come again before the next change
of the release; a failed one makes the release retry once the server
runs the new image. The version probe is bounded by `timeout 10`.
- `postgis_extensions_upgrade()` is not used: it leaves out
`address_standardizer`, and on every call it updates each installed
extension to `ANY` and back, even when nothing changed. Each extension
gets `ALTER EXTENSION ... UPDATE TO <server default_version>`, with
`postgis` first. Each session pins `search_path` to `pg_catalog,
pg_temp`.
- If the image ships no update path for an extension, the pass prints a
warning and leaves it alone. The check uses `pg_extension_update_paths`.
- A tenant transaction can hold a lock an update needs, and one update
can wait for several in turn. Before each `ALTER EXTENSION`, the session
therefore sets `statement_timeout` and `lock_timeout` to what is left of
the Job, keeping the same one-minute reserve as the collation pass. So
every update, lock waits included, ends before the Job does. A database
reached with no time left counts as failed.
- The update scripts run as the superuser and write to tables a tenant's
admin role is granted `ALL` on, `spatial_ref_sys` among them (the
PostGIS 3.7 script inserts into it). A trigger the tenant attached there
would run with the superuser's privileges.
- The update session therefore sets `session_replication_role =
replica`, so triggers enabled the default way (`ENABLE ORIGIN`, what
`CREATE TRIGGER` gives) do not fire.
- Only the table's owner, the superuser, can enable a trigger `ALWAYS`
or `REPLICA`. A database where such a trigger sits on a table of these
extensions is not updated and is reported.
- A database whose extensions cannot be looked up counts as failed, and
the pass continues with the others.
- If an update fails, the other databases are still processed and then
the Job fails. Under `RetryOnFailure` the release is retried and shows
as failed, so the error does not get lost in the log of a Job that is
deleted an hour later. The pass runs after the collation repair, so a
failure here cannot block index rebuilds.
- The section is rendered only for `flavor: postgis`. For the
`postgresql` flavor, the rendered `init.sh` is byte-identical to `main`.

**Needs a separate decision:** PostGIS 3.7 renamed the
`address_standardizer` library (`address_standardizer-3.so` became
`address_standardizer.so`) and ships no update path from 3.6.1. As a
result, an `address_standardizer` created under 3.6.1 fails after the
upgrade with `could not access file "$libdir/address_standardizer-3"`.
`ALTER EXTENSION` cannot fix this; only `DROP EXTENSION` followed by
`CREATE EXTENSION` as the superuser can. This PR only reports it.
Whether the Job should drop and recreate the extension (which risks
tenant objects that depend on it) is left to reviewers.

### Testing

- Docker, rendered `init.sh` (`helm template --set flavor=postgis`) run
with the Job's environment on the `17.12-3.7.0-standard-trixie` image:
- Run against a server still on `17.7-3.6.1`: it waited, printed "the
server did not move to PostGIS 3.7.0 in time … failing the init-job" and
exited 1.
- A tenant transaction held open on `geography_columns` in `appdb`, with
`JOB_DEADLINE=85`:
- the update in `appdb` was cancelled when the budget ran out
(`canceling statement due to statement timeout`, 25 s);
    - `db2`, reached with no time left, was reported as not updated;
    - the Job exited 1 after 25 s;
- after the blocker ended, the next run updated both databases to 3.7.0
and exited 0;
    - a further run was a no-op.
- Tenant trigger: as a non-superuser `appdb_admin` granted `ALL` on
`public` tables, a `BEFORE INSERT ... FOR EACH STATEMENT` trigger on
`spatial_ref_sys` whose function runs `ALTER ROLE appdb_admin
SUPERUSER`.
    - Without the guard, the update made `appdb_admin` a superuser.
- With it, the update to 3.7.0 passed and `appdb_admin` stayed
non-superuser.
- With the same trigger set `ENABLE ALWAYS` (superuser only), the
database was skipped with an error, the Job exited 1, and the role
stayed non-superuser.
- The same data directory restarted on `17.12-3.7.0`: `postgis`,
`postgis_raster`, `postgis_topology` and `postgis_sfcgal` went from
3.6.1 to 3.7.0 and `postgis_tiger_geocoder` from 3.6.1 to 2025.2 in
`appdb`; `postgis` went to 3.7.0 in a second database. Afterwards
`postgis_full_version()` no longer reports "need upgrade". The md5 of a
spatial join and area query was the same before and after.
  - A second run was a no-op.
- A database with a planted conflicting object failed with `function
st_x(geometry) is not a member of extension "postgis"`. The other
database was still updated, and the Job exited 1.
- `flavor: postgresql`: the rendered `init.sh` is identical to `main`.
- `helm unittest packages/apps/postgres`: 31 suites, 247 tests passed.
- Dev cluster: v1.7.1 chart installed (postgis, v17, 2 replicas,
`postgis` in `appdb` at 3.6.1), then `helm upgrade` to this branch:
- The first attempt passed (exit 0). The Job logged `waiting for the
server to offer PostGIS 3.7.0`, then `updating extension postgis in
database appdb from 3.6.1 to 3.7.0`.
- Afterwards the image is `17.12-3.7.0-standard-trixie` and `postgis` is
3.7.0, with no "need upgrade" left. A spatial query gave the same md5 as
before.
  - A second upgrade left the PostGIS section empty.

**This change is verified only by hand** (Docker and the dev cluster
above). No e2e suite upgrades a postgis-flavor release, and one cannot
be added without an upgrade harness.

### Downstream repositories

The diff only touches
`packages/apps/postgres/templates/init-script.yaml`. It adds no values
key, schema field, version enum, default, app, CRD, hack script or
naming change, so none of the triggers in the map apply.

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

If a release note on the website tells users to run `ALTER EXTENSION
postgis UPDATE` by hand, that note becomes obsolete. I did not find such
a note in the trigger map and did not search the website repository.

### Release note

```release-note
[postgres] The init Job of a postgis-flavor Postgres now updates the PostGIS extensions in every database once the server runs the new image, so tenants no longer get "must be owner of extension postgis" or stay on outdated PostGIS SQL objects after an upgrade. An address_standardizer created under PostGIS 3.6 has no update path to 3.7 and is reported, not changed.
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Improvements**
* For PostGIS jobs, installed extensions are checked and updated to the
version available in the job environment, with dependencies handled in
order.
* The process waits for the required PostGIS version and skips
extensions without a supported update path, reporting a warning.
* Databases with relevant always-enabled or replica triggers are
rejected. Checks and updates are bounded by the job deadline; failures
are collected across databases, and the job reports an error if any
database could not be checked or updated.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-27 18:27:52 +03:00
2026-11-27 18:31:07 +05:00
2026-11-04 00:56:56 +01:00
2025-10-08 09:43:34 +05:00
2024-02-08 12:04:32 +01:00

Cozystack Cozystack

GitHub Release Apache-2.0 License Commit activity OpenSSF Best Practices OpenSSF Scorecard CLOMonitor Support

Cozystack

Cozystack is a free and open-source platform and framework for building clouds.

Cozystack is a CNCF Sandbox project that was originally built and sponsored by Ænix.

With Cozystack, you can transform a bunch of servers into an intelligent system with a simple REST API for spawning Kubernetes clusters, Database-as-a-Service, virtual machines, load balancers, HTTP caching services, and other services with ease.

Use Cozystack to build your own cloud or provide a cost-effective development environment.

Cozystack user interface

Use-Cases

Documentation

The documentation is located on the cozystack.io website.

Read the Getting Started section for a quick start.

If you encounter any difficulties, start with the troubleshooting guide and work your way through the process that we've outlined.

Versioning

Versioning adheres to the Semantic Versioning principles.
A full list of the available releases is available in the GitHub repository's Release section.

Contributions

Contributions are highly appreciated and very welcomed!

In case of bugs, please check if the issue has already been opened by checking the GitHub Issues section. If it isn't, you can open a new one. A detailed report will help us replicate it, assess it, and work on a fix.

You are welcome to take on the fix yourself. Commits are used to generate the changelog, and their author will be referenced in it.

If you have a concrete feature request, open an issue. For a cross-cutting or architectural change, open a design proposal in the community repo. For questions or early ideas, use GitHub Discussions. See CONTRIBUTING.md for the full routing.

Community

You are welcome to join us in #cozystack, on either CNCF Slack (get an invite) or Kubernetes Slack (get an invite), in our Telegram group, and at our community meetings. Add them to your Google Calendar or iCal for convenience.

Project documents

Document What it covers
Governance How decisions are made and who makes them
Contributor Ladder How to grow from contributor to maintainer
Maintainers Who maintains the project
Code of Conduct What we expect of each other
Security Policy Reporting a vulnerability, and which releases get fixes
Adopters Who runs Cozystack in production
Roadmap Where the project is going

License

Cozystack is licensed under Apache 2.0.
The code is provided as-is with no warranties.

Commercial Support

A list of companies providing commercial support for this project can be found on official site.

Languages
Go 53.1%
Shell 33%
TypeScript 6.8%
Go Template 4.6%
Makefile 1.3%
Other 1.1%