Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZsDb3-0002ki-1D for pgsql-bugs@arkaria.postgresql.org; Fri, 30 Oct 2015 17:36:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZsDb2-0005xS-DO for pgsql-bugs@arkaria.postgresql.org; Fri, 30 Oct 2015 17:36:12 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZsDb1-0005x2-C7 for pgsql-bugs@postgresql.org; Fri, 30 Oct 2015 17:36:11 +0000 Received: from nm24-vm5.bullet.mail.ne1.yahoo.com ([98.138.91.246]) by makus.postgresql.org with esmtps (TLS1.0:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZsDat-0004l7-RM for pgsql-bugs@postgresql.org; Fri, 30 Oct 2015 17:36:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ymail.com; s=s2048; t=1446226562; bh=tZylTM/Ok+c73VIEuId9PJ4njZMVUkUrZPEgPiR7Sgw=; h=Date:From:Reply-To:To:In-Reply-To:References:Subject:From:Subject; b=lo9X1W/Un60EnfiRgevFNIyl55WbZYAgl7m+SsyMJzTKvH6Pwi6T7yex2W49qBmaCoKVq7nJb+X6FUNCRjEae+KlCC0ayYrVfplwtDERtdddn1pFutXtSJhzcP0WhEkLwNaNhGN3WC80mCebTBDOK19aobIL6IDmZPIYWMCLWgPIk3O/4JsI5sm/BwdtGAOgen3pmtiW6duepCnRErxqUzOgFJrMZy1+ebMVrgoLcZQhSYS0amOlDs4J7W41PZmty0ubIkvjwNqgwdTsefNGOMLh8AWPN/gZ77MRu7CjKU7hKKfy5phB1CVa5bBeK40TzLUU59vPjJ7izfP/NTB87A== Received: from [98.138.100.114] by nm24.bullet.mail.ne1.yahoo.com with NNFMP; 30 Oct 2015 17:36:02 -0000 Received: from [98.138.89.174] by tm105.bullet.mail.ne1.yahoo.com with NNFMP; 30 Oct 2015 17:36:02 -0000 Received: from [127.0.0.1] by omp1030.mail.ne1.yahoo.com with NNFMP; 30 Oct 2015 17:36:02 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 332346.65504.bm@omp1030.mail.ne1.yahoo.com X-YMail-OSG: M28v8ywVM1kUDs3IhUeeToZh9QeYuoD0_9AQkSiVewpotmy19H3WmmXzHuXocpJ 31GDOiBvXWBdFO.x35XtBGZvYqSkYwyIbdOVXaR7RdmrjQBSjuPsrkKVdHm3Q2mCPcbtTTIpHxFh DXwI0dPPKL2aanNNfLvSdq1EeeP_XsgWG2EDxIMMclpCnl.6xT46ART5yXvbl5qqdCfEthNXlks7 1oFQdYNaYlY9c.286atMLPtSITgWhwddLybvrAuhv2ebg9UXSM6U9xS0Ts7ArcDQUnAUnD36mLet 3lzCV.mEzVkfGX_CzNYPeIrRPL0wniMZg9hxgJkCNZFpgE6uv3Olf46NRFiDntL_1PVv6DSoAGAl UGcDX7MhnyCqCJ7MKY6vzt1LRLZ1p1cZUmIDtGllk2EJQ5ymPeGKnKcbJwOGl1SJPnaEJ3a5Xzel tJWjs1KUIMlAS3x5qVIXndotPSW5W9B5LpFhl9V_tPWFCRiYvMc2ZW1li_ZNQjoSOo961 Received: by 98.138.105.222; Fri, 30 Oct 2015 17:36:01 +0000 Date: Fri, 30 Oct 2015 17:36:01 +0000 (UTC) From: Kevin Grittner Reply-To: Kevin Grittner To: Kevin Grittner , "sean@chittenden.org" , "pgsql-bugs@postgresql.org" Message-ID: <1285273220.277372.1446226561203.JavaMail.yahoo@mail.yahoo.com> In-Reply-To: <1810271785.1293615.1444427336478.JavaMail.yahoo@mail.yahoo.com> References: <1810271785.1293615.1444427336478.JavaMail.yahoo@mail.yahoo.com> Subject: Re: BUG #13667: SSI violation... MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_277371_369864714.1446226561203" Content-Length: 7652 X-Pg-Spam-Score: -2.7 (--) 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 ------=_Part_277371_369864714.1446226561203 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On Friday, October 9, 2015 4:49 PM, Kevin Grittner wrote: > I agree this is a bug, and it behave like a race condition with a > very narrow window of time for the second process to hit. It has > proven very hard to find the cause. Thanks to Thomas Munro joining me in a 2.5 day marathon hunt for this bug, we have found it and squashed it with the attached patch. The issue in heap_insert() in heapam.c was the bug that was actually causing the failures in the specific tests we had, but other locations doing insert, update, or delete had similar bugs, and we found a few stray issues in predicate.c that are also fixed here. The basic problem was that in the initial implementation we tried a little too hard to optimize, at the expense of correctness. When we were going to insert, for example, we checked for an existing predicate lock on the relation first, to avoid the effort of inserting the tuple, with WAL-logging, if we were just going to roll back the transaction anyway because we found a rw-conflict that completed a "dangerous structure". The problem is, it allowed this sequence to create an undetected serialization anomaly: T1 ---------- T2 ---------- lock read checklocks lock read insert At this point things are hosed, regardless of timings past this. The window in the test was small because T2 has to acquire its SIReadLock and scan the whole table before T1 manages to insert a tuple, but with enough contention T1 can stall long enough to see this happen. When T2 checks locks prior to its insert it will detect the rw-conflict from T1 to T2, but the rw-conflict in the other direction won't be detected, so no dangerous structure is formed and nothing is rolled back. Moving the check past the insert would fix the problem, but the reason it was put in front is enshrined in a comment at each problem location; for example: * We're about to do the actual insert -- but check for conflict first, to * avoid possibly having to roll back work we've just done. These checks are about as close to free as you can get if the transaction doing the check is not serializable; it doesn't even need to take out a LW lock to determine there is nothing to be done. The reason given in the comment still has merit for serializable transactions; even for them the check is orders of magnitude cheaper than a WAL logged tuple insert. It only requires an occasional serialization failure detection there to come out ahead. So rather than move the existing check, we added a recheck after. Barring objections I will push this tomorrow, including back-patching it to all supported branches. -- Kevin Grittner EDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company ------=_Part_277371_369864714.1446226561203 Content-Type: invalid/octet-stream Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=ssi-bug-fixes-v1.diff Content-ID: <302aa123-cebf-aee5-4697-7125bf14c9c1@yahoo.com> ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2FjY2Vzcy9oZWFwL2hlYXBhbS5j IGIvc3JjL2JhY2tlbmQvYWNjZXNzL2hlYXAvaGVhcGFtLmMKaW5kZXggNjZk ZWIxZi4uZmYyZWVhOCAxMDA2NDQKLS0tIGEvc3JjL2JhY2tlbmQvYWNjZXNz L2hlYXAvaGVhcGFtLmMKKysrIGIvc3JjL2JhY2tlbmQvYWNjZXNzL2hlYXAv aGVhcGFtLmMKQEAgLTI1MTYsNiArMjUxNiw5IEBAIGhlYXBfaW5zZXJ0KFJl bGF0aW9uIHJlbGF0aW9uLCBIZWFwVHVwbGUgdHVwLCBDb21tYW5kSWQgY2lk LAogCWlmICh2bWJ1ZmZlciAhPSBJbnZhbGlkQnVmZmVyKQogCQlSZWxlYXNl QnVmZmVyKHZtYnVmZmVyKTsKIAorCS8qIFJlY2hlY2sgdG8gY2xvc2UgYSB3 aW5kb3cgZm9yIGEgcmFjZSBjb25kaXRpb24uICovCisJQ2hlY2tGb3JTZXJp YWxpemFibGVDb25mbGljdEluKHJlbGF0aW9uLCBOVUxMLCBJbnZhbGlkQnVm ZmVyKTsKKwogCS8qCiAJICogSWYgdHVwbGUgaXMgY2FjaGFibGUsIG1hcmsg aXQgZm9yIGludmFsaWRhdGlvbiBmcm9tIHRoZSBjYWNoZXMgaW4gY2FzZQog CSAqIHdlIGFib3J0LiAgTm90ZSBpdCBpcyBPSyB0byBkbyB0aGlzIGFmdGVy IHJlbGVhc2luZyB0aGUgYnVmZmVyLCBiZWNhdXNlCkBAIC0yODQ1LDYgKzI4 NDgsOSBAQCBoZWFwX211bHRpX2luc2V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0aXRpb25Mb2NrQnlJbmRleChpKSwgTFdfU0hBUkVEKTsKLQlMV0xvY2tB Y3F1aXJlKFNlcmlhbGl6YWJsZVhhY3RIYXNoTG9jaywgTFdfU0hBUkVEKTsK KwlMV0xvY2tBY3F1aXJlKFNlcmlhbGl6YWJsZVhhY3RIYXNoTG9jaywgTFdf RVhDTFVTSVZFKTsKIAogCS8qIFNjYW4gdGhyb3VnaCB0YXJnZXQgbGlzdCAq LwogCWhhc2hfc2VxX2luaXQoJnNlcXN0YXQsIFByZWRpY2F0ZUxvY2tUYXJn ZXRIYXNoKTsK ------=_Part_277371_369864714.1446226561203 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 ------=_Part_277371_369864714.1446226561203--