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 1f0Z9Y-0003q6-Dg for pgsql-hackers@arkaria.postgresql.org; Mon, 26 Mar 2018 20:55:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f0Z9W-0007Tt-9E for pgsql-hackers@arkaria.postgresql.org; Mon, 26 Mar 2018 20:55:38 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f0Z9V-0007Te-Qh for pgsql-hackers@lists.postgresql.org; Mon, 26 Mar 2018 20:55:38 +0000 Received: from mail-wm0-x22d.google.com ([2a00:1450:400c:c09::22d]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f0Z9R-0000zF-Qb for pgsql-hackers@postgresql.org; Mon, 26 Mar 2018 20:55:36 +0000 Received: by mail-wm0-x22d.google.com with SMTP id r82so18003190wme.0 for ; Mon, 26 Mar 2018 13:55:33 -0700 (PDT) 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=wdN4JWvILi25Fjwy+ohYpcfb7QJThmXpBle0otdOa1E=; b=J6pW7Em8GCZXh/B5KecnflrHNiEqZNpivqF35v261AnPTRNrstgpRDkkDN1rmYiq9e WS8LXorzQv0XjBgrCiSGETnZGwE1R6c8zhjfc5sMwkgFZ+bSy0y0YpQzODbq5TMmICN4 NNcQja1YIhoaZyPYUaaFNKxtNJO5IaPFfom26C5jOUrFiv9QM9x5NIErloaFALLlwV0M /XPm0tJQYGmdUOPIPwV8ABTKHsOYl0C7BpGJePOd9N7YO5OKRuoXoXlje660epkrIFk/ A5N05llXyFz0GvjWpdb/7qmOLFVbMIyYm5hZJqyW1Yf9rFDhzjT8U5DJo4klFufkhhJP TJpw== 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=wdN4JWvILi25Fjwy+ohYpcfb7QJThmXpBle0otdOa1E=; b=cYbbg2LnpcaK1psFXc5BozA9Duu/ZykZN37m4vbnSnfqLUMtau6tgynd1I9+Bu4OoJ /ZAdMo3w1bDqzxT2wWpk67iVzAI+PyxOBUy6uIJv+TX3sYDzmNP3C8MEGSsBOh6iLeOv DjH1Lq90PiivPhKruxkdy8irEQvC43vdFDo6PQwYfdfo/re3QqT2qhKdIVm4skeEog9G 8WhAHByvZvDIMRWTbuGojDs+LdhUtilgxWc1w3TXpR0BCjtWstFZqW2liZaKhHt7FUbm GFXZ7iiT/CUAxowf6cy4P89cedjD27Dcwlatb2SjUDKoNrTK1h6DQqz8vUsqZowwRN4Z IKHA== X-Gm-Message-State: AElRT7HsPeHN/YYX0eoJX7GZV2Zm9dBxghZMiYmsWPYK6r5MIYDEJFKl gbizZL7UoXafxScElL/yf63vV1IXUqXYrWlFTNTDblZfJrixxFfppz18S/ShrO7/fh1iIgn2XMu JBiITAlE/K2e2dZDXerh336oVymOWzXV669AYyFvG7Vpfm06DG/6x20C6s5zsfJ1sv3OunRCuWL n7kR38fRme X-Google-Smtp-Source: AG47ELuOrI33tJH7/hHgrPmiDDY5HbawIYbK5KFzLhJDXRue3OI1Iy3tHICtgijR+Xo0YQSaMAgaGA== X-Received: by 10.28.169.150 with SMTP id s144mr15926988wme.40.1522097731513; Mon, 26 Mar 2018 13:55:31 -0700 (PDT) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id y75sm18158313wme.13.2018.03.26.13.55.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Mar 2018 13:55:26 -0700 (PDT) Subject: Re: Parallel Aggregates for string_agg and array_agg To: Tom Lane , David Rowley Cc: Stephen Frost , Andres Freund , PostgreSQL-development References: <24abe018-b3c6-2117-b5b9-39e660db00bd@2ndquadrant.com> <1b6ee15c-c218-761e-0120-c1ed438e2db2@2ndquadrant.com> <657be73a-a21d-0ae1-5c62-870afbdfddd4@2ndquadrant.com> <20180326022647.GL24540@tamriel.snowman.net> <20180326132009.GN24540@tamriel.snowman.net> <6538.1522096067@sss.pgh.pa.us> From: Tomas Vondra Message-ID: Date: Mon, 26 Mar 2018 22:55:22 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <6538.1522096067@sss.pgh.pa.us> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 03/26/2018 10:27 PM, Tom Lane wrote: > David Rowley writes: >> [ combinefn_for_string_and_array_aggs_v7.patch ] > > I spent a fair amount of time hacking on this with intent to commit, > but just as I was getting to code that I liked, I started to have second > thoughts about whether this is a good idea at all. I quote from the fine > manual: > > The aggregate functions array_agg, json_agg, jsonb_agg, > json_object_agg, jsonb_object_agg, string_agg, and xmlagg, as well as > similar user-defined aggregate functions, produce meaningfully > different result values depending on the order of the input > values. This ordering is unspecified by default, but can be controlled > by writing an ORDER BY clause within the aggregate call, as shown in > Section 4.2.7. Alternatively, supplying the input values from a sorted > subquery will usually work ... > > I do not think it is accidental that these aggregates are exactly the ones > that do not have parallelism support today. Rather, that's because you > just about always have an interest in the order in which the inputs get > aggregated, which is something that parallel aggregation cannot support. > I don't think that's quite true. I know plenty of people who do things like this: SELECT a, b, avg(c), sum(d), array_agg(e), array_agg(f), string_agg(g) FROM hugetable GROUP BY a,b HAVING avg(c) > 100.89; and then do some additional processing on the result in some way (subquery, matview, ...). They don't really care about ordering of values in the arrays, as long as orderings of all the arrays match. Currently queries like this can use parallelism at all, and the patch fixes that I believe. > I fear that what will happen, if we commit this, is that something like > 0.01% of the users of array_agg and string_agg will be pleased, another > maybe 20% will be unaffected because they wrote ORDER BY which prevents > parallel aggregation, and the remaining 80% will scream because we broke > their queries. Telling them they should've written ORDER BY isn't going > to cut it, IMO, when the benefit of that breakage will accrue only to some > very tiny fraction of use-cases. > Isn't the ordering unreliable *already*? It depends on ordering of tuples on the input. So if the table is scanned by index scan or sequential scan, that will affect the array_agg/string_agg results. If the input is a join, it's even more volatile. IMHO it's not like we're making the ordering unpredictable - it's been like that since forever. Also, how is this different from ORDER BY clause? If a user does not specify an ORDER BY clause, I don't think we'd care very much about changes to output ordering due to plan changes, for example. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services