Re: Update overwriting

Поиск
Список
Период
Сортировка
От Laurenz Albe
Тема Re: Update overwriting
Дата
Msg-id 1520497505.2564.8.camel@cybertec.at
обсуждение исходный текст
Ответ на Update overwriting  ("Campbell, Lance" <lance@illinois.edu>)
Ответы Re: Update overwriting  ("Campbell, Lance" <lance@illinois.edu>)
Список pgsql-admin
Campbell, Lance wrote:
> PostgreSQL 9.6.x
> I am concerned about update commands overwriting each other.
>  
> Table:
> CREATE TABLE work
> (
>     id integer NOT NULL DEFAULT nextval(('work_id_seq'::text)::regclass),
>     server_id integer default 0,
> //    … other fields
>     CONSTRAINT work_pkey PRIMARY KEY (id)
> );
>  
> Use Case: 
> I have a table that contains data that needs to be processed by N number of applications running in parallel.  Each
rowin the table represents work that needs to be pulled off by an application server to be processed.  When an
applicationis ready to grab records to process, the application will update the server_id from 0 to the application
serversID.
 
>  
> WITH work_select AS (SELECT * FROM work WHERE server_id=0 ORDER BY id ASC LIMIT 100 FOR UPDATE SKIP LOCKED)
> UPDATE work AS work_to_do SET server_id=1 FROM work_select WHERE work_to_do.id=work_select.id;
>  
> Is this the proper command to use to avoid applications assigning their server_id to one that has just been assigned
avalue?
 

Yes, that is safe.

I'd use "SELECT id" instead of "SELECT *" because that is all you need.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com


В списке pgsql-admin по дате отправления:

Предыдущее
От: Doug Gorley
Дата:
Сообщение: How to monitor logical replication initial sync?
Следующее
От: Laurenz Albe
Дата:
Сообщение: Re: Resetting database password