Skip to content

Commit 98e0202

Browse files
committed
Made the cron full vacum opt-in and also the schedule configurable
1 parent cfbace9 commit 98e0202

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ ENV PGROOT=/var/lib/postgresql \
8181
OR_REINDEX_COUNTER=${OR_REINDEX_COUNTER} \
8282
OR_DISABLE_REINDEX=${OR_DISABLE_REINDEX:-false} \
8383
POSTGRES_MAX_CONNECTIONS=${POSTGRES_MAX_CONNECTIONS:-50} \
84-
OR_DISABLE_AUTO_UPGRADE=${OR_DISABLE_AUTO_UPGRADE:-false}
84+
OR_DISABLE_AUTO_UPGRADE=${OR_DISABLE_AUTO_UPGRADE:-false} \
85+
# VACUUM FULL settings (disabled by default)
86+
OR_ENABLE_VACUUM_FULL=${OR_ENABLE_VACUUM_FULL:-false} \
87+
OR_VACUUM_FULL_CRON_SCHEDULE="${OR_VACUUM_FULL_CRON_SCHEDULE:-'0 0 * * 0'}"
8588

8689
WORKDIR /var/lib/postgresql
8790
EXPOSE 5432 8008 8081

Dockerfile.alpine

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ ENV POSTGRES_DB ${POSTGRES_DB:-openremote}
2929
ENV POSTGRES_USER ${POSTGRES_USER:-postgres}
3030
ENV POSTGRES_PASSWORD ${POSTGRES_PASSWORD:-postgres}
3131

32+
# VACUUM FULL settings (disabled by default)
33+
ENV OR_ENABLE_VACUUM_FULL=${OR_ENABLE_VACUUM_FULL:-false}
34+
ENV OR_VACUUM_FULL_CRON_SCHEDULE="${OR_VACUUM_FULL_CRON_SCHEDULE:-'0 0 * * 0'}"
35+
3236
# Add glibc and cron for scheduled VACUUM FULL operations
3337
RUN apk add --no-cache gcompat dcron
3438

docker-entrypoint-initdb.d/200_or_setup_vacuum_full.sh

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
# This script sets up a weekly VACUUM FULL job to clean up database bloat
44
# VACUUM FULL reclaims more space than regular AUTOVACUUM by rewriting the entire table
55

6+
# Check if VACUUM FULL is enabled (default is disabled)
7+
if [ "${OR_ENABLE_VACUUM_FULL:-false}" != "true" ]; then
8+
echo "VACUUM FULL is disabled (OR_ENABLE_VACUUM_FULL is not set to 'true'). Skipping setup."
9+
exit 0
10+
fi
11+
12+
echo "Setting up VACUUM FULL job (OR_ENABLE_VACUUM_FULL=true)..."
13+
614
# Create scripts directory if it doesn't exist
715
mkdir -p /var/lib/postgresql/scripts
816

@@ -13,7 +21,7 @@ cat > /var/lib/postgresql/scripts/vacuum_full.sh << 'EOF'
1321
# Log file for vacuum operations
1422
LOG_FILE="/var/lib/postgresql/vacuum_full.log"
1523
16-
echo "$(date): Starting weekly VACUUM FULL operation" >> "$LOG_FILE"
24+
echo "$(date): Starting VACUUM FULL operation" >> "$LOG_FILE"
1725
1826
# Get list of all tables in the database
1927
TABLES=$(psql -t -c "SELECT schemaname || '.' || tablename FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema')" "${POSTGRES_DB:-postgres}")
@@ -25,14 +33,17 @@ for TABLE in $TABLES; do
2533
echo "$(date): Completed VACUUM FULL on $TABLE" >> "$LOG_FILE"
2634
done
2735
28-
echo "$(date): Weekly VACUUM FULL operation completed" >> "$LOG_FILE"
36+
echo "$(date): VACUUM FULL operation completed" >> "$LOG_FILE"
2937
EOF
3038

3139
# Make the script executable
3240
chmod +x /var/lib/postgresql/scripts/vacuum_full.sh
3341

34-
# Set up cron job to run the script weekly (Sunday at 12:00 AM)
35-
CRON_ENTRY="0 0 * * 0 /var/lib/postgresql/scripts/vacuum_full.sh"
42+
# Parse the cron schedule from environment variable or use default (Sunday at 12:00 AM)
43+
CRON_SCHEDULE="${OR_VACUUM_FULL_CRON_SCHEDULE:-0 0 * * 0}"
44+
CRON_ENTRY="$CRON_SCHEDULE /var/lib/postgresql/scripts/vacuum_full.sh"
45+
46+
echo "Using cron schedule: $CRON_SCHEDULE"
3647

3748
# Detect which cron system is available
3849
if command -v crond > /dev/null 2>&1; then
@@ -41,12 +52,12 @@ if command -v crond > /dev/null 2>&1; then
4152
mkdir -p /etc/crontabs
4253
echo "$CRON_ENTRY" > /etc/crontabs/postgres
4354
chmod 600 /etc/crontabs/postgres
44-
echo "Weekly VACUUM FULL job has been scheduled (Sundays at 12:00 AM) using dcron"
55+
echo "VACUUM FULL job has been scheduled using dcron"
4556

4657
# Create a script to start crond when container starts
4758
cat > /var/lib/postgresql/scripts/start_cron.sh << 'EOF'
4859
#!/bin/sh
49-
if [ -x "$(command -v crond)" ]; then
60+
if [ -x "$(command -v crond)" ] && [ "${OR_ENABLE_VACUUM_FULL:-false}" = "true" ]; then
5061
echo "Starting crond service for scheduled database maintenance..."
5162
crond -b -L /var/lib/postgresql/cron.log
5263
fi
@@ -57,7 +68,7 @@ elif command -v cron > /dev/null 2>&1; then
5768
# Debian/Ubuntu (standard cron)
5869
echo "Setting up cron job using standard cron..."
5970
(crontab -u postgres -l 2>/dev/null || echo "") | echo "$CRON_ENTRY" | crontab -u postgres -
60-
echo "Weekly VACUUM FULL job has been scheduled (Sundays at 12:00 AM) using standard cron"
71+
echo "VACUUM FULL job has been scheduled using standard cron"
6172
else
6273
echo "WARNING: No cron system found. VACUUM FULL will not be automatically scheduled."
6374
fi

or-entrypoint.sh

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,17 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
377377
fi
378378
fi
379379

380-
# Start cron service if it exists (for scheduled VACUUM FULL operations)
381-
if [ -x "$(command -v cron)" ]; then
382-
echo "Starting cron service for scheduled database maintenance..."
383-
service cron start
384-
elif [ -x "$(command -v crond)" ] && [ -f "/var/lib/postgresql/scripts/start_cron.sh" ]; then
385-
echo "Starting Alpine crond service for scheduled database maintenance..."
386-
/var/lib/postgresql/scripts/start_cron.sh
380+
# Start cron service if it exists and VACUUM FULL is enabled
381+
if [ "${OR_ENABLE_VACUUM_FULL:-false}" = "true" ]; then
382+
if [ -x "$(command -v cron)" ]; then
383+
echo "Starting cron service for scheduled database maintenance (OR_ENABLE_VACUUM_FULL=true)..."
384+
service cron start
385+
elif [ -x "$(command -v crond)" ] && [ -f "/var/lib/postgresql/scripts/start_cron.sh" ]; then
386+
echo "Starting Alpine crond service for scheduled database maintenance (OR_ENABLE_VACUUM_FULL=true)..."
387+
/var/lib/postgresql/scripts/start_cron.sh
388+
fi
389+
else
390+
echo "VACUUM FULL is disabled (OR_ENABLE_VACUUM_FULL is not set to 'true'). Skipping cron service."
387391
fi
388392

389393
exec /docker-entrypoint.sh $@

0 commit comments

Comments
 (0)