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

  • Home
  • SEARCH
  • 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 9249557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:13:34+00:00 2026-06-18T10:13:34+00:00

I am building a FIFO queue using postgres and the answer based on apinstein

  • 0

I am building a FIFO queue using postgres and the answer based on apinstein answer to Job queue as SQL table with multiple consumers (PostgreSQL)

The issue is, if I use a transaction so the statement looks like:

    begin;
lock table queued_message in exclusive mode;
update 
    queued_message
set 
    status='IN_PROGRESS'
where
    id in (
        select
            id
        from
            queued_message
        where
            status='SUBMITTED' and queue='BACKFILL_REQUEST'
        order by 
            id asc
        limit 1
    )
returning *;
commit;

Then my returning values get discarded. If I run the same statement without the begin/commit the recordset returns fine.

Obviously I would prefer the transaction; the statement may not even be safe without it. So how do I return my committed recordeset?

EDIT
I’m marking the answer since it got me on the right track, but here is the function I ended up with:

CREATE TYPE returned_message as (id bigint, body json, status character varying(50) , queue character varying(150), last_modified timestamp without time zone)

CREATE OR REPLACE FUNCTION get_next_message(desiredQueue character varying(150)) 
RETURNS returned_message AS $$
    DECLARE result returned_message;
    BEGIN
    lock table queued_message in exclusive mode;
    update queued_message
    set 
        status='IN_PROGRESS'
    where
        id in (
        select
            id
        from
            queued_message
        where
            status='SUBMITTED' and queue=desiredQueue
        order by 
            id asc
        limit 1
        )
    returning * into result;
    RETURN result; 
END;$$LANGUAGE plpgsql; 


select * from get_next_message('BACKFILL_REQUEST')
  • 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-06-18T10:13:35+00:00Added an answer on June 18, 2026 at 10:13 am

    You can create a function returning needed values. Each function executes as transaction.
    Do not place “begin;” and “commit;” in the function body. Function below should work I believe.

    create or replace function set_in_progress()
    returns setof queued_message 
    language sql as $$
        lock table queued_message in exclusive mode;
        update 
            queued_message
        set 
            status='IN_PROGRESS'
        where
            id in (
                select
                    id
                from
                    queued_message
                where
                    status='SUBMITTED' and queue='BACKFILL_REQUEST'
                order by 
                    id asc
                limit 1
            )
        returning *;
    $$;
    
    select * from set_in_progress();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Building a website using a PHP based Ecommerce product Magento. The problem I have
building a site using PHP and MySQL that needs to store a lot of
Building on this answer: https://stackoverflow.com/a/2349824/1382306 Are there further guidelines in A and B's usages,
Building on the query in this answer (please note/assume that the GROUP_CONCAT s are
Building an Android application using Google's GCM service. I have implemented the onRegistered method
Building with clang++ -std=c++0x -stdlib=libc++ main.cpp #include <string> #include <functional> using std::bind; using std::string;
Building my first web app using Yii and wondering if it is best to
Building a Monodroid application we've been using Protobuf-net for serialization. It works in debug
Building a new website and using the 960 grid system, with the following css:
Building a framework on Mac OSX is straightforward using XCode with its ready-made templates.

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.