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 1glbP6-0007jq-76 for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jan 2019 15:22:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1glbP4-0005T2-Um for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jan 2019 15:22:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1glbP4-0005Sv-5v for pgsql-hackers@lists.postgresql.org; Mon, 21 Jan 2019 15:22:22 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1glbP0-0001P8-1m for pgsql-hackers@lists.postgresql.org; Mon, 21 Jan 2019 15:22:20 +0000 Received: by mail-wr1-x442.google.com with SMTP id v13so23833384wrw.5 for ; Mon, 21 Jan 2019 07:22:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=byccmIIdBTHCHIeWHRXSKDFDokkM4S/dc6svQHHxSdc=; b=tWtaGog/N9FLAQrsMDRMtZNUerncGFMP0b45/4TmB9jD7D812B8TawHflkZeNIjbmT wKwzJAevrbSOid4HkxKPNfbDYvxfENZWR7v3nM6RM28cpibqFiVJru5Uh1gbyiUZaXlC g0taJvKkfeDwuyRPj5lbwkYc8MsDgeGVkPKdkRGdSMyBNXqynkLn+BuNaeIwkDaCK/63 TB5nC6vugtRsVMmykWkvuib0b15FM0ywfUeN+CQOMB3kwjDWJDHuBOFi+WwVZcfCSeDO 6u8ZwrGaAb4O5hMDkJs+9BQOxeGNo6wadF/BDqRGnw7FqOzgzGaX2RCIEUcC7gycm7Qj 8cmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=byccmIIdBTHCHIeWHRXSKDFDokkM4S/dc6svQHHxSdc=; b=Urt04SXcKSX4x/JiM6I8ZclRYz938UfwRxKn+agabBhGRCjQsudDUNHnujIro1HNaA 0nO9jFUK4QD7oC+74qMy2IxBz0Np0c+IiYiZxXsOa6bj/g3yrjADkTGiWdYqTc98RPIt N/CvkiaUBqtxqvCgb+yu33Cz65JzAEiuGuRRWvU7+BXg03sKjowz/+dPxfIhW4swWXYq OutBauuL2LCQA53r99wOafNCHKN11KU9Rdnym+YVAfnCirGCby0hPXl4bgaqltpl78pe x9AoQ55a4+Gjhf0KZwrvJa0JdLLVCrZdP98qHjA7RT+Woq2g3D0RO2BEBlLGqV+c64Aw 0X0Q== X-Gm-Message-State: AJcUuke4dCD3B2sux9MCcKI66vX1OohV3sWFLmq9+uUyPT7gFZcpBL3p d6ejeAUJSIe6mmg4ipSVPrEYdRtmlToWSpFiOXQ1VQLhpEVH9C4BQ4CvI4/2/N4iNje2zpGMMDE SXL3UqFcOf8jYw6DDoaz48bLeYbyzJrWa9mgnUCgsPtEmYRGdDD94yAT5pbH+X6RiPy/+9wGUqn qYldrUve5T+olDXhZbOgY= X-Google-Smtp-Source: ALg8bN5hDeebxW9J48SGs9a2VOVZxlQzUOszYlE1oTbHkTszuXZbRhM44wIK1RxpzOpE5S6ztZRmtA== X-Received: by 2002:adf:83e7:: with SMTP id 94mr28954439wre.278.1548084135858; Mon, 21 Jan 2019 07:22:15 -0800 (PST) Received: from [10.137.2.19] (ip-86-49-251-50.net.upcbroadband.cz. [86.49.251.50]) by smtp.gmail.com with ESMTPSA id v6sm68781039wrd.88.2019.01.21.07.22.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Jan 2019 07:22:14 -0800 (PST) Subject: Re: COPY FROM WHEN condition From: Tomas Vondra To: Andres Freund Cc: Surafel Temesgen , alvherre@2ndquadrant.com, Adam Berlin , pgsql-hackers@lists.postgresql.org References: <194e2225-b186-9325-0cd1-9a5b09d28251@2ndquadrant.com> <20181204094418.wpr6mxrtsuxb5mlq@alvherre.pgsql> <3b2b1aac-5861-acd8-fda3-054c4e4da888@2ndquadrant.com> <20190121012405.633row4iw7nfxz7h@alap3.anarazel.de> <6e80b7c1-58cd-4212-41d3-7c519aed0500@2ndquadrant.com> <20190121020805.3kdscvwzwvijlees@alap3.anarazel.de> <20190121021220.7tbu3oizvydov5by@alap3.anarazel.de> <52b10375-aca3-ea9b-10a1-2e4e9a011665@2ndquadrant.com> Message-ID: Date: Mon, 21 Jan 2019 16:22:11 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <52b10375-aca3-ea9b-10a1-2e4e9a011665@2ndquadrant.com> Content-Type: multipart/mixed; boundary="------------7548CC0579F3EA5066C847FA" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------7548CC0579F3EA5066C847FA Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit On 1/21/19 4:33 AM, Tomas Vondra wrote: > > > On 1/21/19 3:12 AM, Andres Freund wrote: >> On 2019-01-20 18:08:05 -0800, Andres Freund wrote: >>> On 2019-01-20 21:00:21 -0500, Tomas Vondra wrote: >>>> >>>> >>>> On 1/20/19 8:24 PM, Andres Freund wrote: >>>>> Hi, >>>>> >>>>> On 2019-01-20 00:24:05 +0100, Tomas Vondra wrote: >>>>>> On 1/14/19 10:25 PM, Tomas Vondra wrote: >>>>>>> On 12/13/18 8:09 AM, Surafel Temesgen wrote: >>>>>>>> >>>>>>>> >>>>>>>> On Wed, Dec 12, 2018 at 9:28 PM Tomas Vondra >>>>>>>> > wrote: >>>>>>>> >>>>>>>> >>>>>>>> Can you also update the docs to mention that the functions called from >>>>>>>> the WHERE clause does not see effects of the COPY itself? >>>>>>>> >>>>>>>> >>>>>>>> /Of course, i  also add same comment to insertion method selection >>>>>>>> / >>>>>>> >>>>>>> FWIW I've marked this as RFC and plan to get it committed this week. >>>>>>> >>>>>> >>>>>> Pushed, thanks for the patch. >>>>> >>>>> While rebasing the pluggable storage patch ontop of this I noticed that >>>>> the qual appears to be evaluated in query context. Isn't that a bad >>>>> idea? ISMT it should have been evaluated a few lines above, before the: >>>>> >>>>> /* Triggers and stuff need to be invoked in query context. */ >>>>> MemoryContextSwitchTo(oldcontext); >>>>> >>>>> Yes, that'd require moving the ExecStoreHeapTuple(), but that seems ok? >>>>> >>>> >>>> Yes, I agree. It's a bit too late for me to hack and push stuff, but I'll >>>> fix that tomorrow. >>> >>> NP. On second thought, the problem is probably smaller than I thought at >>> first, because ExecQual() switches to the econtext's per-tuple memory >>> context. But it's only reset once for each batch, so there's some >>> wastage. At least worth a comment. >> >> I'm tired, but perhaps its actually worse - what's being reset currently >> is the ESTate's per-tuple context: >> >> if (nBufferedTuples == 0) >> { >> /* >> * Reset the per-tuple exprcontext. We can only do this if the >> * tuple buffer is empty. (Calling the context the per-tuple >> * memory context is a bit of a misnomer now.) >> */ >> ResetPerTupleExprContext(estate); >> } >> >> but the quals are evaluated in the ExprContext's: >> >> ExecQual(ExprState *state, ExprContext *econtext) >> ... >> ret = ExecEvalExprSwitchContext(state, econtext, &isnull); >> >> >> which is created with: >> >> /* Get an EState's per-output-tuple exprcontext, making it if first use */ >> #define GetPerTupleExprContext(estate) \ >> ((estate)->es_per_tuple_exprcontext ? \ >> (estate)->es_per_tuple_exprcontext : \ >> MakePerTupleExprContext(estate)) >> >> and creates its own context: >> /* >> * Create working memory for expression evaluation in this context. >> */ >> econtext->ecxt_per_tuple_memory = >> AllocSetContextCreate(estate->es_query_cxt, >> "ExprContext", >> ALLOCSET_DEFAULT_SIZES); >> >> so this is currently just never reset. > > Actually, no. The ResetPerTupleExprContext boils down to > > MemoryContextReset((econtext)->ecxt_per_tuple_memory) > > and ExecEvalExprSwitchContext does this > > MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory); > > So it's resetting the right context, although only on batch boundary. > But now I see 31f38174 does this: > > else if (cstate->whereClause != NULL || > contain_volatile_functions(cstate->whereClause)) > { > ... > insertMethod = CIM_SINGLE; > } > > so it does not do batching with WHERE. But the condition seems wrong, I > guess it should be && instead of ||. Will investigate in the morning. > I think the condition can be just if (contain_volatile_functions(cstate->whereClause)) { ... } Per the attached patch. Surafel, do you agree? >> Seems just using ExecQualAndReset() ought to be sufficient? >> > > That may still be the right thing to do. > Actually, no, because that would reset the context far too early (and it's easy to trigger segfaults). So the reset would have to happen after processing the row, not this early. But I think the current behavior is actually OK, as it matches what we do for defexprs. And the comment before ResetPerTupleExprContext says this: /* * Reset the per-tuple exprcontext. We can only do this if the * tuple buffer is empty. (Calling the context the per-tuple * memory context is a bit of a misnomer now.) */ So the per-tuple context is not quite per-tuple anyway. Sure, we might rework that but I don't think that's an issue in this patch. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --------------7548CC0579F3EA5066C847FA Content-Type: text/x-patch; name="copy-when-fix.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="copy-when-fix.patch" diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 05d53f96f6..e55b992857 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -2612,8 +2612,7 @@ CopyFrom(CopyState cstate) */ insertMethod = CIM_SINGLE; } - else if (cstate->whereClause != NULL || - contain_volatile_functions(cstate->whereClause)) + else if (contain_volatile_functions(cstate->whereClause)) { /* * Can't support multi-inserts if there are any volatile funcation --------------7548CC0579F3EA5066C847FA--