Each thread should have its own database connection.
A PostgreSQL connection can handle only one statement at a given time (unless you are using a server side cursor, but even then the connection can handle only one
FETCH at the same time).
So if several threads were to share a database connection, they’d have to coordinate carefully to make sure that only one thread uses the connection at the same time. For example, you cannot send a new query while another thread is still waiting for a query result.
CLICK HERE to find out more related problems solutions.