mlw <markw@mohawksoft.com> writes:
> I have a couple functions which form the basis of an aggregate.
You can't lie to the system by claiming your state value is an integer
when it's really a pointer to palloc'd memory. The memory will get
recycled out from under you.
Try declaring the aggregate as using int4[] as the transition type,
and make sure that the intermediate states are valid at least to the
point of having a correct varlena length word. This will allow the
system to copy the values around when it needs to.
Alternatively, keep the data structure in a longer-lived context
(TransactionCommandContext should work) instead of the per-tuple
context. That's uglier but would avoid a lot of copying.
See src/backend/executor/nodeAgg.c if you are wondering why the state
values need to be copied around.
regards, tom lane