|
2 | 2 | set -e
|
3 | 3 |
|
4 | 4 | # For Percona PostgreSQL Operator, we need to focus on protecting the postmaster process
|
| 5 | +# Original user in the Percona PostgreSQL Operator image |
| 6 | +ORIGINAL_USER=26 |
5 | 7 | ORIGINAL_ENTRYPOINT="/opt/crunchy/bin/postgres-ha/bootstrap-postgres-ha.sh"
|
6 | 8 |
|
7 |
| -# Try to set OOM score adjustment if possible, but don't fail if we can't |
8 |
| -if command -v sudo >/dev/null 2>&1; then |
9 |
| - # If sudo is available, use it to set OOM score |
10 |
| - sudo -n sh -c 'echo -900 > /proc/self/oom_score_adj' >/dev/null 2>&1 || true |
11 |
| - echo "Attempted to set OOM score adjustment using sudo" |
12 |
| -elif command -v setcap >/dev/null 2>&1; then |
13 |
| - # Try to give the oom-adjuster script the capability to adjust OOM scores |
14 |
| - setcap 'cap_sys_resource=+ep' /usr/local/bin/postgres-oom-adjuster.sh >/dev/null 2>&1 || true |
15 |
| - echo "Attempted to grant capabilities to OOM adjuster" |
| 9 | +# Set OOM score adjustment for our own process (will be inherited) |
| 10 | +if [ -f "/proc/self/oom_score_adj" ]; then |
| 11 | + echo -900 > /proc/self/oom_score_adj |
| 12 | + echo "Set OOM score adjustment to -900 for pid 1" |
| 13 | +else |
| 14 | + echo "WARNING: Cannot set OOM score adjustment (file not found)" |
16 | 15 | fi
|
17 | 16 |
|
18 |
| -# Start a background process to monitor and adjust PostgreSQL if possible |
| 17 | +# Start the OOM adjuster in the background |
19 | 18 | nohup /usr/local/bin/postgres-oom-adjuster.sh >> /tmp/postgres-oom-adjuster.log 2>&1 &
|
20 | 19 | echo "Started postmaster OOM adjuster in background"
|
21 | 20 |
|
22 |
| -# Execute the original entrypoint |
23 |
| -echo "Executing original entrypoint: $ORIGINAL_ENTRYPOINT $@" |
24 |
| -exec "$ORIGINAL_ENTRYPOINT" "$@" |
| 21 | +# Set environment variables for PostgreSQL child processes |
| 22 | +export PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj |
| 23 | +export PG_OOM_ADJUST_VALUE=0 |
| 24 | + |
| 25 | +# Switching to the original user and executing original entrypoint |
| 26 | +echo "Switching to user $ORIGINAL_USER and executing original entrypoint: $ORIGINAL_ENTRYPOINT $@" |
| 27 | + |
| 28 | +# Check which user-switching command is available |
| 29 | +if command -v runuser >/dev/null 2>&1; then |
| 30 | + # Use runuser (available on RHEL/CentOS/Fedora) |
| 31 | + exec runuser -u "#$ORIGINAL_USER" -- "$ORIGINAL_ENTRYPOINT" "$@" |
| 32 | +else |
| 33 | + # Fall back to su |
| 34 | + exec su -s /bin/bash $ORIGINAL_USER -c "$ORIGINAL_ENTRYPOINT $*" |
| 35 | +fi |
0 commit comments