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 1gQNP3-0000KD-An for pgsql-hackers@arkaria.postgresql.org; Sat, 24 Nov 2018 02:10:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gQNO3-0005O7-8F for pgsql-hackers@arkaria.postgresql.org; Sat, 24 Nov 2018 02:09:35 +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 1gQNO2-0005O0-Ta for pgsql-hackers@lists.postgresql.org; Sat, 24 Nov 2018 02:09:35 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gQNNz-0007mv-6g for pgsql-hackers@lists.postgresql.org; Sat, 24 Nov 2018 02:09:33 +0000 Received: by mail-wr1-x443.google.com with SMTP id p4so13820553wrt.7 for ; Fri, 23 Nov 2018 18:09:30 -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=r5VstYfP6iyw0ZKX+bPNAYJLECxJhAibWxVGTOBCVcU=; b=i+g0H0rMwvOZ/7/cS1OxrI+7h/NCqUOPHSeZhb/fIHhhvn7c3lf90ndl1eJksw0aMC Lc5zS+uqu6fv6ki/+napjAivUO5LIsgMA23xuPxNcYtWBWjYygmcT7i2m0uAzQc7tDYf NwFz/05DGpN8auaztOW65X+lzD+3w/EYeLWarLSQD0XkKLWzQFxOv8xQNsedfkTo7qJn qRYGeWbYgVOticyyEsVvD1PVMBdtjo8bOShhU3kj2T7bNr4qx5vWp9W75buLDAdy02O2 TNmowNuJ3/rPeITMKnLdYtOnwE5Nr/LaxSmiAMlTXLFgEmXQAmq4YFLZZYg3cHuQpCLG znAA== 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=r5VstYfP6iyw0ZKX+bPNAYJLECxJhAibWxVGTOBCVcU=; b=lB+a1jqGHx6j6asJTmrJ+K16CKcWHApyNv/0GW1Vs2fWcjrchz9jBDn4xwvM2b5E+h uKVXS2eAyK2jNbaPz77P1Qs7FEvslSPaRRS8CZTFEYuFe71guhXuyjLbnFlULqD2OjBl Ff6qM36sef6BmGxyZaTym22L0OwHd9jsLwB0BLAUEHbu23AsMFTxgsZA+aL0ZREJkhD8 LicMuxsUfWuaH8Y2rnHV7q5XT6En7ogrUSe6XU7pnj/QQeBw+gbP2mC4GCMIfOMrz3LK DYh33U5LZuxQDH+zMoy7jZWC3R4UuGYjYL1ahnK6UQG8e6L0+Q+0KlmJu0zI/OokYfQQ sfZQ== X-Gm-Message-State: AA+aEWYNolFxihjqEwOYVTGm4rZDXiyhIcxkfu7sqQEmWkQNNQ6xhdGj zYNczleZdD00E3lLiYtKMpGA9zx0LZ9k8l1AqsGT6cEHSmJES3P15jIhWgxa2cKbbjPxVrqY24E wo+ebgUN1rvSxGeLFDwa1aJtt66ESzJ/X5rLnEY0/yCmQJNT/7673QPbCAbA2LMGxfULfWr1+Dw qHg5Mqx675FXF+UAolx9I5ww== X-Google-Smtp-Source: AFSGD/UP4cunyFxFOtnrSp2A8zgAIgJdPRevaWOkc23xv0rMSnrBRZHbF450bu60W8wRXdrqsQ7qfg== X-Received: by 2002:adf:eec9:: with SMTP id a9mr15599426wrp.242.1543025369329; Fri, 23 Nov 2018 18:09:29 -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 5sm13691876wmw.8.2018.11.23.18.09.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Nov 2018 18:09:28 -0800 (PST) Subject: Re: COPY FROM WHEN condition To: Surafel Temesgen Cc: berlin.ab@gmail.com, pgsql-hackers@lists.postgresql.org References: <154177868369.24563.8425283889569429535.pgcf@coridan.postgresql.org> <7f9880a1-416e-649d-3a5a-e36bff538814@2ndquadrant.com> From: Tomas Vondra Message-ID: <8c9edfdf-6fad-7c68-8969-e1e315baa7a4@2ndquadrant.com> Date: Sat, 24 Nov 2018 03:09:25 +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: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 11/23/18 12:14 PM, Surafel Temesgen wrote: > > > On Sun, Nov 11, 2018 at 11:59 PM Tomas Vondra > > wrote: > > > So, what about using FILTER here? We already use it for aggregates when > filtering rows to process. > > i think its good idea and describe its purpose more. Attache is a > patch that use FILTER instead Thanks, looks good to me. A couple of minor points: 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. 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. 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. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services