I have a Jenkins job that fires a long-running SQL function in a PostgreSQL database using a psql command in a shell (bash) script. Sometimes the computation needs to be terminated to unblock other queries on the server. But if the job is manually aborted, the SQL function stays running (within a postgresql process) on the server although the psql command gets terminated.
I found out that psql needs to be aborted by SIGINT to cancel the query properly, but Jenkins apparently sends SIGTERM to all processes spawned in within the job execution.
I tried trapping the TERM signal in the job script and sending SIGINT to the psql that was run on background, but psql still received SIGTERM first and finished without stopping the query.
According to this Jenkins kills the whole process group of the script, but starting psql in a separate process group (set -m) also doesn't help.
Is there any way to stop the psql query gracefully upon Jenkins job abort?