Re: Persistence problem

From: "I(dot) B(dot)" <i(dot)bre(at)live(dot)com>
To: <kleptog(at)svana(dot)org>
Cc: <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: Persistence problem
Date: 2010-05-13 12:35:14
Message-ID: SNT128-W18B8F80B976E95FAECA48491FC0@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


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(at)svana(dot)org
> To: i(dot)bre(at)live(dot)com
> CC: dalroi(at)solfertje(dot)student(dot)utwente(dot)nl; pgsql-general(at)postgresql(dot)org
> 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(at)svana(dot)org> 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.
https://signup.live.com/signup.aspx?id=60969

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bill Moran 2010-05-13 12:49:27 Re: Retiring from the PostgreSQL core team
Previous Message Thom Brown 2010-05-13 12:12:11 Re: pg_dumpall custom format?