Solved

update and select to create a queue

Posted on 2011-03-06
1
832 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Format Data Field - SQL 11 40
EditableGrid how to fetch rows from MySql in php 14 44
MS SQL GROUP BY 6 72
SQL Query help 3 24
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

685 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