Skip to content

Commit 2c75d73

Browse files
committed
Add --no-data-checksums flag
to not enable them when upgrading. Add function add_initdb_options to easily add options to PGSETUP_INITDB_OPTIONS. Correct english in usage string.
1 parent d256694 commit 2c75d73

5 files changed

Lines changed: 128 additions & 31 deletions

File tree

bin/postgresql-setup.in

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,18 @@ test 0 -eq @WANT_SYSVINIT@ && srvsuff=".service"
6262
if [ @PGMAJORVERSION@ -ge 18 ]; then
6363
checksums_default_line="
6464
Default behavior when initializing (not upgrading).
65-
To upgrade to PostgreSQL @PGMAJORVERSION@ without
66-
enabling data checksums, add --no-data-checksums
67-
to PGSETUP_INITDB_OPTIONS (will override this flag)
68-
If data checksums are already enabled, there is no
69-
need to specify this flag."
65+
--no-data-checksums Do not enable data checksums for the data directory.
66+
Will not disable them if they are already enabled."
7067
fi
7168

7269
USAGE_STRING=$"\
7370
Usage: $0 MODE_OPTION [--unit=UNIT_NAME] [OPTION...]
7471
75-
Script is aimed to help sysadmin with basic database cluster administration.
72+
The script is aimed to help sysadmins with basic database cluster administration.
7673
Usually, \"@NAME_BINARYBASE@-setup --initdb\" and \"@NAME_BINARYBASE@-setup --upgrade\" is
7774
enough, however there are other options described below.
7875
79-
For more info and howto/when use this script please look at the documentation
76+
For more info and how/when to use this script please look at the documentation
8077
file $README_DIST.
8178
8279
Available operation mode:
@@ -176,11 +173,7 @@ perform_initdb()
176173

177174
# add option to enable data checksums if flag provided
178175
if [ "${option_data_checksums:-0}" -eq 1 ] && [ @PGMAJORVERSION@ -lt 18 ]; then
179-
if [ -n "$PGSETUP_INITDB_OPTIONS" ]; then
180-
export PGSETUP_INITDB_OPTIONS="$PGSETUP_INITDB_OPTIONS --data-checksums"
181-
else
182-
export PGSETUP_INITDB_OPTIONS="--data-checksums"
183-
fi
176+
add_initdb_options "--data-checksums"
184177
fi
185178

186179
# Initialize the database
@@ -321,6 +314,23 @@ detect_old_cluster_locale()
321314
return 1
322315
}
323316

317+
# Try adding an option into $PGSETUP_INITDB_OPTIONS
318+
# Returns 1 if it already exists there
319+
add_initdb_options()
320+
{
321+
local to_add="$1"
322+
if [ -n "$PGSETUP_INITDB_OPTIONS" ]; then
323+
if ! echo "$PGSETUP_INITDB_OPTIONS" | grep -qE -- "$to_add"; then
324+
export PGSETUP_INITDB_OPTIONS="$PGSETUP_INITDB_OPTIONS $to_add"
325+
else
326+
return 1
327+
fi
328+
else
329+
export PGSETUP_INITDB_OPTIONS="$to_add"
330+
fi
331+
return 0
332+
}
333+
324334

325335
enable_checksums()
326336
{
@@ -339,9 +349,8 @@ enable_checksums()
339349
return 1
340350
fi
341351
elif [ "${option_data_checksums:-0}" -eq 1 ]; then
342-
warn "Both --data-checksums and --no-data-checksums flags specified,"
343-
warn_q "upgrading without enabling data checksums!"
344-
option_data_checksums=0
352+
error "Both --data-checksums and --no-data-checksums flag specified!"
353+
return 1
345354
fi
346355
}
347356

@@ -380,29 +389,40 @@ upgrade()
380389
fi
381390

382391
info "Checking data checksums"
392+
if echo "$PGSETUP_INITDB_OPTIONS" | grep -qE -- "--data-checksums"; then
393+
option_data_checksums=1
394+
fi
383395
# timeout for large databases, if checksums are disabled, this command will fail fast
384396
checksums_result=$(timeout 1 "$upgradefrom_engine"/pg_checksums "$upgradefrom_data" 2>&1)
385397
checksums_code=$?
386-
# if timed out, the exit code is 124
398+
# if timed out (which means checksums are enabled), the exit code is 124
387399
if [ $checksums_code -ne 0 ] && [ $checksums_code -ne 124 ]; then
388400
if echo "$checksums_result" | grep -q "not enabled"; then
389401
if [ "${option_data_checksums:-0}" -ne 1 ] \
390402
&& ! echo "$PGSETUP_INITDB_OPTIONS" | grep -qE -- "--(no-)?data-checksums" \
391403
&& [ @PGMAJORVERSION@ -ge 18 ]; then
392404
error "PostgreSQL @PGMAJORVERSION@ has data checksums enabled by default,"
393405
error_q "while your current data directory does not. Please either use the"
394-
error_q "--data-checksums flag to enable them before upgrading, or add"
395-
error_q "--no-data-checksums to the PGSETUP_INITDB_OPTIONS environment variable."
406+
error_q "--data-checksums flag to enable them before upgrading, or the"
407+
error_q "--no-data-checksums to keep them disabled."
396408
exit 1
397409
else
398410
debug "checksums flags provided"
399411
fi
400412
else
401-
# this means that checksums are enabled but something else failed
413+
# pg_checksums failed for another reason than them not being enabled
402414
error "Checksum check failed! Output from pg_checksums:"
403415
error_q $"$checksums_result"
404416
exit 1
405417
fi
418+
elif [ $checksums_code -eq 0 ] || [ $checksums_code -eq 124 ] \
419+
&& echo "$PGSETUP_INITDB_OPTIONS" | grep -qE -- "--no-data-checksums"; then
420+
error "You have specified the --no-data-checksums flag, but they are already enabled."
421+
error_q "Disabling existing checksums while upgrading is not supported:"
422+
error_q "If you are sure you want to do that, please run"
423+
error_q "${upgradefrom_engine}/pg_checksums -d ${upgradefrom_data}"
424+
error_q "yourself. Please remove the flag otherwise."
425+
exit 1
406426
fi
407427

408428
# Set up log file for pg_upgrade
@@ -437,7 +457,9 @@ upgrade()
437457

438458
info $"Upgrading database."
439459

440-
enable_checksums "$pgdataold" "$upgradefrom_engine"
460+
if ! enable_checksums "$pgdataold" "$upgradefrom_engine"; then
461+
script_result=1
462+
fi
441463

442464
# Automatically detect locale settings from old cluster if not already set
443465
# This addresses Bug 1152556: detect --locale from old datadir automatically
@@ -448,7 +470,7 @@ upgrade()
448470
fi
449471
fi
450472

451-
if [ "$locale_already_set" != "true" ]; then
473+
if [ $script_result -eq 0 ] && [ "$locale_already_set" != "true" ]; then
452474
debug "attempting to auto-detect locale from old cluster"
453475
if detect_old_cluster_locale "$pgdataold" "$upgradefrom_engine" "$PGPORT"; then
454476
# Build initdb options with detected locale
@@ -458,17 +480,13 @@ upgrade()
458480
fi
459481

460482
# Append to existing PGSETUP_INITDB_OPTIONS if set, otherwise set it
461-
if [ -n "$PGSETUP_INITDB_OPTIONS" ]; then
462-
export PGSETUP_INITDB_OPTIONS="$PGSETUP_INITDB_OPTIONS $detected_opts"
463-
else
464-
export PGSETUP_INITDB_OPTIONS="$detected_opts"
465-
fi
483+
add_initdb_options "$detected_opts"
466484

467485
info $"Auto-detected locale settings from old cluster: $detected_opts"
468486
else
469487
debug "locale auto-detection failed, proceeding with system default locale"
470488
fi
471-
else
489+
elif [ $script_result -eq 0 ]; then
472490
debug "locale options already set in PGSETUP_INITDB_OPTIONS, skipping auto-detection"
473491
fi
474492

@@ -488,7 +506,9 @@ upgrade()
488506
}
489507

490508
local failure_cleanup=true
491-
if ! check_not_initialized; then
509+
if [ $script_result -ne 0 ]; then
510+
debug "script failed, skipping initdb..."
511+
elif ! check_not_initialized; then
492512
# Don't try to re-init initialized data directory and also do not
493513
# remove it after this unsuccessful upgrade.
494514
script_result=1
@@ -835,7 +855,7 @@ long_opts="\
835855
initdb,upgrade,\
836856
new-systemd-unit,upgrade-ids,\
837857
unit:,service:,port:,datadir:,upgrade-from:,upgrade-from-unit:,\
838-
data-checksums,debug,\
858+
data-checksums,no-data-checksums,debug,\
839859
version,help,usage"
840860

841861
args=`getopt -o "$short_opts" -l "$long_opts" -n "@NAME_BINARYBASE@-setup" -- "$@"` \
@@ -899,6 +919,11 @@ while true; do
899919
shift
900920
;;
901921

922+
--no-data-checksums)
923+
add_initdb_options "--no-data-checksums"
924+
shift
925+
;;
926+
902927
--version)
903928
print_version
904929
exit 0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
summary: Check if upgrading to postgres 18 with already enabled checksums but disabling them during the upgrade works
2+
name: checksums-disable-after-enabling
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
3+
4+
. /usr/share/beakerlib/beakerlib.sh || exit 1
5+
6+
rlJournalStart
7+
rlPhaseStartSetup
8+
rlRun "git clone \"$REPO_URL\" repo"
9+
rlRun "pushd repo"
10+
rlRun "git fetch origin \"$PR_HEAD\""
11+
rlRun "git checkout FETCH_HEAD"
12+
rlRun "cat /etc/fedora-release"
13+
rlRun "dnf -y install postgresql17-server"
14+
15+
rlRun "autoreconf -vfi" 0 "Building and installing postgresql-setup"
16+
rlRun "./configure --prefix=/usr"
17+
rlRun "make"
18+
rlRun "./bin/postgresql-setup --init --data-checksums" 0 "Initializing database dir"
19+
rlRun "systemctl start postgresql" 0 "Starting service"
20+
rlRun "systemctl is-active postgresql" 0 "Verifying service running"
21+
rlPhaseEnd
22+
23+
rlPhaseStartTest
24+
rlRun "su - postgres -c \"
25+
createdb testdb;
26+
psql -U postgres -d testdb -c \\\"create table users (id serial primary key, name text)\\\";
27+
psql -U postgres -d testdb -c \\\"insert into users (name) values ('Alice'), ('Bob'), ('Celine')\\\"
28+
\""
29+
rlRun "su - postgres -c '
30+
psql -U postgres -d testdb -c \"select * from users\"
31+
' > expected.txt"
32+
33+
echo "Expected:"
34+
cat expected.txt
35+
36+
rlRun "dnf -y remove postgresql17*" 0 "Removing postgresql 17"
37+
rlRun "dnf -y install postgresql18-upgrade" 0 "Installing postgresql 18"
38+
39+
rlRun "./bin/postgresql-setup --upgrade --no-data-checksums" 1 "Trying to upgrade with invalid flag"
40+
rlRun "./bin/postgresql-setup --upgrade" 0 "Removed invalid flag"
41+
42+
rlRun "autoreconf -vfi" 0 "Building and installing postgresql-setup"
43+
rlRun "./configure --prefix=/usr"
44+
rlRun "make"
45+
46+
rlRun "systemctl start postgresql" 0 "Starting service again"
47+
rlRun "systemctl is-active postgresql" 0 "Verifying service running"
48+
49+
rlRun "su - postgres -c '
50+
psql -U postgres -d testdb -c \"select * from users\"
51+
' > actual18.txt"
52+
53+
echo "Actual:"
54+
cat actual18.txt
55+
56+
rlAssertNotDiffer expected.txt actual18.txt
57+
rlRun "systemctl stop postgresql" 0 "Stop service"
58+
rlRun "pg_checksums /var/lib/pgsql/data" 0 "Verify checksums enabled"
59+
rlPhaseEnd
60+
61+
rlPhaseStartCleanup
62+
rlRun "popd"
63+
rlRun "rm -rf repo"
64+
rlRun "systemctl stop postgresql" 0 "Stopping postgresql service"
65+
rlRun "rm -rf /var/lib/pgsql" 0 "Removing database folder"
66+
rlRun "dnf -y remove postgresql*" 0 "Uninstalling postgresql"
67+
rlPhaseEnd
68+
rlJournalPrintText
69+
rlJournalEnd

tmt/tests/virtual/checksums/disable-and-enable/test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ rlJournalStart
4141
rlRun "make"
4242

4343
rlRun "./bin/postgresql-setup --upgrade" 1 "Upgrading without checksums flags"
44-
rlRun "PGSETUP_INITDB_OPTIONS='--no-data-checksums' ./bin/postgresql-setup --upgrade --data-checksums" 0 "Upgrading with contradictory flags"
44+
rlRun "./bin/postgresql-setup --upgrade --data-checksums --no-data-checksums" 1 "Upgrading with contradictory flags"
45+
rlRun "PGSETUP_INITDB_OPTIONS='--no-data-checksums' ./bin/postgresql-setup --upgrade --no-data-checksums" 0 "Upgrading with duplicate flags"
4546

4647
rlRun "systemctl start postgresql" 0 "Starting service again"
4748
rlRun "systemctl is-active postgresql" 0 "Verifying service running"

tmt/tests/virtual/checksums/disable/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ rlJournalStart
4141
rlRun "make"
4242

4343
rlRun "./bin/postgresql-setup --upgrade" 1 "Upgrading without checksums flags"
44-
rlRun "PGSETUP_INITDB_OPTIONS='--no-data-checksums' ./bin/postgresql-setup --upgrade" 0 "Upgrading with checksums flag"
44+
rlRun "./bin/postgresql-setup --upgrade --no-data-checksums" 0 "Upgrading with checksums flag"
4545

4646
rlRun "systemctl start postgresql" 0 "Starting service again"
4747
rlRun "systemctl is-active postgresql" 0 "Verifying service running"

0 commit comments

Comments
 (0)