Re: Question about using AggCheckCallContext in a C function - Mailing list pgsql-general

From Matt Solnit
Subject Re: Question about using AggCheckCallContext in a C function
Date
Msg-id 32FECC2D-45CC-4010-9E81-583871BE3A31@soasta.com
Whole thread Raw
In response to Re: Question about using AggCheckCallContext in a C function  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Question about using AggCheckCallContext in a C function  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
On Aug 12, 2013, at 11:53 AM, Tom Lane <tgl@sss.pgh.pa.us>
 wrote:

> Matt Solnit <msolnit@soasta.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


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Question about using AggCheckCallContext in a C function
Next
From: Tom Lane
Date:
Subject: Re: Question about using AggCheckCallContext in a C function