Konrad Scherer <kscherer@magma.ca> writes:
> //if no text, then no
> words
> if (txt->vl_len < 1)
> {
> return
> 0;
> }
> //if there is text, but no commas there must be one
> word
> count =
> 1;
> //ignore commas in first and last postion in
> list
> for (i=1;i<(txt->vl_len)-1;i++)
> {
> if (str[i]==',')
> {
> count++;
> }
You appear to be assuming that txt->vl_len is the length of the string.
It's not (it includes the size of the header). You're scanning a few
garbage bytes beyond the data, and evidently those sometimes look like
commas. The apparent difference in behavior between aggregate and
non-aggregate context likely has something to do with whether the
argument you're given has been copied or not.
> An aside: should I be freeing str after I am done with it?
You can, but it's not really necessary; the palloc'd storage will be
reclaimed soon after your function exits anyway.
regards, tom lane