Re: Wierdness using SUM to add results of custom C function. - Mailing list pgsql-general

From Tom Lane
Subject Re: Wierdness using SUM to add results of custom C function.
Date
Msg-id 24726.1014674995@sss.pgh.pa.us
Whole thread Raw
In response to Re: Wierdness using SUM to add results of custom C function.  (Konrad Scherer <kscherer@magma.ca>)
List pgsql-general
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

pgsql-general by date:

Previous
From: "Thomas T. Thai"
Date:
Subject: Re: help with getting index scan
Next
From: Tom Lane
Date:
Subject: Re: Permissions on file created by COPY TO