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 1f0aO0-0008EH-Lv for pgsql-hackers@arkaria.postgresql.org; Mon, 26 Mar 2018 22:14: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 1f0aNy-0008BT-MR for pgsql-hackers@arkaria.postgresql.org; Mon, 26 Mar 2018 22:14: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 1f0aNy-0008BJ-C6 for pgsql-hackers@lists.postgresql.org; Mon, 26 Mar 2018 22:14:38 +0000 Received: from mail-wr0-x22f.google.com ([2a00:1450:400c:c0c::22f]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f0aNu-0002tz-C1 for pgsql-hackers@postgresql.org; Mon, 26 Mar 2018 22:14:37 +0000 Received: by mail-wr0-x22f.google.com with SMTP id p53so12887766wrc.10 for ; Mon, 26 Mar 2018 15:14:34 -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=MAE3oVysi6kOsF9Ky4n+1aJG7Eh7n+QMV2upPrZlzRw=; b=vU9akWq1yPOnEmxnfh3UyUjVYswCk1zbJvXip9dkN9M7dHfedqeq7bT879nMKx0kMz sUlW3S9jBT2waExq7VgG3ytEPffj3XyJYoQfZsPq2+LB1VACYCSiW/XcXxl4xNgIg3fl lrbQ1C7c3bqHtLlxhhwbqU6EWYYZdR/haQYL02aJ5K7ETukNTF6dg6MN6Rm0y8Gl9Uvk Q875hbye/5jEBb/yKUoK+b6IW7Y2YpBzfBjF4/s3V3ZnAmtg+BBxxfOFpCNKf8QqeHfJ w+38VtCju398RMRDlQ4IzyB/017j8fAoGpdb2as7bxQQ+LgQWee42VnWQJpLK9oNLqh2 C45Q== 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=MAE3oVysi6kOsF9Ky4n+1aJG7Eh7n+QMV2upPrZlzRw=; b=Me5YhNASp1KGcqH92KnIOFXK+2xv1rk5P15uqDDJCMVG2a8wtEA3lgCPyIALqak8yL K+oLLODPAuGqAq1H7iRiKUbR50iGtgiMEdFFJhU/qKU8mbAQ7W8vJmKAgqZtCAG+ZYdw MRpqGCFqQed1k1Q0RjghIreCPmU8KsGtU88BPOKsbE0hBvVH73brGGhqLRU5Tvx/THB7 ECck0dJF6/QaXTxNw43ly2mooMdEELsxglh0GmvRXFxOJTb1IHh+/hFIJrgnWvJfl8fc P6ur7K8rIKH3arhc4U/Jh8jegP8znSVbG4NdkhQOdZOVUbZ0o8t0NfjVGGAmAPNE6SVi VsQw== X-Gm-Message-State: AElRT7F+omR6n5U9DDQgkoUdu5vMo2+3QeokfHJABqlz25ccox4G8BXu RtwHaRavaPx0OYRIGHBBu2xlow/sLXDsTtIDLApPUkx5T65aDRGSjBUYcuF8K5tUOSU1xVZRxAU EGRGEs/AiScb+up3uLfHNioa0dm1b7Fnzarhd8sXKXJNcE4aE8XOPt4xdJuSjywTYlPfod/ymss 9Rf0tTLYO5 X-Google-Smtp-Source: AG47ELsamhpHFmCntRKLR6MOKVaVaAEhLFrE/gA+4fo2gXMxX1n9tH9qH+gqTNuumWUhhkOK3HXzZw== X-Received: by 10.223.172.236 with SMTP id o99mr28066764wrc.36.1522102472025; Mon, 26 Mar 2018 15:14:32 -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 d48sm26178794wrd.12.2018.03.26.15.14.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Mar 2018 15:14:31 -0700 (PDT) Subject: Re: Parallel Aggregates for string_agg and array_agg To: Tom Lane Cc: David Rowley , 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> <18594.1522099194@sss.pgh.pa.us> From: Tomas Vondra Message-ID: <13a2ab54-0d83-d2a2-faa2-963310d8f75f@2ndquadrant.com> Date: Tue, 27 Mar 2018 00:14:27 +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: <18594.1522099194@sss.pgh.pa.us> 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 03/26/2018 11:19 PM, Tom Lane wrote: > Tomas Vondra writes: >> On 03/26/2018 10:27 PM, Tom Lane wrote: >>> 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*? > > Not if the query is such that what gets chosen is, say, an indexscan > or mergejoin. It might be theoretically unreliable and yet work fine > for a given application. > What about parallel versions of those plans, though? That is, what prevents the planner from generating plans like this Hash Aggregate -> Gather -> Partial Index Scan AFAIK that's a valid plan, and it does not have the stable ordering property of a non-parallel index scan. Similarly for Merge Join. > I might be too pessimistic about the fraction of users who are depending > on ordered input without having written anything that explicitly forces > that ... but I stand by the theory that it substantially exceeds the > fraction of users who could get any benefit. > Not sure, it's pretty much impossible to produce a number that would meaningfully represent the whole user base. All I can say is that the number of people I know, who would benefit from this is significant. > > Your own example of assuming that separate aggregates are computed > in the same order reinforces my point, I think. In principle, anybody > who's doing that should write > > array_agg(e order by x), > array_agg(f order by x), > string_agg(g order by x) > > because otherwise they shouldn't assume that; the manual certainly doesn't > promise it. But nobody does that in production, because if they did > they'd get killed by the fact that the sorts are all done independently. Yes, the manual does not promise that. But I find that expectation way more "natural" than deterministic ordering without ORDER BY clause. I expect each tuple to be processed "at once" - that it's added to all arrays in the same step, irrespectedly of the ordering of input tuples. > (We should improve that someday, but it hasn't been done yet.) So I think > there are an awful lot of people out there who are assuming more than a > lawyerly reading of the manual would allow. Their reaction to this will > be about like ours every time the GCC guys decide that some longstanding > behavior of C code isn't actually promised by the text of the C standard. > But we're not *already* providing reliable ordering without an ORDER BY clause (both for queries as a whole and array_agg/string_agg). It can produce arbitrary ordering, depending on what plan you happen to get. So I don't think we'd be changing any longstanding behavior, because it's already unreliable. If it was semi-reliable before, assuming the query only ever chose index scans or merge joins, it likely got broken by introduction of parallel versions of those plans. If the users disable parallel queries (to get the old plans), they're going to get non-parallel semi-reliable plans again. OTOH we are currently accumulating all the arrays in the same order, so the results will match. (At least that's what I believe is the current reliable behavior.) So following the GCC analogy, changing this seems much more akin to GCC people changing some longstanding behavior. Also, if the arrays were not accumulated in the same way reliably, wouldn't this mean all the discussion about reliable ordering with certain plans is pointless? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services