Solved

update and select to create a queue

Posted on 2011-03-06
1
810 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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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.
This video discusses moving either the default database or any database to a new volume.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now