Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqMNs-0004HO-J6 for pgsql-general@arkaria.postgresql.org; Fri, 18 Dec 2020 20:29:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kqMNr-0001ac-DL for pgsql-general@arkaria.postgresql.org; Fri, 18 Dec 2020 20:29:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqMNq-0001aN-Mx for pgsql-general@lists.postgresql.org; Fri, 18 Dec 2020 20:29:51 +0000 Received: from mail-qv1-xf2d.google.com ([2607:f8b0:4864:20::f2d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kqMNj-0004x4-AC for pgsql-general@postgresql.org; Fri, 18 Dec 2020 20:29:49 +0000 Received: by mail-qv1-xf2d.google.com with SMTP id a13so1545013qvv.0 for ; Fri, 18 Dec 2020 12:29:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=entrata.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=evDSv5n2J5fJLtXubBGCmdrmijNBuchEzdpwAkh8/pw=; b=XC7L3wZVdqrO22OclkSYO+Iu2ZmLvTlgJhnnijxOMLQ4kagsDMwZmPHXbiawR+11CI 5N3OyazTthENu6Xc4Y/70H40osSyuwz1wO1KOMH3m1R18F0qE+is1lOODoJ6GS6m0Rul pfT31eyS6t2DbaQsmUi8FPE+nOdKK8zdyYpWYkv66jY3Glz1yKUKiQj2cHlCyK/i0A8L 2u+Tuwt5zXZlBAfg4cMvmOmZo8mIipCAWrC8oK+I9/NJvoINJ4f1JsPtvHd/CgLIAjrO bDjP8oCS566EZLJvLAX04gyU+Wrz++Hy0OFkoSiyoi+iq2lnoX8QnADYFb8wpb085X74 5Huw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=evDSv5n2J5fJLtXubBGCmdrmijNBuchEzdpwAkh8/pw=; b=SJ5mDmEBwV6zAmkPgMbzwB04DufKC1k84i3+Fnf7xKIhWBMqLPmLvjp/yqV0Z1okXo 9KhxyHvL4VOsD6YxN56hEgLt58kvWd+2Uys4AYdfmuz0A9PyJ9mSg7/c/EkOGrhTpvd4 Fg47A/uXIMZtQtAwP5T7DgWKInw9iBH5HN7JfibdODdCIy0ed/+iX48EV6nr2UGy6M5r OgtppPl78IrWcT1vv19GSH0suJ5QlVtoElagqgZ42R3A3ak2pxh60msbylc9jn9VpsLx 3J9L7xyACUArtlO7kLhF8k95nBhzic2b02nDY35m0UXR+jOW80CeIIFbqjl6iUgjptaq H0Zg== X-Gm-Message-State: AOAM532wnv9ZhBWC25Ix+8wuytC3hyTCXL9BmqGwFlZbYZR+6tBkHP1r luX3J/xjfxkjF7bqluJSHoKopHo4NAYFd02tYVLMYQ== X-Google-Smtp-Source: ABdhPJworxsijjBpTOIja0B4FR0C7kT5mexaGxCFu/q723/yWaDY2plUEwSVoSepIt5IW4P2pZOrG2igcoO2hszWVbg= X-Received: by 2002:a0c:fcc5:: with SMTP id i5mr6430266qvq.48.1608323381543; Fri, 18 Dec 2020 12:29:41 -0800 (PST) MIME-Version: 1.0 References: <5bd2c45ec838ae7bd87e6940b344b5e2638d124d.camel@cybertec.at> <167832.1608318979@sss.pgh.pa.us> In-Reply-To: <167832.1608318979@sss.pgh.pa.us> From: Michael Lewis Date: Fri, 18 Dec 2020 13:29:15 -0700 Message-ID: Subject: Re: Unexpected result count from update statement on partitioned table To: Tom Lane Cc: Laurenz Albe , Craig McIlwee , pgsql-general Content-Type: multipart/alternative; boundary="000000000000d7654405b6c2f795" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --000000000000d7654405b6c2f795 Content-Type: text/plain; charset="UTF-8" On Fri, Dec 18, 2020 at 12:16 PM Tom Lane wrote: > Laurenz Albe 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? --000000000000d7654405b6c2f795 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Fri, Dec 18, 2020 at 12:16 PM Tom Lane= <tgl@sss.pgh.pa.us> wrote:<= br>
Laurenz Albe <laurenz.albe@cybertec.at> writes:
> The subquery is executed twice, and the two executions obviously don&#= 39;t
> return the same results.=C2=A0 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&q= uot;
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 a= ny volatile function (or behavior like skip locked) in a sub-query? It isn&= #39;t apparent to me why the subquery is executed twice for this example ei= ther and since that is a pre-req for hitting this unexpected situation... w= hat 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 r= ows updated?
--000000000000d7654405b6c2f795--