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 1glhrI-0006um-F4 for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jan 2019 22:15:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1glhrG-00028w-PX for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jan 2019 22:15:54 +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 1glhrG-00028p-Em for pgsql-hackers@lists.postgresql.org; Mon, 21 Jan 2019 22:15:54 +0000 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1glhrC-0002Ud-Gk for pgsql-hackers@lists.postgresql.org; Mon, 21 Jan 2019 22:15:52 +0000 Received: by mail-wr1-x441.google.com with SMTP id t6so25109143wrr.12 for ; Mon, 21 Jan 2019 14:15:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=kkJFzdQRLcBzoUrkpqa8txfWo/zXex9qZe8TEY0v/5w=; b=Ch9Zp1vvKURN5Sc3TfvKKw3K3cvsJYMh6bez6GvooeBvIPvdwqKwgPTTvP12fEhxeO wZPub4cxte+kgsem9jPXcJw+PkShzL6qOmwgducLLKrCSOZJP63h+62Nw0tFh975D2pc dDkYCdeJS71iUGRxJ/Cs/VnHirBr5/ROWTEsHCPC+oxGpyFPZpr8haoIqd1WnGzdNZVO OX1P+4MfwRr6DyBhUPd3ZwKJDcgM/CawlFkeCfKfQ9611aKfh9E83rqi6d9xtTW1qDgK P1v49AQRkmPEF8N1hprpwL6hol0am4O9k4i/OEo7yLuZcUyaB/watOqneMb/Zp5Vpi0S Dz/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=kkJFzdQRLcBzoUrkpqa8txfWo/zXex9qZe8TEY0v/5w=; b=ZuckQ6nD05qfeMkqi4NtUrQLFrwefEEjAKPA0tdFawmpRAy+p89T5Nt6iF2T7dWZ41 56tiX4smR65K7pviaxOBdz+Sy7iLlXqaBp/1pM8DX30jK0GqMOXrQgXMSHmlQdbOVP7z Ei4UUNAsDh3AxX2kKbHexQfDqt0ekGRz2QWHU0DSqgYebkiEe+ac+FkhfxE7qfl8ZS7o f1zX+7YdHOQXijQr3tHwHk5eSSaVGB/0JvSWqoPVzxUp2xE5SbmtUzGVWeWKvEh+zUIC BOOoAW9Paur6snJyj35QLy0+WFyKWK1HWRmH0rcdKtRFsuOtGFC54xoXE6tZE6VP0cQi jr0A== X-Gm-Message-State: AJcUukeSAXiDWzFIAmGBIM27/gX75BGsKf1ZhCJbSVOl65oxEqS59vjF zjbK82RfjDpi9SNBUXjJpWzBACg5bMDN//jWJ6CV/U3PKsjTHmOg6Vbvdfjl6ECXNh3p2vBS2Qe Air1RR/DW5hxySdT5Pn0EMGvsqNJ+02yss2tQ3ym6CwfyLMfByTTWp4Zm/xtFavLwLKu/4N0vZw SqRDuB+5gZjtX3pWw1qYM= X-Google-Smtp-Source: ALg8bN4w8LcJ/baD6ofVhJH/gt+GKa4zi/8gMsR7ZWYBJtRi4yKXbZ66gV815hx9xthkcIgPd52D9Q== X-Received: by 2002:a5d:494a:: with SMTP id r10mr30770273wrs.272.1548108948213; Mon, 21 Jan 2019 14:15:48 -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 t4sm65117687wrb.64.2019.01.21.14.15.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Jan 2019 14:15:47 -0800 (PST) Subject: Re: COPY FROM WHEN condition To: Andres Freund Cc: Surafel Temesgen , alvherre@2ndquadrant.com, Adam Berlin , pgsql-hackers@lists.postgresql.org References: <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> <20190121185102.m7keci23l7q6byou@alap3.anarazel.de> From: Tomas Vondra Message-ID: <0a3d8cff-0b9d-ea2e-634b-5dcb8d0d5766@2ndquadrant.com> Date: Mon, 21 Jan 2019 23:15:45 +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: <20190121185102.m7keci23l7q6byou@alap3.anarazel.de> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/21/19 7:51 PM, Andres Freund wrote: > Hi, > > On 2019-01-21 16:22:11 +0100, Tomas Vondra wrote: >> >> >> 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. > >>>> 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. > > Yea, sorry, I was too tired yesterday evening. I'd spent 10h splitting > up the pluggable storage patch into individual pieces... > > >> 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. > > I'm *not* convinced by this. I think it's bad enough that we do this for > normal COPY, but for WHEN, we could end up *never* resetting before the > end. Consider a case where a single tuple is inserted, and then *all* > rows are filtered. I think this needs a separate econtext that's reset > every round. Or alternatively you could fix the code not to rely on > per-tuple not being reset when tuples are buffered - that actually ought > to be fairly simple. > I think separating the per-tuple and per-batch contexts is the right thing to do, here. It seems the batching was added somewhat later and using the per-tuple context is rather confusing. cheers -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services