I am a little bit concerned about the possibility of queries running concurrently. I want to run a set of queries at worst as soon as the previous is ready through psql (PostgreSQL) and a shell script.

My initial attempt was something like this example:

queries=("SELECT ...", "SELECT ...")
for query in ${#queries[@]}
    "$query" | psql -U postgres -d database 

I am pretty sure that with the way this is written that the bash script is waiting for each other command to be executed before moving to the next. But I am not sure on the interaction with psql - can there still exist the possibility that the database is executing queries concurrently? If so, how do I approach this so that my commands and queries are executed after each other?


1 Answer 1


As @a_horse_with_no_name said, if you want to restrict the order on db level, use sequential statements, either with psql -f queries.sql or :

psql <<EOF 

In your example you call psql for each query. You don't send them to background so each psql waits previous to end before calling next. Concurrency should be sane here as well. Things that you have different here: lack of control over transaction, you open/close many connections instead of one and you put sql code into bash variable

  • Thanks for your reply. To confirm, are you saying that in my example code, psql is waiting for previous query to fininsh executing? My worry is that the code opens many connections before the previous finished fully executing. Commented Apr 27, 2017 at 15:50
  • (I want to restrict the order on as many levels as possible) Commented Apr 27, 2017 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.