user defined type

From: Kjetil Haaland <kjetil(dot)haaland(at)student(dot)uib(dot)no>
To: pgsql-novice(at)postgresql(dot)org
Subject: user defined type
Date: 2004-11-08 12:26:55
Message-ID: 200411081326.55139.kjetil.haaland@student.uib.no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hello

I am writing my own type for use in postgres. I want to be able to save a
value - an integer, and a string in this type. I have looked at the
documentation for how to do this. The insert into the type works, but when i
try to select all from the table, postgres crashes. I don't see what's wrong,
so I hope someone can help.

thanks
-Kjetil

the code:
typedef struct alignres {
int value;
char *fstring;
}alignres;
PG_FUNCTION_INFO_V1(alignres_in);

Datum alignres_in(PG_FUNCTION_ARGS) {
char *in = PG_GETARG_CSTRING(0);

int v;
char *first;

alignres *result;

if(sscanf(in, "(%d, %s)", &v, &first) != 2) {
ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input type for alignment: \"%s\"", in)));
}
result = (alignres *) palloc(sizeof(alignres));
result->value = v;
result->fstring = first;

PG_RETURN_POINTER(result);
}

PG_FUNCTION_INFO_V1(alignres_out);

Datum alignres_out(PG_FUNCTION_ARGS) {
alignres *align = (alignres *) PG_GETARG_POINTER(0);
char *result;

char temp[4+sizeof(align->fstring)+4+4];

sprintf(temp, "(%d, %s)",
align->value,
align->fstring);

result = (char*) palloc(strlen(temp)+1);
strcpy(result, temp);
PG_RETURN_CSTRING(result);
}

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message John DeSoi 2004-11-08 13:12:28 Re: Pgsql install
Previous Message Michael Fuhr 2004-11-08 05:37:46 Re: PREPARE function