@@ -2491,35 +2491,6 @@ select c2, sum(c1) from ft2 group by c2 having avg(c1) < 500 and sum(c1) < 49800
24912491 2 | 49700
24922492(2 rows)
24932493
2494- -- Using expressions in HAVING clause
2495- explain (verbose, costs off)
2496- select c5, count(c2) from ft1 group by c5, sqrt(c2) having sqrt(max(c2)) = sqrt(2) order by 1, 2;
2497- QUERY PLAN
2498- --------------------------------------------------------------------------------------------------------------------------------------------------------------
2499- Sort
2500- Output: c5, (count(c2)), (sqrt((c2)::double precision))
2501- Sort Key: ft1.c5, (count(ft1.c2))
2502- -> Foreign Scan
2503- Output: c5, (count(c2)), (sqrt((c2)::double precision))
2504- Relations: Aggregate on (public.ft1)
2505- Remote SQL: SELECT c5, count(c2), sqrt(c2) FROM "S 1"."T 1" GROUP BY c5, (sqrt(c2)) HAVING ((sqrt(max(c2)) = 1.41421356237309515::double precision))
2506- (7 rows)
2507-
2508- select c5, count(c2) from ft1 group by c5, sqrt(c2) having sqrt(max(c2)) = sqrt(2) order by 1, 2;
2509- c5 | count
2510- --------------------------+-------
2511- Sat Jan 03 00:00:00 1970 | 10
2512- Tue Jan 13 00:00:00 1970 | 10
2513- Fri Jan 23 00:00:00 1970 | 10
2514- Mon Feb 02 00:00:00 1970 | 10
2515- Thu Feb 12 00:00:00 1970 | 10
2516- Sun Feb 22 00:00:00 1970 | 10
2517- Wed Mar 04 00:00:00 1970 | 10
2518- Sat Mar 14 00:00:00 1970 | 10
2519- Tue Mar 24 00:00:00 1970 | 10
2520- Fri Apr 03 00:00:00 1970 | 10
2521- (10 rows)
2522-
25232494-- Unshippable HAVING clause will be evaluated locally, and other qual in HAVING clause is pushed down
25242495explain (verbose, costs off)
25252496select count(*) from (select c5, count(c1) from ft1 group by c5, sqrt(c2) having (avg(c1) / avg(c1)) * random() <= 1 and avg(c1) < 500) x;
@@ -2844,21 +2815,20 @@ returns anyelement language sql as
28442815create aggregate least_agg(variadic items anyarray) (
28452816 stype = anyelement, sfunc = least_accum
28462817);
2818+ -- Disable hash aggregation for plan stability.
2819+ set enable_hashagg to false;
28472820-- Not pushed down due to user defined aggregate
28482821explain (verbose, costs off)
28492822select c2, least_agg(c1) from ft1 group by c2 order by c2;
2850- QUERY PLAN
2851- -------------------------------------------------------------
2852- Sort
2853- Output: c2, (least_agg(VARIADIC ARRAY[c1]))
2854- Sort Key: ft1.c2
2855- -> HashAggregate
2856- Output: c2, least_agg(VARIADIC ARRAY[c1])
2857- Group Key: ft1.c2
2858- -> Foreign Scan on public.ft1
2859- Output: c2, c1
2860- Remote SQL: SELECT "C 1", c2 FROM "S 1"."T 1"
2861- (9 rows)
2823+ QUERY PLAN
2824+ ----------------------------------------------------------------------------------
2825+ GroupAggregate
2826+ Output: c2, least_agg(VARIADIC ARRAY[c1])
2827+ Group Key: ft1.c2
2828+ -> Foreign Scan on public.ft1
2829+ Output: c2, c1
2830+ Remote SQL: SELECT "C 1", c2 FROM "S 1"."T 1" ORDER BY c2 ASC NULLS LAST
2831+ (6 rows)
28622832
28632833-- Add function and aggregate into extension
28642834alter extension postgres_fdw add function least_accum(anyelement, variadic anyarray);
@@ -2900,20 +2870,18 @@ alter server loopback options (set extensions 'postgres_fdw');
29002870-- Not pushed down as we have dropped objects from extension.
29012871explain (verbose, costs off)
29022872select c2, least_agg(c1) from ft1 group by c2 order by c2;
2903- QUERY PLAN
2904- -------------------------------------------------------------
2905- Sort
2906- Output: c2, (least_agg(VARIADIC ARRAY[c1]))
2907- Sort Key: ft1.c2
2908- -> HashAggregate
2909- Output: c2, least_agg(VARIADIC ARRAY[c1])
2910- Group Key: ft1.c2
2911- -> Foreign Scan on public.ft1
2912- Output: c2, c1
2913- Remote SQL: SELECT "C 1", c2 FROM "S 1"."T 1"
2914- (9 rows)
2873+ QUERY PLAN
2874+ ----------------------------------------------------------------------------------
2875+ GroupAggregate
2876+ Output: c2, least_agg(VARIADIC ARRAY[c1])
2877+ Group Key: ft1.c2
2878+ -> Foreign Scan on public.ft1
2879+ Output: c2, c1
2880+ Remote SQL: SELECT "C 1", c2 FROM "S 1"."T 1" ORDER BY c2 ASC NULLS LAST
2881+ (6 rows)
29152882
29162883-- Cleanup
2884+ reset enable_hashagg;
29172885drop aggregate least_agg(variadic items anyarray);
29182886drop function least_accum(anyelement, variadic anyarray);
29192887-- Testing USING OPERATOR() in ORDER BY within aggregate.