Skip to content

Commit

Permalink
honor psql's exit status (fixes gbb#6)
Browse files Browse the repository at this point in the history
The sub-shells exit code wasn't checked, so even if psql exited with
a non-zero status code, par_psql would continue. We can check this
directly for sequential queries, but need to do some housekeeping
for the background processes.

It is possible to exit immediately upon encountering a failed
sub-shell, but the later ones might still be running and print
into the terminal, leaving a dirty prompt.

The exit code `3` is what `psql -v ON_ERROR_STOP=1` returns.
  • Loading branch information
breunigs committed Mar 24, 2023
1 parent 10a4eed commit a5a75dd
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions par_psql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ parpsqlmarker=0
parmode=0
lineno=1
cmdline="$@"
pids=()

# helper function
function debug {
Expand All @@ -33,6 +34,25 @@ function debug {
fi
}

# wait on background processes individually to check their exit status
function sync {
debug "waiting on ${#pids[@]} SQL queries to finish"
error=0
for pid in ${pids[*]}; do
wait $pid
if [ $? -ne 0 ];
then
error=1
fi
done

if [ $error -eq 1 ];
then
exit 3
fi
pids=()
}

# Check that the input file is specified with --file

debug "inputfile: $inputfile"
Expand Down Expand Up @@ -124,7 +144,7 @@ parseloop() {
echo "$sqlline" >> $currentfile

# start running the current file in parallel
( eval psql "$cmdline" --file=$currentfile && rm -f "$currentfile" ) &
( eval psql "$cmdline" --file=$currentfile && rm -f "$currentfile" ) & pids+=($!)

# make a new current file.
currentfile=$(mktemp)
Expand All @@ -138,10 +158,10 @@ parseloop() {

# first, run the 'serial code' file that was previously built up, non-parallelised
# wait for it to finish
( eval psql $cmdline --file=$serialtodofile && rm -f "$serialtodofile" )
( eval psql $cmdline --file=$serialtodofile && rm -f "$serialtodofile" ) || exit $?

# then run the current file in parallel && delete it after
( eval psql $cmdline --file=$currentfile && rm -f "$currentfile" ) &
( eval psql $cmdline --file=$currentfile && rm -f "$currentfile" ) & pids+=($!)

# start parallel mode
parmode=1
Expand All @@ -157,7 +177,7 @@ parseloop() {
debug 'ending parallel mode, starting serial mode'

# Syncronise parallel tasks.
wait
sync

# Save the current line to the current file.
echo "$sqlline" >> $currentfile
Expand Down Expand Up @@ -211,8 +231,8 @@ done < $inputfile

rm $currentfile

wait
( eval psql $cmdline --file=$serialtodofile && rm -f "$serialtodofile" )
sync
( eval psql $cmdline --file=$serialtodofile && rm -f "$serialtodofile" ) || exit $?

# At the end, we must ensure everything is synchronised then run 'serialtodofile' in case anything is left
# e.g. in case we ended in serial mode.
Expand Down

0 comments on commit a5a75dd

Please sign in to comment.