pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: Glen Huang <heyhgl@gmail.com>
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 11:17:56 -0400
Message-ID: <20210409151756.GA12920@momjian.us> (raw)
In-Reply-To: <1637E977-A3B0-4699-B6F6-07E2DDA4F295@gmail.com>
References: <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>

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  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  If only the physical world exists, free will is an illusion.






view thread (22+ messages)

Message-ID: <20210409151756.GA12920@momjian.us>
Permalink:  ../20210409151756.GA12920@momjian.us/
Also on:    postgresql.org/message-id/20210409151756.GA12920@momjian.us

 · 

reply

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: bruce@momjian.us, heyhgl@gmail.com, davecramer@postgres.rocks
  Subject: Re: Is replacing transactions with CTE a good idea?
  In-Reply-To: <20210409151756.GA12920@momjian.us>
  References: <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> <20210409151756.GA12920@momjian.us>

* 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