Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gX81x-0001nU-Pb for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Dec 2018 17:10:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gX81u-0008Er-6g for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Dec 2018 17:10:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gX81t-0008DP-VP for pgsql-bugs@lists.postgresql.org; Wed, 12 Dec 2018 17:10:38 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gX81q-0004zR-GK for pgsql-bugs@postgresql.org; Wed, 12 Dec 2018 17:10:37 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id wBCHATjL020130; Wed, 12 Dec 2018 12:10:29 -0500 From: Tom Lane To: Bezverhijs Eduards cc: "pgsql-bugs@postgresql.org" Subject: Re: segmentation fault with simple UPDATE statement (postgres 10.5) In-reply-to: <15361.1544631197@sss.pgh.pa.us> References: <15361.1544631197@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Wed, 12 Dec 2018 11:13:17 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <20128.1544634629.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 12 Dec 2018 12:10:29 -0500 Message-ID: <20129.1544634629@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I wrote: > Bezverhijs Eduards writes: >> We encountered a bug in our systems with update statement, but long sto= ry short, here's the self-containing test case which results in segmentati= on fault. > Huh. I can reproduce this in 9.6 and 10, but not earlier or later > branches. Looking ... Ah, I see the problem: v10 generates a plan like this: regression=3D# explain verbose UPDATE t1 SET (a) =3D (SELECT b FROM t2 WHERE t2.b =3D t1.a) WHERE current_user !=3D 'x'; QUERY PLAN = = --------------------------------------------------------------------------= ----- Update on public.t1 (cost=3D0.01..100995.96 rows=3D2470 width=3D43) -> Result (cost=3D0.01..100995.96 rows=3D2470 width=3D43) Output: $1, ((SubPlan 1 (returns $1))), t1.ctid One-Time Filter: (CURRENT_USER <> 'x'::name) -> Seq Scan on public.t1 (cost=3D0.01..100995.96 rows=3D2470 wi= dth=3D43) Output: $1, (SubPlan 1 (returns $1)), t1.ctid SubPlan 1 (returns $1) -> Seq Scan on public.t2 (cost=3D0.00..40.88 rows=3D12 = width=3D5) Output: t2.b Filter: ((t2.b)::text =3D (t1.a)::text) (10 rows) The implementation of multiassignments assumes that Params referencing the output of a multiassignment subplan will appear in the same targetlist as the SubPlan node for that multiassignment. Here, that's valid in the SeqScan's tlist, but the Result has another occurrence of the same Param, and that one is misplaced. That's because fix_upper_expr_mutator is doing things in the wrong order, causing it to emit a naked Param where it should emit a Var referencing the Param output from the lower plan node. I think it's just accidental that v11 and HEAD don't show the same problem. We've refactored where the main tlist evaluation happens: regression=3D# explain verbose UPDATE t1 SET (a) =3D (SELECT b FROM t2 WHERE t2.b =3D t1.a) WHERE current_user !=3D 'x'; QUERY PLAN = -------------------------------------------------------------------------- Update on public.t1 (cost=3D0.01..100995.96 rows=3D2470 width=3D43) -> Result (cost=3D0.01..100995.96 rows=3D2470 width=3D43) Output: $1, (SubPlan 1 (returns $1)), t1.ctid One-Time Filter: (CURRENT_USER <> 'x'::name) -> Seq Scan on public.t1 (cost=3D0.01..34.70 rows=3D2470 width=3D= 11) Output: t1.a, t1.ctid SubPlan 1 (returns $1) -> Seq Scan on public.t2 (cost=3D0.00..40.88 rows=3D12 width=3D= 5) Output: t2.b Filter: ((t2.b)::text =3D (t1.a)::text) (10 rows) but if there were somehow another plan node in between, it'd be just as broken. The same is true of 9.5. (Note: in these examples, I replaced your constant-true WHERE clause with "current_user !=3D 'x'", because v11+ are smart enough to fold the NOT IN to constant true and then not generate a Result at all, obscuring the issue.) regards, tom lane