I have an application (Ruby on Rails 4) and using a postgresql query that mainly looks at the Opportunity table, randomly search among rows where there is an “available = true” option and update the selected rows with “available = false” ". Each time the user clicks on the" try opportunity ", the application uses the request below.
UPDATE opportunities s
SET opportunity_available = false
FROM (
SELECT id
FROM opportunities
WHERE deal_id =
AND opportunity_available
AND pg_try_advisory_xact_lock(id)
LIMIT 1
FOR UPDATE
) sub
WHERE s.id = sub.id
RETURNING s.prize_id, s.id;
// inspired by https://stackoverflow.com/questions/33128531/put-pg-try-advisory-xact-lock-in-a-nested-subquery
I struggled for 5 days, but now I managed to understand how he behaves (badly): now I need to know how to change it.
Indeed, the application “gets a little crazy” and selects (and updates the row) the last updated one.
,
, , , : , , , ( prized_id = empty)...
: ( pgadmin), : 32, 45, 67, , , , , , : 67, line45, 32..
, ( , = true).
"pg_try_advisory_xact_lock (id)", , , .
, 20 , 4 , , ( pgadmin ... , backround, postgresql ?) , 4 , .
, , , (: 3, 4, 5 , pgadmin, ). , , ....
, .
: 100% , : , 4 , 2 , , , , ( ) , , ...
1
Opportunity (, , 21, = 3) = > ( ), , _ (.. pr_id = , )
SQL (2.4ms)
UPDATE "opportunities"
SET "prize_id" = 21
WHERE "opportunities"."id"
IN (
SELECT "opportunities"."id"
FROM "opportunities"
WHERE (deal_id = 341 AND prize_id IS NULL)
ORDER BY RANDOM()
LIMIT 3) //
(0.9ms) COMMIT
SQL- Rails- ( Randumb: github.com/spilliton/randumb)
2
: Postgresql 9.4 - FASTEST ( > 30M ) /
, , , RANDOM ARBITRARY.
Erwin NOWAIT, ?, , ( "" , , Postgresql, , , , ". postgresql " LIMIT 1 ", = , . , - Winning.