Re: Parallel aggregates in PG 16.1

From: Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>
To: ZIMANYI Esteban <esteban(dot)zimanyi(at)ulb(dot)be>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Parallel aggregates in PG 16.1
Date: 2023-11-10 12:27:58
Message-ID: CAEze2WhpnVjHFv6z9kEhdnhCUCYx8go_PbMjMDdiBkZJuiiv=g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, 10 Nov 2023 at 11:47, ZIMANYI Esteban <esteban(dot)zimanyi(at)ulb(dot)be> wrote:
>
> In MobilityDB
> https://github.com/MobilityDB/MobilityDB
> we have defined a tstzspan type which is a fixed-size equivalent of the tstzrange type in PostgreSQL.
>
> We have a span_union aggregate function which is the equivalent of the range_agg function in PostgreSQL defined as follows
>
> CREATE FUNCTION tstzspan_union_finalfn(internal)
> RETURNS tstzspanset
> AS 'MODULE_PATHNAME', 'Span_union_finalfn'
> LANGUAGE C IMMUTABLE PARALLEL SAFE;
>
> CREATE AGGREGATE span_union(tstzspan) (
> SFUNC = array_agg_transfn,
> STYPE = internal,
> COMBINEFUNC = array_agg_combine,
> SERIALFUNC = array_agg_serialize,
> DESERIALFUNC = array_agg_deserialize,
> FINALFUNC = tstzspan_union_finalfn
> );
>
> As can be seen, we reuse the array_agg function to accumulate the values in an array and the final function just does similar work as the range_agg_finalfn to merge the overlapping spans.

Did you note the following section in the CREATE AGGREGATE documentation [0]?

"""
An aggregate can optionally support partial aggregation, as described
in Section 38.12.4.
This requires specifying the COMBINEFUNC parameter. If the
state_data_type is internal, it's usually also appropriate to provide
the SERIALFUNC and DESERIALFUNC parameters so that parallel
aggregation is possible.
Note that the aggregate must also be marked PARALLEL SAFE to enable
parallel aggregation.
"""

From this, it seems like the PARALLEL = SAFE argument is missing from
your aggregate definition as provided above.

Kind regards,

Matthias van de Meent
Neon (https://neon.tech)

[0] https://www.postgresql.org/docs/16/sql-createaggregate.html

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Laurenz Albe 2023-11-10 12:43:51 Re: Bug: RLS policy FOR SELECT is used to check new rows
Previous Message torikoshia 2023-11-10 11:32:34 Re: Add new option 'all' to pg_stat_reset_shared()