Re: Properly handling aggregate in nested function call

From: Matt Magoffin <postgresql(dot)org(at)msqr(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Properly handling aggregate in nested function call
Date: 2021-12-15 22:14:51
Message-ID: 45CF47FB-A07A-44C5-9559-9C731B917967@msqr.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 15/12/2021, at 11:51 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Hmm, I think you're abusing the ArrayBuildState API. In particular,
> what guarantees that the astate->dvalues and astate->dnulls arrays
> are long enough for what you're stuffing into them?

The length is defined in the aggregate transition function (i.e. the length of the first input array) and enforced there [1] so I can stuff away here.

> You should
> probably palloc temp arrays right here and then use construct_md_array
> directly instead of dealing with an ArrayBuildState.

OK, I can try that out. I did have another branch where I create the ArrayBuildState in this final function [2] but I still got the same result. I’ll try using construct_md_array instead as you suggest, thank you.

> Also, I wonder what happens when state->vec_counts[i] is zero.
> That's seemingly not your problem right now, since the ereport(NOTICE)
> is being reached; but it sure looks like trouble waiting to happen.

It can happen, but the ArrayBuildState is initialised with all astate->dnulls[i] as true at the start [3], so the

if (state->vec_counts[i]) {
}

test just skips those elements and leaves them as NULL.

— m@

[1] https://github.com/SolarNetwork/aggs_for_vecs/blob/9e742cdc32a113268fd3c1f928c8ac724acec9f5/vec_stat_accum.c#L47-L49 <https://github.com/SolarNetwork/aggs_for_vecs/blob/9e742cdc32a113268fd3c1f928c8ac724acec9f5/vec_stat_accum.c#L47-L49>

[2] https://github.com/SolarNetwork/aggs_for_vecs/blob/ab5bea1039f865c26b899f99de866f172d125dc2/vec_agg_mean.c#L23 <https://github.com/SolarNetwork/aggs_for_vecs/blob/ab5bea1039f865c26b899f99de866f172d125dc2/vec_agg_mean.c#L23>

[3] https://github.com/SolarNetwork/aggs_for_vecs/blob/ab5bea1039f865c26b899f99de866f172d125dc2/util.c#L94-L96 <https://github.com/SolarNetwork/aggs_for_vecs/blob/ab5bea1039f865c26b899f99de866f172d125dc2/util.c#L94-L96>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2021-12-15 22:31:50 Re: Why can't I have a "language sql" anonymous block?
Previous Message Bèrto ëd Sèra 2021-12-15 22:11:15 Re: Best Strategy for Large Number of Images