diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index b6b3a81..7682ae2 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -2863,6 +2863,196 @@ select c1, rank(c1, c2) within group (order by c1, c2) from ft1 group by c1, c2 6 | 1 (1 row) +-- case statement with aggregate functions, Aggregate able to pushdown without CASE. +EXPLAIN (VERBOSE, COSTS OFF) +select count(c6), case when count(c6)>50 then 'T' else 'F' end from ft1 group by c2 order by 1,2 limit 10; + QUERY PLAN +--------------------------------------------------------------------------------------------------------- + Limit + Output: (count(c6)), (CASE WHEN ((count(c6)) > 50) THEN 'T'::text ELSE 'F'::text END), c2 + -> Sort + Output: (count(c6)), (CASE WHEN ((count(c6)) > 50) THEN 'T'::text ELSE 'F'::text END), c2 + Sort Key: (count(ft1.c6)), (CASE WHEN ((count(ft1.c6)) > 50) THEN 'T'::text ELSE 'F'::text END) + -> Foreign Scan + Output: (count(c6)), CASE WHEN ((count(c6)) > 50) THEN 'T'::text ELSE 'F'::text END, c2 + Relations: Aggregate on (public.ft1) + Remote SQL: SELECT count(c6), c2 FROM "S 1"."T 1" GROUP BY c2 +(9 rows) + +select count(c6), case when count(c6)>50 then 'T' else 'F' end from ft1 group by c2 order by 1,2 limit 10; + count | case +-------+------ + 100 | T + 100 | T + 100 | T + 100 | T + 100 | T + 100 | T + 100 | T + 100 | T + 100 | T + 100 | T +(10 rows) + +-- Aggregate with typecast in immutable function. +EXPLAIN (VERBOSE, COSTS OFF) +select date_pl_interval(c5::date, '1 year'), sum(c2) from ft1 group by date_pl_interval(c5::date, '1 year') HAVING sum(c2) = 50 order by 1; + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Sort + Output: (date_pl_interval((c5)::date, '@ 1 year'::interval)), (sum(c2)) + Sort Key: (date_pl_interval((ft1.c5)::date, '@ 1 year'::interval)) + -> Foreign Scan + Output: (date_pl_interval((c5)::date, '@ 1 year'::interval)), (sum(c2)) + Relations: Aggregate on (public.ft1) + Remote SQL: SELECT date_pl_interval(c5::date, '@ 1 year'::interval), sum(c2) FROM "S 1"."T 1" GROUP BY (date_pl_interval(c5::date, '@ 1 year'::interval)) HAVING ((sum(c2) = 50)) +(7 rows) + +select date_pl_interval(c5::date, '1 year'), sum(c2) from ft1 group by date_pl_interval(c5::date, '1 year') HAVING sum(c2) = 50 order by 1; + date_pl_interval | sum +--------------------------+----- + Wed Jan 06 00:00:00 1971 | 50 + Sat Jan 16 00:00:00 1971 | 50 + Tue Jan 26 00:00:00 1971 | 50 + Fri Feb 05 00:00:00 1971 | 50 + Mon Feb 15 00:00:00 1971 | 50 + Thu Feb 25 00:00:00 1971 | 50 + Sun Mar 07 00:00:00 1971 | 50 + Wed Mar 17 00:00:00 1971 | 50 + Sat Mar 27 00:00:00 1971 | 50 + Tue Apr 06 00:00:00 1971 | 50 +(10 rows) + +-- aggregate with foreign table on view created in remote server. +CREATE VIEW "S 1".t1_vw as select * from "S 1"."T 1"; +CREATE FOREIGN TABLE ft1_vw ( + c0 int, + c1 int NOT NULL, + c2 int NOT NULL, + c3 text, + c4 timestamptz, + c5 timestamp, + c6 varchar(10), + c7 char(10) default 'ft1', + c8 user_enum +) SERVER loopback OPTIONS (schema_name 'S 1', table_name 't1_vw'); +EXPLAIN (VERBOSE, COSTS OFF) +select count(c2) from ft1_vw group by c2 order by 1 limit 10; + QUERY PLAN +----------------------------------------------------------------------------- + Limit + Output: (count(c2)), c2 + -> Sort + Output: (count(c2)), c2 + Sort Key: (count(ft1_vw.c2)) + -> Foreign Scan + Output: (count(c2)), c2 + Relations: Aggregate on (public.ft1_vw) + Remote SQL: SELECT count(c2), c2 FROM "S 1".t1_vw GROUP BY c2 +(9 rows) + +select count(c2) from ft1_vw group by c2 order by 1 limit 10; + count +------- + 100 + 100 + 100 + 100 + 100 + 100 + 100 + 100 + 100 + 100 +(10 rows) + +-- Aggregate with Boolean data every() . +CREATE TABLE "S 1"."T 7" ( + c1 int, + c2 boolean +); +CREATE FOREIGN TABLE ft7 ( + c1 int, + c2 boolean +) SERVER loopback OPTIONS (schema_name 'S 1', table_name 'T 7'); +insert into "S 1"."T 7" ( + select trunc(random() * 9 + 1), + cast(cast(random() as integer) as boolean) + from generate_series(1,100) +); +EXPLAIN (VERBOSE, COSTS OFF) +select every(c2) from ft7 group by c1 order by 1; + QUERY PLAN +----------------------------------------------------------------------- + Sort + Output: (every(c2)), c1 + Sort Key: (every(ft7.c2)) + -> Foreign Scan + Output: (every(c2)), c1 + Relations: Aggregate on (public.ft7) + Remote SQL: SELECT every(c2), c1 FROM "S 1"."T 7" GROUP BY c1 +(7 rows) + +select every(c2) from ft7 group by c1 order by 1; + every +------- + f + f + f + f + f + f + f + f + f +(9 rows) + +-- Not pushed down as the join is with base table and foreign server tables. +EXPLAIN (VERBOSE, COSTS OFF) +select avg(t1.c1), sum(t4.c1) FROM ft1 t1 JOIN "S 1"."T 4" t4 ON t1.c1 = t4.c1 group by t4.c1 order by 1 limit 10; + QUERY PLAN +--------------------------------------------------------------------------------------------------- + Limit + Output: (avg(t1.c1)), (sum(t4.c1)), t4.c1 + -> Sort + Output: (avg(t1.c1)), (sum(t4.c1)), t4.c1 + Sort Key: (avg(t1.c1)) + -> GroupAggregate + Output: avg(t1.c1), sum(t4.c1), t4.c1 + Group Key: t4.c1 + -> Merge Join + Output: t4.c1, t1.c1 + Merge Cond: (t1.c1 = t4.c1) + -> Foreign Scan on public.ft1 t1 + Output: t1.c1 + Remote SQL: SELECT "C 1" FROM "S 1"."T 1" ORDER BY "C 1" ASC NULLS LAST + -> Sort + Output: t4.c1 + Sort Key: t4.c1 + -> Seq Scan on "S 1"."T 4" t4 + Output: t4.c1 +(19 rows) + +select avg(t1.c1), sum(t4.c1) FROM ft1 t1 JOIN "S 1"."T 4" t4 ON t1.c1 = t4.c1 group by t4.c1 order by 1 limit 10; + avg | sum +---------------------+----- + 3.0000000000000000 | 3 + 6.0000000000000000 | 6 + 9.0000000000000000 | 9 + 12.0000000000000000 | 12 + 15.0000000000000000 | 15 + 18.0000000000000000 | 18 + 21.0000000000000000 | 21 + 24.0000000000000000 | 24 + 27.0000000000000000 | 27 + 30.0000000000000000 | 30 +(10 rows) + +-- Cleanup: +DROP FOREIGN TABLE ft1_vw; +DROP FOREIGN TABLE ft7; +DROP VIEW "S 1".t1_vw; +DROP TABLE "S 1"."T 7"; -- User defined function for user defined aggregate, VARIADIC create function least_accum(anyelement, variadic anyarray) returns anyelement language sql as diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index c7487b4..7710864 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -687,6 +687,65 @@ explain (verbose, costs off) select c1, rank(c1, c2) within group (order by c1, c2) from ft1 group by c1, c2 having c1 = 6 order by 1; select c1, rank(c1, c2) within group (order by c1, c2) from ft1 group by c1, c2 having c1 = 6 order by 1; +-- case statement with aggregate functions, Aggregate able to pushdown without CASE. +EXPLAIN (VERBOSE, COSTS OFF) +select count(c6), case when count(c6)>50 then 'T' else 'F' end from ft1 group by c2 order by 1,2 limit 10; +select count(c6), case when count(c6)>50 then 'T' else 'F' end from ft1 group by c2 order by 1,2 limit 10; + +-- Aggregate with typecast in immutable function. +EXPLAIN (VERBOSE, COSTS OFF) +select date_pl_interval(c5::date, '1 year'), sum(c2) from ft1 group by date_pl_interval(c5::date, '1 year') HAVING sum(c2) = 50 order by 1; +select date_pl_interval(c5::date, '1 year'), sum(c2) from ft1 group by date_pl_interval(c5::date, '1 year') HAVING sum(c2) = 50 order by 1; + +-- aggregate with foreign table on view created in remote server. +CREATE VIEW "S 1".t1_vw as select * from "S 1"."T 1"; +CREATE FOREIGN TABLE ft1_vw ( + c0 int, + c1 int NOT NULL, + c2 int NOT NULL, + c3 text, + c4 timestamptz, + c5 timestamp, + c6 varchar(10), + c7 char(10) default 'ft1', + c8 user_enum +) SERVER loopback OPTIONS (schema_name 'S 1', table_name 't1_vw'); + +EXPLAIN (VERBOSE, COSTS OFF) +select count(c2) from ft1_vw group by c2 order by 1 limit 10; +select count(c2) from ft1_vw group by c2 order by 1 limit 10; + +-- Aggregate with Boolean data every() . +CREATE TABLE "S 1"."T 7" ( + c1 int, + c2 boolean +); + +CREATE FOREIGN TABLE ft7 ( + c1 int, + c2 boolean +) SERVER loopback OPTIONS (schema_name 'S 1', table_name 'T 7'); +insert into "S 1"."T 7" ( + select trunc(random() * 9 + 1), + cast(cast(random() as integer) as boolean) + from generate_series(1,100) +); + +EXPLAIN (VERBOSE, COSTS OFF) +select every(c2) from ft7 group by c1 order by 1; +select every(c2) from ft7 group by c1 order by 1; + +-- Not pushed down as the join is with base table and foreign server tables. +EXPLAIN (VERBOSE, COSTS OFF) +select avg(t1.c1), sum(t4.c1) FROM ft1 t1 JOIN "S 1"."T 4" t4 ON t1.c1 = t4.c1 group by t4.c1 order by 1 limit 10; +select avg(t1.c1), sum(t4.c1) FROM ft1 t1 JOIN "S 1"."T 4" t4 ON t1.c1 = t4.c1 group by t4.c1 order by 1 limit 10; + +-- Cleanup: +DROP FOREIGN TABLE ft1_vw; +DROP FOREIGN TABLE ft7; +DROP VIEW "S 1".t1_vw; +DROP TABLE "S 1"."T 7"; + -- User defined function for user defined aggregate, VARIADIC create function least_accum(anyelement, variadic anyarray) returns anyelement language sql as