Re: Question about using AggCheckCallContext in a C function

From: Matt Solnit <msolnit(at)soasta(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Question about using AggCheckCallContext in a C function
Date: 2013-08-12 19:37:13
Message-ID: 32FECC2D-45CC-4010-9E81-583871BE3A31@soasta.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Aug 12, 2013, at 11:53 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
wrote:

> Matt Solnit <msolnit(at)soasta(dot)com> writes:
>> After poring over the code in nodeAgg.c, and looking at the in8inc()
>> function, I think I know what the problem is: the typical use of
>> AggCheckCallContext() is not compatible with TOAST-able data types.
>
> That's nonsense. There are several standard aggregates that use
> that with array transition values.
>
> Personally, I'd wonder about the blind-faith assumption in your code that
> all the input arrays are exactly the same length, with no NULL elements.
> At the very least a check for that would seem advisable. An empty
> (zero-dimensional) array could also make this code crash, so I'd be
> inclined to put in a check that ARR_NDIM() is 1, too.
>
> regards, tom lane

Thanks for your reply, albeit gruffly-worded :-). I'm certainly a
novice to the PostgreSQL source code, so I'm not surprised that my
hypothesis was wrong.

Regarding the assumptions, I am perfectly okay with them because I
have complete control over the inputs. We're using this function
with a very precise data set. I did, however, take a moment to
verify that *every* value in the table matches my assumptions, and
it does.

So where do I go from here?

Additional information that might helpful:

1. When the crash occurs, and I inspect using gdb, I consistently
find that the first array's contents are "garbage". For example:

(gdb) print array1->dataoffset
$6 = -1795162110

(gdb) print array1->ndim
$9 = 989856262

while the second array looks fine:

(gdb) print array2->dataoffset
$7 = 0

(gdb) print array2->ndim
$10 = 1

2. The function seems to work consistently when I do a SELECT
SUM(mycol) without any GROUP BY. It's only when I add grouping that
the failures happen. I'm not sure if this is a real clue or a red
herring.

Finally, can you tell me what precisely happens when you call
datumCopy() with ArrayType? If it's only returning a copy of
the TOAST reference, then how is it safe for the transition function
to modify the content? I'm probably *completely* misunderstanding
how this works, so I would love to be enlightened :-).

Sincerely,
Matt Solnit

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2013-08-12 19:47:17 Re: Question about using AggCheckCallContext in a C function
Previous Message Tom Lane 2013-08-12 18:53:30 Re: Question about using AggCheckCallContext in a C function