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 1lUshf-00087v-Dx for pgsql-general@arkaria.postgresql.org; Fri, 09 Apr 2021 15:05:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lUshe-0002hZ-BZ for pgsql-general@arkaria.postgresql.org; Fri, 09 Apr 2021 15:05:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lUshe-0002hS-0K for pgsql-general@lists.postgresql.org; Fri, 09 Apr 2021 15:05:46 +0000 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lUshZ-0001kB-Jv for pgsql-general@lists.postgresql.org; Fri, 09 Apr 2021 15:05:45 +0000 Received: by mail-pg1-x535.google.com with SMTP id l76so4119684pga.6 for ; Fri, 09 Apr 2021 08:05:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=rJnNk7McwV3P94PUOijjkVQ9jbms/boaaSVIob+lM4U=; b=IGipaQZYse+zLhXrHxIVvvYH8PktvLbLYxP6O8q49UYLn1QcEXbFiHpy6omL//IB4T xjfiXt5ESPSOUkL6CFovYWdpo/Bis2DA1jbZDwwSPsMOPF1n9Y8XEqfXOZ1IUWocz16z lD/0pFTrmCh9FdLeMpCXSZv+SCsoghUhqJV4zCfTz5K7T5D5cjSHcZOKf9+nlTiZXCvT 9kOoJFbEJCMXT1eVe8FJ/BgKHUWNjl9uWZXNNe04Hz34GhnmFOScsekQrIncoQ8NO45D dF3e0j+X2SbZ7KmCGFUdQyyUCThhlvxDrarc5QYpWWq5ZAaazfKcATXikhrwqHGhKs2h QL2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:mime-version:subject:from:in-reply-to :date:cc:content-transfer-encoding:message-id:references:to; bh=rJnNk7McwV3P94PUOijjkVQ9jbms/boaaSVIob+lM4U=; b=hd4dwLDn9UJeE1oKYMeknlG2iyYJv1a9rtfI+66LbL4ghi7JCJSAVHbEOJBey9aofO gmGXFqY3ZPbRp9d81fn+sOBCmtpKKQprouZwL2Qk+qjI1lDR9BQspZp30FaCrG5ZjApV 0b84iqohJVHT3xoYKPI4+vQSwlcXotlQycQsFFUqolQbGkJ6YPJQ4gRjaGrL43pr3GX0 RvB/GnWhi2LhORv2OEuPDMS9oH3K11CefTyeclSn1Sz/4VVtfPCT8PaxxZ8CuutsYy6z ZUoUv4B48gVoLo/Ektm8+wWzRutv1BfZPdELJ0bBlVYAdBvA153HqGK6RJ8IcuJZUb9A iC7Q== X-Gm-Message-State: AOAM530fP73Uf0qw6z3xJTao46roSUiwCuSKPpmQ/ICO38YQn8Q4fwAs rAw/yRl7n2oBhqtMJLJU91A= X-Google-Smtp-Source: ABdhPJyCyHukXXmSKclRR/wX/pGvjtH1C0arTZJurLm1Mu85SX8DITNbjmxwzhT7oNF43P7mNUxm5A== X-Received: by 2002:a05:6a00:7c5:b029:21d:2ea5:a684 with SMTP id n5-20020a056a0007c5b029021d2ea5a684mr12614723pfu.36.1617980739105; Fri, 09 Apr 2021 08:05:39 -0700 (PDT) Received: from glens-imac.lan (67.230.181.248.16clouds.com. [67.230.181.248]) by smtp.gmail.com with ESMTPSA id e3sm2680899pfm.43.2021.04.09.08.05.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 09 Apr 2021 08:05:38 -0700 (PDT) Sender: Glen Huang Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 11.5 \(3445.9.7\)) Subject: Re: Is replacing transactions with CTE a good idea? From: Glen Huang In-Reply-To: <20210405184131.GB12174@momjian.us> Date: Fri, 9 Apr 2021 23:05:34 +0800 Cc: Dave Cramer , "pgsql-generallists.postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: <1637E977-A3B0-4699-B6F6-07E2DDA4F295@gmail.com> References: <7CFF1F8C-E83D-4A68-AD89-6DD3FE2E2493@gmail.com> <20210401193929.GA18029@momjian.us> <20210404131231.GA1730@momjian.us> <20210405181821.GA12174@momjian.us> <20210405184131.GB12174@momjian.us> To: Bruce Momjian X-Mailer: Apple Mail (2.3445.9.7) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This discussion really questioned my understanding of concurrency in = PostgreSQL, thanks a lot. I gave the corresponding part of the doc some more read, and I=E2=80=99m = now in the option that insolation level has no effect on CTEs, but = please correct me if I=E2=80=99m wrong. If notionally all queries execute at the same time, even if they are = executed in read committed, they behave like repeatable read. This = should also be true for serializable, since the anomalies that isolation = level tries to address won=E2=80=99t occur in a CTE. @Bruce The gotchas you mentions are really interesting, I have a follow up = question if you don=E2=80=99t mind: CREATE foo(n int); CREATE bar(n int REFERENCES foo(n)); WITH t AS ( INSERT INTO foo(n) VALUES(1) ) INSERT INTO bar(n) VALUES(1); Is the CTE guaranteed to success or it=E2=80=99s actually unspecified? I = ran it a couple times without issues, but I can=E2=80=99t be sure. If = it=E2=80=99s unspecified any idea how should I correct it? > On Apr 6, 2021, at 2:41 AM, Bruce Momjian wrote: >=20 > On Mon, Apr 5, 2021 at 02:32:36PM -0400, Dave Cramer wrote: >> On Mon, 5 Apr 2021 at 14:18, Bruce Momjian wrote: >> I think we are in agreement. My point was that WITH queries don't = change the >> isolation semantics.=20 >=20 > My point is that when you combine individual queries in a single WITH > query, those queries run together with snaphot behavior as if they = were > in a repeatable-read multi-statement transaction. >=20 > --=20 > Bruce Momjian https://momjian.us > EDB https://enterprisedb.com >=20 > If only the physical world exists, free will is an illusion. >=20