user defined type - Mailing list pgsql-novice

From Kjetil Haaland
Subject user defined type
Date
Msg-id 200411081326.55139.kjetil.haaland@student.uib.no
Whole thread Raw
Responses Re: user defined type
List 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);
}

pgsql-novice by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: PREPARE function
Next
From: John DeSoi
Date:
Subject: Re: Pgsql install