Thread: extending by using procedurallanguage C : problems accessing shared memory

extending by using procedurallanguage C : problems accessing shared memory

From
Feite Brekeveld
Date:
Hi,

I'm having some trouble extending postgresql with some C-functions.

Although I have done some tests with examples that work fine  I have
trouble in a function in which I wat to access data that is in shared
memory.

Functionalty is split into 2 functions because the ti_shmmav fuction is
also used by other programs.

Trouble is in the second function where I use the shmat call .  Is there
a way to do this ?

When running :

select shmmav(2785298, 1);        // the large number is the segment id

I get the following message:

server closed the connection unexpectedly
      This probably means the server terminated abnormally
      before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.

* There is data in the shared memory and I've tested it using a
perlscript to perform the same kind of operation

Some hints would be very helpful,

thanks ,

Feite Brekeveld
----------------------code snippet
--------------------------------------------

double *shmmav(int *shmid, int *period)
{
double *ret = palloc(sizeof(double));

*ret = ti_shmmav(shmid , period);
return ret;
}


double ti_shmmav(int *shmid, int *period)
{
double ret;
PREC *pprec;
int i = 0;
double total = 0.0;

pprec = (PREC *)shmat( *shmid, NULL, SHM_RDONLY);
if ( pprec == NULL) {
                fprintf(stderr, "Error attaching SHM: id %d\n", *shmid);
}
while (i < *period) {
    total += pprec[i].c;
    i++;
}

ret = total / (float)(*period);
shmdt(pprec);

return ret;
}

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



Re: extending by using procedurallanguage C : problems

From
Peter Eisentraut
Date:
Feite Brekeveld writes:

> I'm having some trouble extending postgresql with some C-functions.

It seems to me that you should actually read the documentation on writing
C extension functions, found in the Programmer's Guide, because the
interface you are using is completely wrong.

--
Peter Eisentraut   peter_e@gmx.net


Re: extending by using procedurallanguage C : problems

From
Feite Brekeveld
Date:
Peter Eisentraut wrote:

>Feite Brekeveld writes:
>
>
>
>>I'm having some trouble extending postgresql with some C-functions.
>>
>>
>
>It seems to me that you should actually read the documentation on writing
>C extension functions, found in the Programmer's Guide, because the
>interface you are using is completely wrong.
>
>
>
Hi,

that seems like an 'ouch' for me!

I've read chapter 12.5.3 !

using the examples for the 'old-style' the only remark I can make myself on the email sent is the misuse of the word
'procedural'.

An example from an earlier version of postgres works fine:
--------------------------------------------------------------
#include "/usr/include/pgsql/server/postgres.h"

double *ctof(double *deg)
{
  double *ret = palloc(sizeof(double));

  *ret = (*deg * 9.0 / 5.0) + 32.0;
  return ret;
}

create or replace function ctof(float8) RETURNS float8
  AS '/users/home/feite/proj/postgres.d/pgsql/pg_osiris' LANGUAGE C
  WITH (isStrict);
-----------------------------------------------------------------
Also thisone is working fine:
-----------------------------------------------------------------
#include "/usr/include/pgsql/server/postgres.h"
#include "examp.h"

double *mav(double *a, double *b)
{
  double *ret = palloc(sizeof(double));

  *ret = ti_mav(a , b);
  return ret;
}

#include "examp.h"

double ti_mav(double *a, double *b)
{
  double ret;

  ret = (*a + *b) / 2.0;
  return ret;
}

[feite@osiris-1 pgsql]$ psql -U postgres tst
Welcome to psql, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help on internal slash commands
       \g or terminate with semicolon to execute query
       \q to quit

tst=# select mav(8,22);
 mav
-----
  15
(1 row)


---------------------------------------------------------------
The code I sent before is a minor change to get data from shared memory instead. The exception occurs ONLY when the
'shmat'statement is there. 

(Same kind of function written in pl/perlu works fine !)

It seems that you know a lot about it. Please give me some tips on this issue. I would appreciate is very much !

Thanks,

Feite Brekeveld










Re: extending by using procedurallanguage C : problems

From
Peter Eisentraut
Date:
Feite Brekeveld writes:

> An example from an earlier version of postgres works fine:
> --------------------------------------------------------------
> #include "/usr/include/pgsql/server/postgres.h"
>
> double *ctof(double *deg)
> {

You probably inferred from that that you can also use int*.  You can't.
int32's happen to be passed by value.  But don't use the old interface.

--
Peter Eisentraut   peter_e@gmx.net