Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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