Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kEAUp-0002ip-5a for pgsql-novice@arkaria.postgresql.org; Fri, 04 Sep 2020 12:07:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kEAUo-00013L-4c for pgsql-novice@arkaria.postgresql.org; Fri, 04 Sep 2020 12:07:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kEAUn-00012A-U5 for pgsql-novice@lists.postgresql.org; Fri, 04 Sep 2020 12:07:09 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kEAUj-0002tY-3L for pgsql-novice@lists.postgresql.org; Fri, 04 Sep 2020 12:07:08 +0000 Received: by mail-wr1-x443.google.com with SMTP id w5so6498025wrp.8 for ; Fri, 04 Sep 2020 05:07:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=LH4u8kDKG3VQs5ABWlKV4QVS70jA4wLI3W0SmXF+K3k=; b=A4sHJthsjKHuzbYxAR3XX+U/m9j4lE0XI7nUNONrQZc+xhFdXHCoQZt+BYHa6vWBD+ TKYBGTplQJiW8kqWu5DqCb00et9ykK8NG4Oh/t51EVEz7/sE5LBMq9YGyw3wzjThWJWB hjTsgf7i3ZXEfs6rmJBulvlNulrx3B9Ujjm2fFy1RWufjYRiZDK/QoV0Mk3FA3pUFh1z 6NDkHWFIEdr1Yd8P9/uNU+ZWLExHAdHM+CcnHP9zK9yaWtLGd+WtAxhraBUFN+yirGWL /sMuBLGd+RtOFDx3avaEXbXlKNvH25jtfLJblErTXHgPlcAwYLVtdovMK37J1JpTqlzZ k1bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=LH4u8kDKG3VQs5ABWlKV4QVS70jA4wLI3W0SmXF+K3k=; b=q92N+6DI8MWCrW5wYTalj+H6WNohdYwNM5fabhyS+O7th7U7wqqqNXLbBuyxGV8GMa 8PAeOXs6bRpoMqafLprANJOR9jHVnLj9ovY4jzyybdc/Wpj0CarVCrKbpOVFGuLKw18z MPtL7MPMzIhqSWxwdDGByl2IEeKIAY3gZFYORIxh801XKuIlRz7SnNLz6RxGKtZGIood ph9hGjY1IwEvaHK2oLehPTNWMv/ySAeBNVdNpJkjKvp5Nox2qReapvzue9ZyoeryKC9u GiW1RTVfm2Cxs1EmwLCyLXnsXKMrT4bXWeiRIPO8Vlh1wCCbvn6RS23IJyQihvC9WYfC pypw== X-Gm-Message-State: AOAM532LL2Bi1CDWTBltk1G0yau0Q1CrbOHOJnhOzlVJssmowgEHnNh+ F981bBqX3Ow4Ysl/vZ8yZZjt6A== X-Google-Smtp-Source: ABdhPJzx/s0PhfGZ8nnrL70gtuxofDCUvVyuiQ+M4agNahUnZYNs+/gM5E8oEqRXFhF7CClGUku0jw== X-Received: by 2002:adf:ba83:: with SMTP id p3mr7570825wrg.246.1599221222660; Fri, 04 Sep 2020 05:07:02 -0700 (PDT) Received: from localhost.localdomain (217-149-169-204.nat.highway.telekom.at. [217.149.169.204]) by smtp.gmail.com with ESMTPSA id m125sm11541807wme.35.2020.09.04.05.07.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 05:07:02 -0700 (PDT) Message-ID: Subject: Re: Confusions regards serializable transaction From: Laurenz Albe To: Hannah Huang , pgsql-novice@lists.postgresql.org Date: Fri, 04 Sep 2020 14:07:01 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-1.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 2020-09-04 at 10:20 +1000, Hannah Huang wrote: > I’m confused about what will trigger the serializable error. My test environment is PG v12. > > This is how you can produce the test table: > create table rollover (id int primary key, n int not null); > insert into rollover values (1,100), (2,10); > I then > 1. start serializable transaction A > 2. do an update > 3. start serializable transaction B > 4. Do an update > 5. Commit update of transaction B > 6. Commit update of transaction A. > > I would be expecting an error throw out from transaction A commit as the data has been changed in transaction B, however, both transactions are executed successfully. > > Session A: > > [20:14:59] postgres@pgb : 285650 =# begin isolation level serializable; > BEGIN > [20:15:01] postgres@pgb : 285650 =# update rollover > set n = n + (select n from rollover where id = 2) > where id = 1; > UPDATE 1 > [20:15:06] postgres@pgb : 285650 =# > [20:15:23] postgres@pgb : 285650 =# > [20:15:23] postgres@pgb : 285650 =# commit; > COMMIT > > > > Session B: > > [20:14:57] postgres@pgb : 286411 =# begin isolation level serializable; > BEGIN > [20:15:14] postgres@pgb : 286411 =# update rollover set n = n + 1 where id = 2; > UPDATE 1 > [20:15:17] postgres@pgb : 286411 =# commit; > COMMIT > > I then did a bit of change in the SQL statement executed in both transactions by add a select * from rollover table,the commit of transaction A failed as expected. I don’t know WHY… Even though session A committed after session B, it happens "logically" before B. You could first execute session A, then session B so that they don't overlap, and the result would be the same. That means that there is an equivalent serial execution, so the two transactions are serializable. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com