How to perform postgres SQL using multiple threads

I am grateful for all the help, and feel a bit ashamed to ask another question whilst I haven't found the time to go through all the results.

I want to query a postgres database, and want to use a persistent connection pool. It seems the sqlx package provides this functionality. However, I would like to use multiple threads to perform work.

Serial queries do work:

use sqlx::postgres::PgPoolOptions;

async fn main() -> Result<(), sqlx::Error> {
    let pool = PgPoolOptions::new()

    for _ in 0..1000 {
        let row: (i64, ) = sqlx::query_as("SELECT $1").bind(150_i64).fetch_one(&pool).await?;


What I would like to do, is perform the queries (sqlx::query_as) in a number of threads.
I have tried several examples, but every time I wrap the query part in some form of threading, errors indicating unsatisfied traits.

I would think it should be simple to spawn multiple threads performing work, but I cannot get my head around it.

How can I get a unit of work, such as the for loop and the query (using the connection pool, that is very important!) be executed at the same time in threads?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.