Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y6SQy-00053X-0Z for pgsql-bugs@arkaria.postgresql.org; Wed, 31 Dec 2014 23:12:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y6SQx-0005DT-5Z for pgsql-bugs@arkaria.postgresql.org; Wed, 31 Dec 2014 23:12:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y6SQu-0005DK-CX for pgsql-bugs@postgresql.org; Wed, 31 Dec 2014 23:12:05 +0000 Received: from nm25-vm6.bullet.mail.ne1.yahoo.com ([98.138.91.118]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Y6SQk-0006fw-Mo for pgsql-bugs@postgresql.org; Wed, 31 Dec 2014 23:12:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ymail.com; s=s2048; t=1420067510; bh=wL2H7V+PX2in0wv2iOK4HxYvDW+AeRkBs1WwdgoslCM=; h=Date:From:Reply-To:To:Cc:In-Reply-To:References:Subject:From:Subject; b=aAqTn8zg03HcVXO+j/KVSKSGq/nZB4PeXUC105n1w5bJWqcoEVzu4dYX6bChpua8Q99Bgsun1Hrd4x1FO4810SZqR0AVwwfE+4N+Vhr0FIt4dNN9Gbor663VqVF4p7tcRcjb0nYrvRgXusuqBuSabO8e9I+0MlXwIYdd7EgNMr7BQ3XrglVtw5re1RMF2DhIAfYwJRyFYB99GjQ9m8F3Kh+8PuFBYK5G2FVCr5O8RuXX71tmkF7GDJdeY71j5gxnZGCZUsAOQbuv1wh+/Bo2RlWXGSuAPKqCXcHvMweuSOw9akqAnOI1SRWLCwk6TI5cork2Kr4nNlK1PncmzWfZvQ== DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s2048; d=ymail.com; b=Xja4RW+M5m5UHiNq3/f8EmWsfGZxxfeg7aZal/km1L184gIO3KqcO4NWC9UtCM8x+sdq16og4iPONmCGU53eL8NlcHkzrqRALwQTcwLgFLBrpWB7zqEXVuu9GQ09HRVgsAKT9buH6ppW7rUUiVqoFoUAk086nl2ef0TyuhFOXs+rsUPC1h06Z+ImUevG/o3kGDCgnOlkcYjZlHylcob0pJvnHJd23RZO1xGJ71Q/QwICJNqKirh/NSPJtfrZw65ZCyzaxGeNkjagrlPgDUlfmSieRV08+FnfO1OrA6SDYXe0Pjg5qzdhjPjB3NYBYSOZ/JC74gf0zQbSTLgfACWXGQ==; Received: from [98.138.100.112] by nm25.bullet.mail.ne1.yahoo.com with NNFMP; 31 Dec 2014 23:11:50 -0000 Received: from [98.138.87.9] by tm103.bullet.mail.ne1.yahoo.com with NNFMP; 31 Dec 2014 23:11:50 -0000 Received: from [127.0.0.1] by omp1009.mail.ne1.yahoo.com with NNFMP; 31 Dec 2014 23:11:50 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 276064.76990.bm@omp1009.mail.ne1.yahoo.com X-YMail-OSG: pJ_ZuncVM1l2GyxaHb6wDZDFzqpooAKr82U3eX.1UdaFIG6KpOyKxECSeLl.wIf C0lQuV8AEfLPWne7ex4HEDI4L2qMeg0Jb4XfaWJISfreOQA8Aq69sdb9pW9KHVT6No09z42kHwjV eAbxKleVQbhmFn4RpSumnzFikmNbl1yDXE.Q8xEao9mCazqtt_ea_sC8vjvpmY53CXjBti8_eArR 3dOc3NKfZJpWSgvpzuWz8mFn7w5W15CsSMUu8Y6cLk4pQBCVtdhKZXSNWi4F9Im4cZ2Wb8zMHNKm gYjSrk3YJ5w.BypmHh35uDaOGAIRDf6nlCuWYsJXZL.A9W12xqrn9H.sL8XJfAt9XqmvWqnbphvo DhQ7jo9g1Qrhgk_9gnbOWmk.0lHfEA41tIHqkoNBlWcGHAELBtFgxkYVPWkfPD2092K..eul2M8k w7em66QRRHS0s5VYEmMSfwX4ilOfi99.4y2IkQDdmHIQ8WwzSYnWFNpoePGFOg0WxXCeq5bf71GV 4kkKnPPXhkcT12Ko_OoRT8zwUa2XJ2ppErWSDH.uI8Np7YyepA9SQQMe.uQ-- Received: by 98.138.101.180; Wed, 31 Dec 2014 23:11:49 +0000 Date: Wed, 31 Dec 2014 23:11:48 +0000 (UTC) From: Kevin Grittner Reply-To: Kevin Grittner To: Alvaro Herrera Cc: Peter Bailis , "pgsql-bugs@postgresql.org" Message-ID: <2081625385.1952368.1420067509048.JavaMail.yahoo@jws100150.mail.ne1.yahoo.com> In-Reply-To: <20141113175157.GH1791@alvin.alvh.no-ip.org> References: <20141113175157.GH1791@alvin.alvh.no-ip.org> Subject: Re: BUG #11732: Non-serializable outcomes under serializable isolation MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_1952367_1162398396.1420067509047" Content-Length: 9014 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 ------=_Part_1952367_1162398396.1420067509047 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Alvaro Herrera wrote: > Kevin Grittner wrote: >> Everything suggests a race condition. I haven't been able to >> pin down exactly where it happens, but that's just a matter of >> time. It is interesting that the data type of the column >> changes the timing enough to have such a large effect on seeing >> the failure. I've set it aside for the moment but expect to get >> back to it later this week. > > Any luck here? Yes and no. I have further evidence, and have narrowed the area of investigation, but I haven't been able to pin down the cause yet. I'll post what I have "for the record" and in case someone has an idea that will help me find it. I was able to create a bash script and simple plpgsql function to demonstrate the problem. They're short enough to include in the email, but I will attach, too, in case the email formatting mangles it. Bash file "stress": #!/bin/bash pg_ctl -D Debug/data -m fast -w stop rm -fr Debug/data/* initdb Debug/data echo "default_transaction_isolation = 'serializable'" >> Debug/data/postgresql.conf echo "shared_buffers = 1GB" >> Debug/data/postgresql.conf echo "max_connections = 200" >> Debug/data/postgresql.conf pg_ctl -D Debug/data -l Debug/data/logfile -w start || ( cat Debug/data/logfile && exit 1 ) createdb test psql -f ~/stress.sql test psql -c "CREATE TABLE \"stress\" (\"id\" serial primary key, \"key\" text);" test for i in {1..1000} do echo $i for j in {1..128} do psql -c "select stress_fn('$i');" test >/dev/null 2>&1 & done wait sleep 0.3s done psql -c "select count(*) from stress;" test psql -c "select key, count(*) from stress group by key having count(*) > 1 order by key::int;" test "stress.sql" file: CREATE FUNCTION stress_fn(target_key text) RETURNS VOID LANGUAGE plpgsql VOLATILE AS $$ BEGIN PERFORM 1 AS one FROM "stress" WHERE "stress"."key" = target_key; IF FOUND THEN RAISE EXCEPTION 'already inserted'; END IF; INSERT INTO "stress" ("key") VALUES (target_key); END; $$; So far this has generated 1 to 3 duplicated values per run. I have changed the timing to 10.0 seconds and still had an occurrence. Changing the "key" column and the corresponding parameter to integer causes it to run without generating any duplicates (so far, at least). For each non-duplicated "key" value I get about 10 instances of this: ERROR: could not serialize access due to read/write dependencies among transactions DETAIL: Reason code: Canceled on identification as a pivot, during write. HINT: The transaction might succeed if retried. CONTEXT: SQL statement "INSERT INTO "stress" ("key") VALUES (target_key)" PL/pgSQL function stress_fn(text) line 8 at SQL statement STATEMENT: select stress_fn('1'); ... followed by enough of these to add up to 127 errors per value: ERROR: already inserted STATEMENT: select stress_fn('1'); For the "key" values that generate one or more duplicates I get different logging. I see about 30 of these: ERROR: could not serialize access due to read/write dependencies among transactions DETAIL: Reason code: Canceled on conflict out to pivot 9137, during read. HINT: The transaction might succeed if retried. CONTEXT: SQL statement "SELECT 1 AS one FROM "stress" WHERE "stress"."key" = target_key" PL/pgSQL function stress_fn(text) line 3 at PERFORM STATEMENT: select stress_fn('339'); ... before it switches to: ERROR: already inserted STATEMENT: select stress_fn('339'); We used a different reason code in the detail for each spot that could generate a serialization failure based on what serializable transactions add to the checking done for repeatable read transactions. This particular reason code can only be generated if a "dangerous structure" of read-write dependencies is found where the "pivot" has committed -- a fairly rare occurrence. So far in this test, I have not seen that reason code without the duplicates, nor duplicates without that reason code. For those not intimately familiar with our serializable implementation but who want to follow along to some degree or other, the only way that serialization anomalies can occur under snapshot isolation (what we have for repeatable read) is when a "pivot" transaction (Tpivot) has a read-write dependency out to a transaction (Tout) and a read-write dependency in from a transaction (Tin) (in other words, Tout writes something that would have been read by Tpivot if Tout had been committed before Tpivot began, and Tpivot writes something that would have been read by Tin if Tpivot had committed before Tin began) and Tout commits before Tin or Tpivot commit. As a further test to avoid false positives, an anomaly cannot occur if Tin is read-only (or if it commits without modifying data) and Tin started before Tout committed. Just for completeness, Tin and Tout can be the same transaction. So, to hit this code, Tin must be reading data written by Tpivot, Tpivot must have committed (or at least be well into the commit process), and Tout must have committed before Tpivot committed. Tin is executing a SELECT and checks MVCC visibility information of a row and finds that it was written by a transaction which is committed (or at least past the point where it can throw a serialization failure), *and* Tpivot had either read data that had been written by Tout or Tout had written data that predicate locks should show had already been read by Tpivot. Either way, what Tin is seeing should not be possible, because what I just described should have caused a dangerous structure for which should have caused either Tpivot to be canceled. Following from that it appears that the different reason code Tin is seeing is probably a symptom of a failure which already occurred -- otherwise how could both of the other transactions have committed? Since there is no index on the "key" column, we must do a heap scan, and that means a predicate lock on the heap relation. Somehow, two connections get that predicate lock, scan the relation and find that the key value doesn't yet exist, and insert without seeing the other transaction's predicate lock. But I'm not seeing how that could happen. Another possibility is that this is somehow happening as the result of some bug in the "commit sequence number" used by the serializable implementation. In the absence of a real CSN, and to avoid additional contention on the PROCARRAY locking, we created an approximation of a CSN which plays some tricks which assume atomic memory access where perhaps that isn't safe or needs a memory barrier. If neither Tpivot nor Tout sees the other as having committed first, we could see exactly this behavior. Those aren't the only places this race condition bug could be hiding, but they seem like the two most obvious candidates. -- Kevin Grittner EDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company ------=_Part_1952367_1162398396.1420067509047 Content-Type: application/octet-stream Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=stress Content-ID: <48f9c30d-9bb1-3443-0238-3e5647e6b8d4@yahoo.com> IyEvYmluL2Jhc2gKCnBnX2N0bCAtRCBEZWJ1Zy9kYXRhIC1tIGZhc3QgLXcg c3RvcApybSAtZnIgRGVidWcvZGF0YS8qCmluaXRkYiBEZWJ1Zy9kYXRhCmVj aG8gImRlZmF1bHRfdHJhbnNhY3Rpb25faXNvbGF0aW9uID0gJ3NlcmlhbGl6 YWJsZSciID4+IERlYnVnL2RhdGEvcG9zdGdyZXNxbC5jb25mCmVjaG8gInNo YXJlZF9idWZmZXJzID0gMUdCIiA+PiBEZWJ1Zy9kYXRhL3Bvc3RncmVzcWwu Y29uZgplY2hvICJtYXhfY29ubmVjdGlvbnMgPSAyMDAiID4+IERlYnVnL2Rh dGEvcG9zdGdyZXNxbC5jb25mCnBnX2N0bCAtRCBEZWJ1Zy9kYXRhIC1sIERl YnVnL2RhdGEvbG9nZmlsZSAtdyBzdGFydCB8fCAoIGNhdCBEZWJ1Zy9kYXRh L2xvZ2ZpbGUgJiYgZXhpdCAxICkKY3JlYXRlZGIgdGVzdAoKcHNxbCAtZiB+ L3N0cmVzcy5zcWwgdGVzdApwc3FsIC1jICJDUkVBVEUgVEFCTEUgXCJzdHJl c3NcIiAoXCJpZFwiIHNlcmlhbCBwcmltYXJ5IGtleSwgXCJrZXlcIiB0ZXh0 KTsiIHRlc3QKCmZvciBpIGluIHsxLi4xMDAwfQpkbwogIGVjaG8gJGkKICBm b3IgaiBpbiB7MS4uMTI4fQogIGRvCiAgICBwc3FsIC1jICJzZWxlY3Qgc3Ry ZXNzX2ZuKCckaScpOyIgdGVzdCA+L2Rldi9udWxsIDI+JjEgJgogIGRvbmUK ICB3YWl0CiAgc2xlZXAgMC4zcwpkb25lCgpwc3FsIC1jICJzZWxlY3QgY291 bnQoKikgZnJvbSBzdHJlc3M7IiB0ZXN0CnBzcWwgLWMgInNlbGVjdCBrZXks IGNvdW50KCopIGZyb20gc3RyZXNzIGdyb3VwIGJ5IGtleSBoYXZpbmcgY291 bnQoKikgPiAxIG9yZGVyIGJ5IGtleTo6aW50OyIgdGVzdAo= ------=_Part_1952367_1162398396.1420067509047 Content-Type: application/sql Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=stress.sql Content-ID: <2cabec6a-db02-3e6a-515c-be6991f440f9@yahoo.com> Q1JFQVRFIEZVTkNUSU9OIHN0cmVzc19mbih0YXJnZXRfa2V5IHRleHQpCiAg UkVUVVJOUyBWT0lECiAgTEFOR1VBR0UgcGxwZ3NxbAogIFZPTEFUSUxFCkFT ICQkIApCRUdJTiAKICBQRVJGT1JNIDEgQVMgb25lIEZST00gInN0cmVzcyIg V0hFUkUgInN0cmVzcyIuImtleSIgPSB0YXJnZXRfa2V5OwogIElGIEZPVU5E IFRIRU4KICAgIFJBSVNFIEVYQ0VQVElPTiAnYWxyZWFkeSBpbnNlcnRlZCc7 CiAgRU5EIElGOwoKICBJTlNFUlQgSU5UTyAic3RyZXNzIiAoImtleSIpCiAg ICBWQUxVRVMgKHRhcmdldF9rZXkpOwpFTkQ7CiQkOwo= ------=_Part_1952367_1162398396.1420067509047 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_1952367_1162398396.1420067509047--