@@ -62,21 +62,18 @@ test 0 -eq @WANT_SYSVINIT@ && srvsuff=".service"
6262if [ @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."
7067fi
7168
7269USAGE_STRING=$" \
7370Usage: $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.
7673Usually, \" @NAME_BINARYBASE@-setup --initdb\" and \" @NAME_BINARYBASE@-setup --upgrade\" is
7774enough, 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
8077file $README_DIST .
8178
8279Available 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
325335enable_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="\
835855initdb,upgrade,\
836856new-systemd-unit,upgrade-ids,\
837857unit:,service:,port:,datadir:,upgrade-from:,upgrade-from-unit:,\
838- data-checksums,debug,\
858+ data-checksums,no-data-checksums, debug,\
839859version,help,usage"
840860
841861args=` 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
0 commit comments