pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Michael Lewis <mlewis@entrata.com>
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 15:57:45 -0500
Message-ID: <212891.1608325065@sss.pgh.pa.us> (raw)
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>

Michael Lewis <mlewis@entrata.com> writes:
> On Fri, Dec 18, 2020 at 12:16 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Yeah, this is a fairly fundamental shortcoming in inheritance_planner():
>> it supposes that it can duplicate the whole query for each target table.

> Are there other examples of gotchas with this? Would it be any volatile
> function (or behavior like skip locked) in a sub-query?

Right, anything that causes multiple executions to not deliver identical
results.  The different executions will use the same snapshot, so there's
not a hazard from external changes to the DB, but internal sources of
nonrepeatability are a problem.

> ... what is the
> factor that means the sub-query would be executed multiple times?

If it's in the FROM clause of an UPDATE or DELETE on a table with
inheritance children (either traditional inheritance or partitioning).

Actually, after further thought, I'm not entirely sure that the issue is
confined to inherited UPDATE/DELETE.  If you had such a sub-SELECT in
an ordinary join, and the planner chose to put it on the inside of a
nestloop, you'd have a problem.  I do not think there's any check to
avoid doing that just because the subquery's results are potentially
volatile.  Probably evaluation-cost considerations would discourage
such a plan in most cases, but there's no direct defense AFAIR.

> 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?

Exactly.

			regards, tom lane





view thread (7+ messages)

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