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.92) (envelope-from ) id 1jGSCk-0002Ti-3E for pgsql-bugs@arkaria.postgresql.org; Mon, 23 Mar 2020 18:53:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jGSCi-0001KH-Tz for pgsql-bugs@arkaria.postgresql.org; Mon, 23 Mar 2020 18:53:40 +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 1jGSCg-0001K9-Or for pgsql-bugs@lists.postgresql.org; Mon, 23 Mar 2020 18:53:40 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jGSCc-0002f7-Sy for pgsql-bugs@lists.postgresql.org; Mon, 23 Mar 2020 18:53:38 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 637725C022D; Mon, 23 Mar 2020 14:53:32 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Mon, 23 Mar 2020 14:53:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm1; bh=8ncwAq9jJHEp/1E1r6GobCsJKpj SwcM0ZApi90bv+yE=; b=XYV9s7vqleAIsVBaKYthxm4SCtqFaNJcsC81T7aaNzZ hrgkGwdgMML7UBzbeeKo9Eca1a+38i+LoKlVlrTUNKUtFK/BzrD4pKw0+2rkSKky EM9+zawa+aWEcBdZSKKByUGhhTXBwhFDx9UaW1Hu/qlpfSon/MPqg/8rvKVWqCZ3 RI0YzfdNEBJWbzfke4lERdobrZ5rLoxVl+qQ6FWbQEc3MtZRDOnLfVrAwwAQYzn6 hRbKKQCLFO4Vk3KIebSnMfX3Vzw5LLpaSjX5jj/5A5gy/KOSyxGIeH5/ar2Qa2N4 wWuy///z8OZbPPihbBGPOX3bpGyiuhJKcae1DCcp+pA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=8ncwAq 9jJHEp/1E1r6GobCsJKpjSwcM0ZApi90bv+yE=; b=XbJvzh2Us4UKrvNOQG8XTl Myg2MnxFUAIhQbMVPqEu3eTaZRDAO/6QCX2znBROOS+0dU4J/XRbeKyvhlFuUURS Ox8uz00l2xz230qhG2uBG1PjUnl6ILVIYYKATdftZIXm0cbjT7cjtVd3vSqSo2Ni /THhxmJBjNjHCvXBsk8vzfw0WYU6KXR7Ol8v+ELDZFZwiek5I5zAYutDdzdEPCNe JyQzTXTlllzbXcu1Dya9DVCcfLOKgHr2lkRetjoPprGzfXCreQKH6zH8fFUGdOcM GX0wHBE5usw9NAqPXpT71wCv1fAcLGB/CvgfmAmEYLE4f6N+8n8kQEFnlR5uG7TQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrudegkedguddujecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvuffkfhggtggujgesthdtredttddtvdenucfhrhhomheptehnughr vghsucfhrhgvuhhnugcuoegrnhgurhgvshesrghnrghrrgiivghlrdguvgeqnecukfhppe eijedrudeitddrvddujedrvdehtdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgr mhepmhgrihhlfhhrohhmpegrnhgurhgvshesrghnrghrrgiivghlrdguvg X-ME-Proxy: Received: from intern.anarazel.de (c-67-160-217-250.hsd1.ca.comcast.net [67.160.217.250]) by mail.messagingengine.com (Postfix) with ESMTPA id D5DAC328006A; Mon, 23 Mar 2020 14:53:31 -0400 (EDT) Date: Mon, 23 Mar 2020 11:53:30 -0700 From: Andres Freund To: Amit Langote Cc: Tom Lane , Daniel WM , PostgreSQL mailing lists Subject: Re: BUG #16293: postgres segfaults and returns SQLSTATE 08006 Message-ID: <20200323185330.hbcizqtmtrappvoj@alap3.anarazel.de> References: <16293-26f5777d10143a66@postgresql.org> <28203.1583878791@sss.pgh.pa.us> <21088.1584220864@sss.pgh.pa.us> <20200314212657.jnvxfocyd5redf3p@alap3.anarazel.de> <20200315003541.ptc7742cbouixgcl@alap3.anarazel.de> <20200322065217.eme64ycrrwmrjdre@alap3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200322065217.eme64ycrrwmrjdre@alap3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, On 2020-03-21 23:52:17 -0700, Andres Freund wrote: > On 2020-03-17 19:49:43 +0900, Amit Langote wrote: > > On Sun, Mar 15, 2020 at 9:36 AM Andres Freund wrote: > > > I don't think it's ok for ExecConstraint() to overwrite the tuple > > > descriptor of a slot "owned" by nodeResult.c. Am I missing something, or > > > is that broken? > > > > Looking into it, that partitioning code in ExecConstraint() would not > > "normally" overwrite a slot that is not managed by partitioning. > > Normally, there would be a dedicated "partition" slot that would be > > used, but only if tuple conversion from root parent to leaf partition > > is necessary before routing the tuple. > > > What is not "normal" in this case is that tuple conversion is deemed > > necessary when converting from leaf partition to root parent whereas > > not in the other direction. It's because one of the partition's > > attributes has atthasmissing set to true, which triggers this code in > > convert_tuples_by_name(): > > Hm. I don't think we generally reach this path only with a "partition" > slot? I'm looking at 11, for the purpose of this bug. The INSERT case > "normally" is only reached with the slot returned by > ExecPrepareTupleRouting(), true. But ExecConstraint() is also > e.g. called from ExecUpdate() - and as far as I can tell that will be > either the slot from the plan, or the slot from the junkfilter. Ah - it looks like we'll usually not (never?) have to do the conversion for ExecUpdate(), because it'll execute ExecConstraints() with the leaf partition's ResultRelInfo. I'm a bit confused as to what this whole thing is supposed to be doing, tbh. The explanator comment says: /* * If the tuple has been routed, it's been converted to the * partition's rowtype, which might differ from the root * table's. We must convert it back to the root table's * rowtype so that val_desc shown error message matches the * input tuple. */ but it's not clear at all why this is a good thing to be doing. For one, the violation very well might be dependent on the child table's definition. For another, we actually display the child table in the error message: SCHEMA NAME: public TABLE NAME: other_partition CONSTRAINT NAME: dummy_check so it's not clear why we'd want to show the tuple in the "root" format. And lastly, given that we're not actually showing the input tuple (i.e. planSlot), but rather the tuple already modified by default values, triggers, etc, I fail to understand why this is something we should do at all. > > > It seems fairly insane how many places have this approximate code, > > > btw. In 11 we have a copy of nearly the same ~40 lines each in at least > > > ExecPartitionCheckEmitError(), ExecConstraints (twice) and > > > ExecWitchCheckOption(). > > > > That is certainly bad. I think we should get that code in one place > > and inside ExecBuildSlotValueDescription() seems like a good place, > > because that's what needs a converted tuple to build a string from it. > > I have tried that in the attached -- need different patches for > > different branches as there have been a lot of small changes in this > > code over releases. > > That certainly looks better. One thing that concerns me with this change is that now ExecBuildSlotValueDescription() creates a slot each time its called. That's fine and dandy for routines that immediately throw an error, but it's not too hard to imagine ExecBuildSlotValueDescription() being used for other things too. Btw, I don't think this is something we can apply to the back branches without very good reasons - changing ExecBuildSlotValueDescription() could break extensions. I'm only mentioning this because your cleanup patch doesn't seem to be based on HEAD. If we actually want to keep this conversion, I wonder if the right answer would be to change those routines to pass in mtstate->mt_root_tuple_slot instead (and ensure it's created when needed). One way to achieve that would be to move ModifyTableState->mt_root_tuple_slot to ResultRelInfo - and potentially create it on demand? Greetings, Andres Freund