Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL clarification

Tags:

postgresql

I have written a function inside PostgreSQL which has the following code:


for (i = 0; i < 4; i++) 
{
   Datum dat_value = CStringGetDatum(inp->str[0][i]);
   values[i] = datumCopy(dat_value,
                            stats->attrtype->typbyval,
                            stats->attrtype->typlen);
}

The input strings are {ALGERIA,ARGENTINA,BRAZIL,CANADA}. The code runs for ALGERIA,ARGENTINA but terminates abruptly for BRAZIL. When I investigated I found that inside datumCopy function, the statement after memcpy is not getting printed. I checked if palloc failed with (s == NULL) condition, but that seems to be not the reason. I think memcpy is failing. Any reason why? Thanks!

Datum
datumCopy(Datum value, bool typByVal, int typLen)
{
Datum       res;

if (typByVal)
    res = value;
else
{
    Size        realSize;
    char       *s;

    if (DatumGetPointer(value) == NULL)
        return PointerGetDatum(NULL);

    realSize = datumGetSize(value, typByVal, typLen);

    s = (char *) palloc(realSize);

    printf ("Value : %s\n",DatumGetPointer(value));
    memcpy(s, DatumGetPointer(value), realSize);
    printf ("Not printing \n");
    res = PointerGetDatum(s);
}
return res;
}

EDITED : Ok this is really wierd. When the input is one of {BRAZIL,PAKISTAN,FRANCE}, the code terminates abruptly. If I have other countries (I haven't tried extensively, but some countries), the code runs correctly.

EDITED 2 : Found the cause and rectified the issue. If we are passing C strings to datumCopy, we have to pass -2 for typLen parameter. I had been passing it incorrectly.

Thanks!

like image 866
user2761431 Avatar asked Apr 21 '26 06:04

user2761431


1 Answers

I have found the cause and rectified the issue.

If we are passing C strings to datumCopy, we have to pass -2 for typLen parameter. I had been passing it incorrectly.

like image 118
user2761431 Avatar answered Apr 24 '26 13:04

user2761431