Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1etqrY-0001hv-KZ for pgsql-admin@arkaria.postgresql.org; Thu, 08 Mar 2018 08:25:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1etqrX-0005c8-4E for pgsql-admin@arkaria.postgresql.org; Thu, 08 Mar 2018 08:25:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1etqrW-0005bq-TB for pgsql-admin@lists.postgresql.org; Thu, 08 Mar 2018 08:25:18 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1etqrT-0000E9-LH for pgsql-admin@lists.postgresql.org; Thu, 08 Mar 2018 08:25:18 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id B4B7C40F74; Thu, 8 Mar 2018 09:25:13 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter03.heinlein-hosting.de (spamfilter03.heinlein-hosting.de [80.241.56.117]) (amavisd-new, port 10030) with ESMTP id ceMkP48rEByV; Thu, 8 Mar 2018 09:25:07 +0100 (CET) Message-ID: <1520497505.2564.8.camel@cybertec.at> Subject: Re: Update overwriting From: Laurenz Albe To: "Campbell, Lance" , "pgsql-admin@lists.postgresql.org" Date: Thu, 08 Mar 2018 09:25:05 +0100 In-Reply-To: <1ADF4886-8AFB-4B94-8077-9BD0AA2230E8@illinois.edu> References: <1ADF4886-8AFB-4B94-8077-9BD0AA2230E8@illinois.edu> Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 row in the table represents work that needs to be pulled off by an application server to be processed. When an application is ready to grab records to process, the application will update the server_id from 0 to the application servers ID. > > 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 a value? 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