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 1kqLEs-0001HR-4o for pgsql-general@arkaria.postgresql.org; Fri, 18 Dec 2020 19:16:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kqLEr-0000Cn-3l for pgsql-general@arkaria.postgresql.org; Fri, 18 Dec 2020 19:16:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqLEq-0000CR-SS for pgsql-general@lists.postgresql.org; Fri, 18 Dec 2020 19:16:28 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqLEk-0001j4-3Z for pgsql-general@postgresql.org; Fri, 18 Dec 2020 19:16:27 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 0BIJGJKE167833; Fri, 18 Dec 2020 14:16:19 -0500 From: Tom Lane To: Laurenz Albe cc: Craig McIlwee , pgsql-general@postgresql.org Subject: Re: Unexpected result count from update statement on partitioned table In-reply-to: <5bd2c45ec838ae7bd87e6940b344b5e2638d124d.camel@cybertec.at> References: <5bd2c45ec838ae7bd87e6940b344b5e2638d124d.camel@cybertec.at> Comments: In-reply-to Laurenz Albe message dated "Fri, 18 Dec 2020 10:06:03 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <167831.1608318979.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 18 Dec 2020 14:16:19 -0500 Message-ID: <167832.1608318979@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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. It seems to work as desired if you stick the unstable result into a CTE: =3D# explain with sub as (select id from task_parent where reserved =3D false and task_type =3D 1 or task_type =3D 2 order by task_timestamp limit 50 for update skip locked) update task_parent set reserved =3D true from sub where sub.id =3D task_parent.id returning task_parent.id; QUER= Y PLAN = = --------------------------------------------------------------------------= --------------------------------------------------------------------------= -- Update on task_parent (cost=3D6.30..10069.93 rows=3D100 width=3D57) Update on task_child_1 task_parent_1 Update on task_child_2 task_parent_2 CTE sub -> Limit (cost=3D0.85..4.68 rows=3D50 width=3D26) -> LockRows (cost=3D0.85..38252.82 rows=3D500000 width=3D26) -> Merge Append (cost=3D0.85..33252.82 rows=3D500000 wi= dth=3D26) Sort Key: task_parent_3.task_timestamp -> Index Scan using task_child_1_task_timestamp_id= x on task_child_1 task_parent_4 (cost=3D0.42..14123.60 rows=3D249960 widt= h=3D26) Filter: (((NOT reserved) AND (task_type =3D 1= )) OR (task_type =3D 2)) -> Index Scan using task_child_2_task_timestamp_id= x on task_child_2 task_parent_5 (cost=3D0.42..14129.20 rows=3D250040 widt= h=3D26) Filter: (((NOT reserved) AND (task_type =3D 1= )) OR (task_type =3D 2)) -> Hash Join (cost=3D1.62..5032.07 rows=3D50 width=3D57) Hash Cond: (task_parent_1.id =3D sub.id) -> Seq Scan on task_child_1 task_parent_1 (cost=3D0.00..4092.60= rows=3D249960 width=3D24) -> Hash (cost=3D1.00..1.00 rows=3D50 width=3D40) -> CTE Scan on sub (cost=3D0.00..1.00 rows=3D50 width=3D4= 0) -> Hash Join (cost=3D1.62..5033.18 rows=3D50 width=3D57) Hash Cond: (task_parent_2.id =3D sub.id) -> Seq Scan on task_child_2 task_parent_2 (cost=3D0.00..4093.40= rows=3D250040 width=3D24) -> Hash (cost=3D1.00..1.00 rows=3D50 width=3D40) -> CTE Scan on sub (cost=3D0.00..1.00 rows=3D50 width=3D4= 0) (22 rows) It's been obvious for some time that inheritance_planner() needs to be nuked from orbit, because aside from this fundamental semantic issue it's got horrible performance problems with large inheritance trees (ie many partitions). We might finally get that done for v14 --- at least, there's a patch in the queue about it. In existing releases, I recommend the CTE solution. regards, tom lane