Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y2DOe-00081E-26 for pgsql-hackers@arkaria.postgresql.org; Sat, 20 Dec 2014 06:20:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y2DOc-00018t-PZ for pgsql-hackers@arkaria.postgresql.org; Sat, 20 Dec 2014 06:20:10 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y2DOZ-000170-Nc for pgsql-hackers@postgresql.org; Sat, 20 Dec 2014 06:20:08 +0000 Received: from mail-pd0-x234.google.com ([2607:f8b0:400e:c02::234]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Y2DOV-00008i-R7 for pgsql-hackers@postgresql.org; Sat, 20 Dec 2014 06:20:05 +0000 Received: by mail-pd0-f180.google.com with SMTP id w10so2515757pde.25 for ; Fri, 19 Dec 2014 22:20:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=4NYOqbD2l/P2dGYOPdRIf9ds3kOVoBDCR2oxImzHn9k=; b=TSlFgVtBCU/XO22hz5BAXDX6JHEdr1x0TKP+/LxQMCIaeBwiJ+eawHBpGTykF5kIbq PcDRFaRZiznzeekQfkCpvDW8ICY8UIpsZ5tDyVURF3hMTScoAp14cePULJ9HDBLw8/LF aacyEonV54PsaLbR4zGkZagevVSUC1xywZeyo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-type:content-disposition:in-reply-to :user-agent; bh=4NYOqbD2l/P2dGYOPdRIf9ds3kOVoBDCR2oxImzHn9k=; b=Rvk4ryjBJtlf6Qx8X2uPN02LK7iCHIOcpl3p8cpQNeM4mZTyML9Je07Pc4khcCrixQ y7yTIhIvOPvp25BbU14ZJDCeWBc9isJtpeYMlCKL9gmhojjBUaYDjLXRqchet7yd0sk2 Ez8/JM0u3OFE0OXNkWRkoAPABllW7DlL54mZvhJ9ar2R/BuErZ+zFBsFJybPjukGo582 ZlL8/ZnVnqfQbvcEspNpQDvhfwEdNkmg82EIELQpRowICovg3VxUpATh4PVpMsOpT7Xs 6ku8/0CCek8k9b6/vDkwPNFVgWDNYvdmtUg0g8V3+x2FNN5XrOt1JOs+mOiRSq0Lf0jC An2A== X-Gm-Message-State: ALoCoQlO307WRcfuXko7o5N5zLJm5s0ZpueX4HhN7z7j83/PSPiDiyrRgYGU79uBllWK8OdoXshc X-Received: by 10.66.159.67 with SMTP id xa3mr18483114pab.13.1419056402047; Fri, 19 Dec 2014 22:20:02 -0800 (PST) Received: from tornado.leadboat.com (c-50-131-89-155.hsd1.ca.comcast.net. [50.131.89.155]) by mx.google.com with ESMTPSA id df1sm11371727pbb.2.2014.12.19.22.20.00 (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 19 Dec 2014 22:20:01 -0800 (PST) Date: Sat, 20 Dec 2014 01:19:58 -0500 From: Noah Misch To: dgrowley@gmail.com, Tom Lane Cc: pgsql-hackers@postgresql.org Subject: Re: pgsql: Allow pushdown of WHERE quals into subqueries with window functi Message-ID: <20141220061958.GB1838835@tornado.leadboat.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On Sat, Jun 28, 2014 at 06:08:05AM +0000, Tom Lane wrote: > Allow pushdown of WHERE quals into subqueries with window functions. > > We can allow this even without any specific knowledge of the semantics > of the window function, so long as pushed-down quals will either accept > every row in a given window partition, or reject every such row. Because > window functions act only within a partition, such a case can't result > in changing the window functions' outputs for any surviving row. > Eliminating entire partitions in this way obviously can reduce the cost > of the window-function computations substantially. > > The fly in the ointment is that it's hard to be entirely sure whether > this is true for an arbitrary qual condition. This patch allows pushdown > if (a) the qual references only partitioning columns, and (b) the qual > contains no volatile functions. We are at risk of incorrect results if > the qual can produce different answers for values that the partitioning > equality operator sees as equal. While it's not hard to invent cases > for which that can happen, it seems to seldom be a problem in practice, > since no one has complained about a similar assumption that we've had > for many years with respect to DISTINCT. The potential performance > gains seem to be worth the risk. SQL leaves room for our DISTINCT optimization but not for this. Where pushdown of a qual beneath DISTINCT changes query results, it does so by changing which one of various equal, distinguishable values survives to represent a group. SQL says: If the DISTINCT is specified, and the most specific type of a result column is character string, datetime with time zone or a user-defined type, then the precise values retained in that column after eliminating redundant duplicates is implementation-dependent. PostgreSQL conforms, making qual pushdown one of the factors determining which value to retain. This influences queries having a pushed-down qual that distinguishes equal values. An optimization fence changes the result: SELECT * FROM (SELECT DISTINCT x FROM (VALUES (1.00),(1.1),(1.0)) t1(x) OFFSET 0) t0 WHERE length(x::text) <> 4; x ----- 1.1 (1 row) SELECT * FROM (SELECT DISTINCT x FROM (VALUES (1.00),(1.1),(1.0)) t1(x)) t0 WHERE length(x::text) <> 4; x ----- 1.0 1.1 (2 rows) Type "timetamptz" is unaffected in PostgreSQL. Character strings are affected less than SQL contemplates, because PostgreSQL strings use binary equality. I don't know why SQL omits "numeric" from the list of affected types. Here is the corresponding scenario for window functions: SELECT x, y FROM ( SELECT x, first_value(x) OVER (PARTITION BY x ORDER BY length(x::text)) AS y FROM (VALUES (1.00),(1.0)) t1(x) OFFSET 0 ) t0 WHERE length(x::text) = 4; x | y ------+----- 1.00 | 1.0 (1 row) SELECT x, y FROM ( SELECT x, first_value(x) OVER (PARTITION BY x ORDER BY length(x::text)) AS y FROM (VALUES (1.00),(1.0)) t1(x) ) t0 WHERE length(x::text) = 4; x | y ------+------ 1.00 | 1.00 (1 row) Only the first result is correct. SQL does not give us the wiggle room it offered for DISTINCT. The conformance sacrifice is perhaps worthwhile nonetheless. I didn't see this highlighted in the original discussion, so I wished to ensure this compromise is known explicitly. If the system catalogs recorded whether each equality operator is also an identity operator, we could restrict the optimization to types where the equality operator of the partitioning opclass is so marked. That eliminates the conformance problem. Such an annotation would facilitate other planner advances; for example, predtest.c could attempt to refute f(x) given x = k. Thanks, nm -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers