# Thread safe operations with LibPQ.jl and AMPQ.jl

**URL:** <https://discourse.julialang.org/t/thread-safe-operations-with-libpq-jl-and-ampq-jl/62873>\
**Category:** New to Julia\
**Created:** [June 14, 2021, 9:51am UTC](https://discourse.julialang.org/t/thread-safe-operations-with-libpq-jl-and-ampq-jl/62873 "2021-06-14T09:51:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)\
**Post date:** [June 14, 2021, 9:51am UTC](https://discourse.julialang.org/t/thread-safe-operations-with-libpq-jl-and-ampq-jl/62873/1 "2021-06-14T09:51:35Z")

</div>

How should I implement **thread safe operations** with **LibPQ.jl** (PostgreSQL database) and **AMPQ.jl** (RabbitMQ)? I’m using 8 threads, and I pulling/pushing messages from/to RabbitMQ (using only one queue). Also, I’m using PostgreSQL database operations INSERT/SELECT.

Should I use some kind of semaphore, lock, or atomic variable when executing commands “execute” and “basic\_get”?

I’m looking for general, simple example which can help me understand how it is working in Julia language. For example 8 threads, each of them has DB and queue connection.

Thank you

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [June 14, 2021, 11:19am UTC](https://discourse.julialang.org/t/thread-safe-operations-with-libpq-jl-and-ampq-jl/62873/2 "2021-06-14T11:19:07Z")

</div>

Both data bases (like Postgres) and message queues (like Rabbit) already support multiple clients doing read / write operations at the same time, even if it is on the same tables / queues.  
Are there any concreate issues you encountered?

---

<div class="post-metadata">

**Author:** ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)\
**Post date:** [June 14, 2021, 11:38am UTC](https://discourse.julialang.org/t/thread-safe-operations-with-libpq-jl-and-ampq-jl/62873/3 "2021-06-14T11:38:22Z")

</div>

PostgreSQL and RabbitMQ like service - support multiple clients doing read / write. - Yes, I know it.

I meant concrete package AMQP.jl and LibPQ.jl, does it support? What happen if I’ll pull message from same RabbitMQ queue in same channel on 8 threads in same time? I’m C++ developer so maybe I’m thinking about this issue in lower level.

Similar for PostgreSQL. I know that database as service is supporting multiple INSERT and so on. I meant example: All threads are executing function “execute” in same time from package PostgreSQL.jl. Is it supported by this package?

Let’s summarize it: If we have more threads, we cannot write value to same variable in same time. It must be handled f.e. by semaphore. Regarding to LibPQ.jl and RabbitMQ.jl → Is this implemented in packages? Is this thread safe?

Thank you
