Skip to content

Commit a7aa9f2

Browse files
committed
Don't try to parallelize array_agg() on an anonymous record type.
This doesn't work because record_recv requires the typmod that identifies the specific record type (in our session) and array_agg_deserialize has no convenient way to get that information. The result is an "input of anonymous composite types is not implemented" error. We could probably make this work if we had to, but it does not seem worth the trouble, given that it took this long to get a field report. Just shut off parallelization, as though record_recv didn't exist. Oversight in commit 16fd03e. Back-patch to v16 where that came in. Reported-by: Kirill Zdornyy <kirill@dineserve.com> Diagnosed-by: Richard Guo <guofenglinux@gmail.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: David Rowley <dgrowleyml@gmail.com> Discussion: https://postgr.es/m/atLI5Kce2ie1zcYjU0w_kjtVaxiYbYGTihrkLDmGZQnRDD4pnXukIATaABbnIj9pUnelC4ESvCXMm4HAyHg-v61XABaKpERj0A2IXzJZM7g=@dineserve.com Backpatch-through: 16
1 parent 61513da commit a7aa9f2

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/backend/parser/parse_agg.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,7 @@ resolve_aggregate_transtype(Oid aggfuncid,
19581958

19591959
/*
19601960
* agg_args_support_sendreceive
1961-
* Returns true if all non-byval of aggref's arg types have send and
1961+
* Returns true if all non-byval types of aggref's args have send and
19621962
* receive functions.
19631963
*/
19641964
bool
@@ -1973,6 +1973,15 @@ agg_args_support_sendreceive(Aggref *aggref)
19731973
TargetEntry *tle = (TargetEntry *) lfirst(lc);
19741974
Oid type = exprType((Node *) tle->expr);
19751975

1976+
/*
1977+
* RECORD is a special case: it has typsend/typreceive functions, but
1978+
* record_recv only works if passed the correct typmod to identify the
1979+
* specific anonymous record type. array_agg_deserialize cannot do
1980+
* that, so we have to disclaim support for the case.
1981+
*/
1982+
if (type == RECORDOID)
1983+
return false;
1984+
19761985
typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type));
19771986
if (!HeapTupleIsValid(typeTuple))
19781987
elog(ERROR, "cache lookup failed for type %u", type);

src/test/regress/expected/aggregates.out

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2033,8 +2033,8 @@ explain (costs off) select * from v_pagg_test order by y;
20332033
-> Parallel Seq Scan on pagg_test
20342034
(13 rows)
20352035

2036-
set max_parallel_workers_per_gather = 0;
20372036
-- Ensure results are the same without parallel aggregation.
2037+
set max_parallel_workers_per_gather = 0;
20382038
select * from v_pagg_test order by y;
20392039
y | tmin | tmax | tndistinct | bmin | bmax | bndistinct | amin | amax | andistinct | aamin | aamax | aandistinct
20402040
---+------+------+------------+------+------+------------+------+------+------------+-------+-------+-------------
@@ -2050,6 +2050,24 @@ select * from v_pagg_test order by y;
20502050
9 | 19 | 4999 | 250 | 1019 | 999 | 250 | 19 | 4999 | 250 | 19 | 4999 | 250
20512051
(10 rows)
20522052

2053+
-- Check that we don't fail on anonymous record types.
2054+
set max_parallel_workers_per_gather = 2;
2055+
explain (costs off)
2056+
select array_dims(array_agg(s)) from (select * from pagg_test) s;
2057+
QUERY PLAN
2058+
--------------------------------------------
2059+
Aggregate
2060+
-> Gather
2061+
Workers Planned: 2
2062+
-> Parallel Seq Scan on pagg_test
2063+
(4 rows)
2064+
2065+
select array_dims(array_agg(s)) from (select * from pagg_test) s;
2066+
array_dims
2067+
------------
2068+
[1:5000]
2069+
(1 row)
2070+
20532071
-- Clean up
20542072
reset max_parallel_workers_per_gather;
20552073
reset bytea_output;

src/test/regress/sql/aggregates.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,11 +805,16 @@ select * from v_pagg_test order by y;
805805
-- Ensure parallel aggregation is actually being used.
806806
explain (costs off) select * from v_pagg_test order by y;
807807

808-
set max_parallel_workers_per_gather = 0;
809-
810808
-- Ensure results are the same without parallel aggregation.
809+
set max_parallel_workers_per_gather = 0;
811810
select * from v_pagg_test order by y;
812811

812+
-- Check that we don't fail on anonymous record types.
813+
set max_parallel_workers_per_gather = 2;
814+
explain (costs off)
815+
select array_dims(array_agg(s)) from (select * from pagg_test) s;
816+
select array_dims(array_agg(s)) from (select * from pagg_test) s;
817+
813818
-- Clean up
814819
reset max_parallel_workers_per_gather;
815820
reset bytea_output;

0 commit comments

Comments
 (0)