zlacker

[return to "Choose Postgres queue technology"]
1. aduffy+82[view] [source] 2023-09-24 20:41:49
>>bo0tzz+(OP)
For several projects I’ve opted for the even dumber approach, that works out of the box with every ORM/Query DSL framework in every language: using a normal table with SELECT FOR UPDATE SKIP LOCKED

https://www.pgcasts.com/episodes/the-skip-locked-feature-in-...

It’s not “web scale” but it easily extends to several thousand background jobs in my experience

◧◩
2. surpri+rg[view] [source] 2023-09-24 22:39:31
>>aduffy+82
I recently published a manifesto and code snippets for exactly this in Postgres!

  delete from task
  where task_id in
  ( select task_id
    from task
    order by random() -- use tablesample for better performance
    for update
    skip locked
    limit 1
  )
  returning task_id, task_type, params::jsonb as params
[1] https://taylor.town/pg-task
◧◩◪
3. thom+yy[view] [source] 2023-09-25 02:11:13
>>surpri+rg
Presumably it's okay that this loses work if your task runner has an error?
◧◩◪◨
4. surpri+kz[view] [source] 2023-09-25 02:23:54
>>thom+yy
If you read my guide, you’ll see that I embed it in a transaction that doesn’t COMMIT until the companion code is complete :)

For example, I run the above query to grab a queued email, send it using mailgun, then COMMIT. Nothing is changed in the DB unless the email is sent.

◧◩◪◨⬒
5. w23j+I51[view] [source] 2023-09-25 08:51:51
>>surpri+kz
Long running transactions can lead to an accumulation of dead tuples: https://brandur.org/postgres-queues
◧◩◪◨⬒⬓
6. bavell+hi1[view] [source] 2023-09-25 10:53:53
>>w23j+I51
This is from 2015, does it still hold true in 2023?
◧◩◪◨⬒⬓⬔
7. sgarla+bs1[view] [source] 2023-09-25 12:08:22
>>bavell+hi1
Yes, in that Postgres still uses oldest-to-newest tuple ordering, and its MVCC hasn’t changed, so you can still cause the issues listed.

Careful monitoring and tuning of parameters mentioned by the sibling comment to you can help mitigate this, though.

Ultimately at scale, no, RDBMS shouldn’t be a queue. But most have a long way to go before they hit that point.

[go to top]