Re: [GENERAL] Text function problem - Mailing list pgsql-general

From tolik@icomm.ru (Anatoly K. Lasareff)
Subject Re: [GENERAL] Text function problem
Date
Msg-id 87pv3encj8.fsf@tolikus.hq.aaanet.ru
Whole thread Raw
In response to Text function problem  (Adriaan Joubert <a.joubert@albourne.com>)
List pgsql-general
>>>>> "AJ" == Adriaan Joubert <a.joubert@albourne.com> writes:

 AJ> Hi,
 AJ> I've been adding some bit functions to postgres and this works fine,
 AJ> until I tried to construct a function that returns 8 bits as a string,
 AJ> e.g. 4 as 00000100. I constructed the function with the help of the
 AJ> programmer manual.

 AJ> Now I have the following problem on Friday's snapshot: the first time I
 AJ> select an integer as bitstring it works fine, but the second time I get
 AJ> a segflt. So I'm evidently missing something somewhere. I'd appreciate
 AJ> it if somebody could tell me what I'm doing wrong.

 AJ> I'm getting (from the backend)

. . .

skip

. . .
 AJ> text * bout (int a) {
 AJ> int32 new_text_size = VARHDRSZ + sizeof(char)*8;
 AJ> text *new_text = (text *) palloc(new_text_size);
 AJ> int i;
 AJ> for (i=0; i<8; i++)
 AJ> VARDATA(new_text)[i] = (a>>(7-i))%2 ? '1' : '0';
 AJ> return new_text;
 AJ> }

You do not set length of data in *new_text. You must write about this:


 ---------------------------------
text * bout (int a) {
int32 new_text_size = VARHDRSZ + sizeof(char)*8;
text *new_text = (text *) palloc(new_text_size);
int i;

VARSIZE(new_text) = new_text_size; /* !!!!! */

for (i=0; i<8; i++)
VARDATA(new_text)[i] = (a>>(7-i))%2 ? '1' : '0';
return new_text;
}
 ---------------------------------

 More: it'll be better if you change 'int' to 'int4' data type in your
nexts.

--
Anatoly K. Lasareff              Email:       tolik@icomm.ru
Senior programmer

pgsql-general by date:

Previous
From: "Natalya S. Makushina"
Date:
Subject: Re: [GENERAL] ' syntax (using PHP3)
Next
From: Stuart Rison
Date:
Subject: RE: [GENERAL][SQL] 'denormalising' with a select