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 1lUstX-0008VP-RC for pgsql-general@arkaria.postgresql.org; Fri, 09 Apr 2021 15:18:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lUstW-0003CV-3s for pgsql-general@arkaria.postgresql.org; Fri, 09 Apr 2021 15:18:02 +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 1lUstV-0003CO-Jx for pgsql-general@lists.postgresql.org; Fri, 09 Apr 2021 15:18:01 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lUstS-0000z1-GK for pgsql-general@lists.postgresql.org; Fri, 09 Apr 2021 15:17:59 +0000 Received: from bruce by momjian.us with local (Exim 4.92) (envelope-from ) id 1lUstQ-0006Pa-AN; Fri, 09 Apr 2021 11:17:56 -0400 Date: Fri, 9 Apr 2021 11:17:56 -0400 From: Bruce Momjian To: Glen Huang Cc: Dave Cramer , "pgsql-generallists.postgresql.org" Subject: Re: Is replacing transactions with CTE a good idea? Message-ID: <20210409151756.GA12920@momjian.us> References: <7CFF1F8C-E83D-4A68-AD89-6DD3FE2E2493@gmail.com> <20210401193929.GA18029@momjian.us> <20210404131231.GA1730@momjian.us> <20210405181821.GA12174@momjian.us> <20210405184131.GB12174@momjian.us> <1637E977-A3B0-4699-B6F6-07E2DDA4F295@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1637E977-A3B0-4699-B6F6-07E2DDA4F295@gmail.com> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Apr 9, 2021 at 11:05:34PM +0800, Glen Huang wrote: > 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’m now > in the option that insolation level has no effect on CTEs, but please > correct me if I’m wrong. Yes, isolation only controls whether a new snapshot is computed _between_ queries in a multi-statement transaction. Single queries always use a single snapshot, except for maintenance commands like VACUUM. > 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’t occur in a CTE. You mean multiple queries in a single CTE, yes, they are like repeatable read. > @Bruce The gotchas you mentions are really interesting, I have a > follow up question if you don’t 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’s actually unspecified? I ran > it a couple times without issues, but I can’t be sure. If it’s > unspecified any idea how should I correct it? Uh, the SELECT manual page explains that non-SELECT queries in a CTE do behave unusually: The primary query and the WITH queries are all (notionally) executed at the same time. This implies that the effects of a data-modifying statement in WITH cannot be seen from other parts of the query, other than by reading its RETURNING output. If two such data-modifying statements attempt to modify the same row, the results are unspecified. and the quoted paragraph suggests that your query should not work. However, you are not referencing the foo table directly, but via referential integrity check, which I guess does work. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com If only the physical world exists, free will is an illusion.