writing backend extensions using Visual Studio

Lists: pgsql-hackers-win32
From: Greg Landrum <greg(dot)landrum(at)gmail(dot)com>
To: pgsql-hackers-win32(at)postgresql(dot)org
Subject: writing backend extensions using Visual Studio
Date: 2005-03-25 22:28:03
Message-ID: 60825b0f05032514285d9be9e2@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers-win32

Greetings,

I am trying to port a library of backend functions that I've built and
tested on linux to the windows version of postgresql (v8.0.1).

I installed pgSQL on the windows box (win2k) using the binary
installer a while ago and the base installation works fine, so long as
I don't attempt to use my backend functions.

Because the extension functions use a bunch of in-house libraries that
are built with Visual Studio, I really need an approach that works
with MSVC.

After playing around for a while, I managed to get a DLL to build and
work at least far enough that I can CREATE and call the functions from
psql. However, it looks like every argument I pass into the functions
comes in as null (e.g. PG_ARGISNULL returns true). This clearly is
less than helpful. :-)

Can anyone point me to documentation or example files for how to set
up a project for building extensions functions for postgres on
windows?

Thanks for any help,
-greg


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Greg Landrum <greg(dot)landrum(at)gmail(dot)com>
Cc: pgsql-hackers-win32(at)postgresql(dot)org
Subject: Re: writing backend extensions using Visual Studio
Date: 2005-03-25 22:57:07
Message-ID: 15980.1111791427@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers-win32

Greg Landrum <greg(dot)landrum(at)gmail(dot)com> writes:
> After playing around for a while, I managed to get a DLL to build and
> work at least far enough that I can CREATE and call the functions from
> psql. However, it looks like every argument I pass into the functions
> comes in as null (e.g. PG_ARGISNULL returns true). This clearly is
> less than helpful. :-)

Sounds like MSVC is interpreting the layout of the FunctionCallInfo
struct differently than the MinGW compiler did. You should probably
look into whether there are "pragma pack" directives lurking in your
program. ("pragma pack" is evil...)

regards, tom lane


From: Greg Landrum <greg(dot)landrum(at)gmail(dot)com>
To: pgsql-hackers-win32(at)postgresql(dot)org
Subject: Re: writing backend extensions using Visual Studio
Date: 2005-03-26 15:58:30
Message-ID: 60825b0f05032607584c245e3a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers-win32

On Fri, 25 Mar 2005 17:57:07 -0500, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Greg Landrum <greg(dot)landrum(at)gmail(dot)com> writes:
> > After playing around for a while, I managed to get a DLL to build and
> > work at least far enough that I can CREATE and call the functions from
> > psql. However, it looks like every argument I pass into the functions
> > comes in as null (e.g. PG_ARGISNULL returns true). This clearly is
> > less than helpful. :-)
>
> Sounds like MSVC is interpreting the layout of the FunctionCallInfo
> struct differently than the MinGW compiler did. You should probably
> look into whether there are "pragma pack" directives lurking in your
> program. ("pragma pack" is evil...)

I've got the library stripped down to the bare minimum (just a couple
of demo functions) and I can't seem to find any uses of pragma pack,
so I don't think that's it.

-greg


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Greg Landrum <greg(dot)landrum(at)gmail(dot)com>
Cc: pgsql-hackers-win32(at)postgresql(dot)org
Subject: Re: writing backend extensions using Visual
Date: 2005-03-26 16:25:27
Message-ID: 42458CF7.2050606@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers-win32

Greg Landrum wrote:

>On Fri, 25 Mar 2005 17:57:07 -0500, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
>
>>Greg Landrum <greg(dot)landrum(at)gmail(dot)com> writes:
>>
>>
>>>After playing around for a while, I managed to get a DLL to build and
>>>work at least far enough that I can CREATE and call the functions from
>>>psql. However, it looks like every argument I pass into the functions
>>>comes in as null (e.g. PG_ARGISNULL returns true). This clearly is
>>>less than helpful. :-)
>>>
>>>
>>Sounds like MSVC is interpreting the layout of the FunctionCallInfo
>>struct differently than the MinGW compiler did. You should probably
>>look into whether there are "pragma pack" directives lurking in your
>>program. ("pragma pack" is evil...)
>>
>>
>
>I've got the library stripped down to the bare minimum (just a couple
>of demo functions) and I can't seem to find any uses of pragma pack,
>so I don't think that's it.
>
>
>
>

Do we actually support building backend extensions with anything other
than our standard build environment? I know we support building libpq
using MSVC, but that is for client use. I'm not even mildly surprised
that building a backend extension with MSVC breaks.

cheers

andrew


From: Greg Landrum <greg(dot)landrum(at)gmail(dot)com>
To: pgsql-hackers-win32(at)postgresql(dot)org
Subject: Re: writing backend extensions using Visual Studio
Date: 2005-03-26 16:39:20
Message-ID: 60825b0f05032608392b7a5ee4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers-win32

On Sat, 26 Mar 2005 11:25:27 -0500, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>
> Do we actually support building backend extensions with anything other
> than our standard build environment? I know we support building libpq
> using MSVC, but that is for client use. I'm not even mildly surprised
> that building a backend extension with MSVC breaks.

This goes a long way towards answering my question.

So given that I want to work with backend extensions on windows, I
need to pull down a copy of mingw and use that?

-greg


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Greg Landrum <greg(dot)landrum(at)gmail(dot)com>
Cc: pgsql-hackers-win32(at)postgresql(dot)org
Subject: Re: writing backend extensions using Visual Studio
Date: 2005-03-26 17:40:09
Message-ID: 200503261740.j2QHe9n05029@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers-win32

Greg Landrum wrote:
> On Sat, 26 Mar 2005 11:25:27 -0500, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
> >
> > Do we actually support building backend extensions with anything other
> > than our standard build environment? I know we support building libpq
> > using MSVC, but that is for client use. I'm not even mildly surprised
> > that building a backend extension with MSVC breaks.
>
> This goes a long way towards answering my question.
>
> So given that I want to work with backend extensions on windows, I
> need to pull down a copy of mingw and use that?

Yes, read the MinGW FAQ that explains the things you need.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Greg Landrum <greg(dot)landrum(at)gmail(dot)com>, pgsql-hackers-win32(at)postgresql(dot)org
Subject: Re: writing backend extensions using Visual
Date: 2005-03-26 18:55:06
Message-ID: 14211.1111863306@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers-win32

Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
> Do we actually support building backend extensions with anything other
> than our standard build environment? I know we support building libpq
> using MSVC, but that is for client use. I'm not even mildly surprised
> that building a backend extension with MSVC breaks.

I'm not either, but given that he can get as far as building a library
that will load into the backend, it's surprising that it would fall down
on the small matter of passing function arguments. It seems worth
trying to understand exactly what's happening there.

Greg, how sure are you about the diagnosis of "PG_ARGISNULL always
returns true" --- have you stepped through your code with a debugger?

regards, tom lane


From: Greg Landrum <greg(dot)landrum(at)gmail(dot)com>
To: pgsql-hackers-win32(at)postgresql(dot)org
Subject: Re: writing backend extensions using Visual
Date: 2005-03-26 19:18:04
Message-ID: 60825b0f05032611182d2f6b31@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers-win32

On Sat, 26 Mar 2005 13:55:06 -0500, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> I'm not either, but given that he can get as far as building a library
> that will load into the backend, it's surprising that it would fall down
> on the small matter of passing function arguments. It seems worth
> trying to understand exactly what's happening there.
>
> Greg, how sure are you about the diagnosis of "PG_ARGISNULL always
> returns true" --- have you stepped through your code with a debugger?

Nope, I haven't gone that far. I just had the code that was causing
the crash (from accessing bad memory) inside:
if(!PG_ARGISNULL(0)) {
}
and I'm sure that the function isn't actually being called with a null
argument (for one thing, it's declared 'strict').

-greg


From: Greg Landrum <greg(dot)landrum(at)gmail(dot)com>
To: pgsql-hackers-win32(at)postgresql(dot)org
Subject: Re: writing backend extensions using Visual Studio
Date: 2005-03-28 19:45:45
Message-ID: 60825b0f0503281145480afd39@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers-win32

When I switched to using mingw to compile the library containing my
extension functions, everything works fine.

This wasn't a trivial undertaking, and I'd certainly prefer to be able
to use MSVC to build the libraries, but I now have a working extension
library and that's the most important thing at the moment.

For the sake of anyone following later: If you're using postgresql 8.0
under windows and you want to build extension libraries, it looks like
you need to use mingw to build the libraries. You'll also need to
build a local copy of postgresql itself in order to get the
appropriate libraries and header files; it seems that the development
files distributed with the win32 installer aren't sufficient to the
task.

Thanks to everyone for the help,
-greg