Drizzle support SELECT FOR UPDATE of postgres to avoid logical race conditions ?
I have a similar use case like this: https://on-systems.tech/blog/128-preventing-read-committed-sql-concurrency-errors/
Currently I use a mutex in my node main process, but I think is better to move this to the db.
If is possible can you show me a minimal example
Thank you so much
Systems and Tech Thoughts
Preventing Postgres SQL Race Conditions with SELECT FOR UPDATE
Learn how to prevent logical race conditions in read-modify-write SQL workloads using SELECT FOR UPDATE.
18 Replies
For example how to do this statement?
I tried with this, but this is called 50 time but only increment the counter 1 time, not like the expected behavior
I'm seeing the same behavior
But this is not an issue with drizzle as the statements it's outputing are all correct
It's the database that's not doing what's supposed to do
I make this work with Prisma and node-postgres, I try to migrate it to drizzle, but I can't 😦
Prisma often times does multiple queries ... you should check the emitted sql by logging it to console - so that you can figure what prisma does.
In prisma I use raw sql since it not support
select ... for update
, but I can't figure out how to do it with drizzle methodsCan you show the code you used for prisma?
It's something like that
Did you call that function concurrently 50 times and it worked?
With the same driver and database?
Yes, also you can add select pg_sleep(5) before commit, and call it from multiple clients and it do correct the row lock
I can do more tests later but can you double check that this would work as you expect with the prisma code?
Yes, I tested with 20k items at works fine
Even better, if you could create a reproduction repository with prisma and drizzle? I'm also interested in this, but I'm tied up right now.
I will try tomorrow 🙂
I had the same issue with drizzle and I ended by finding the issue. I was using Client session, when I switched to Pool it started working
https://orm.drizzle.team/docs/get-started-postgresql#node-postgres
Drizzle ORM - PostgreSQL
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
this fixed the issue for me too. i could not get Drizzle to work with Client, only Pool works for me.
thank you @Fayssal!
@Angelelz This is a bug in Drizzle and the way it uses
node-postgress
in Client mode. Since the Client mode is using only 1 connection, you cannot have more than 1 transaction running at the same time so what happens here is that all the db statements get crammed in a single transaction (concurrent transaction BEGIN
statements are treated as one because there is only 1 client)here's a quick and dirty hack to fix the bug
i think the same bug could potentially occur in the POOL mode as well in case the connection that's already running a transaction gets reused to run another one, but I'm not sure how it works internally