Thread: c extension

c extension

From
Kjetil Haaland
Date:
Hello

I am writing a c extension function to my postgres database and in this i am
trying to allocate memory space for a table of char. When i try to call a
function that returns a value from this table postgres crashes. It works fine
running it as c code. I have added part of the code and hope that someone can
help me.

char *scoreChar;

void readScoreMatrix(char *filename) {
    scoreChar = (char*)palloc(20*sizeof(char));
      if(scoreChar == NULL) {
            printf("\n failed to allocate memory for scoreChar");
                ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR),
                errmsg("failed to allocate memory for scoreChar")));

then i fill the table with 20 characters.

PG_FUNCTION_INFO_V1(setscorematrix);

void setscorematrix(PG_FUNCTION_ARGS) {
      void *fileName = PG_GETARG_POINTER(0);
      char *file;
      file = DatumGetCString((char *)DatumGetPointer(fileName));
      readScoreMatrix(file);
}
Datum getscorechar(PG_FUNCTION_ARGS) {
      int32 i = PG_GETARG_INT32(0);
      char c = scoreChar[i];
      //ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR),
      //          errmsg("test:%c", c)));
      PG_RETURN_CHAR(c);
}
CREATE FUNCTION getscorechar(integer) returns char
    AS '/home/funcs/test'
    LANGUAGE C STRICT;

The reading of the scorechar works fine, returns no error. And if i write out
the value from the scoreChar as an error i get the correct character.

thanks for any help
-Kjetil

Re: c extension

From
Joe Conway
Date:
Kjetil Haaland wrote:
> I am writing a c extension function to my postgres database and in this i am
> trying to allocate memory space for a table of char. When i try to call a
> function that returns a value from this table postgres crashes. It works fine
> running it as c code. I have added part of the code and hope that someone can
> help me.

>     scoreChar = (char*)palloc(20*sizeof(char));

It doesn't look like that palloc is done in a sufficiently long-lived
memory context. Read up on them in src/backend/utils/mmgr/README.

Joe

Re: c extension

From
Kjetil Haaland
Date:
On Wednesday 03 November 2004 15:58, Joe Conway wrote:
>It doesn't look like that palloc is done in a sufficiently long-lived
>memory context. Read up on them in src/backend/utils/mmgr/README.

Many thanks for good answer. I have also allocated memory for a table of
integers in the same file, and this table seems to work fine, i can print out
values from it without any problems.

Is there any good reason it should work with integers and not with characters?

In what memory context should it be created to last until the user logs out?

-Kjetil

Re: c extension

From
Joe Conway
Date:
Kjetil Haaland wrote:
> Many thanks for good answer. I have also allocated memory for a table of
> integers in the same file, and this table seems to work fine, i can print out
> values from it without any problems.
>
> Is there any good reason it should work with integers and not with characters?

Not sure -- sheer luck ;-)

> In what memory context should it be created to last until the user logs out?

Probably TopMemoryContext

"TopMemoryContext --- this is the actual top level of the context tree;
  every other context is a direct or indirect child of this one.
  Allocating here is essentially the same as "malloc", because this
  context will never be reset or deleted."

I.e. anything allocated in TopMemoryContext will live until the session
ends.

See:
   http://www.joeconway.com/tut_oscon_2004.pdf
particularly starting at or about page 64. Much of the detailed
explanation is not in the slides, but it should give you some examples
and ideas. There are examples throughout the presentation, and several
of them have examples of memory context use.

Joe

Re: c extension

From
Tom Lane
Date:
Joe Conway <mail@joeconway.com> writes:
> Kjetil Haaland wrote:
>> Is there any good reason it should work with integers and not with characters?

> Not sure -- sheer luck ;-)

Maybe it's a memory clobber, like writing one more array position than
you actually allocated?  Check your address and sizing arithmetic.

            regards, tom lane

Re: c extension

From
Kjetil Haaland
Date:
On Wednesday 03 November 2004 20:53, Tom Lane wrote:
> Joe Conway <mail@joeconway.com> writes:
> > Kjetil Haaland wrote:
> >> Is there any good reason it should work with integers and not with
> >> characters?
> >
> > Not sure -- sheer luck ;-)
>
> Maybe it's a memory clobber, like writing one more array position than
> you actually allocated?  Check your address and sizing arithmetic.
>
>             regards, tom lane

Have checked it, and it is correct. I don't go pass what i have actually
allocated. I have looked at your slides but i don't understand how I change
the memorycontext. I now about the MemoryContextSwitchTo method but what i
don't understand is how to define the correct memorycontext, let's say
TopMemoryContext. Does anyone now how i do this?

-Kjetil

Re: c extension

From
Kjetil Haaland
Date:
On Wednesday 03 November 2004 17:42, Joe Conway wrote:
> Probably TopMemoryContext
>
> "TopMemoryContext --- this is the actual top level of the context tree;
>   every other context is a direct or indirect child of this one.
>   Allocating here is essentially the same as "malloc", because this
>   context will never be reset or deleted."
>
> I.e. anything allocated in TopMemoryContext will live until the session
> ends.

Hi again

I have no managed to set the memorycontext to TopMemoryContext but it still
doesn't work. The table with the integers is still working fine, but the
table with the characters are no good and it crashes when i try to read it.
In the log it says that there is a record with zero length.

Does anyone have any suggestion of what could be wrong?

-Kjetil

Re: c extension

From
Joe Conway
Date:
Kjetil Haaland wrote:
> I have no managed to set the memorycontext to TopMemoryContext but it still
> doesn't work. The table with the integers is still working fine, but the
> table with the characters are no good and it crashes when i try to read it.
> In the log it says that there is a record with zero length.
>
> Does anyone have any suggestion of what could be wrong?

Are you handling NULLs correctly?

Hard to say -- if you really want help you'll need to post your C function.

Joe