From: Glen Huang <heyhgl@gmail.com>
To: Bruce Momjian <bruce@momjian.us>
Cc: Dave Cramer <davecramer@postgres.rocks>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Is replacing transactions with CTE a good idea?
Date: Fri, 9 Apr 2021 23:05:34 +0800
Message-ID: <1637E977-A3B0-4699-B6F6-07E2DDA4F295@gmail.com> (raw)
In-Reply-To: <20210405184131.GB12174@momjian.us>
References: <CADK3HHLzbbn0EQg_rgL8iiro-LZT6fG7rHdp_WW5uB36aJKspQ@mail.gmail.com>
<7CFF1F8C-E83D-4A68-AD89-6DD3FE2E2493@gmail.com>
<CADK3HHJ5m_dJsSsW8sLK+M9RfVWV0x42o0r8E4H1O4PtmBemiQ@mail.gmail.com>
<20210401193929.GA18029@momjian.us>
<CADK3HHKcZ09TLNNUvcm_++NwDQeHR=J=Qx=oZwayP5e0ZDweTw@mail.gmail.com>
<20210404131231.GA1730@momjian.us>
<CADK3HHKiQRpzhM70PE0BbCj9Xjoa8B4qkVfQo8G5oUNkwK2sHA@mail.gmail.com>
<20210405181821.GA12174@momjian.us>
<CADK3HHKvOouGTHCAyWc=9cqZnLxj_3_UEqeowmwHGR3Jm1+xXA@mail.gmail.com>
<20210405184131.GB12174@momjian.us>
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.
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.
@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?
> On Apr 6, 2021, at 2:41 AM, Bruce Momjian <bruce@momjian.us> wrote:
>
> On Mon, Apr 5, 2021 at 02:32:36PM -0400, Dave Cramer wrote:
>> On Mon, 5 Apr 2021 at 14:18, Bruce Momjian <bruce@momjian.us> wrote:
>> I think we are in agreement. My point was that WITH queries don't change the
>> isolation semantics.
>
> 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.
>
> --
> Bruce Momjian <bruce@momjian.us> https://momjian.us
> EDB https://enterprisedb.com
>
> If only the physical world exists, free will is an illusion.
>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: heyhgl@gmail.com, bruce@momjian.us, davecramer@postgres.rocks
Subject: Re: Is replacing transactions with CTE a good idea?
In-Reply-To: <1637E977-A3B0-4699-B6F6-07E2DDA4F295@gmail.com>
References: <CADK3HHLzbbn0EQg_rgL8iiro-LZT6fG7rHdp_WW5uB36aJKspQ@mail.gmail.com> <7CFF1F8C-E83D-4A68-AD89-6DD3FE2E2493@gmail.com> <CADK3HHJ5m_dJsSsW8sLK+M9RfVWV0x42o0r8E4H1O4PtmBemiQ@mail.gmail.com> <20210401193929.GA18029@momjian.us> <CADK3HHKcZ09TLNNUvcm_++NwDQeHR=J=Qx=oZwayP5e0ZDweTw@mail.gmail.com> <20210404131231.GA1730@momjian.us> <CADK3HHKiQRpzhM70PE0BbCj9Xjoa8B4qkVfQo8G5oUNkwK2sHA@mail.gmail.com> <20210405181821.GA12174@momjian.us> <CADK3HHKvOouGTHCAyWc=9cqZnLxj_3_UEqeowmwHGR3Jm1+xXA@mail.gmail.com> <20210405184131.GB12174@momjian.us> <1637E977-A3B0-4699-B6F6-07E2DDA4F295@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox