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 1glQ4V-0007z8-IQ for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jan 2019 03:16:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1glQ4U-0003w9-AI for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jan 2019 03:16:22 +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 1glQ4T-0003w2-Tn for pgsql-hackers@lists.postgresql.org; Mon, 21 Jan 2019 03:16:22 +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 1glQ4R-0000Ij-1N for pgsql-hackers@lists.postgresql.org; Mon, 21 Jan 2019 03:16:21 +0000 Received: by mail-wm1-x343.google.com with SMTP id y8so9314246wmi.4 for ; Sun, 20 Jan 2019 19:16:18 -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=0cldUd0mt3rthTd7YpmJHXx71iEU95WAJWHguVxUwLw=; b=RXImje0FILm8xLsBlatSN4+xhVI56nRIHSEMIYi4DLxaDATVxRjlV9Ng2568CwlgBT s5H0v6mDIqNsGyzyjfKijzn2gRb1dyPLr5WObuLAhEfqUvaEKg+DZFz9SIUBBJhTJ6WT XdE0BADtGKHQphkjxaGLEJENzIl10y9Gg1sjb5XcMhWd9uwaQNdSI5nXBiuEwbnknCrx rIhycx4radgDAMaJjfwl5eLFj2EERppdIBhjwjAFKle9Jm6RhTiWKriTtHqo324cidvR I4DAgxacWYn6QShlZR80qyXvGVUxBUQ5QWSJHxiPbv6alq1qZIsG1wWhg3AMPpB1pmlc y1Tg== 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=0cldUd0mt3rthTd7YpmJHXx71iEU95WAJWHguVxUwLw=; b=jjMhhEZnw29JBtxTVUzuSndgLT0GCuQ6gxUEy0mcDdVJsEr/qgDcw32VRBGSasHQge 0ZL0VBmJVlDoyl5nrVWvBgCEnVjaQFVGSXopqY7Ke5EcqcP8QFzQto4zVb+oUxUJKs5r oifrwPCA/p2YRbIUqkNfgafLtA+WQd4mRKlnA4OkWxG+zfJQbfRtszp58dJc+JJfWiDc P6w+/FB9xXh6NvNAUgBX5StIawon2BXsWtdtx3y5DtkgOoWqE35/pz4TsDWrkSw4Y7Gy gjS4bHdPOmS5beXMnhlSZLw5Ay/5ssYSty8DODICoLda7XOOeO7YruHCtv4/AAtd9w89 Jc4A== X-Gm-Message-State: AJcUukchUTjFIqSHqlSFgWv13hQ/oLzmQwR0RSnf1BTBB7UOqquTcx+y dh7UQk/9iVwCva4CG9NzZUeGGnS92QnbstxLRwOL91I3rF1QdEjASRYpPgXlsH5NSK+RdtHF8fi yRj173vYusVXvSL3QpCP/cWcocT546TdkmIUQ2JfXnkvgGP8IXjEdXu8Pu5PqX2ZqRfZ2SknJ9E AqPZuCxSTNZQU4mycB6/Q= X-Google-Smtp-Source: ALg8bN4kSBvZj7BR9xCc2KKVP9Fs9F2nmEhSL4E+bAsd2CchfrgKkSUmXCbg3YDRUKZatJSsNxr9Jw== X-Received: by 2002:a1c:3c06:: with SMTP id j6mr21158169wma.27.1548040576358; Sun, 20 Jan 2019 19:16:16 -0800 (PST) Received: from [10.137.2.21] (ip-86-49-251-50.net.upcbroadband.cz. [86.49.251.50]) by smtp.gmail.com with ESMTPSA id o81sm74919473wmd.10.2019.01.20.19.16.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 20 Jan 2019 19:16:15 -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: <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> From: Tomas Vondra Message-ID: Date: Mon, 21 Jan 2019 04:16:13 +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: <20190121021220.7tbu3oizvydov5by@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 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. So context, much simple. Wow. > Seems just using ExecQualAndReset() ought to be sufficient? > Seems like it. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services