Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gIZKo-0002LZ-5r for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Nov 2018 13:17:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gIZKl-0007kQ-8C for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Nov 2018 13:17:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gIZKk-0007kJ-TR for pgsql-hackers@lists.postgresql.org; Fri, 02 Nov 2018 13:17:55 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gIZKi-0004vF-0h for pgsql-hackers@lists.postgresql.org; Fri, 02 Nov 2018 13:17:53 +0000 Received: by mail-wr1-x442.google.com with SMTP id d17-v6so1938747wre.11 for ; Fri, 02 Nov 2018 06:17:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=26ft5dOT0HUDrbKsJe2S1rDF9GKIVR8UGOaSesK11FM=; b=T/YZiATKcMRohKGXQx8Hz+Au6Vr79i7RbPfnhoMbdnly+Kep+4gzpEv7Zipco2UcFS Xb0tfKEkaZ4f5u1nMm8bYDzUcLEy/sHYK/JWdi2MLWNLXlUqd+VjNUulwdVYKI7+Mrp0 HRC/5XVy412qe32lResO+mNqfLuy0uJ3nD+QxlQM0AsQTbt+tlVwCtd9D8pPngDyV151 N5PC+iIJfx7mDpaZykrd67HQhzAIR+Tbvs2vDA9ijsjauOwHY7oI2KLpYKzgo4jMaRjV wNDupDNyPMKWeNu5hY+dbZUh0DzYnAJyp0xsDOVrF9TKMW9lMRZ5HrNG8N3wgDAf8n0U DLKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=26ft5dOT0HUDrbKsJe2S1rDF9GKIVR8UGOaSesK11FM=; b=NYAsMowGmJyKbB90vYktbKIqE4HJBilaPKRSy2VwqIvAbzXOBSMT21OELzKR1T8DfV R57ACzRG7nnJO1+pE6V5bOWyEeAq7QvYkNTEb613s2EdlyjzERcWgHDSQoabHPirhkMa nqvff6UpZkoJuRVsg/YupOtgPEOz8dTxVk8rHkieESSD3596Yts5thn5Rbl2R4jwmr5+ o1rGVk2xkucnfCnspWLw/uiu824eOY2o6EJXy7b4Vxw70a/C+c4mN5oVIzf5ZUmf6p62 9yy5aMpPq50T4KQJyB/r23GXbnYjOUJZQdcVvDwA4Qgyb0BhGLrcTe3jCPtEFelN18yu vPvg== X-Gm-Message-State: AGRZ1gInkVtfv/lHwS+n57DwVlxc3NoTt1OQIXGrVcpg0squpmmltDHN L6q2WgQTMf09fzEKjVBP2RGPdOJ5vdAQD8DLQgx9ldQy7BEoQDGz6GAvt3JyNzSdVUPvhcTB2qz 7SCrxD1967xR4vcjAXVkR4FmKgbGVgYtrBI8q0AsTXlGK8oh59v/HoF9aGi1TDMxu3ZBWaIniXW 6Y/1qQ2wkhMvMDwaiU2lmB/g== X-Google-Smtp-Source: AJdET5doEkQnxHCseQ8ZYKC1B3gGAd8CswYfiOC6fcOZh24wTm1jEAkSQL+pdL9+v+xECve7g5uKTg== X-Received: by 2002:adf:a512:: with SMTP id i18-v6mr10947508wrb.220.1541164670090; Fri, 02 Nov 2018 06:17:50 -0700 (PDT) Received: from [10.137.2.19] (ip-86-49-245-152.net.upcbroadband.cz. [86.49.245.152]) by smtp.gmail.com with ESMTPSA id 125-v6sm21524096wmy.32.2018.11.02.06.17.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Nov 2018 06:17:49 -0700 (PDT) Subject: Re: COPY FROM WHEN condition To: pgsql-hackers@lists.postgresql.org References: <20181011085925.5zopu7jvxzs5c4xq@squirrel.exwg.net> <20181011153505.GL6157@fetter.org> <5E61B32E-61CB-4EBB-BC8E-BE37DC6E79AB@amazon.com> <20181101004155.GI12677@fetter.org> From: Tomas Vondra Message-ID: <0623219e-c034-bc10-c546-a01da35e6349@2ndquadrant.com> Date: Fri, 2 Nov 2018 14:17:46 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 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/02/2018 03:57 AM, Corey Huinker wrote: > > Are you thinking something like having a COPY command that provides > > results in such a way that they could be referenced in a FROM clause > > (perhaps a COPY that defines a cursor…)? > > That would also be nice, but what I was thinking of was that some > highly restricted subset of cases of SQL in general could lend > themselves to levels of optimization that would be impractical in > other contexts. > > > If COPY (or a syntactical equivalent) can return a result set, then the > whole of SQL is available to filter and aggregate the results and we > don't have to invent new syntax, or endure confusion whenCOPY-WHEN > syntax behaves subtly different from a similar FROM-WHERE. > > Also, what would we be saving computationally? The whole file (or > program output) has to be consumed no matter what, the columns have to > be parsed no matter what. At least some of the columns have to be > converted to their assigned datatypes enough to know whether or not to > filter the row, but we might be able push that logic inside a copy. I'm > thinking of something like this: > > SELECT x.a, sum(x.b) > FROM ( COPY INLINE '/path/to/foo.txt' FORMAT CSV ) as x( a integer, > b numeric, c text, d date, e json) ) > WHERE x.d >= '2018-11-01' > > > In this case, there is the /opportunity/ to see the following optimizations: > - columns c and e are never referenced, and need never be turned into a > datum (though we might do so just to confirm that they conform to the > data type) > - if column d is converted first, we can filter on it and avoid > converting columns a,b > - whatever optimizations we can infer from knowing that the two > surviving columns will go directly into an aggregate > > If we go this route, we can train the planner to notice other > optimizations and add those mechanisms at that time, and then existing > code gets faster. > > If we go the COPY-WHEN route, then we have to make up new syntax for > every possible future optimization. IMHO those two things address vastly different use-cases. The COPY WHEN case deals with filtering data while importing them into a database, while what you're describing seems to be more about querying data stored in a CSV file. But we already do have a solution for that - FDW, and I'd say it's working pretty well. And AFAIK it does give you tools to implement most of what you're asking for. I don't see why should we bolt this on top of COPY, or how is it an alternative to COPY WHEN. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services