Missing magic block

Lists: pgsql-general
From: "Brad Buran" <bburan(at)MIT(dot)EDU>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Missing magic block
Date: 2007-04-22 22:52:33
Message-ID: 004601c78530$eab8c310$0c05fa12@issphoenix
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

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


From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Brad Buran <bburan(at)MIT(dot)EDU>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Missing magic block
Date: 2007-04-23 06:15:03
Message-ID: 20070423061503.GC20543@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

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(at)svana(dot)org> http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.


From: "Brad Buran" <bburan(at)MIT(dot)EDU>
To: "'Martijn van Oosterhout'" <kleptog(at)svana(dot)org>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Missing magic block
Date: 2007-04-23 20:15:55
Message-ID: 004101c785e4$33469590$7b006412@issphoenix
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

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.


From: Mario Munda <mario(dot)munda(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Missing magic block
Date: 2007-05-10 20:00:33
Message-ID: 1178827233.440481.189560@y80g2000hsf.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


"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.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Mario Munda <mario(dot)munda(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Missing magic block
Date: 2007-05-11 02:39:08
Message-ID: 10115.1178851148@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Mario Munda <mario(dot)munda(at)gmail(dot)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


From: Mario Munda <mario(dot)munda(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Missing magic block
Date: 2007-05-11 07:27:00
Message-ID: 1178868420.304740.143250@q75g2000hsh.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

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(dot)munda(at)gmail(dot)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


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Mario Munda <mario(dot)munda(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Missing magic block
Date: 2007-05-12 18:22:40
Message-ID: 23608.1178994160@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Mario Munda <mario(dot)munda(at)gmail(dot)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