Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ihMue-0002Sy-JI for pgsql-general@arkaria.postgresql.org; Wed, 18 Dec 2019 00:10:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ihMuc-0003YB-UT for pgsql-general@arkaria.postgresql.org; Wed, 18 Dec 2019 00:09:58 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1ihMub-0003Y4-Of for pgsql-general@lists.postgresql.org; Wed, 18 Dec 2019 00:09:58 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ihMuS-0007Zn-Kr for pgsql-general@lists.postgresql.org; Wed, 18 Dec 2019 00:09:56 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id F11E322133; Tue, 17 Dec 2019 19:09:46 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 17 Dec 2019 19:09:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=C +OOfHJ0HNLGPVKVrNsUjJT0WwYFMD6ZWee0WvXtK0I=; b=Y/zrvVUn/LECAQq+R //N+asMLPJENOauP5FmXapdRomCdhsPEWDYUBoTEhJwSVy1TTduaGK6W9Rk/CBT9 wjS57VEARC+e5L64wKhy/TlVJioRMHgRJZb7FHV+uglKbZE6BDha5pNogwskznyb vL8tQi7FY84L1jXQe/ccxPcQBh31BfewunyF2IOqZ4cN5AgeOM9sOLb7Kp0YI8/E +z8nPL5D7Y4URp9gmLPlnRY8nzlgpCrUC2082+jdfSP/y/S7hrO0hCeGov3g0EzV URfoq0cXHvDgs288CybKt155fQrnicQcQvlfoHIzHK73Q+ds1focxtEdC4v0Rqjb 8dh5w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=C+OOfHJ0HNLGPVKVrNsUjJT0WwYFMD6ZWee0WvXtK 0I=; b=i/b3sZYHn1pMdPA9GMjr7HkBjhybxtCqnSnl72JgTwn4pvLEcdRNkk9Lp 7GtbAG37itjKpeES3GwnQ+uKVhaDX9luewSU9tDicwN5y+m+OI9CIDs6PF7Z0EoT Nze1oB36/IAaJHR32QGapLSrBkUzQIqRPkWdO9IZ2YNnHdat2itS7uXM1TwDB5X6 AANMOh+H+E0BTx5crvu54SS3pN4NRoRJzB7xrcC5EjTtbpTgi1gRz2cbHHGeLr8W ZnAZc5pBuMGeKTcpc5T6iRivKZUVyuBQYcIb76jqbPHlq2R2+pmEKaRMPQRQPk6i 1iro7I/l2dreX/C08QT5gBz4xqwtw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrvddtkedgudekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthejredttdefheenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgnecukfhppeejuddrvddu vddruddvledrudelfeenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklh grvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id F26C480059; Tue, 17 Dec 2019 19:09:45 -0500 (EST) Subject: Re: Row locks, SKIP LOCKED, and transactions To: Steven Winfield , "pgsql-general@lists.postgresql.org" References: From: Adrian Klaver Message-ID: Date: Tue, 17 Dec 2019 16:09:45 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/17/19 8:12 AM, Steven Winfield wrote: > Hi all, > > I'm seeing some unexpected behaviour with SELECT ... FOR UPDATE SKIP LOCKED, and having finding it tricky to boil it down to a simple repro case as there's almost certainly a race condition somewhere (more later). So I thought I would ask if what I'm doing is unsupported (or just plain wrong!), before expending more effort in reproducing it. > > I'm running v11.5, RHEL 7.7. > > I have two tables jobs and results: > CREATE TABLE job (id integer PRIMARY KEY); > CREATE TABLE result (id integer PRIMARY KEY); > (obviously the real tables have more columns, but that's not too important here) > > Something populates the job table with IDs. > A job is done if its id appears in the result table. > I would like to have multiple worker processes working on jobs. > > > I thought I could achieve this with each working doing the following: > > BEGIN; > > SELECT id > FROM job > WHERE NOT EXISTS (SELECT 1 FROM result WHERE result.id = job.id) > LIMIT 1 > FOR UPDATE SKIP LOCKED; > > -- worker process does some work for the selected ID here > > INSERT INTO result (id) VALUES (the_id_from_above); > > COMMIT; > > > However, even with just two worker processes, I quickly found that one worker process would be assigned a job id that had *very* recently been completed by the other worker. > > Some more potentially useful information: > * The LockRows node of the plan for the SELECT query above doesn't receive any tuples until about a second after the query begins executing > * If worker 2 begins querying for a new job id half a second before worker 1 commits then worker 2 will pick up the job id that worker 1 has just finished with. > * I observe this even if I crank up the transaction isolation level to repeatable read and serializable. > > > I'm wondering if row locks are not obeying the same transactional semantics as row data, as a potential explanation for the above behaviour is as follows (W1/2 = Worker 1/2): > > W1: BEGIN; > W1: SELECT ... > W1: (SELECT returns id=1. W1 now has job(id=1) locked.) > W1: INSERT INTO result (id) VALUES (1) > > W2: BEGIN; > W2: SELECT ... > > W1: COMMIT; job(id=1) is now unlocked. > > W2: (SELECT returns id=1: W1 had not committed when the SELECT started, so result(id=1) is not visible, but LockRows found that job(id=1) was not locked. W2 now has job(id=1) locked.) > > > ...i.e. W2's SELECT could not see the row that W1 INSERTed (because W2's BEGIN occurs and W2's SELECT begins before W1's commit), but W2's SELECT *could* see the removal of W1's row lock. Gotta believe it is this: https://www.postgresql.org/docs/11/transaction-iso.html#XACT-READ-COMMITTED "UPDATE, DELETE, SELECT FOR UPDATE, and SELECT FOR SHARE commands ..." If I read correctly, transactions can see the effects of other transactions that commit during their lifetime. > > > Perhaps this is a misuse of the locking system, since I'm locking a row "FOR UPDATE" but not actually updating it, but as row locks are released at the end of a transaction (according to the docs) then my expectation was for the unlocking and the visibility of newly committed rows to be atomic. > I've tried FOR NO KEY UPDATE too, without luck. > > If I'm doing something forbidden (and the docs say so) then I'd be grateful if someone could point that out! > > Best, > Steven. > > > > -- Adrian Klaver adrian.klaver@aklaver.com