From 98cb879ee1872d8d9f88a1a57a31876ad71dc3db Mon Sep 17 00:00:00 2001 From: Renz Date: Fri, 14 Nov 2025 08:34:16 +0000 Subject: [PATCH] Add BLOB/large object verification script for backup diagnostics --- fix_max_locks.sh | 49 ++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/fix_max_locks.sh b/fix_max_locks.sh index 5bf3c9e..2e9ea86 100755 --- a/fix_max_locks.sh +++ b/fix_max_locks.sh @@ -6,44 +6,53 @@ set -euo pipefail NEW_VALUE=${1:-256} -CONFIG_FILE="$(sudo -u postgres psql -t -c "SHOW config_file;" | tr -d '[:space:]')" -if [ -z "$CONFIG_FILE" ]; then - echo "Could not locate postgresql.conf; aborting" - exit 1 -fi - -echo "postgres config file: $CONFIG_FILE" +CONFIG_FILE="/var/lib/pgsql/data/postgresql.conf" BACKUP_FILE="${CONFIG_FILE}.bak.$(date +%s)" +echo "PostgreSQL config file: $CONFIG_FILE" + # Create a backup sudo cp "$CONFIG_FILE" "$BACKUP_FILE" -sudo chown postgres:postgres "$BACKUP_FILE" -chmod 600 "$BACKUP_FILE" - echo "Backup written to $BACKUP_FILE" -# Use sed to update (or add) setting -# If the setting is present (commented or uncommented), replace the line. Otherwise append. -if sudo grep -q "^\s*max_locks_per_transaction\s*=\s*" "$CONFIG_FILE"; then +# Check if setting exists (commented or not) +if sudo grep -qE "^\s*#?\s*max_locks_per_transaction\s*=" "$CONFIG_FILE"; then echo "Updating existing max_locks_per_transaction to $NEW_VALUE" - sudo sed -ri "s#^\s*#?max_locks_per_transaction\s*=.*#max_locks_per_transaction = $NEW_VALUE#" "$CONFIG_FILE" + # Replace the line (whether commented or not) + sudo sed -i "s/^\s*#\?\s*max_locks_per_transaction\s*=.*/max_locks_per_transaction = $NEW_VALUE/" "$CONFIG_FILE" else echo "Adding max_locks_per_transaction = $NEW_VALUE to config" - echo "\n# Increased by fix_max_locks.sh on $(date)\nmax_locks_per_transaction = $NEW_VALUE" | sudo tee -a "$CONFIG_FILE" >/dev/null + # Append at the end + echo "" | sudo tee -a "$CONFIG_FILE" >/dev/null + echo "# Increased by fix_max_locks.sh on $(date)" | sudo tee -a "$CONFIG_FILE" >/dev/null + echo "max_locks_per_transaction = $NEW_VALUE" | sudo tee -a "$CONFIG_FILE" >/dev/null fi # Ensure correct permissions sudo chown postgres:postgres "$CONFIG_FILE" sudo chmod 600 "$CONFIG_FILE" +# Test the config before restarting +echo "Testing PostgreSQL config..." +sudo -u postgres /usr/bin/postgres -D /var/lib/pgsql/data -C max_locks_per_transaction 2>&1 | head -5 + # Restart PostgreSQL and verify echo "Restarting PostgreSQL service..." sudo systemctl restart postgresql -sleep 2 +sleep 3 -echo "Verifying new value:" -sudo -u postgres psql -c "SHOW max_locks_per_transaction;" - -echo "If the restart fails, you can restore the previous config with:\n sudo cp $BACKUP_FILE $CONFIG_FILE && sudo systemctl restart postgresql" +if sudo systemctl is-active --quiet postgresql; then + echo "✅ PostgreSQL restarted successfully" + sudo -u postgres psql -c "SHOW max_locks_per_transaction;" +else + echo "❌ PostgreSQL failed to start!" + echo "Restoring backup..." + sudo cp "$BACKUP_FILE" "$CONFIG_FILE" + sudo systemctl start postgresql + echo "Original config restored. Check /var/log/postgresql for errors." + exit 1 +fi +echo "" +echo "Success! Backup available at: $BACKUP_FILE" exit 0