Don't try to parallelize array_agg() on an anonymous record type.
authorTom Lane <[email protected]>
Sun, 9 Mar 2025 17:11:20 +0000 (13:11 -0400)
committerTom Lane <[email protected]>
Sun, 9 Mar 2025 17:11:20 +0000 (13:11 -0400)
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 16fd03e95.  Back-patch to v16 where that
came in.

Reported-by: Kirill Zdornyy <[email protected]>
Diagnosed-by: Richard Guo <[email protected]>
Author: Tom Lane <[email protected]>
Reviewed-by: David Rowley <[email protected]>
Discussion: https://postgr.es/m/atLI5Kce2ie1zcYjU0w_kjtVaxiYbYGTihrkLDmGZQnRDD4pnXukIATaABbnIj9pUnelC4ESvCXMm4HAyHg-v61XABaKpERj0A2IXzJZM7g=@dineserve.com
Backpatch-through: 16

src/backend/parser/parse_agg.c
src/test/regress/expected/aggregates.out
src/test/regress/sql/aggregates.sql

index 9e567f3cc45287205a548f7a5dd862acd8e1fb8c..0ac8966e30ff31e20b39122850002973c36e0b4e 100644 (file)
@@ -2052,7 +2052,7 @@ resolve_aggregate_transtype(Oid aggfuncid,
 
 /*
  * agg_args_support_sendreceive
- *     Returns true if all non-byval of aggref's arg types have send and
+ *     Returns true if all non-byval types of aggref's args have send and
  *     receive functions.
  */
 bool
@@ -2067,6 +2067,15 @@ agg_args_support_sendreceive(Aggref *aggref)
        TargetEntry *tle = (TargetEntry *) lfirst(lc);
        Oid         type = exprType((Node *) tle->expr);
 
+       /*
+        * RECORD is a special case: it has typsend/typreceive functions, but
+        * record_recv only works if passed the correct typmod to identify the
+        * specific anonymous record type.  array_agg_deserialize cannot do
+        * that, so we have to disclaim support for the case.
+        */
+       if (type == RECORDOID)
+           return false;
+
        typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type));
        if (!HeapTupleIsValid(typeTuple))
            elog(ERROR, "cache lookup failed for type %u", type);
index 8c4f8ce27ed09c95d4a07a3d0292782a48fc5134..6b6371c3e74b332598f810a4ade69802ce469412 100644 (file)
@@ -2151,8 +2151,8 @@ explain (costs off) select * from v_pagg_test order by y;
                                        ->  Parallel Seq Scan on pagg_test
 (13 rows)
 
-set max_parallel_workers_per_gather = 0;
 -- Ensure results are the same without parallel aggregation.
+set max_parallel_workers_per_gather = 0;
 select * from v_pagg_test order by y;
  y | tmin | tmax | tndistinct | bmin | bmax | bndistinct | amin | amax | andistinct | aamin | aamax | aandistinct 
 ---+------+------+------------+------+------+------------+------+------+------------+-------+-------+-------------
@@ -2168,6 +2168,24 @@ select * from v_pagg_test order by y;
  9 |   19 | 4999 |        250 | 1019 | 999  |        250 |   19 | 4999 |        250 |    19 |  4999 |         250
 (10 rows)
 
+-- Check that we don't fail on anonymous record types.
+set max_parallel_workers_per_gather = 2;
+explain (costs off)
+select array_dims(array_agg(s)) from (select * from pagg_test) s;
+                 QUERY PLAN                 
+--------------------------------------------
+ Aggregate
+   ->  Gather
+         Workers Planned: 2
+         ->  Parallel Seq Scan on pagg_test
+(4 rows)
+
+select array_dims(array_agg(s)) from (select * from pagg_test) s;
+ array_dims 
+------------
+ [1:5000]
+(1 row)
+
 -- Clean up
 reset max_parallel_workers_per_gather;
 reset bytea_output;
index a1dc94bff57c16ae7ce5d146b34b9013433d94db..2c47a462b7e473f88f7a904ad1588fe6fa404479 100644 (file)
@@ -852,11 +852,16 @@ select * from v_pagg_test order by y;
 -- Ensure parallel aggregation is actually being used.
 explain (costs off) select * from v_pagg_test order by y;
 
-set max_parallel_workers_per_gather = 0;
-
 -- Ensure results are the same without parallel aggregation.
+set max_parallel_workers_per_gather = 0;
 select * from v_pagg_test order by y;
 
+-- Check that we don't fail on anonymous record types.
+set max_parallel_workers_per_gather = 2;
+explain (costs off)
+select array_dims(array_agg(s)) from (select * from pagg_test) s;
+select array_dims(array_agg(s)) from (select * from pagg_test) s;
+
 -- Clean up
 reset max_parallel_workers_per_gather;
 reset bytea_output;