Solved

update and select to create a queue

Posted on 2011-03-06
1
823 Views
Last Modified: 2012-05-11
In a queue table we have:

DELETE FROM alert_queue WHERE id = (SELECT MIN(id) FROM alert_queue) RETURNING *

Which allows many processes to get (and process) a single id from the queue table, with no 2 processes getting the same id to be processed.

I would like to do the same kind of thing in a regular table, but with select and update (no delete) - in 1 sql command (no transactions).

Using Postgres 9.0 and Perl DBI,

we have rows of that have a bool column "to_be_processed". I want to get the next  row id  where "to_be_processed" = 'f'  while updating the column to 't' and have many processes running the same query with each getting a separate row (i.e no 2 processes getting the same row).

Quasi SQL:
Update  alert_queue set to_be_processed = 't'  WHERE id = (SELECT MIN(id) FROM alert_queue where to_be_processed = 'f') RETURNING id


Thanks.
0
Comment
Question by:freshgrill
1 Comment
 
LVL 7

Accepted Solution

by:
MrNed earned 500 total points
ID: 35053290
I can't think of any way to do it with a single query. Relevant discussion with workable solutions here: http://stackoverflow.com/questions/389541/select-unlocked-row-in-postgresql

Basically:
1. Select an unlocked row and lock it
2. Process/Delete it
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question