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 6921265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:15:16+00:00 2026-05-27T10:15:16+00:00

If there’s a queue of work todo in a table that is going to

  • 0

If there’s a queue of work todo in a table that is going to be periodically polled by a number of different worker clients…what’s the best way to prevent each worker from getting the same item to work on?

Say a table like: ItemId, LastAttemptDateTime, AttemptCount, and various item details.

Given an index on LastAttemptDateTime and sorted in ascending order and various clients are querying the table to grab an item to be worked on.

I use a stored procedure in MS SQL to do this…something like:

CREATE PROCEDURE GetNextQueueItem AS

SET NOCOUNT ON 

DECLARE @ItemId INT

UPDATE myqueue SET @ItemId=ItemId, AttemptCount=AttemptCount+1, LastAttemptDateTime=GetDate() 
WHERE ItemId=(SELECT TOP 1 ItemId 
FROM myqueue 
ORDER BY LastAttemptDateTime ASC)

SELECT ItemId, AttemptCount, and various item detail fields 
FROM myqueue 
WHERE ItemId = @ItemId

I’m fairly new to PostgreSQL and was wondering if there’s alternate approaches available. (The TOP 1 will change to LIMIT 1.)

  • 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-27T10:15:17+00:00Added an answer on May 27, 2026 at 10:15 am

    PostgreSQL equivalent could look like this:

    CREATE OR REPLACE FUNCTION get_next_queue_item()
      RETURNS SETOF myqueue AS
    $BODY$
    BEGIN
        RETURN QUERY
        UPDATE myqueue
        SET    attempt_count = attempt_count + 1
              ,last_attempt_ts = now()
        WHERE  item_id = (
            SELECT item_id
            FROM   myqueue 
            ORDER  BY last_attempt_ts
            LIMIT  1
            )
        RETURNING myqueue.*;
    
    END;
    $BODY$
      LANGUAGE plpgsql VOLATILE;
    

    Major points

    • You only need 1 statement to do it all. UPDATE can return the updated row in the same command with the RETURNING clause.
      State of the row is post-update. There is ways to get the pre-update state if needed.

    • No need for any variables.

    • I changed all identifiers to lower case, which is the cleanest style in PostgreSQL.

    • I renamed your column LastAttemptDateTime to last_attempt_ts
      ts .. for “timestamp”, because that’s the name of the timestamp / datetime type in Postgres.

    • As you mentioned yourself, LIMIT 1 instead of TOP 1.

    • I use RETURNS SETOF myqueue as return type.
      myqueue is the associated row-type of the table myqueue – for every table or view a row-type of the same name is automatically created in PostgreSQL.
      This declaration allows for multiple rows to be returned, but LIMIT 1 guarantees that it will only ever be one.

    • This return type allows for RETURN QUERY to return the resulting row directly without any intermediate step. Fast, clean.

    Actually, you don’t need a plpgsql function at all. You can do it with a simple SQL statement:

    UPDATE myqueue
    SET    attempt_count = attempt_count + 1
          ,last_attempt_ts = now()
    WHERE  item_id = (
        SELECT item_id
        FROM   myqueue 
        ORDER  BY last_attempt_ts
        LIMIT  1
        )
    RETURNING myqueue.*;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a field in my company's Contacts table. In that table, there is
There is a website called Gild.com that has different coding puzzles/challenges for users to
There is a conversion process that is needed when migrating Visual Studio 2005 web
There is a table view with three sections. The last section may contain many
There is no doubt that MonoTouch is one of the great cross-compiler(s). Similarly, SenchaTouch
There are 2 websites. Mine and a clients. The client needs to be able
There are 3 different ways to get data out of a BLOB column from
There are a number of questions already on the definition of ref and out
There is a class that's a fully fledged UIViewController and when that page loads
There is a security concept regarding the calls to the WCF service that I

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.