pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Lewis <mlewis@entrata.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Laurenz Albe <laurenz.albe@cybertec.at>
Cc: Craig McIlwee <craigm@vt.edu>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Unexpected result count from update statement on partitioned table
Date: Fri, 18 Dec 2020 13:29:15 -0700
Message-ID: <CAHOFxGpFViJVnATuYfaT0-RfconotraJiMPTaUgLQOnUSw-G3A@mail.gmail.com> (raw)
In-Reply-To: <167832.1608318979@sss.pgh.pa.us>
References: <CAGqBcTZJOBpX4kaP-5ySn27=4poFkzm7XN_ZktmuUrbZmwLreg@mail.gmail.com>
	<5bd2c45ec838ae7bd87e6940b344b5e2638d124d.camel@cybertec.at>
	<167832.1608318979@sss.pgh.pa.us>

On Fri, Dec 18, 2020 at 12:16 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Laurenz Albe <laurenz.albe@cybertec.at> writes:
> > The subquery is executed twice, and the two executions obviously don't
> > return the same results.  I am at a loss for an explanation ...
>
> Yeah, this is a fairly fundamental shortcoming in inheritance_planner():
> it supposes that it can duplicate the whole query for each target table.
> If you have a sub-SELECT that generates unstable results, then the
> duplicated copies don't necessarily generate the same results.
> And multiple executions of a sub-SELECT with "for update skip locked"
> are guaranteed to not give the same results, because the second one
> will skip the row(s) already locked by the first one.
>

Are there other examples of gotchas with this? Would it be any volatile
function (or behavior like skip locked) in a sub-query? It isn't apparent
to me why the subquery is executed twice for this example either and since
that is a pre-req for hitting this unexpected situation... what is the
factor that means the sub-query would be executed multiple times?

With the behavior change for CTEs to no longer be materialized by default
in PG12... why does the CTE still mean it is executed only once? Is it
because it is NOT side effect free (locking) so it cannot be in-lined? If
it were a volatile function instead, might we have gotten more than 50 rows
updated?

view thread (7+ messages)  latest in thread

Message-ID: <CAHOFxGpFViJVnATuYfaT0-RfconotraJiMPTaUgLQOnUSw-G3A@mail.gmail.com>
Permalink:  ../CAHOFxGpFViJVnATuYfaT0-RfconotraJiMPTaUgLQOnUSw-G3A@mail.gmail.com/
Also on:    postgresql.org/message-id/CAHOFxGpFViJVnATuYfaT0-RfconotraJiMPTaUgLQOnUSw-G3A@mail.gmail.com

 · 

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: mlewis@entrata.com, tgl@sss.pgh.pa.us, laurenz.albe@cybertec.at, craigm@vt.edu
  Subject: Re: Unexpected result count from update statement on partitioned table
  In-Reply-To: <CAHOFxGpFViJVnATuYfaT0-RfconotraJiMPTaUgLQOnUSw-G3A@mail.gmail.com>
  References: <CAGqBcTZJOBpX4kaP-5ySn27=4poFkzm7XN_ZktmuUrbZmwLreg@mail.gmail.com> <5bd2c45ec838ae7bd87e6940b344b5e2638d124d.camel@cybertec.at> <167832.1608318979@sss.pgh.pa.us> <CAHOFxGpFViJVnATuYfaT0-RfconotraJiMPTaUgLQOnUSw-G3A@mail.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