Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7057481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:59:29+00:00 2026-05-28T03:59:29+00:00

I have a priority queue table that can be simplistically represented as: CREATE TABLE

  • 0

I have a priority queue table that can be simplistically represented as:

CREATE TABLE test (
  id int PRIMARY KEY,
  priority int,
  status int
)

where priority is the priority (lowest first) and status is the item state (0 means OK to retrieve). The task is to retrieve the element with highest priority and reset its status, so the code goes like this (pseudocode):

 try = 0;
 while(try++ < max_tries) {
   result = query("SELECT id FROM test WHERE status=0 ORDER BY priority LIMIT 0,1");
   if(result not empty) {
      /* found a row, try to update */
      result2 = query("UPDATE test SET status=1 WHERE id={result[id]} AND status=0");
      if(affected_rows(result2) > 0) {
         /* if update worked fine, we can use this ID */
         return result[id];
      }
      /* otherwise try again */
 }
 return NULL;

This code should run (with appropriate modifications, like different LIMIT syntax) on any sql database (current requirement is Mysql, Oracle, SQL Server and DB2, but may be more). As far as I know, all of required DBs support “affected rows” API for updates. Any potential problems or pitfalls with this approach? Should SELECT … FOR UPDATE be used in above statement and if yes, why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:59:29+00:00Added an answer on May 28, 2026 at 3:59 am

    The SELECT / UPDATE combination listed seems to address the concrrency issue with the UPDATE’s WHERE clause. I would however lean toward a stored procedure if that were acceptable: depending on the sql flavour, the stored proc could get rid of the concurrency problem with an atomic “UPDATE … RETURNING id INTO… “.

    Regarding the states, if they are something like:

    • 0 “okay to retrieve”
    • 1 “in process”
    • 2 “complete”

    then the state acts as a lock. There would be a concern if the process dies after the state changes from 0 to 1, but before the job was actually processed: the record can remain locked and unprocessed until a cleanup job runs. Another approach is to use a separate timestamp column for the lock. Then the query includes a filter “WHERE now() – timeout > NVL(lockTimestamp, beginning of time)” and locking is accomplished by setting the lockTimestamp to now() rather than relying on setting the status=1. This way the lock can auto-expire after a reasonable wait (timeout) and the item becomes available again to be picked up by the next processor. No clean-up job required.

    Any chance of starvation where a low priority job might not get picked up?

    Does an additional ordering need to be added? If the items are all of the same priority, does it matter which are processed first (fifo, lifo)?

    I guess it should be noted that if there are multiple instances of this fetch routine running, then multiple queue items may be processed concurrently and the order of processing is not necessarily sequential (process1 gets item1, process2 gets item2, process2 completes item2, process1 completes item1, …), so hopefully that is okay, otherwise the locking needs to check that nothing else is in process.

    I also don’t particularly see a need for a SELECT … FOR UPDATE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a priority queue implementation in C# that I want to add a
How can I remove an arbitrary item from a priority queue. Suppose I have
Suppose that I have a priority queue which removes elements in increasing order, and
Is it possible to have a Priority Queue with the same key? so have
I have implemented Priority Queue interface for making heap. Can you tell me how
I need B class to have a min priority queue of AToTime objects. AToTime
I have a table as below Name Priority Date ------------------------- A 2 d1 B
I have a requirement that a SPList should be sorted by the Priority field
I have List I want to sort Desc by Priority, which is int and
I have a List where element is: struct element { double priority; int value;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.