Postgresql – avoid lock and allow two scripts to insert data in one table

lockingpostgresql

I have a database called db1 and I'm filling it with other two different databases, let's call db2 and db3. I connect db2 and db3 to db1 using postgres_fdw and now I'm running scripst like this:

insert into db1.table from select a,b,c from db2.table

and

insert into db1.table from select a,b,c from db3.table

turns out that only the first script is running, the second is waiting with idle in transaction. This is what htop shows me from the second script, while the first is ok.

htop

looking for reasons, I found out that this may be happening due to a lock, so investigating on postgres, I also found this

lock

I'm inserting data into interactions and each interactions has user, so I'm selecting the correct user before insert into interaction.
Is there a way to bypass the lock? If so, is it safe?

Best Answer

idle in transaction means it is waiting for data from the client, not that is waiting on a lock.

And your excerpt from pg_lock supports this, assuming the last column is the "granted" column. All locks have been granted, no one is blocking.

If your scripts are blocking each other, it seems to be happening outside of the database. Or at least, outside of that particular database.