Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XEKs2-0000BY-2w for pgsql-bugs@arkaria.postgresql.org; Mon, 04 Aug 2014 16:12:22 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XEKs0-0006qc-QD for pgsql-bugs@arkaria.postgresql.org; Mon, 04 Aug 2014 16:12:20 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XEKrz-0006qT-EP for pgsql-bugs@postgresql.org; Mon, 04 Aug 2014 16:12:19 +0000 Received: from mail-pd0-x229.google.com ([2607:f8b0:400e:c02::229]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XEKrv-0003dB-Dz for pgsql-bugs@postgresql.org; Mon, 04 Aug 2014 16:12:17 +0000 Received: by mail-pd0-f169.google.com with SMTP id y10so9949897pdj.0 for ; Mon, 04 Aug 2014 09:12:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janrain.com; s=google; h=date:from:to:subject:message-id:mail-followup-to:references :mime-version:content-type:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=cTSNsjsrZrBwcIdRpzEQ8+Yo7slbN1ST3bbqWK4zZxk=; b=IuLl3Cn6m9wdFMuZKL8rJ+kp8IT/73SEp0mda9GN2bMp6k/4diwCanHwKRvP+FxinU OinC+5Wb1J5zRfPZzAgt6hoZY3oqD46NVCotoJWf4xbloFymvU01Neb6XqIDTytfGLYO wcR4rJXyvxd5rMNCSm7O1GxwfQfUeO02XBRS8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:mail-followup-to :references:mime-version:content-type:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=cTSNsjsrZrBwcIdRpzEQ8+Yo7slbN1ST3bbqWK4zZxk=; b=UH3N+brGOYw0TlYrKxwTwzY0NGqtM+LynkdDwzFr3sci6/ZaYRt9IJPN1tbBUZLF7E tt40/YrSzBV8/PP3BGH54pLNAUwI89pnExsh7rtrhtbySTfR+BOnpU93iZ8Kw4QwwB81 Vm4MlCV/rslDaxg2mus04xp4Y6ZBb40GbZM6jC7kbBxCU5muenarvXgEN6Y914M+Pi94 b2D8JPszFlwlwHWpQdfyueOW9bEeZPRD0kJL00KmVswtzfNA7AQ6be9qHuXbeedAWJFI Bd9MCLhoDuGqKAB+ls5pIOgElexG1bRsT5dcXBwLu5LyNHjFSIPPlDzhRjsaeOZzt2va Oc8w== X-Gm-Message-State: ALoCoQn6/ZZIY/bw5f5pwQtcMIy5Ag2+Gc/qSwDxlDhnFJ1ZzmAdJOak+nDoQ+ZEQYBESE2niece X-Received: by 10.70.103.106 with SMTP id fv10mr25277970pdb.63.1407168733452; Mon, 04 Aug 2014 09:12:13 -0700 (PDT) Received: from andrew-ThinkPad-X230 (173-164-68-213-Oregon.hfc.comcastbusiness.net. [173.164.68.213]) by mx.google.com with ESMTPSA id up2sm6477067pbc.21.2014.08.04.09.12.11 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Mon, 04 Aug 2014 09:12:12 -0700 (PDT) Date: Mon, 4 Aug 2014 09:12:05 -0700 From: Andrew Sackville-West To: pgsql-bugs@postgresql.org Subject: Re: regression, deadlock in high frequency single-row UPDATE Message-ID: <20140804161205.GA30380@andrew-ThinkPad-X230> Mail-Followup-To: pgsql-bugs@postgresql.org References: <20140731233051.GN17765@andrew-ThinkPad-X230> <20140801005950.GF5475@eldon.alvh.no-ip.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="FCuugMFkClbJLl1L" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20140801005950.GF5475@eldon.alvh.no-ip.org> User-Agent: Mutt/1.5.21 (2010-09-15) X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --FCuugMFkClbJLl1L Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Thu, Jul 31, 2014 at 08:59:50PM -0400, Alvaro Herrera wrote: > Andrew Sackville-West wrote: > > Hi, > > > > I've discussed this problem on irc a couple of times and think I've > > found a regression that plagues our application, introduced in some > > version newer than 9.1.9, and still present in 9.3.5. > > I don't think you have provided everything: for instance I see that your > log has INSERTs into table access_tokens, which has a foreign key > relationship to/from some other table you showed. Ah you're correct, I'll attach the description of that table. But, really, that is all that's going on in the database. > > I'm too lazy to unscramble the whole thing into a real schema right now. > pg_dump output would be more helpful, as would some trivial sample data > enough to reproduce the problem. It's really minimal data. I'll attach a dump of the access_grants and oauth_clients table. The access_tokens table is not empty, but I'm not sure how relevant the data is. What is shown in the log is typical. There is another table in play here, refresh_tokens, but we're not actually using them in this case -- the table is empty, and the reference to it in access_tokens in null. > It'd probably be easier to reproduce if you wrap the statements in > transactions and have them sleep before COMMIT. The issue here is, with no code changes, this works fine under very heavy load in production against 9.0.x, 9.1.9, etc. But under 9.3.{3,4,5} it fails. That code has no transaction. I think adding a transaction is a distraction from what appears to me to be a regression. Interestingly, if I change the code to include a transaction around the UPDATE, the problem goes away (or so it seems so far in my testing). > (If you run two of > these updates in parallel, the first one blocks the second until the > first one commits, because they update the same row. What happens if > the blocking session runs the aforementioned INSERT while the other is > blocked? Perhaps three sessions are needed to show a problem without > explicit transactions.) I've run pg_bench from 3 machines all pointing at the same database running this same set of statements and it fails to induce the deadlock. It occurs to me that I haven't completely described the sequence of actions, so here is a snippet of the actual SQL that a given action induces, when it works as expected: SET standard_conforming_strings TO on; SET datestyle TO ISO; SET client_encoding TO utf8; SELECT "user_1"."id", "user_1"."uuid", "user_1"."lastUpdated" AT TIME ZONE 'UTC', "user_1"."id" as _id FROM "z8z6px927zu6qzzbnb5ntgghxg"."user_1" AS "user_1" WHERE "user_1"."id" = 129; UPDATE "z8z6px927zu6qzzbnb5ntgghxg"."access_grants" ag SET last_issued=DEFAULT FROM "z8z6px927zu6qzzbnb5ntgghxg"."oauth_clients" oc WHERE oc.id = ag.client_id AND ag.entity_name = 'user' AND ag.entity_id = 129 AND oc.client_id = '3hp45h9d4f9wwtx7cvpus6rdb4s5kb9f' RETURNING ag.id; INSERT INTO "z8z6px927zu6qzzbnb5ntgghxg"."access_tokens" (token, access_id, refresh_token_id, issued, expiry) VALUES ('xys7vzs94huex2y7', 2, null, timezone('UTC'::text, now()), timezone('UTC'::text, now()) + '1:00:00':: interval); This represents one complete action. There are several of these running concurrently, interleaved together. There is no transaction involved here. As far as I can tell, the application is using bog-standard libpq (via FFI wrapper in Haskell) with no non-default configuration applied to the connection. I realize there's not a lot to go on here, and I don't expect help debugging my application. I've got a patch that seems to resolve the problem for us. But I'm suspicious of this change in behavior and hope that pointing it out can help in some way. regards, A > > -- > Álvaro Herrera http://www.2ndQuadrant.com/ > PostgreSQL Development, 24x7 Support, Training & Services -- --FCuugMFkClbJLl1L Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=access_tokens_table Table "z8z6px927zu6qzzbnb5ntgghxg.access_tokens" Column | Type | Modifiers ------------------+-----------------------------+------------------------------------------------------------------------ id | bigint | not null default nextval('access_tokens_id_seq'::regclass) token | character(16) | not null access_id | bigint | not null refresh_token_id | bigint | issued | timestamp without time zone | not null default timezone('UTC'::text, now()) expiry | timestamp without time zone | not null default (timezone('UTC'::text, now()) + '01:00:00'::interval) refresh_secret | character varying(64) | Indexes: "access_tokens_pkey" PRIMARY KEY, btree (id) "access_tokens_token_key" UNIQUE CONSTRAINT, btree (token) "access_tokens_access_id_idx" btree (access_id) "access_tokens_refresh_to_idx" btree (refresh_token_id) Foreign-key constraints: "access_tokens_access_id_fkey" FOREIGN KEY (access_id) REFERENCES access_grants(id) ON DELETE CASCADE "access_tokens_refresh_token_id_fkey" FOREIGN KEY (refresh_token_id) REFERENCES refresh_tokens(id) ON DELETE CASCADE --FCuugMFkClbJLl1L Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=sample_data id | client_id | entity_name | entity_id | originally_issued | last_issued ----+-----------+-------------+-----------+----------------------------+---------------------------- 2 | 1 | user | 129 | 2014-07-10 19:15:37.116183 | 2014-07-31 02:11:25.286745 1 | 1 | user | 1 | 2014-03-19 01:43:12.856049 | 2014-07-10 21:19:27.43646 (2 rows) id | client_id | client_secret | description ----+----------------------------------+----------------------------------+------------------- 1 | 3hp45h9d4f9wwtx7cvpus6rdb4s5kb9f | wwyxv338q8qhd72yhvtk45znw2kzhw32 | application owner (1 row) --FCuugMFkClbJLl1L Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --FCuugMFkClbJLl1L--