0

I have a Postgres database. When I query the same table with multiple queries asynchronously (concurrently), each query takes longer than if I call the query only once or in a synchronous (sequential) manner. I have tried via a REST API into a EJB server and using psql directly to the database.

For the same query, called 10 times each, this is the result per call. This is the command used: docker exec -i container-name psql -Uusername -d databasename -c "..."

Synchronously (Sequential)

Finished 01 in 0h 0m 5s 134 ms.
Finished 02 in 0h 0m 5s 139 ms.
Finished 03 in 0h 0m 5s 181 ms.
Finished 04 in 0h 0m 5s 270 ms.
Finished 05 in 0h 0m 5s 157 ms.
Finished 06 in 0h 0m 5s 163 ms.
Finished 07 in 0h 0m 5s 178 ms.
Finished 08 in 0h 0m 5s 205 ms.
Finished 09 in 0h 0m 5s 169 ms.
Finished 10 in 0h 0m 5s 155 ms.

Asynchronously (Concurrent)

Finished 03 in 0h 0m 12s 634 ms.
Finished 04 in 0h 0m 18s 87 ms.
Finished 07 in 0h 0m 21s 209 ms.
Finished 01 in 0h 0m 21s 279 ms.
Finished 05 in 0h 0m 21s 302 ms.
Finished 02 in 0h 0m 21s 659 ms.
Finished 09 in 0h 0m 22s 127 ms.
Finished 06 in 0h 0m 22s 394 ms.
Finished 08 in 0h 0m 22s 667 ms.
Finished 10 in 0h 0m 22s 671 ms.

I found this post (Postgres: Concurrent queries in a connection )that mentions concurrent queries but I do not really know how to proceed from here. I have looked at the pg_stat_activity table when querying asynchronously and it has a row per query but I am not sure if this means multiple connections or just multiple queries per connection.

New: This is the script I use to run asynchronously or concurrently. If you remove the '&' then it runs each call to the function sequentially/synchronously.

#!/usr/bin/env bash

set -e

function runScript1() {
    _START_T=$(date +%s%N)
    docker exec -i container-name psql -Uusername -d databse -c "SELECT ... FROM ..."
    _END_T=$(date +%s%N)
    _ELAPSED_MS=$(((_END_T - _START_T) / 1000000))
    _SECONDS=$(( _ELAPSED_MS / 1000 ))
    _MILLISECONDS=$(( _ELAPSED_MS % 1000 ))
    _MINUTES=$(( _SECONDS / 60 ))
    _HOURS=$(( _MINUTES / 60 ))

    # Print the result
    echo "Finished $1 in ${_HOURS}h $((_MINUTES % 60))m $((_SECONDS % 60))s $_MILLISECONDS ms."
}

_START_TOTAL=$(date +%s%N)
runScript1 "01" &
runScript1 "02" &
runScript1 "03" &
runScript1 "04" &
runScript1 "05" &
runScript1 "06" &
runScript1 "07" &
runScript1 "08" &
runScript1 "09" &
runScript1 "10" &
_END_TOTAL=$(date +%s%N)
4
  • What does it mean you run a query asynchronously with psql? Commented Feb 9, 2024 at 21:52
  • @jjanes By that I mean I run all the queries at the same time. I have an sh script that calls the same query (via psql) concurrently. You will see in the results that the ordering is not sequential since it prints the total time when it is done. Commented Feb 12, 2024 at 6:12
  • @jjanes I have added the script I use to the body of the question. Commented Feb 12, 2024 at 6:21
  • So it took about 50 seconds to run sequentially and a little less than 25 seconds to run concurrently. If you only have 2 CPUs, that seems like a reasonable thing to expect to happen. Commented Feb 12, 2024 at 19:19

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.