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 1gU7b6-0005vv-5o for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Dec 2018 10:06:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gU7b4-0004ii-F2 for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Dec 2018 10:06:30 +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 1gU7b4-0004hv-4T for pgsql-hackers@lists.postgresql.org; Tue, 04 Dec 2018 10:06:30 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gU7aw-0002s6-6S for pgsql-hackers@lists.postgresql.org; Tue, 04 Dec 2018 10:06:28 +0000 Received: by mail-wm1-x342.google.com with SMTP id a18so8681569wmj.1 for ; Tue, 04 Dec 2018 02:06:21 -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=ouEpnG4oc32RiAM0Docsz+tagQllVKg/ldMxNrTtn0A=; b=arseN3lr+tB5Z8XYGol+ST9fKnXtE0/YTfyW6sLVTw/x/3k3O/vocbXdNIHtgX8yfZ S5gU1tSOgJ0ogyr90FTdpcPUSTS8eW6esDye/z/TZbAWOsz8YsES8gwIoGEvb02n4rKd JHCYVhcushNK2MLPvefehBqxFZw26IM+ddgVD7OfPonFA2CkDaaL+37DRw0QTm+o4g+p MUhD9LeZtrrFBdFZpVzxXzVCDW6fbR1jQ1/1PHi877D8KHrthx8ExqqJz4XwbZ8suVoI aMeCkPfDmxgVp6kg8b3mnhqMHuzXCpSTrKnoiVwbMaiI/GTqYKTqNCK4OaMvQlRS3jax a9mA== 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=ouEpnG4oc32RiAM0Docsz+tagQllVKg/ldMxNrTtn0A=; b=ZtdUMEnz4QFbj4m5JTgCxfdhUMZzvQhDSFIfgdv8cPgERxPSCc/DbJARENhYiEhzyr eJx+vzitADEcICZzekKvm+YMcXh73E8X9+jQvbnHjYDTMl6EfXYF+pgDf39JEuAOsFUO bJYZRYeH2L70mBnJ8juSniIL6hApNdu6c2mPaCd+5yOxqdwmVCZNqs5nSR9S+qZIR4gG AFCcusiMdpmKlWRo7e3kZWGWyAdp2g+X2s9K5xhOTOcCiECBo40MFhikLMRMEi/Ev3Xc NjHaic4vbwmKVI32KdAxkbkJIvt7Qqd1JJEpy+j53ozow/Kx7Z1n7W7LIdKF8oy7pvmP BqKA== X-Gm-Message-State: AA+aEWZoKEjeLWuCoY7vFhwy34abxlkYxY7Rsh6BlweSnKKuop4bE5Jh 8OYVy+1DyPkQ2FUQRQHR1MtuUjascJIH7pwqPcxQPtRv8FgVMIJnVQb5zAMLARyAD3wdluTLKiY 0MWus0gKaaThMkN/cEHqKyqO1agv7uhgabpuQj3wf2rh5/FCHmjTBGY6cYCWmzYcqG1EwbYedTB T7AacNWn8S5TtT+w6seQDTww== X-Google-Smtp-Source: AFSGD/XynKbN9uSdTiof9mOv7mi6HO1wzWRL6SdH2z5z+PWwTwzkceLld57At2n9Rof6g1U4NzFRBg== X-Received: by 2002:a1c:c645:: with SMTP id w66mr12444571wmf.18.1543917979956; Tue, 04 Dec 2018 02:06:19 -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 q9sm15140061wrp.0.2018.12.04.02.06.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Dec 2018 02:06:19 -0800 (PST) Subject: Re: COPY FROM WHEN condition To: Alvaro Herrera Cc: Surafel Temesgen , Adam Berlin , pgsql-hackers@lists.postgresql.org References: <20181204094418.wpr6mxrtsuxb5mlq@alvherre.pgsql> From: Tomas Vondra Message-ID: <4e9c8b43-9f84-37d4-960c-1c4f4ec83c65@2ndquadrant.com> Date: Tue, 4 Dec 2018 11:06:16 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.1 MIME-Version: 1.0 In-Reply-To: <20181204094418.wpr6mxrtsuxb5mlq@alvherre.pgsql> 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 12/4/18 10:44 AM, Alvaro Herrera wrote: > After reading this thread, I think I like WHERE better than FILTER. > Tally: > > WHERE: Adam Berlin, Lim Myungkyu, Dean Rasheed, yours truly > FILTER: Tomas Vondra, Surafel Temesgen > > Couldn't find others expressing an opinion in this regard. > While I still like FILTER more, I won't object to using WHERE if others thinks it's a better choice. > On 2018-Nov-30, Tomas Vondra wrote: > >> I think it should be enough just to switch to CIM_SINGLE and >> increment the command counter after each inserted row. > > Do we apply command counter increment per row with some other COPY > option? I don't think we increment the command counter anywhere, most likely because COPY is not allowed to run any queries directly so far. > Per-row CCI makes me a bit uncomfortable because with you'd get in > trouble with a large copy. I think it's particularly nasty here, > precisely because you may want to filter out some rows of a very > large file, and the CCI may prevent that from working. Sure. > I'm not convinced by the example case of reading how many tuples > you've imported so far in the WHERE/WHEN/FILTER clause each time > (that'd become incrementally slower as it progresses). > Well, not sure how else am I supposed to convince you? It's an example of a behavior that's IMHO surprising and inconsistent with things that might be reasonably expected to behave similarly. It may not be a perfect example, but that's the price for simplicity. FWIW, another way to achieve mostly the same filtering feature is a BEFORE INSERT trigger: create or replace function copy_filter() returns trigger as $$ declare v_c int; begin select count(*) into v_c from t; if v_c >= 100 then return null; end if; return NEW; end; $$ language plpgsql; create trigger filter before insert on t for each row execute procedure copy_filter(); This behaves consistently with INSERT, i.e. it enforces the total count constraint the same way. And the COPY FILTER behaves differently. FWIW I do realize this is not a particularly great check - for example, it will not see effects of concurrent transactions etc. All I'm saying is I find it annoying/strange that it behaves differently. Also, considering the trigger does the right thing, maybe I spoke too early about the command counter not being incremented? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services