pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedIs replacing transactions with CTE a good idea?
22+ messages / 6 participants
[nested] [flat]
* Is replacing transactions with CTE a good idea?
@ 2021-04-01 14:49 Glen Huang <heyhgl@gmail.com>
0 siblings, 2 replies; 22+ messages in thread
From: Glen Huang @ 2021-04-01 14:49 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi all,
From application’s standpoint, it seems using CTE saves a lot work. You no longer need to parse values out only to pass them back in, and only one round-trip to the db server.
If I’m not wrong, CTE is equivalent to serializable transactions? So I guess the downsize is that quarries can’t be run in parallel?
If I decide to replace all my transaction code with CTE, will I shoot myself in the foot down the road?
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 14:58 Brian Dunavant <dunavant@gmail.com>
parent: Glen Huang <heyhgl@gmail.com>
1 sibling, 2 replies; 22+ messages in thread
From: Brian Dunavant @ 2021-04-01 14:58 UTC (permalink / raw)
To: Glen Huang <heyhgl@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
On Thu, Apr 1, 2021 at 10:49 AM Glen Huang <heyhgl@gmail.com> wrote:
> If I decide to replace all my transaction code with CTE, will I shoot
> myself in the foot down the road?
>
I do this all the time and makes code way cleaner. It's very
straightforward with inserts queries. When you deal with updates/deletes,
things can be trickier. I usually leave these in a transaction if there is
any concern.
They can also be hard for future programmers that may not understand SQL.
Make sure you comment your queries for maintainability long term.
I have yet to regret replacing a transaction with a CTE over the past
decade. YMMV
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 15:04 Dave Cramer <davecramer@postgres.rocks>
parent: Glen Huang <heyhgl@gmail.com>
1 sibling, 1 reply; 22+ messages in thread
From: Dave Cramer @ 2021-04-01 15:04 UTC (permalink / raw)
To: Glen Huang <heyhgl@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thu, 1 Apr 2021 at 10:50, Glen Huang <heyhgl@gmail.com> wrote:
> Hi all,
>
> From application’s standpoint, it seems using CTE saves a lot work. You no
> longer need to parse values out only to pass them back in, and only one
> round-trip to the db server.
>
> If I’m not wrong, CTE is equivalent to serializable transactions? So I
> guess the downsize is that quarries can’t be run in parallel?
>
I do not think a CTE changes the isolation level.
>
> If I decide to replace all my transaction code with CTE, will I shoot
> myself in the foot down the road?
>
Dave Cramer
www.postgres.rocks
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 15:04 Rob Sargent <robjsargent@gmail.com>
parent: Brian Dunavant <dunavant@gmail.com>
1 sibling, 1 reply; 22+ messages in thread
From: Rob Sargent @ 2021-04-01 15:04 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 4/1/21 8:58 AM, Brian Dunavant wrote:
> On Thu, Apr 1, 2021 at 10:49 AM Glen Huang <heyhgl@gmail.com
> <mailto:heyhgl@gmail.com>> wrote:
>
> If I decide to replace all my transaction code with CTE, will I
> shoot myself in the foot down the road?
>
>
> I do this all the time and makes code way cleaner. It's very
> straightforward with inserts queries. When you deal with
> updates/deletes, things can be trickier. I usually leave these in a
> transaction if there is any concern.
>
> They can also be hard for future programmers that may not understand
> SQL. Make sure you comment your queries for maintainability long term.
>
> I have yet to regret replacing a transaction with a CTE over the past
> decade. YMMV
This must assume auto-commit mode where every single statement is
"committed"?
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 15:06 Glen Huang <heyhgl@gmail.com>
parent: Brian Dunavant <dunavant@gmail.com>
1 sibling, 1 reply; 22+ messages in thread
From: Glen Huang @ 2021-04-01 15:06 UTC (permalink / raw)
To: Brian Dunavant <dunavant@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
> When you deal with updates/deletes, things can be trickier
Care to expand why they are tricker? I presume they run the risk of being referenced more than once?
> On Apr 1, 2021, at 10:58 PM, Brian Dunavant <dunavant@gmail.com> wrote:
>
>
>> On Thu, Apr 1, 2021 at 10:49 AM Glen Huang <heyhgl@gmail.com> wrote:
>> If I decide to replace all my transaction code with CTE, will I shoot myself in the foot down the road?
>
> I do this all the time and makes code way cleaner. It's very straightforward with inserts queries. When you deal with updates/deletes, things can be trickier. I usually leave these in a transaction if there is any concern.
>
> They can also be hard for future programmers that may not understand SQL. Make sure you comment your queries for maintainability long term.
>
> I have yet to regret replacing a transaction with a CTE over the past decade. YMMV
>
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 15:09 Glen Huang <heyhgl@gmail.com>
parent: Dave Cramer <davecramer@postgres.rocks>
0 siblings, 1 reply; 22+ messages in thread
From: Glen Huang @ 2021-04-01 15:09 UTC (permalink / raw)
To: Dave Cramer <davecramer@postgres.rocks>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
No, but are they equivalent to serializable transactions?
> On Apr 1, 2021, at 11:04 PM, Dave Cramer <davecramer@postgres.rocks> wrote:
>
>
>
>
>
>> On Thu, 1 Apr 2021 at 10:50, Glen Huang <heyhgl@gmail.com> wrote:
>> Hi all,
>>
>> From application’s standpoint, it seems using CTE saves a lot work. You no longer need to parse values out only to pass them back in, and only one round-trip to the db server.
>>
>> If I’m not wrong, CTE is equivalent to serializable transactions? So I guess the downsize is that quarries can’t be run in parallel?
>
> I do not think a CTE changes the isolation level.
>>
>> If I decide to replace all my transaction code with CTE, will I shoot myself in the foot down the road?
>
>
> Dave Cramer
> www.postgres.rocks
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 15:10 Dave Cramer <davecramer@postgres.rocks>
parent: Glen Huang <heyhgl@gmail.com>
0 siblings, 2 replies; 22+ messages in thread
From: Dave Cramer @ 2021-04-01 15:10 UTC (permalink / raw)
To: Glen Huang <heyhgl@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thu, 1 Apr 2021 at 11:09, Glen Huang <heyhgl@gmail.com> wrote:
> No, but are they equivalent to serializable transactions?
>
No, they are not.
Dave Cramer
www.postgres.rocks
>
> On Apr 1, 2021, at 11:04 PM, Dave Cramer <davecramer@postgres.rocks>
> wrote:
>
>
>
>
>
> On Thu, 1 Apr 2021 at 10:50, Glen Huang <heyhgl@gmail.com> wrote:
>
>> Hi all,
>>
>> From application’s standpoint, it seems using CTE saves a lot work. You
>> no longer need to parse values out only to pass them back in, and only one
>> round-trip to the db server.
>>
>> If I’m not wrong, CTE is equivalent to serializable transactions? So I
>> guess the downsize is that quarries can’t be run in parallel?
>>
>
> I do not think a CTE changes the isolation level.
>
>>
>> If I decide to replace all my transaction code with CTE, will I shoot
>> myself in the foot down the road?
>>
>
>
> Dave Cramer
> www.postgres.rocks
>
>
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 15:20 Glen Huang <heyhgl@gmail.com>
parent: Dave Cramer <davecramer@postgres.rocks>
1 sibling, 1 reply; 22+ messages in thread
From: Glen Huang @ 2021-04-01 15:20 UTC (permalink / raw)
To: Dave Cramer <davecramer@postgres.rocks>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Sorry, my mistake. I misunderstood serializable. Are queries in a CTE equivalent to those in a repeatable read transaction?
> On Apr 1, 2021, at 11:10 PM, Dave Cramer <davecramer@postgres.rocks> wrote:
>
>
>
>
>> On Thu, 1 Apr 2021 at 11:09, Glen Huang <heyhgl@gmail.com> wrote:
>> No, but are they equivalent to serializable transactions?
>
> No, they are not.
>
>
>
> Dave Cramer
> www.postgres.rocks
>>
>>>> On Apr 1, 2021, at 11:04 PM, Dave Cramer <davecramer@postgres.rocks> wrote:
>>>>
>>>
>>>
>>>
>>>
>>>> On Thu, 1 Apr 2021 at 10:50, Glen Huang <heyhgl@gmail.com> wrote:
>>>> Hi all,
>>>>
>>>> From application’s standpoint, it seems using CTE saves a lot work. You no longer need to parse values out only to pass them back in, and only one round-trip to the db server.
>>>>
>>>> If I’m not wrong, CTE is equivalent to serializable transactions? So I guess the downsize is that quarries can’t be run in parallel?
>>>
>>> I do not think a CTE changes the isolation level.
>>>>
>>>> If I decide to replace all my transaction code with CTE, will I shoot myself in the foot down the road?
>>>
>>>
>>> Dave Cramer
>>> www.postgres.rocks
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 15:24 Dave Cramer <davecramer@postgres.rocks>
parent: Glen Huang <heyhgl@gmail.com>
0 siblings, 2 replies; 22+ messages in thread
From: Dave Cramer @ 2021-04-01 15:24 UTC (permalink / raw)
To: Glen Huang <heyhgl@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
CTE's don't change the isolation level. I'm not sure what you are getting
at here ?
Dave Cramer
www.postgres.rocks
On Thu, 1 Apr 2021 at 11:20, Glen Huang <heyhgl@gmail.com> wrote:
> Sorry, my mistake. I misunderstood serializable. Are queries in a CTE
> equivalent to those in a repeatable read transaction?
>
> On Apr 1, 2021, at 11:10 PM, Dave Cramer <davecramer@postgres.rocks>
> wrote:
>
>
>
>
> On Thu, 1 Apr 2021 at 11:09, Glen Huang <heyhgl@gmail.com> wrote:
>
>> No, but are they equivalent to serializable transactions?
>>
>
> No, they are not.
>
>
>
> Dave Cramer
> www.postgres.rocks
>
>>
>> On Apr 1, 2021, at 11:04 PM, Dave Cramer <davecramer@postgres.rocks>
>> wrote:
>>
>>
>>
>>
>>
>> On Thu, 1 Apr 2021 at 10:50, Glen Huang <heyhgl@gmail.com> wrote:
>>
>>> Hi all,
>>>
>>> From application’s standpoint, it seems using CTE saves a lot work. You
>>> no longer need to parse values out only to pass them back in, and only one
>>> round-trip to the db server.
>>>
>>> If I’m not wrong, CTE is equivalent to serializable transactions? So I
>>> guess the downsize is that quarries can’t be run in parallel?
>>>
>>
>> I do not think a CTE changes the isolation level.
>>
>>>
>>> If I decide to replace all my transaction code with CTE, will I shoot
>>> myself in the foot down the road?
>>>
>>
>>
>> Dave Cramer
>> www.postgres.rocks
>>
>>
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 15:26 Glen Huang <heyhgl@gmail.com>
parent: Dave Cramer <davecramer@postgres.rocks>
1 sibling, 0 replies; 22+ messages in thread
From: Glen Huang @ 2021-04-01 15:26 UTC (permalink / raw)
To: Dave Cramer <davecramer@postgres.rocks>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Ah, I see what you mean. You still have to wrap a CTE inside a transaction to specify the isolation level? By default, queries in a CTE run with the read committed isolation level?
> On Apr 1, 2021, at 11:10 PM, Dave Cramer <davecramer@postgres.rocks> wrote:
>
>
>
>
>> On Thu, 1 Apr 2021 at 11:09, Glen Huang <heyhgl@gmail.com> wrote:
>> No, but are they equivalent to serializable transactions?
>
> No, they are not.
>
>
>
> Dave Cramer
> www.postgres.rocks
>>
>>>> On Apr 1, 2021, at 11:04 PM, Dave Cramer <davecramer@postgres.rocks> wrote:
>>>>
>>>
>>>
>>>
>>>
>>>> On Thu, 1 Apr 2021 at 10:50, Glen Huang <heyhgl@gmail.com> wrote:
>>>> Hi all,
>>>>
>>>> From application’s standpoint, it seems using CTE saves a lot work. You no longer need to parse values out only to pass them back in, and only one round-trip to the db server.
>>>>
>>>> If I’m not wrong, CTE is equivalent to serializable transactions? So I guess the downsize is that quarries can’t be run in parallel?
>>>
>>> I do not think a CTE changes the isolation level.
>>>>
>>>> If I decide to replace all my transaction code with CTE, will I shoot myself in the foot down the road?
>>>
>>>
>>> Dave Cramer
>>> www.postgres.rocks
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 15:29 Glen Huang <heyhgl@gmail.com>
parent: Dave Cramer <davecramer@postgres.rocks>
1 sibling, 0 replies; 22+ messages in thread
From: Glen Huang @ 2021-04-01 15:29 UTC (permalink / raw)
To: Dave Cramer <davecramer@postgres.rocks>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
I had the impression that since they are chained together, somehow they run “tighter” 😂.
Thanks for pointing out that mistake.
> On Apr 1, 2021, at 11:25 PM, Dave Cramer <davecramer@postgres.rocks> wrote:
>
>
> CTE's don't change the isolation level. I'm not sure what you are getting at here ?
>
> Dave Cramer
> www.postgres.rocks
>
>
>> On Thu, 1 Apr 2021 at 11:20, Glen Huang <heyhgl@gmail.com> wrote:
>> Sorry, my mistake. I misunderstood serializable. Are queries in a CTE equivalent to those in a repeatable read transaction?
>>
>>>> On Apr 1, 2021, at 11:10 PM, Dave Cramer <davecramer@postgres.rocks> wrote:
>>>>
>>>
>>>
>>>
>>>> On Thu, 1 Apr 2021 at 11:09, Glen Huang <heyhgl@gmail.com> wrote:
>>>> No, but are they equivalent to serializable transactions?
>>>
>>> No, they are not.
>>>
>>>
>>>
>>> Dave Cramer
>>> www.postgres.rocks
>>>>
>>>>>> On Apr 1, 2021, at 11:04 PM, Dave Cramer <davecramer@postgres.rocks> wrote:
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>> On Thu, 1 Apr 2021 at 10:50, Glen Huang <heyhgl@gmail.com> wrote:
>>>>>> Hi all,
>>>>>>
>>>>>> From application’s standpoint, it seems using CTE saves a lot work. You no longer need to parse values out only to pass them back in, and only one round-trip to the db server.
>>>>>>
>>>>>> If I’m not wrong, CTE is equivalent to serializable transactions? So I guess the downsize is that quarries can’t be run in parallel?
>>>>>
>>>>> I do not think a CTE changes the isolation level.
>>>>>>
>>>>>> If I decide to replace all my transaction code with CTE, will I shoot myself in the foot down the road?
>>>>>
>>>>>
>>>>> Dave Cramer
>>>>> www.postgres.rocks
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 15:49 Brian Dunavant <dunavant@gmail.com>
parent: Glen Huang <heyhgl@gmail.com>
0 siblings, 0 replies; 22+ messages in thread
From: Brian Dunavant @ 2021-04-01 15:49 UTC (permalink / raw)
To: Glen Huang <heyhgl@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
On Thu, Apr 1, 2021 at 11:06 AM Glen Huang <heyhgl@gmail.com> wrote:
> Care to expand why they are tricker? I presume they run the risk of being
> referenced more than once?
>
There are lots of gotchas. It's also been a few years since I dug deep into
this, so some of this may have changed in more recent versions.
* Changes in a CTE aren't visible to later CTEs since they haven't happened
yet. Often times people are updating a table and then doing further
things and can hit situations they weren't expecting.
db=> create table foo ( a integer primary key );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "foo_pkey"
for table "foo"
CREATE TABLE
db=> insert into foo values ( 1 );
INSERT 0 1
db=> with upt as ( update foo set a = 2 ) insert into foo values (1);
ERROR: duplicate key value violates unique constraint "foo_pkey"
DETAIL: Key (a)=(1) already exists.
* Unless you reference between the CTEs to force ordering, CTEs can happen
in any order, which can cause things to get out of the order people
expected.
* Just like you can cause deadlocks between threads in a transaction, you
can do the same thing by shoving all those statements into a single CTE
query.
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-01 19:39 Bruce Momjian <bruce@momjian.us>
parent: Dave Cramer <davecramer@postgres.rocks>
1 sibling, 1 reply; 22+ messages in thread
From: Bruce Momjian @ 2021-04-01 19:39 UTC (permalink / raw)
To: Dave Cramer <davecramer@postgres.rocks>; +Cc: Glen Huang <heyhgl@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thu, Apr 1, 2021 at 11:24:48AM -0400, Dave Cramer wrote:
> CTE's don't change the isolation level. I'm not sure what you are getting at
> here ?
I think what he/she means here is that all queries in a CTE use a single
snapshot, meaning you don't see changes by commits that happen between
queries that are part of the same CTE. If you were running the queries
separately in read committed mode, you would see those changes, but you
would not see them in repeatable read or serializable transaction mode.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-04 02:06 Ron <ronljohnsonjr@gmail.com>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 0 replies; 22+ messages in thread
From: Ron @ 2021-04-04 02:06 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 4/1/21 10:04 AM, Rob Sargent wrote:
> On 4/1/21 8:58 AM, Brian Dunavant wrote:
>> On Thu, Apr 1, 2021 at 10:49 AM Glen Huang <heyhgl@gmail.com
>> <mailto:heyhgl@gmail.com>> wrote:
>>
>> If I decide to replace all my transaction code with CTE, will I shoot
>> myself in the foot down the road?
>>
>>
>> I do this all the time and makes code way cleaner. It's very
>> straightforward with inserts queries. When you deal with
>> updates/deletes, things can be trickier. I usually leave these in a
>> transaction if there is any concern.
>>
>> They can also be hard for future programmers that may not understand
>> SQL. Make sure you comment your queries for maintainability long term.
>>
>> I have yet to regret replacing a transaction with a CTE over the past
>> decade. YMMV
> This must assume auto-commit mode where every single statement is "committed"?
That's the only way to explain an otherwise very puzzling question. OP must
not do lots of DML in each transaction.
--
Angular momentum makes the world go 'round.
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-04 12:35 Dave Cramer <davecramer@postgres.rocks>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 22+ messages in thread
From: Dave Cramer @ 2021-04-04 12:35 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Glen Huang <heyhgl@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thu, 1 Apr 2021 at 15:39, Bruce Momjian <bruce@momjian.us> wrote:
> On Thu, Apr 1, 2021 at 11:24:48AM -0400, Dave Cramer wrote:
> > CTE's don't change the isolation level. I'm not sure what you are
> getting at
> > here ?
>
> I think what he/she means here is that all queries in a CTE use a single
> snapshot, meaning you don't see changes by commits that happen between
> queries that are part of the same CTE. If you were running the queries
> separately in read committed mode, you would see those changes, but you
> would not see them in repeatable read or serializable transaction mode.
>
OK, that makes sense, but I think it is wrong minded to think that this
absolves one of taking isolation into account.
When you make the first read you will still have to deal with all of the
isolation issues
Dave Cramer
www.postgres.rocks
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-04 13:12 Bruce Momjian <bruce@momjian.us>
parent: Dave Cramer <davecramer@postgres.rocks>
0 siblings, 1 reply; 22+ messages in thread
From: Bruce Momjian @ 2021-04-04 13:12 UTC (permalink / raw)
To: Dave Cramer <davecramer@postgres.rocks>; +Cc: Glen Huang <heyhgl@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Sun, Apr 4, 2021 at 08:35:41AM -0400, Dave Cramer wrote:
>
>
> On Thu, 1 Apr 2021 at 15:39, Bruce Momjian <bruce@momjian.us> wrote:
>
> On Thu, Apr 1, 2021 at 11:24:48AM -0400, Dave Cramer wrote:
> > CTE's don't change the isolation level. I'm not sure what you are getting
> at
> > here ?
>
> I think what he/she means here is that all queries in a CTE use a single
> snapshot, meaning you don't see changes by commits that happen between
> queries that are part of the same CTE. If you were running the queries
> separately in read committed mode, you would see those changes, but you
> would not see them in repeatable read or serializable transaction mode.
>
> OK, that makes sense, but I think it is wrong minded to think that this
> absolves one of taking isolation into account.
>
> When you make the first read you will still have to deal with all of the
> isolation issues
I have no idea what you are saying above. Why is a SELECT-only CTE not
the same as a repeatable-read SELECT-only multi-statement transaction?
Are you saying that a SELECT in a CTE doesn't do SELECT FOR UPDATE?
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-04 14:02 Dave Cramer <davecramer@postgres.rocks>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 22+ messages in thread
From: Dave Cramer @ 2021-04-04 14:02 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Glen Huang <heyhgl@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Sun, 4 Apr 2021 at 09:12, Bruce Momjian <bruce@momjian.us> wrote:
> On Sun, Apr 4, 2021 at 08:35:41AM -0400, Dave Cramer wrote:
> >
> >
> > On Thu, 1 Apr 2021 at 15:39, Bruce Momjian <bruce@momjian.us> wrote:
> >
> > On Thu, Apr 1, 2021 at 11:24:48AM -0400, Dave Cramer wrote:
> > > CTE's don't change the isolation level. I'm not sure what you are
> getting
> > at
> > > here ?
> >
> > I think what he/she means here is that all queries in a CTE use a
> single
> > snapshot, meaning you don't see changes by commits that happen
> between
> > queries that are part of the same CTE. If you were running the
> queries
> > separately in read committed mode, you would see those changes, but
> you
> > would not see them in repeatable read or serializable transaction
> mode.
> >
> > OK, that makes sense, but I think it is wrong minded to think that this
> > absolves one of taking isolation into account.
> >
> > When you make the first read you will still have to deal with all of the
> > isolation issues
>
> I have no idea what you are saying above. Why is a SELECT-only CTE not
> the same as a repeatable-read SELECT-only multi-statement transaction?
> Are you saying that a SELECT in a CTE doesn't do SELECT FOR UPDATE?
No, but where is this documented ?
Dave Cramer
www.postgres.rocks
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-05 18:18 Bruce Momjian <bruce@momjian.us>
parent: Dave Cramer <davecramer@postgres.rocks>
0 siblings, 1 reply; 22+ messages in thread
From: Bruce Momjian @ 2021-04-05 18:18 UTC (permalink / raw)
To: Dave Cramer <davecramer@postgres.rocks>; +Cc: Glen Huang <heyhgl@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Sun, Apr 4, 2021 at 10:02:20AM -0400, Dave Cramer wrote:
> On Sun, 4 Apr 2021 at 09:12, Bruce Momjian <bruce@momjian.us> wrote:
> > OK, that makes sense, but I think it is wrong minded to think that this
> > absolves one of taking isolation into account.
> >
> > When you make the first read you will still have to deal with all of the
> > isolation issues
>
> I have no idea what you are saying above. Why is a SELECT-only CTE not
> the same as a repeatable-read SELECT-only multi-statement transaction?
> Are you saying that a SELECT in a CTE doesn't do SELECT FOR UPDATE?
>
>
> No, but where is this documented ?
Well, every query runs with a single snapshot, even WITH queries. We do
document how non-SELECT WITH visibility is handled:
https://www.postgresql.org/docs/13/sql-select.html
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.
A key property of WITH queries is that they are normally evaluated only
once per execution of the primary query, even if the primary query
refers to them more than once. In particular, data-modifying statements
are guaranteed to be executed once and only once, regardless of whether
the primary query reads all or any of their output.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-05 18:32 Dave Cramer <davecramer@postgres.rocks>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 22+ messages in thread
From: Dave Cramer @ 2021-04-05 18:32 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Glen Huang <heyhgl@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Mon, 5 Apr 2021 at 14:18, Bruce Momjian <bruce@momjian.us> wrote:
> On Sun, Apr 4, 2021 at 10:02:20AM -0400, Dave Cramer wrote:
> > On Sun, 4 Apr 2021 at 09:12, Bruce Momjian <bruce@momjian.us> wrote:
> > > OK, that makes sense, but I think it is wrong minded to think that
> this
> > > absolves one of taking isolation into account.
> > >
> > > When you make the first read you will still have to deal with all
> of the
> > > isolation issues
> >
> > I have no idea what you are saying above. Why is a SELECT-only CTE
> not
> > the same as a repeatable-read SELECT-only multi-statement
> transaction?
> > Are you saying that a SELECT in a CTE doesn't do SELECT FOR UPDATE?
> >
> >
> > No, but where is this documented ?
>
> Well, every query runs with a single snapshot, even WITH queries. We do
> document how non-SELECT WITH visibility is handled:
>
> https://www.postgresql.org/docs/13/sql-select.html
>
> 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.
>
> A key property of WITH queries is that they are normally evaluated
> only
> once per execution of the primary query, even if the primary query
> refers to them more than once. In particular, data-modifying
> statements
> are guaranteed to be executed once and only once, regardless of
> whether
> the primary query reads all or any of their output.
>
>
I think we are in agreement. My point was that WITH queries don't change
the isolation semantics.
I was pretty sure we didn't do a SELECT FOR UPDATE which would imply a lock.
Dave Cramer
www.postgres.rocks
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-05 18:41 Bruce Momjian <bruce@momjian.us>
parent: Dave Cramer <davecramer@postgres.rocks>
0 siblings, 1 reply; 22+ messages in thread
From: Bruce Momjian @ 2021-04-05 18:41 UTC (permalink / raw)
To: Dave Cramer <davecramer@postgres.rocks>; +Cc: Glen Huang <heyhgl@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
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.
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-09 15:05 Glen Huang <heyhgl@gmail.com>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 22+ messages in thread
From: Glen Huang @ 2021-04-09 15:05 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Dave Cramer <davecramer@postgres.rocks>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
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.
>
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is replacing transactions with CTE a good idea?
@ 2021-04-09 15:17 Bruce Momjian <bruce@momjian.us>
parent: Glen Huang <heyhgl@gmail.com>
0 siblings, 0 replies; 22+ messages in thread
From: Bruce Momjian @ 2021-04-09 15:17 UTC (permalink / raw)
To: Glen Huang <heyhgl@gmail.com>; +Cc: Dave Cramer <davecramer@postgres.rocks>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
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.
^ permalink raw reply [nested|flat] 22+ messages in thread
end of thread, other threads:[~2021-04-09 15:17 UTC | newest]
Thread overview: 22+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-04-01 14:49 Is replacing transactions with CTE a good idea? Glen Huang <heyhgl@gmail.com>
2021-04-01 14:58 ` Brian Dunavant <dunavant@gmail.com>
2021-04-01 15:04 ` Rob Sargent <robjsargent@gmail.com>
2021-04-04 02:06 ` Ron <ronljohnsonjr@gmail.com>
2021-04-01 15:06 ` Glen Huang <heyhgl@gmail.com>
2021-04-01 15:49 ` Brian Dunavant <dunavant@gmail.com>
2021-04-01 15:04 ` Dave Cramer <davecramer@postgres.rocks>
2021-04-01 15:09 ` Glen Huang <heyhgl@gmail.com>
2021-04-01 15:10 ` Dave Cramer <davecramer@postgres.rocks>
2021-04-01 15:20 ` Glen Huang <heyhgl@gmail.com>
2021-04-01 15:24 ` Dave Cramer <davecramer@postgres.rocks>
2021-04-01 15:29 ` Glen Huang <heyhgl@gmail.com>
2021-04-01 19:39 ` Bruce Momjian <bruce@momjian.us>
2021-04-04 12:35 ` Dave Cramer <davecramer@postgres.rocks>
2021-04-04 13:12 ` Bruce Momjian <bruce@momjian.us>
2021-04-04 14:02 ` Dave Cramer <davecramer@postgres.rocks>
2021-04-05 18:18 ` Bruce Momjian <bruce@momjian.us>
2021-04-05 18:32 ` Dave Cramer <davecramer@postgres.rocks>
2021-04-05 18:41 ` Bruce Momjian <bruce@momjian.us>
2021-04-09 15:05 ` Glen Huang <heyhgl@gmail.com>
2021-04-09 15:17 ` Bruce Momjian <bruce@momjian.us>
2021-04-01 15:26 ` Glen Huang <heyhgl@gmail.com>
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