Обсуждение: RE: Strangeness/bug when working with my own datatype in P ostgreSQL
RE: Strangeness/bug when working with my own datatype in P ostgreSQL
От
Солодовников Константин
Дата:
Hello, Tom.
I tried to reproduce the situation from the very beginning (created a new
DB, created the datatype in it) but the effect did not reproduce.
I continued the investigation and found that it is just suffictient to
re-create a datatype in an existing DB for the effect to disappear.
Thus, I think that the reason was in some accidental combination of
circumstances.
Unfortunately, I cannot recall the order of my actions, which lead to this
strange situation.
But I still feel curious about this fact.
If you also do, and if you think that my sources can help you in analyzing
the situation, here is the code I used to build my datatype:
1. Here's the code of the four "C" functions, used serve my "int1" datatype:
------------------------8<------------------------
#include "postgres.h"
#include <stdio.h>
typedef unsigned char int1;
// IN - OUT functions
int1 *int1_in(char *str);
char *int1_out(int1 *i);
// Convertion functions
int1 *i4toi1(int4 i);
int4 i1toi4(int1 *i);
/***************************************************************************
*** Input/Output functions
****************************************************************************
*/
int1 *int1_in(char *str)
{unsigned int i;int1 *result;if ( sscanf( str, "%u", &i ) != 1 ){ elog(ERROR, "int1_in: error in parsing \"%s\"", str);
returnNULL;}if ( ( i < 0 ) || ( i > 255 ) ){ elog(ERROR, "int1_in: error in parsing \"%s\"", str); return NULL;}result
=(int1*)palloc(sizeof(int1));(*result) = i;return result;
}
char *int1_out(int1 * i)
{char *result;if ( i == NULL ) return NULL;result = (char *) palloc( 60);sprintf(result, "%u", (*i));return result;
}
/***************************************************************************
*** Type conversion functions
****************************************************************************
*/
int1 *i4toi1(int4 i)
{int1 *result;if ( ( i < 0 ) || ( i > 255 ) ){ elog(ERROR, "i4toi1: %d is out of bounds (0...255)", i); return
NULL;}result= (int1*)palloc(sizeof(int1));(*result) = i;return result;
}
int4 i1toi4(int1 *i)
{return (*i);
}
------------------------8<------------------------
2. That's how I built the library:
cc -I /usr/local/pgsql/include -I /usr/local/include -fpic -DPIC -shared -o
int1.so int1.c
3. Here are the sql statements I used to create the datatype:
create function int1_in(opaque) returns int1 as
'/usr/local/pgsql/data/int1.so' language 'C';
create function int1_out(opaque) returns opaque as
'/usr/local/pgsql/data/int1.so' language 'C';
create type int1 ( input = int1_in, output = int1_out, internallength = 1,
externallength = 3, default = "" );
create function int1(int4) returns int1 as '/usr/local/pgsql/data/int1.so',
'i4toi1' language 'C';
create function int4(int1) returns int4 as '/usr/local/pgsql/data/int1.so',
'i1toi4' language 'C';
Hope, this will help you.
Best Regards,
Konstantin Solodovnikov.
> -----Original Message-----
> From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
> To: Солодовников Константин
> Subject: Re: [BUGS] Strangeness/bug when working with my own datatype in
PostgreSQL
>
> > When I insert a new row into this table without providing a
> value for the
> > "i1" column, I get a value of "45" in it.
>
> Seems odd to me too; it should default to NULL if no value is
> specified
> or available from a DEFAULT clause. Can't guess why you're seeing a
> problem without more details, though. Could you show us the code you
> used to define your datatype?
>
> regards, tom lane
>
Солодовников Константин <ks@tcnet.ru> writes:
> create type int1 ( input = int1_in, output = int1_out, internallength = 1,
> externallength = 3, default = "" );
I'm not sure what it will do to specify a default value for a type that
is not a legal value of the datatype --- but it can't be good. Take out
the default clause.
It also seems a tad bizarre to be treating int1 as a pass-by-reference
datatype, but that's probably not causing any misbehavior, just
inefficiency...
regards, tom lane