Skip to content

feat: declarative offline in-place major upgrades of PostgreSQL - #6664

Merged
mnencia merged 38 commits into
cloudnative-pg:mainfrom
mnencia:dev/inplace
Mar 28, 2025
Merged

mnencia merged 38 commits into
cloudnative-pg:mainfrom
mnencia:dev/inplace

Conversation

@mnencia

@mnencia mnencia commented Jan 25, 2025 •

Copy link
Copy Markdown
Member

This PR introduces the ability to upgrade PostgreSQL clusters to a higher major version, enabling a fully declarative approach for major version upgrades within CloudNativePG.

IMPORTANT: While the upgrade job area still requires refactoring, we’ve decided to merge this patch into the main branch and release version 1.26.0-rc1 to gather early community feedback at KubeCon Europe 2025 in London. The refactoring will likely require a second release candidate before the final 1.26.0 release.

With this update, users can specify a new image with a higher major version using any supported method. The operator tracks the previous image version in .status.majorVersionUpgradeFromImage, which also serves as an indicator that a major version upgrade is in progress.

The upgrade process starts by deleting all pods and jobs to ensure the data directory is inaccessible. A dedicated upgrade job is created for the primary instance, using an initContainer to copy the old PostgreSQL installation and running pg_upgrade --link for an efficient migration. Once the upgrade completes, all non-primary instances are removed, and new standbys are cloned to restore the desired number of instances.

Closes #4682

Release Notes

Declarative Offline In-Place Major Upgrades of PostgreSQL: Introduced support for offline in-place major upgrades when a new operand container image with a higher PostgreSQL major version is applied to a cluster. All cluster pods are shut down during the upgrade to ensure data consistency. A new job is created to validate upgrade conditions, run pg_upgrade, and set up new directories for PGDATA, WAL files, and tablespaces as needed. Once the upgrade is complete, replicas are re-created. Failed upgrades can be rolled back declaratively. (#6664)

@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.22 labels Jan 25, 2025
@github-actions

Copy link
Copy Markdown
Contributor

❗ By default, the pull request is configured to backport to all release branches.

  • To stop backporting this pr, remove the label: backport-requested ◀️ or add the label 'do not backport'
  • To stop backporting this pr to a certain release branch, remove the specific branch label: release-x.y

@mnencia

mnencia commented Jan 25, 2025

Copy link
Copy Markdown
Member Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@pschichtel

Copy link
Copy Markdown

Just to be sure I understand this correctly: This is an offline migration as in: The cluster gets taken down completely, the primary upgraded in-place, started up again and new replicas are cloned from that, right?

@mnencia

mnencia commented Jan 27, 2025

Copy link
Copy Markdown
Member Author

Just to be sure I understand this correctly: This is an offline migration as in: The cluster gets taken down completely, the primary upgraded in-place, started up again and new replicas are cloned from that, right?

Yes exactly. However, it will be speedy, as no data will be copied around.

@mnencia mnencia added do not backport This PR must not be backported - it will be in the next minor release and removed backport-requested ◀️ This pull request should be backported to all supported releases release-1.22 labels Jan 27, 2025
solanyn added a commit to solanyn/home-ops that referenced this pull request Jan 30, 2025

@saolof saolof Feb 19, 2025 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a heads up that this will perform noticeably better on systems/containers with coreutils 9.x or higher when using xfs/btrfs/zfs/bcachefs, where cp reflinks by default instead of copying.

@borisroman

Copy link
Copy Markdown

Hi @mnencia, this looks like a wonderful PR! Are you still working on it? Or is there another reason it is still in draft?

@pschichtel

Copy link
Copy Markdown

just FYI, not in any way a request or anything: In my homelab I'm using zalando's operator, which is based around patroni and their spilo images. While there are a bunch of things I don't like about that operator, it's in-place major upgrades work quite well. I tested that on the weekend and it seems it keeps the replicas around while upgrading the master, so read queries could still be answered. Might be worth looking at what they are doing.

@armru armru self-assigned this Mar 6, 2025
@leonardoce leonardoce self-assigned this Mar 11, 2025

@leonardoce leonardoce Mar 19, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this code, we’re creating the instance manager reconciler just to call functions that download the secrets and make sure the WAL volume is set up correctly. The problem is, the first function doesn’t return any error, so we’re not really sure if the secrets were downloaded or not.

A similar issue pops up with the "initdb" call and the one that creates the metrics server. The "initdb" call should be encapsulated, and the metrics server creation shouldn't even be here in the first place.

Before merging this, we need to clean up the implementation to make it more maintainable and avoid repeating code, kind of like what we did with the subreconciler in the operator. This will take some time, and if we go down that path, we might not have enough time to hit the 1.26 release as planned.

Important: we need E2e tests too, and we should be careful on this one.

I’d love to hear what the other maintainers think. What’s your opinion?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest proceeding incrementally to ensure both functionality and maintainability. We should first validate the feature mechanics in their current form to confirm they work as expected. At the same time, we should complete the implementation of the patch's E2E tests to demonstrate how the feature operates, providing a reliable safety net before making structural changes. Once this validation is complete and the E2E tests are in place, we can refactor the code to improve its structure and maintainability while preserving functionality.

For the 1.26.0 release, if we don’t have enough time to complete the full work before 1.26.0rc1, we should prioritize merging the patch with the E2E tests in place, leaving the refactor for later. This will allow users to try it out and provide feedback. Based on that input, we can complete the refactoring and issue a new RC release.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but only partially. I won't call that refactor - there's no tests in place, I call it clean up.
It is not the first time we postponed the clean up phase, and the duplicated code is still there needing a clean up.
One example is that RefreshSecrets call, another one is the NewExporter call.
In this patch we add new duplication for the initdb command.

If this patch reaches a release we give value and help people immediately - and that's our goal - but we won't be able to continue helping later.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the 1.26.0 release, if we don’t have enough time to complete the full work before 1.26.0rc1, we should prioritize merging the patch with the E2E tests in place, leaving the refactor for later. This will allow users to try it out and provide feedback. Based on that input, we can complete the refactoring and issue a new RC release.

This confirms that there will definitely be an RC2. Is this what you mean, @mnencia? Help me understand. Thanks.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the 1.26.0 release, if we don’t have enough time to complete the full work before 1.26.0rc1, we should prioritize merging the patch with the E2E tests in place, leaving the refactor for later. This will allow users to try it out and provide feedback. Based on that input, we can complete the refactoring and issue a new RC release.

This confirms that there will definitely be an RC2. Is this what you mean, @mnencia? Help me understand. Thanks.

Yes, in that scenario, an RC2 would be necessary. If we merge the patch with E2E tests in place but defer the refactor, we will need an additional RC release once the cleanup is complete to ensure a properly maintained and polished implementation.

@mnencia
mnencia marked this pull request as ready for review March 24, 2025 18:17
@mnencia
mnencia requested a review from a team March 24, 2025 18:17
@mnencia

mnencia commented Mar 24, 2025

Copy link
Copy Markdown
Member Author

/test tl=4 d=main

leonardoce and others added 24 commits March 27, 2025 22:35
Signed-off-by: Niccolò Fei <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
A job retry could fail because the wal directory created for the upgrade
already exists. Clean it up before retrying.

Signed-off-by: Francesco Canovai <[email protected]>
Some storage classes don't seem to follow expected directory
permissions, so we explicitly set them before running pg_upgrade to
avoid errors during initial checks.

Signed-off-by: Francesco Canovai <[email protected]>
pg_upgrade until version 16 needs details about locales, or its
incompatibility check will fail if there are differences. The failure
happens on the default settings, given that the operator uses the C
locale by default and pg_upgrade uses en_US.utf-8.
Let's avoid adding support for upgrades to versions that are already
old, and keep the patch simpler.

Signed-off-by: Francesco Canovai <[email protected]>
Add e2e for major upgrades for postgresql and postgis images.

Signed-off-by: Francesco Canovai <[email protected]>
Fix pg_upgrade failures on OpenShift where the user is some random high
number.

Signed-off-by: Francesco Canovai <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Inform the user about the need of an ANALYZE after the upgrade.
Add a note about major upgrade e2e.

Signed-off-by: Francesco Canovai <[email protected]>
Signed-off-by: Francesco Canovai <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
@mnencia

mnencia commented Mar 27, 2025

Copy link
Copy Markdown
Member Author

/test tl=4 d=main

@github-actions

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not backport This PR must not be backported - it will be in the next minor release ok to merge 👌 This PR can be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Declarative support for PostgreSQL major in-place upgrades

10 participants