Search Postgresql Archives

Re: Persistence problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thanks for the reply. However, I don't really understand how it works... Can you help? How should this function look like? Can I still create the type the same way I did for now and add something like this:

Datum mpoint_in(PG_FUNCTION_ARGS)
{
    char *str = PG_GETARG_CSTRING(0);
    mPoint *result;
    mPoint *finalResult;

    result = (mPoint *) create_mPoint(str);

    if (result == NULL) {
        Log ("mpoint_in: reporting error for invalid input syntax for mPoint");
        ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for mPoint: \"%s\"", str)));
    }
   
    finalResult = (mPoint *)palloc(VARSIZE(result));
    memset (finalResult, 0, VARSIZE(result));
    SET_VARSIZE(finalResult, VARSIZE(result));
    memcpy((void *) VARDATA(finalResult),
           (void *) VARDATA(result),
           VARSIZE(result) - VARHDRSZ);
   
    PG_RETURN_POINTER(finalResult);
}

I should still return Datum, right? At least version 1 calling convetion says that. And should I change something on the SQL side?

I would appreciate any help.
Thanks.
Ivan

> Date: Thu, 13 May 2010 12:42:09 +0200
> From: kleptog@xxxxxxxxx
> To: i.bre@xxxxxxxx
> CC: dalroi@xxxxxxxxxxxxxxxxxxxxxxxxxxxx; pgsql-general@xxxxxxxxxxxxxx
> Subject: Re: [GENERAL] Persistence problem
>
> On Thu, May 13, 2010 at 12:04:56PM +0200, I. B. wrote:
> >
> >
> > I'll try to explain with as less code as possible.
> > One of the types I wanted to create is called mpoint. This is a part of code:
>
> <snip>
>
> > typedef struct {
> > int4 length;
> > int noOfUnits;
> > void *units; // this is later casted to uPoint *
> > } mapping_t;
> >
>
> This is not the correct way to handle varlena types. You can create the
> datum that way, but if PostgreSQL decides to compress it (as it may
> when writing to disk) you won't be able to read it back. Notably, the
> "length" part of a varlena type is not always 4 bytes.
>
> Make sure you have fully understood this page:
> http://www.postgresql.org/docs/8.4/static/xfunc-c.html
> it has a number of examples dealing with variable length types. You
> MUST use the VARDATA/VARATT/etc macros to construct and read your data.
>
> Hope this helps,
> --
> Martijn van Oosterhout <kleptog@xxxxxxxxx> http://svana.org/kleptog/
> > Patriotism is when love of your own people comes first; nationalism,
> > when hate for people other than your own comes first.
> > - Charles de Gaulle


Hotmail: Free, trusted and rich email service. Get it now.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux