Skip to content

[DPE-10780] Configure PostgreSQL workload OOM protection - #1993

Open
oleksii-buhaiov wants to merge 3 commits into
16/edgefrom
feat/dpe-10780-oom-protection
Open

oleksii-buhaiov wants to merge 3 commits into
16/edgefrom
feat/dpe-10780-oom-protection

Conversation

@oleksii-buhaiov

Copy link
Copy Markdown

Issue

DPE-10780: The charm does not configure OOM protection for the PostgreSQL
workload, leaving database availability exposed to process termination
under memory pressure.

Solution

Configure snapd's resilience.vitality-hint for charmed-postgresql
before snap installation or refresh. Preserve existing entries and their
order, append PostgreSQL only when absent, and verify the configuration
after writing it.

On fresh deployments, snapd starts Patroni with a negative OOM adjustment,
which its PostgreSQL processes inherit. This change does not force a
restart of running services. During a snap-changing refresh, the normal
snap service restart activates the setting.

A charm-only upgrade with an unchanged snap revision does not currently
run this configuration step. Activation for that upgrade path is outside
this change.

Manual validation

Use an amd64 build environment with the repository's build dependencies
installed and a working LXD Juju controller capable of creating VMs.
Use a PR checkout under your home directory that the Juju snap can access.

From the PR checkout:

charmcraftcache pack
juju add-model oom-validation

juju deploy ./postgresql_ubuntu@24.04-amd64.charm postgresql \
  --base ubuntu@24.04 \
  --constraints 'arch=amd64 virt-type=virtual-machine cores=2 mem=2G root-disk=16G' \
  --config profile=testing

Wait for the unit to become active and idle, then connect:

juju wait-for unit postgresql/0 --timeout=30m \
  --query='workload-status=="active" && agent-status=="idle"'

juju ssh postgresql/0

Inside the VM:

sudo snap get system resilience.vitality-hint

systemctl show snap.charmed-postgresql.patroni.service \
  --property=OOMScoreAdjust

group=$(systemctl show snap.charmed-postgresql.patroni.service \
  --property=ControlGroup --value)

printf 'OOM_ADJ PID COMMAND\n'
for pid in $(cat "/sys/fs/cgroup${group}/cgroup.procs"); do
    printf '%s ' "$(cat "/proc/$pid/oom_score_adj")"
    ps -p "$pid" -o pid=,args=
done

Expected on this fresh VM: the hint is charmed-postgresql, systemd
reports OOMScoreAdjust=-899, and Patroni, the PostgreSQL postmaster,
and its listed children all show -899. This value assumes PostgreSQL
is the first and only hint entry.

This checks fresh-install configuration and live process adjustments;
it does not test OOM survival or upgrade activation.

@github-actions github-actions Bot added the Libraries: Out of sync The charm libs used are out-of-sync label Sep 23, 2026
@oleksii-buhaiov oleksii-buhaiov added bug Something isn't working as expected and removed Libraries: Out of sync The charm libs used are out-of-sync labels Sep 23, 2026
@oleksii-buhaiov
oleksii-buhaiov force-pushed the feat/dpe-10780-oom-protection branch from 790f4e9 to 752b74a Compare September 23, 2026 12:43
@github-actions github-actions Bot added the Libraries: Out of sync The charm libs used are out-of-sync label Sep 23, 2026
@oleksii-buhaiov
oleksii-buhaiov marked this pull request as ready for review September 23, 2026 19:06
@oleksii-buhaiov
oleksii-buhaiov requested a review from a team as a code owner September 23, 2026 19:06
Comment thread src/oom.py Outdated
Comment on lines +51 to +56
subprocess.check_call([ # noqa: S603
"/usr/bin/snap",
"set",
"system",
f"{SNAP_VITALITY_HINT}={updated_hint}",
])

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.

Can we use the charmlibs-snap to set and get the config?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

looks like we can, will take a look at it

@marceloneppel marceloneppel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great work, @oleksii-buhaiov! I left a comment about one important check.

Comment thread src/charm.py
logger.error("Unavailable snap architecture %s", platform.machine())
raise
try:
ensure_snap_oom_protection(charm_refresh.snap_name())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you also test whether a refresh from, for example, the charm revision from the 16/stable channel will enable this OOM protection when refreshed to the revision build from this PR's code?

This branch has not been deployed

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

Labels

bug Something isn't working as expected Libraries: Out of sync The charm libs used are out-of-sync

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants