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 1iXwAH-0004nH-Pd for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Nov 2019 23:47:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iXwAG-0004hN-84 for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Nov 2019 23:47:08 +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 1iXwAF-0004gx-S9 for pgsql-bugs@lists.postgresql.org; Thu, 21 Nov 2019 23:47:08 +0000 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iXwAC-0007dM-GD for pgsql-bugs@lists.postgresql.org; Thu, 21 Nov 2019 23:47:07 +0000 Received: by mail-wm1-x343.google.com with SMTP id y5so5638047wmi.5 for ; Thu, 21 Nov 2019 15:47:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=4qOaXzLSJGhatFWU42g9kG33bnE2qbMiKnAO68hZ6CU=; b=wv0WwqmewpfTw8IGPl+4sRhMd4rUltxyoXY+c4T3RJRsAtVWlF/dYSCdm7HPCqxT9r Kw3auTbqdom11CjARMO2coBf6DJuiW/PJ0rzoFY9vtrGfWGA9KF1SsIDPORQ9zkDv1wD RSPac46zlxhErEfZUVeBeviM1tRIbk193BSzA+yHjFbYQcXkTZppSkh7KyJ3HDdvdab6 trOs4QgUoWsGxCy1Ja9HgYry4rFMYoCtmLKcBTTHwih5J+RrsdbqE8eiyDsJrf81dnSU jt7rKBSJPOAZDaS+COmw5gI2+UEj46GI0RhZg1A3G5P81fB6Tc5icQySL9lTlbt2ej6U 1e0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=4qOaXzLSJGhatFWU42g9kG33bnE2qbMiKnAO68hZ6CU=; b=dlfUWjui26o9DVucoufs95EVJwSYEti3nHYR9/1Rn4IJSLYDr7Y/814Ns47WJpCmYI YoSJr+Ba9V3bcExDq95K2R6oKNMPmUKIY9k/TXoHdypVQq27SHaObp4O9BT7WYqzFAHz vVVlASjMZ0pzeQAeF+QjBa9yJBpDKflzQ13M7VTbMOtz8CodrWtREgGriO4sW01oklO7 HX8UdkYAkFtmBfRLwbvq7GjGdrZFr+aV+krFA+bCAu0xSwpHiyMzaKLkiTSpNckYvMdo R2VsSHBjAB8lbegBCn7CSZBJpHG7DavjrpUEBifrl18gGbXAr+pARV5WMHZ1Ulkffcp0 06yg== X-Gm-Message-State: APjAAAWkvI2JVCLvoBo2Oavj4Lz8Fb/YrsJHR2erJ7VQgbID8wxJSikb o7GKHeHBCrhzl6gddt+u78/Obg== X-Google-Smtp-Source: APXvYqwL1ogAp4S0n0gnoWRGR7bdS1Zyhff7io9T7BB4QlWig5f5DSTcNBW/MHv16C5uDv144GM5lw== X-Received: by 2002:a7b:c857:: with SMTP id c23mr12581051wml.126.1574380023323; Thu, 21 Nov 2019 15:47:03 -0800 (PST) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id x8sm5348398wrm.7.2019.11.21.15.47.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2019 15:47:02 -0800 (PST) Date: Fri, 22 Nov 2019 00:47:01 +0100 From: Tomas Vondra To: Tom Lane Cc: =?utf-8?Q?Ond=C5=99ej?= Jirman , pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16129: Segfault in tts_virtual_materialize in logical replication worker Message-ID: <20191121234701.2wtyc77zd5ux3myf@development> References: <20191121150716.szxqukxrtpjpckm3@core.my.home> <20191121152732.hu6y4wzw7nkxpsvk@development> <20191121155707.pqkgjphh65o5fapz@core.my.home> <20191121161502.xfiy65i3cm6adscr@core.my.home> <20191121165752.dffge6bh756xlfdg@development> <20191121173555.midj6vp4dmiiqs24@core.my.home> <20191121185844.6qsqhwzzuhu3fpsg@core.my.home> <20191121214115.3tgefmvkmrzaim74@development> <20191121220854.ebdzjtloel56sfwu@development> <13122.1574375845@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <13122.1574375845@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Nov 21, 2019 at 05:37:25PM -0500, Tom Lane wrote: >Tomas Vondra writes: >> FWIW my hunch is the bug is somewhere in this chunk of code from >> apply_heap_update: > >> oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); >> ExecCopySlot(remoteslot, localslot); >> slot_modify_cstrings(remoteslot, rel, newtup.values, newtup.changed); >> MemoryContextSwitchTo(oldctx); > >My first reaction to looking at this was that ExecCopySlot() could >be dropped entirely, because the first thing that slot_modify_cstrings >does is > > slot_getallattrs(slot); > ExecClearTuple(slot); > >That slot_getallattrs call seems 100% useless as well, because >surely ExecClearTuple is just going to drop all the data. > I don't think that's quite true. After the ExecCopySlot call, the pass-by-ref Datums in remoteslot will point to a tuple attached to localslot. But it does not pass the tuple 'ownership' to the remoteslot, i.e. the flag TTS_FLAG_SHOULDFREE won't be set, i.e. the tuple won't be freed. >On closer inspection, though, it looks like the author of this >code is relying on ExecClearTuple to *not* destroy the data, >because the subsequent loop might overwrite only some of the >slot's columns, before it does > > ExecStoreVirtualTuple(slot); > >which supposes that all the columns are valid. > >But of course this is 100% broken, because whatever ExecClearTuple >may have done or not done to the slot's datum/isnull arrays, it >certainly pfree'd the slot's physical tuple. So any pass-by-ref >datums are now dangling pointers. > >I imagine the only reason this code has gotten past the valgrind >animals is that we're not testing cases where non-replaced columns >in the subscriber table are of pass-by-ref types. > I haven't checked, but I'd imagine we actually do such tests. I've however tried to reproduce this, unsuccessfully. >I draw a direct line between the existence of this bug and the lack >of commentary in slot_modify_cstrings about what it's doing. If the >author had troubled to write a comment explaining these machinations, >he might've noticed the bug, or at least reviewers might have. > Not sure. More comments would not hurt, but I don't see any bug yet. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services