Обсуждение: Bug in user defined types in postgres?! (fwd)

Поиск
Список
Период
Сортировка

Bug in user defined types in postgres?! (fwd)

От
Petr Danecek
Дата:
Hallo,

I am having enormous troubles with user defined types in langugae c in
postgres. Do you know what am I doing wrong? I am afraid there is a
bug in postgres.

Look at this:

File testdb.c:
---------------------
#include <stdio.h>
#include <string.h>
#include <malloc.h>

typedef struct _mytype {int a;int b;
} mytype;

mytype *
mytype_in (char *str)
{mytype *ret;
ret = malloc (sizeof(mytype));ret->a = 1;ret->b = 2;return (ret);
}

char *
mytype_out (mytype *mt)
{return (strdup("9,9"));
}

-----------------------------

You can see that this type is good for nothing.
(Except for demonstrational purpose.)

This C-code I compile:gcc -fPIC -c testdb.cld -G -Bdynamic -o testdb.so testdb.o


And then run these queries:CREATE FUNCTION mytype_in (opaque)RETURNS mytypeAS '/localpath/testdb.so'LANGUAGE 'c';
CREATE FUNCTION mytype_out (opaque)RETURNS opaqueAS '/localpath/testdb.so'LANGUAGE 'c';
CREATE TYPE mytype (internallength = 8,input = mytype_in,output = mytype_out);
CREATE TABLE pok (x mytype, txt varchar);
insert into pok (x,txt) values ('Anything','Anything');

------------

You will see that so far goes everything fine.
After typing "Select * from pok" we get core dump (in earlier versions of
psql) or an infinite loop in brand new versions.

You will also find that when the user defined type goes in the 'pok' table
as the last one, everything will work.


I am running postgres 6.4.2 on SunOS 5.5.1.

Any ideas?
Your help will be much appreciated.

Petr Danecek













Re: [HACKERS] Bug in user defined types in postgres?! (fwd)

От
Tom Ivar Helbekkmo
Дата:
Petr Danecek <petr@ics.cas.cz> writes:

> #include <stdio.h>
> #include <string.h>
> #include <malloc.h>

You can't use malloc() -- you have to use PostgreSQL's own palloc().
So you want to replace that "#include <malloc.h>" with:
 #include <postgres.h> #include <utils/palloc.h>

So, the actual allocation in mytype_in() must be changed:

> mytype *
> mytype_in (char *str)
> {
>     mytype *ret;
> 
>     ret = malloc (sizeof(mytype));

Here, the call should be to palloc() instead of malloc(), thus:
ret = palloc (sizeof(mytype));

The reason for this is that PostgreSQL does its own memory management,
for efficiency reasons, and if you suddenly start calling malloc(),
you screw up its logic.

-tih
-- 
Popularity is the hallmark of mediocrity.  --Niles Crane, "Frasier"