Thread: Missing magic block

Missing magic block

From
"Brad Buran"
Date:
I'm trying to learn how to write custom extensions to postgres so wrote a
basic C function to see how it works.  However, I keep getting the following
error "Missing magic block" when I try to add the function to the database.
According to the documentation in the manual, all I need to do is add the
following:

#include "server/fmgr.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

To my C file and it will work.  However, I guess it is not working?  Below
is the source code:

#include "server/postgres.h"
#include <string.h>

#include "server/fmgr.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

int
add_one(int arg)
{
    return arg + 1;
}

And the sql statement I am using is:

CREATE FUNCTION add_one(IN int)
    RETURNS int
    AS 'add_one'
    LANGUAGE C;

Any feedback as to how to correct it is appreciated!
Thanks,
Brad


Re: Missing magic block

From
Martijn van Oosterhout
Date:
On Sun, Apr 22, 2007 at 06:52:33PM -0400, Brad Buran wrote:
> I'm trying to learn how to write custom extensions to postgres so wrote a
> basic C function to see how it works.  However, I keep getting the following
> error "Missing magic block" when I try to add the function to the database.
> According to the documentation in the manual, all I need to do is add the
> following:

<snip>

> And the sql statement I am using is:
>
> CREATE FUNCTION add_one(IN int)
>     RETURNS int
>     AS 'add_one'
>     LANGUAGE C;

Shouldn't the name of the library appear in there somewhere?

Also, you may need to exit and restart psql to get a new session to
make sure the new version of the library is loaded.

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Attachment

Re: Missing magic block

From
"Brad Buran"
Date:
Hi Martijn,

Thank you very much for the suggestion:

> > CREATE FUNCTION add_one(IN int)
> >     RETURNS int
> >     AS 'add_one'
> >     LANGUAGE C;

I corrected this to say:

    AS 'Project1', 'add_one'

And restarted psql (rebooted for that matter as well) and am still getting
the same error.


Re: Missing magic block

From
Mario Munda
Date:
"Brad Buran" je napisal:
> Hi Martijn,
>
> Thank you very much for the suggestion:
>
> > > CREATE FUNCTION add_one(IN int)
> > >     RETURNS int
> > >     AS 'add_one'
> > >     LANGUAGE C;
>
> I corrected this to say:
>
>     AS 'Project1', 'add_one'
>
> And restarted psql (rebooted for that matter as well) and am still getting
> the same error.
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings

The same problem with me.

-- test_func.c

#include <postgres.h>
//#include <executor/spi.h>
//#include <commands/trigger.h>
#include <fmgr.h>
//#include <access/heapam.h>
#include <utils/syscache.h>
#include <catalog/pg_proc.h>
#include <catalog/pg_type.h>
#include "pgmagic.h"

PG_FUNCTION_INFO_V1(plsample_call_handler);

Datum plsample_call_handler(PG_FUNCTION_ARGS)
{
    Datum          retval;

 //       retval = ...

    return retval;
}

I had to comment some includes out, or else i get some errors.

-- pgmagic.h

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

-- this is my makefile

all: gen_code.c
        g++ -fpic -I/usr/local/include -I/usr/local/include/postgresql/
server/ -c test_func.c
        g++ -shared -o test_func.so test_func.o

This is what psql returns (phppgadmin):

SQL error:

ERROR:  incompatible library "/home/mario/tests/psql_c_func/
test_func.so": missing magic block
HINT:  Extension libraries are required to use the PG_MODULE_MAGIC
macro.

In statement:
CREATE FUNCTION "plsample_call_handler" () RETURNS void AS '/home/
mario/tests/psql_c_func/test_func.so','plsample_call_handler' LANGUAGE
"C"

I have allready lost four hours for this. What is the problem??

P.S.:
select version() returns:

PostgreSQL 8.2.0 on i386-unknown-freebsd6.1, compiled by GCC gcc (GCC)
3.4.4 [FreeBSD] 20050518

Thanks in advance.


Re: Missing magic block

From
Tom Lane
Date:
Mario Munda <mario.munda@gmail.com> writes:
> I had to comment some includes out, or else i get some errors.

Perhaps you are compiling against an old or incomplete set of
Postgres header files?

> #ifdef PG_MODULE_MAGIC
> PG_MODULE_MAGIC;
> #endif

The problem with that coding is that it will silently not produce
a magic block if you are compiling against pre-8.2 Postgres headers.
If you remove the #ifdef protection does it still compile?

            regards, tom lane

Re: Missing magic block

From
Mario Munda
Date:
I've changed:

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

to:

#define PG_MODULE_MAGIC ;

and the .so does compile, but the database is still reporting the same
error when i try to CREATE FUNCTION:

ERROR:  incompatible library "/home/mario/tests/psql_c_func/
test_func.so": missing magic block
HINT:  Extension libraries are required to use the PG_MODULE_MAGIC
macro.

I searched for all fmgr.h files in the /usr and i got the output:

/usr/local/include/postgresql/server/storage/bufmgr.h
/usr/local/include/postgresql/server/fmgr.h
/usr/local/include/postgresql/server/fmgr.h.gch
/usr/local/pgsql/include/server/storage/bufmgr.h
/usr/local/pgsql/include/server/fmgr.h

So there obviously are more headers on the system, but i've tried to
compile the .so with the second and the last one. Both time the same
result.



Tom Lane je napisal:
> Mario Munda <mario.munda@gmail.com> writes:
> > I had to comment some includes out, or else i get some errors.
>
> Perhaps you are compiling against an old or incomplete set of
> Postgres header files?
>
> > #ifdef PG_MODULE_MAGIC
> > PG_MODULE_MAGIC;
> > #endif
>
> The problem with that coding is that it will silently not produce
> a magic block if you are compiling against pre-8.2 Postgres headers.
> If you remove the #ifdef protection does it still compile?
>
>             regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings


Re: Missing magic block

From
Tom Lane
Date:
Mario Munda <mario.munda@gmail.com> writes:
> I've changed:
> #ifdef PG_MODULE_MAGIC
> PG_MODULE_MAGIC;
> #endif

> to:

> #define PG_MODULE_MAGIC ;

Uh, that turns it into a complete no-op.  It should just be

PG_MODULE_MAGIC;

If you tried that and it didn't compile, that's proof that you're using
pre-8.2 header files ...

            regards, tom lane