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 1gS95T-0007Vg-Fi for pgsql-hackers@arkaria.postgresql.org; Wed, 28 Nov 2018 23:17: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 1gS95R-0000AQ-Vs for pgsql-hackers@arkaria.postgresql.org; Wed, 28 Nov 2018 23:17:41 +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 1gS95R-0000AH-Lk for pgsql-hackers@lists.postgresql.org; Wed, 28 Nov 2018 23:17:41 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gS95O-00074m-46 for pgsql-hackers@lists.postgresql.org; Wed, 28 Nov 2018 23:17:40 +0000 Received: by mail-wm1-x341.google.com with SMTP id r11-v6so372327wmb.2 for ; Wed, 28 Nov 2018 15:17:37 -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=OrFf2PWj8PeZTu0J6g1Nqjuw/HdKBkJIQAFPXLyX2BE=; b=I6fJgtmQE/fqTm+e6S6gSdzFQk7UYrcfMyGbaHSmURD1cbRn8u8SuJlTxCQGBx5Ihv 9vimLqaxmBl34wlWAvXHcud8whux6mY1GF9D1ZhTfPgM5Sfu95z5p94M+j4HshXspnaW 59/pxGXWH9sKomj1ys7fp6IOKLwntAzn015KwqU5W+9/D4jSZRg+wtjqaLJRyh/qGIQM ZYNtAInwxNZ73j35qgcrNbWO4EWReJdY2TQ/jYbqMEuPcf7SB1TMUh4NXcTL4mufgTW9 P8jNts4ihoCRt0h4axKWMJGHgSuGpxz5jD+J6aoeUJaIeR6IKpGgaOKDw9xLpDrEKIY/ pWzg== 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=OrFf2PWj8PeZTu0J6g1Nqjuw/HdKBkJIQAFPXLyX2BE=; b=gU7EuOkn3Xw9iMPIbemtdPuCpwtiBUWDhKjcb8EkOyNAimsBlf9At8EFr9Vnp8biv9 Lcu9nY4PuK62ZNLqRU2Ny0LDTngYUQZI15+HDP32y4aLw1shx9Cg17d4ywNUJ47RfQAM 3BFeFzHBwi+pzPctbVyIkAD6FiMrlCT0Hwl2chCDVV++2eG8eWj9SYuXR8hYcUYYkhGM ckeSg6KEWs62Tz+qWJN+wKe9AsbR7jG1PkK8IAWW3OKXTTMOlEQIKtur/ieYbIe0wP1Q EwXsTy214R8phDaERwmh032Dos+5tG0iZ9M+XpegKNCVsS/ba9yU7o6/JcITY6cMy1tp 6ojg== X-Gm-Message-State: AA+aEWaMU3aaZatP1V9o+Y5hXZGXHjCIVCt9yIXah9I0lBD7YkUIZCJ2 8siGttpODJIuCiiDxzGJLUvRM+fSBv93blEaW7g9Na1ExLSvQkjpKlCMAshrSqjTWcdbVc1xF8X ZWCcyWYqhMFSH5pxfnjj52cH6VWUioljv3Sum8mc4uYdLR/Q796a5qBif877+uIna85UU2L00Vl 38bRclJ9HqdNo4Oly+WjPfuw== X-Google-Smtp-Source: AFSGD/XrmiMhZEdOPs/alAPloAMNug8OB9rxHRZRjdooD8h1xXYExPGKRanj5u+ZR1kc+8/dzKw9aQ== X-Received: by 2002:a1c:544f:: with SMTP id p15mr3372429wmi.37.1543447055223; Wed, 28 Nov 2018 15:17:35 -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 a12sm12831wrm.45.2018.11.28.15.17.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Nov 2018 15:17:34 -0800 (PST) Subject: Re: COPY FROM WHEN condition To: Surafel Temesgen Cc: Adam Berlin , pgsql-hackers@lists.postgresql.org References: <154177868369.24563.8425283889569429535.pgcf@coridan.postgresql.org> <7f9880a1-416e-649d-3a5a-e36bff538814@2ndquadrant.com> <8c9edfdf-6fad-7c68-8969-e1e315baa7a4@2ndquadrant.com> From: Tomas Vondra Message-ID: Date: Thu, 29 Nov 2018 00:17:31 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: 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 11/26/18 2:25 PM, Surafel Temesgen wrote: > > > On Sat, Nov 24, 2018 at 5:09 AM Tomas Vondra > > wrote: > > > 1) While comparing this to the FILTER clause we already have for > aggregates, I've noticed the aggregate version is > >     FILTER '(' WHERE a_expr ')' > > while here we have > >     FILTER '(' a_expr ')' > > For a while I was thinking that maybe we should use the same syntax > here, but I don't think so. The WHERE bit seems rather unnecessary and > we probably implemented it only because it's required by SQL standard, > which does not apply to COPY. So I think this is fine. > >   > ok > > > 2) The various parser checks emit errors like this: > >     case EXPR_KIND_COPY_FILTER: >         err = _("cannot use subquery in copy from FILTER condition"); >         break; > > I think the "copy from" should be capitalized, to make it clear that it > refers to a COPY FROM command and not a copy of something. > > > 3) I think there should be regression tests for these prohibited things, > i.e. for a set-returning function, for a non-existent column, etc. > > > fixed > > > 4) What might be somewhat confusing for users is that the filter uses a > single snapshot to evaluate the conditions for all rows. That is, one > might do this > >     create or replace function f() returns int as $$ >         select count(*)::int from t; >     $$ language sql; > > and hope that > >     copy t from '/...' filter (f() <= 100); > > only ever imports the first 100 rows - but that's not true, of course, > because f() uses the snapshot acquired at the very beginning. For > example INSERT SELECT does behave differently: > >     test=# copy t from '/home/user/t.data' filter (f() < 100); >     COPY 81 >     test=# insert into t select * from t where f() < 100; >     INSERT 0 19 > > Obviously, this is not an issue when the filter clause references only > the input row (which I assume will be the primary use case). > > Not sure if this is expected / appropriate behavior, and if the patch > needs to do something else here. > > Comparing with overhead of setting snapshot before evaluating every row > and considering this > > kind of usage is not frequent it seems to me the behavior is acceptable > I'm not really buying the argument that this behavior is acceptable simply because using the feature like this will be uncommon. That seems like a rather weak reason to accept it. I however agree we don't want to make COPY less efficient, at least not unless absolutely necessary. But I think we can handle this simply by restricting what's allowed to appear the FILTER clause. It should be fine to allow IMMUTABLE and STABLE functions, but not VOLATILE ones. That should fix the example I shared, because f() would not be allowed. We could mark is as STABLE explicitly, which would make it usable in the FILTER clause, but STABLE says "same result for all calls in the statement" so the behavior would be expected and essentially legal (in a way it'd be mislabeling, but we trust it on other places too). So I think there are four options: (a) accept the current behavior (single snapshot, same result for all function calls) (b) prohibit VOLATILE functions in the FILTER clause altogether (c) allow VOLATILE functions in the FILTER clause, but change the behavior to make the behavior sane I definitely vote -1 on (a) unless someone presents a much better argument for allowing it than "it's uncommon". Which leaves us with (b) and (c). Clearly, (b) is simpler to implement, because it (c) needs to do the detection too, and then some additional stuff. I'm not sure how much more complex (c) is, compared to (b). After eyeballing the copy.c code for a bit, I think it would need to use CIM_SINGLE when there are volatile functions, similarly to volatile default values (see volatile_defexprs), and then increment the command ID after each insert. Of course, we don't want to do this by default, but only when actually needed (with VOLATILE functions), because the multi-inserts are quite a bit more efficient. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services