D
D
Dmitry Osipov2019-02-04 16:21:51
PostgreSQL
Dmitry Osipov, 2019-02-04 16:21:51

How to make a selection, update records for it and return a cursor with them?

Good day to all, Postgres database I'm
new to writing functions.
I want to take N records from the database for processing in Java. This action may be attempted by several different applications. To prevent a situation where several applications have taken a new record to work and process it together, there is a status column. 0-new record, 1-in process, 2-done.
The essence of the question is - I want to get N records with a select, set their status to 1 and pass the cursor on them to the Java application. Thus, other applications will not be able to take these records. For the life of me, I can't do it in a function in Postgres, apparently Java of the brain. Can you give an approximate sequence of actions to get the desired result?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2019-02-04
@Shiftuia

There is no need to reinvent the wheel with another queue. It won't work under load. Take pgq.
Well, if you still want to collect this rake:

update tablename set status = 1
where status = 0 and id in (
select id from tablename where ..... order by ... limit ... for update
)
returning ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question