Re: plperl and inline functions -- first draft

Lists: pgsql-hackers
From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: plperl and inline functions -- first draft
Date: 2009-11-05 22:37:06
Message-ID: 20091105223706.GW28186@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I've been trying to make pl/perl support 8.5's inline functions, with the
attached patch. The basics seem to be there, with at least one notable
exception, namely that plperl functions can do stuff only plperlu should do. I
presume this is because I really don't understand yet how plperl's trusted
interpreter initialization works, and have simply copied what looked like
important stuff from the original plperl call handler. I tested with this to
prove it:

DO $$ qx{touch test.txt}; $$ language plperl;

This works both with plperl and plperlu. Hints, anyone? Comments?

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com

Attachment Content-Type Size
plperl_inline.patch text/x-diff 4.9 KB

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-05 22:51:45
Message-ID: 4AF35701.4010806@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Joshua Tolley wrote:
> I've been trying to make pl/perl support 8.5's inline functions, with the
> attached patch.

Wow, this is the second time this week that people have produced patches
for stuff I was about to do. Cool!

> The basics seem to be there, with at least one notable
> exception, namely that plperl functions can do stuff only plperlu should do. I
> presume this is because I really don't understand yet how plperl's trusted
> interpreter initialization works, and have simply copied what looked like
> important stuff from the original plperl call handler.
>

I'll check that out.

cheers

andrew


From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-05 23:17:18
Message-ID: 20091105231718.GY28186@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Nov 05, 2009 at 05:51:45PM -0500, Andrew Dunstan wrote:
> Joshua Tolley wrote:
>> I've been trying to make pl/perl support 8.5's inline functions, with the
>> attached patch.
>
> Wow, this is the second time this week that people have produced patches
> for stuff I was about to do. Cool!

Well, I warmed up with PL/LOLCODE :)

>> The basics seem to be there, with at least one notable
>> exception, namely that plperl functions can do stuff only plperlu should do. I
>> presume this is because I really don't understand yet how plperl's trusted
>> interpreter initialization works, and have simply copied what looked like
>> important stuff from the original plperl call handler.
>>
>
> I'll check that out.

Many thanks.

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-06 19:10:27
Message-ID: 4AF474A3.9030502@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Joshua Tolley wrote:
>>> The basics seem to be there, with at least one notable
>>> exception, namely that plperl functions can do stuff only plperlu should do. I
>>> presume this is because I really don't understand yet how plperl's trusted
>>> interpreter initialization works, and have simply copied what looked like
>>> important stuff from the original plperl call handler.
>>>
>>>
>> I'll check that out.
>>
>
>
>

Ok, I have a handle on the trusted/nontrusted issue. But I think the
piece that's missing here is that it needs to save the calling context
etc. and use PG_TRY() and friends, just like plperl_call_handler(). I'll
work on that.

cheers

andrew


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-06 23:37:38
Message-ID: 4AF4B342.6030605@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


I wrote:
>
> Ok, I have a handle on the trusted/nontrusted issue. But I think the
> piece that's missing here is that it needs to save the calling context
> etc. and use PG_TRY() and friends, just like plperl_call_handler().
> I'll work on that.
>
>

OK, I committed the previously discussed change to store the language
trusted flag in the InlineCodeBlock structure. Following that, here is
my reworking of Josh's patch for DO blocks for plperl.

Missing are docs and regression tests.

cheers

andrew

Attachment Content-Type Size
plperl-inline.patch text/x-patch 4.9 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Joshua Tolley <eggyknap(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-07 01:17:57
Message-ID: 12025.1257556677@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
> ! * plperl_call_handler and plperl_inline_handler are the only
> ! * externally-visible parts of the plperl call interface. The Postgres function
> ! * and trigger managers call plperl_call_handler to execute a perl function, and
> ! * call plperl_inline_handler to execute plperl code in a DO statement.

This comment should be updated to mention the validator. (What it
replaces was wrong before, but that's no excuse for not making it
right while you're touching it.)

The spacing seems a bit random too. pgindent will fix some of that,
but it's not very bright about making vertical spacing (ie extra
blank lines) consistent.

regards, tom lane


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Joshua Tolley <eggyknap(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-07 01:20:34
Message-ID: 4AF4CB62.3020508@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Tom Lane wrote:
> Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
>
>> ! * plperl_call_handler and plperl_inline_handler are the only
>> ! * externally-visible parts of the plperl call interface. The Postgres function
>> ! * and trigger managers call plperl_call_handler to execute a perl function, and
>> ! * call plperl_inline_handler to execute plperl code in a DO statement.
>>
>
> This comment should be updated to mention the validator. (What it
> replaces was wrong before, but that's no excuse for not making it
> right while you're touching it.)
>
> The spacing seems a bit random too. pgindent will fix some of that,
> but it's not very bright about making vertical spacing (ie extra
> blank lines) consistent.
>
>
>

OK, I'll clean it up.

cheers

andrew


From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-07 02:37:35
Message-ID: 20091107023735.GB4974@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Nov 06, 2009 at 06:37:38PM -0500, Andrew Dunstan wrote:
>
> I wrote:
>>
>> Ok, I have a handle on the trusted/nontrusted issue. But I think the
>> piece that's missing here is that it needs to save the calling context
>> etc. and use PG_TRY() and friends, just like plperl_call_handler().
>> I'll work on that.
>>
>>
>
> OK, I committed the previously discussed change to store the language
> trusted flag in the InlineCodeBlock structure. Following that, here is
> my reworking of Josh's patch for DO blocks for plperl.
>
> Missing are docs and regression tests.

Attached is a cleaned up comment with documentation. I looked through the
regression tests and didn't find any that used plperl -- should we add one for
this (or for this and all kinds of other stuff)? Is there some way to make
running the regression test conditional on having built --with-perl in the
first place?

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com

Attachment Content-Type Size
plperl-inline.patch text/x-diff 5.8 KB

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-07 02:53:20
Message-ID: 4AF4E120.60509@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Joshua Tolley wrote:
> I looked through the
> regression tests and didn't find any that used plperl -- should we add one for
> this (or for this and all kinds of other stuff)? Is there some way to make
> running the regression test conditional on having built --with-perl in the
> first place?
>
>

Look in src/pl/plperl/{sql,expected}

cheers

andrew


From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-09 16:07:42
Message-ID: 20091109160742.GG4974@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Nov 06, 2009 at 09:53:20PM -0500, Andrew Dunstan wrote:
>
>
> Joshua Tolley wrote:
>> I looked through the
>> regression tests and didn't find any that used plperl -- should we add one for
>> this (or for this and all kinds of other stuff)? Is there some way to make
>> running the regression test conditional on having built --with-perl in the
>> first place?
>>
>>
>
> Look in src/pl/plperl/{sql,expected}

Ok, updated patch attached. As far as I know, this completes all outstanding
issues:

1) weird comment in plperl.c is corrected and formatted decently
2) plperlu vs. plperl actually works (thanks again, Andrew)
3) docs included
4) regression tests included

Some items of note include that this makes the regression tests add not only
plperl to the test database but also plperlu, which is a new thing. I can't
see why this might cause problems, but thought I'd mention it. The tests
specifically try to verify that plperl doesn't allow 'use Data::Dumper', and
plperlu does. Since Data::Dumper is part of perl core, that seemed safe, but
it is another dependency, and perhaps we don't want to do that. If not, is
there some other useful way of testing plperlu vs. plperl, and does it really
matter?

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com

Attachment Content-Type Size
plperl-inline.patch text/x-diff 7.5 KB

From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-12 15:21:29
Message-ID: 20091112152129.GC22192@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Nov 06, 2009 at 09:53:20PM -0500, Andrew Dunstan wrote:
> Joshua Tolley wrote:
>> I looked through the
>> regression tests and didn't find any that used plperl -- should we add one for
>> this (or for this and all kinds of other stuff)? Is there some way to make
>> running the regression test conditional on having built --with-perl in the
>> first place?
>
> Look in src/pl/plperl/{sql,expected}
>
> cheers
>
> andrew

FWIW, I've added this to the upcoming commitfest page.

https://commitfest.postgresql.org/action/patch_view?id=206

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-13 15:05:58
Message-ID: 4AFD75D6.40309@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Joshua Tolley wrote:
> Some items of note include that this makes the regression tests add not only
> plperl to the test database but also plperlu, which is a new thing. I can't
> see why this might cause problems, but thought I'd mention it. The tests
> specifically try to verify that plperl doesn't allow 'use Data::Dumper', and
> plperlu does. Since Data::Dumper is part of perl core, that seemed safe, but
> it is another dependency, and perhaps we don't want to do that. If not, is
> there some other useful way of testing plperlu vs. plperl, and does it really
> matter?
>
>

Loading both plperl and plperlu could have problems, as there are some
platforms where we can't use them both in the same session, due to some
perl oddities. We would need to test this on one such - I don't recall
which they are.

"Config" might be a better choice than "Data::Dumper". The Perl team or
some packagers could drop Data::Dumper some day, but they aren't likely
to drop Config.

cheers

andrew


From: Brendan Jurd <direvus(at)gmail(dot)com>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-15 01:10:33
Message-ID: 37ed240d0911141710v1afad71fl40288a29d9e8ad96@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2009/11/10 Joshua Tolley <eggyknap(at)gmail(dot)com>:
> Ok, updated patch attached. As far as I know, this completes all outstanding
> issues:
>

Hi Joshua,

I'm taking a look at this patch for the commitfest. I see that Andrew
has already taken an interest in the technical aspects of the patch,
so I'll focus on submission/code style/documentation.

I noticed that there was a fairly large amount of bogus/inconsistent
whitespace in the patch, particularly in the body of
plperl_inline_handler(). Some of the lines were indented with tabs,
others with spaces. You should stick with tabs. There were also a
lot of lines with a whole lot of trailing whitespace at the end.

See attached patch which repairs the whitespace. I see you generated
the patch with git, so I recommend `git diff --check`, it'll helpfully
report about some types of whitespace error.

In the documentation you refer to this feature as "inline functions".
I think this might be mixing up the terminology ... although the code
refers to "inline handlers" internally, the word "inline" doesn't
appear in the user-facing documentation for the DO command. Instead
they are referred to as "anonymous code blocks". I think it would
improve consistency if the PL/Perl mention used the same term.

Apart from those minor quibbles, the patch appears to apply, compile
and test fine, and work as advertised.

Cheers,
BJ

Attachment Content-Type Size
plperl-do-whitespace.patch application/octet-stream 3.2 KB

From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: Brendan Jurd <direvus(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-16 22:19:45
Message-ID: 20091116221945.GQ22192@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sun, Nov 15, 2009 at 12:10:33PM +1100, Brendan Jurd wrote:
> I noticed that there was a fairly large amount of bogus/inconsistent
> whitespace in the patch, particularly in the body of
> plperl_inline_handler(). Some of the lines were indented with tabs,
> others with spaces. You should stick with tabs. There were also a
> lot of lines with a whole lot of trailing whitespace at the end.

Thanks -- I tend to forget whitespace :)

> In the documentation you refer to this feature as "inline functions".
> I think this might be mixing up the terminology ... although the code
> refers to "inline handlers" internally, the word "inline" doesn't
> appear in the user-facing documentation for the DO command. Instead
> they are referred to as "anonymous code blocks". I think it would
> improve consistency if the PL/Perl mention used the same term.

I can accept that argument. The attached patch modifies the documentation, and
fixes another inconsistency I found.

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com

Attachment Content-Type Size
plperl-inline.patch text/x-diff 8.9 KB

From: Brendan Jurd <direvus(at)gmail(dot)com>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-17 22:35:35
Message-ID: 37ed240d0911171435o228cb59i79c724dfd7623d3c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2009/11/17 Joshua Tolley <eggyknap(at)gmail(dot)com>:
> On Sun, Nov 15, 2009 at 12:10:33PM +1100, Brendan Jurd wrote:
>> I noticed that there was a fairly large amount of bogus/inconsistent
>> whitespace
...
>
> Thanks -- I tend to forget whitespace :)
>
>> In the documentation you refer to this feature as "inline functions".
>> I think this might be mixing up the terminology
...
> I can accept that argument. The attached patch modifies the documentation, and
> fixes another inconsistency I found.
>

Cool. I have no gripes with the revised patch. I'm marking this as
ready for committer now. Thanks!

Cheers,
BJ


From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: Brendan Jurd <direvus(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-17 22:40:44
Message-ID: 20091117224044.GA3803@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Nov 18, 2009 at 09:35:35AM +1100, Brendan Jurd wrote:
> 2009/11/17 Joshua Tolley <eggyknap(at)gmail(dot)com>:
> > On Sun, Nov 15, 2009 at 12:10:33PM +1100, Brendan Jurd wrote:
> >> I noticed that there was a fairly large amount of bogus/inconsistent
> >> whitespace
> ...
> >
> > Thanks -- I tend to forget whitespace :)
> >
> >> In the documentation you refer to this feature as "inline functions".
> >> I think this might be mixing up the terminology
> ...
> > I can accept that argument. The attached patch modifies the documentation, and
> > fixes another inconsistency I found.
> >
>
> Cool. I have no gripes with the revised patch. I'm marking this as
> ready for committer now. Thanks!

Thanks to you, as well, and Andrew for his work.

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com


From: Alexey Klyukin <alexk(at)waki(dot)ru>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-17 22:43:18
Message-ID: 246B7575-5825-4E48-BED6-4F63582727E8@waki.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On Nov 9, 2009, at 6:07 PM, Joshua Tolley wrote:
>
> Ok, updated patch attached. As far as I know, this completes all outstanding
> issues:
>
> 1) weird comment in plperl.c is corrected and formatted decently
> 2) plperlu vs. plperl actually works (thanks again, Andrew)
> 3) docs included
> 4) regression tests included
>
> Some items of note include that this makes the regression tests add not only
> plperl to the test database but also plperlu, which is a new thing. I can't
> see why this might cause problems, but thought I'd mention it. The tests
> specifically try to verify that plperl doesn't allow 'use Data::Dumper', and
> plperlu does. Since Data::Dumper is part of perl core, that seemed safe, but
> it is another dependency, and perhaps we don't want to do that. If not, is
> there some other useful way of testing plperlu vs. plperl, and does it really
> matter?

I've noticed that the patch doesn't install current_call_data before calling plperl_call_perl_func, although it saves and restores its previous value. This breaks spi code, which relies on current_call_data->prodesc, i.e.:

postgres=# DO $$ $result = spi_exec_query("select 1"); $$ LANGUAGE plperl;

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.

rogram received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x00000001006f0336 in plperl_spi_exec (query=0x1007ecb60 "select 1", limit=0) at plperl.c:1895
warning: Source file is more recent than executable.
1895 spi_rv = SPI_execute(query, current_call_data->prodesc->fn_readonly,
(gdb) bt
#0 0x00000001006f0336 in plperl_spi_exec (query=0x1007ecb60 "select 1", limit=0) at plperl.c:1895

Also, a call to to plperl_call_perl_func should be cast to void to avoid a possible compiler warning (although It doesn't emit one on my system):

(void) plperl_call_perl_func(&desc, &fake_fcinfo);

--
Alexey Klyukin http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Alexey Klyukin <alexk(at)waki(dot)ru>
Cc: Joshua Tolley <eggyknap(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-17 23:05:19
Message-ID: 4B032C2F.3080804@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alexey Klyukin wrote:
>
> I've noticed that the patch doesn't install current_call_data before calling plperl_call_perl_func, although it saves and restores its previous value. This breaks spi code, which relies on current_call_data->prodesc, i.e.:
>
> postgres=# DO $$ $result = spi_exec_query("select 1"); $$ LANGUAGE plperl;
>

Yeah, good catch. We need to lift some stuff out of
plperl_func_handler(), because this code bypasses that. Not only setting
the call_data but also connectin g to the SPI manager and maybe one or
two other things.

> Also, a call to to plperl_call_perl_func should be cast to void to avoid a possible compiler warning (although It doesn't emit one on my system):
>
> (void) plperl_call_perl_func(&desc, &fake_fcinfo);
>
>

Right.

cheers

andrew


From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Alexey Klyukin <alexk(at)waki(dot)ru>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-18 02:11:00
Message-ID: 20091118021100.GB3803@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Nov 17, 2009 at 06:05:19PM -0500, Andrew Dunstan wrote:
>
>
> Alexey Klyukin wrote:
>>
>> I've noticed that the patch doesn't install current_call_data before calling plperl_call_perl_func, although it saves and restores its previous value. This breaks spi code, which relies on current_call_data->prodesc, i.e.:
>>
>> postgres=# DO $$ $result = spi_exec_query("select 1"); $$ LANGUAGE plperl;
>>
>
> Yeah, good catch. We need to lift some stuff out of
> plperl_func_handler(), because this code bypasses that. Not only setting
> the call_data but also connectin g to the SPI manager and maybe one or
> two other things.

I kept thinking I had to test SPI, but I guess I hadn't ever done it. The
attached takes care of such stuff, I think.

>> Also, a call to to plperl_call_perl_func should be cast to void to avoid a possible compiler warning (although It doesn't emit one on my system):
>>
>> (void) plperl_call_perl_func(&desc, &fake_fcinfo);
>
> Right.

I don't get the warning either, and didn't realize it could produce one.
Thanks -- that change is also in the attached version.

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com

Attachment Content-Type Size
plperl-inline.patch text/x-diff 9.3 KB

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: Alexey Klyukin <alexk(at)waki(dot)ru>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-18 03:46:31
Message-ID: 4B036E17.1060402@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Joshua Tolley wrote:
> + plperl_call_data *save_call_data = current_call_data;
> + bool oldcontext = trusted_context;
> +
> + if (SPI_connect() != SPI_OK_CONNECT)
> + elog(ERROR, "could not connect to SPI manager");
>
...
> + current_call_data = (plperl_call_data *) palloc0(sizeof(plperl_call_data));
> + current_call_data->fcinfo = &fake_fcinfo;
> + current_call_data->prodesc = &desc;
>

I don't think this is done in the right order. If it is then this
comment in plperl_func_handler is wrong (as well as containing a typo):

/*
* Create the call_data beforing connecting to SPI, so that it is not
* allocated in the SPI memory context
*/

cheers

andrew


From: Alexey Klyukin <alexk(at)commandprompt(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Joshua Tolley <eggyknap(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-18 10:38:00
Message-ID: E072FC9F-B9FB-4EB7-94CC-9B8EE8ED98D7@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On Nov 18, 2009, at 5:46 AM, Andrew Dunstan wrote:

>
>
> Joshua Tolley wrote:
>> + plperl_call_data *save_call_data = current_call_data;
>> + bool oldcontext = trusted_context;
>> + + if (SPI_connect() != SPI_OK_CONNECT)
>> + elog(ERROR, "could not connect to SPI manager");
>>
> ...
>> + current_call_data = (plperl_call_data *) palloc0(sizeof(plperl_call_data));
>> + current_call_data->fcinfo = &fake_fcinfo;
>> + current_call_data->prodesc = &desc;
>>
>
> I don't think this is done in the right order. If it is then this comment in plperl_func_handler is wrong (as well as containing a typo):
>
> /*
> * Create the call_data beforing connecting to SPI, so that it is not
> * allocated in the SPI memory context
> */
>

Yes, current_call_data can't be allocate in the SPI memory context, since it's used to extract the result after SPI_finish is called, although it doesn't lead to problems here since no result is returned. Anyway, I'd move SPI_connect after the current_call_data initialization.

I also noticed that no error context is set in the inline handler, not sure whether it really useful except for the sake of consistency, but in case it is - here is the patch:

Attachment Content-Type Size
inline_callback.diff application/octet-stream 2.1 KB

From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: Alexey Klyukin <alexk(at)commandprompt(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-20 00:04:22
Message-ID: 20091120000422.GO3803@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Nov 18, 2009 at 12:38:00PM +0200, Alexey Klyukin wrote:
> Yes, current_call_data can't be allocate in the SPI memory context, since it's used to extract the result after SPI_finish is called, although it doesn't lead to problems here since no result is returned. Anyway, I'd move SPI_connect after the current_call_data initialization.
>
> I also noticed that no error context is set in the inline handler, not sure whether it really useful except for the sake of consistency, but in case it is - here is the patch:

Makes sense on both counts. Thanks for the help. How does the attached look?

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com

Attachment Content-Type Size
plperl-inline.patch text/x-diff 10.4 KB

From: u235sentinel <u235sentinel(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: plruby code and postgres ?
Date: 2009-11-20 00:15:05
Message-ID: 4B05DF89.8030400@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Does anyone have a link for pl/ruby? I found a link under the postgres
documentation and found a web site from there talking about the code.
However when I clicked on the link to download it I noticed ftp wouldn't
respond on their site.

Thanks!


From: Tim Bunce <Tim(dot)Bunce(at)pobox(dot)com>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: Alexey Klyukin <alexk(at)commandprompt(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-20 13:50:04
Message-ID: 20091120135004.GH80631@timac.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Nov 19, 2009 at 05:04:22PM -0700, Joshua Tolley wrote:
> The body of the function is ordinary Perl code. In fact, the PL/Perl
> ! glue code wraps it inside a Perl subroutine. Anonymous code blocks cannot
> ! return a value; PL/Perl functions created with CREATE FUNCTION must always
> ! return a scalar value. You can return more complex structures (arrays,
> ! records, and sets) by returning a reference, as discussed below. Never
> ! return a list.
> </para>

The "must always" and "Never return a list" seem needlessly strong, not
very helpful, and slightly misleading. The key point is that the call is
made in a scalar context. The implications of that follow naturally.

I'd suggest:

...; PL/Perl functions created with CREATE FUNCTION are called in a
scalar context, so can't return a list. You can return more complex
structures (arrays, records, and sets) by returning a reference, as
discussed below.

That only mentions "functions created with CREATE FUNCTION" though.
Perhaps it needs to be generalized to cover DO as well.

> + Datum
> + plperl_inline_handler(PG_FUNCTION_ARGS)
> + {

> + desc.proname = "Do Inline Block";

> + PG_TRY();
> + {
> +
> + desc.reference = plperl_create_sub("DO Inline Block",
> + codeblock->source_text,
> + desc.lanpltrusted);
> +
> + (void) plperl_call_perl_func(&desc, &fake_fcinfo);
> + }
> + PG_CATCH();
> + {
> + error_context_stack = pl_error_context.previous;
> + current_call_data = save_call_data;
> + restore_context(oldcontext);
> + PG_RE_THROW();
> + }
> + PG_END_TRY();
> +
> + if (SPI_finish() != SPI_OK_FINISH)
> + elog(ERROR, "SPI_finish() failed");
> +
> + error_context_stack = pl_error_context.previous;
> + current_call_data = save_call_data;
> + restore_context(oldcontext);
> +
> + PG_RETURN_VOID();

When does the reference held by desc.reference get freed?
At the moment it looks like this would leak memory for each DO.

> + static void
> + plperl_inline_callback(void *arg)
> + {
> + errcontext("PL/Perl anonymous code block");
> + }

I'd like to see more consistent terminlogy:

desc.proname = "Do Inline Block";
plperl_create_sub("DO Inline Block",
errcontext("PL/Perl anonymous code block");

Tim.


From: Alexey Klyukin <alexk(at)commandprompt(dot)com>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-20 14:34:45
Message-ID: 3E4C20CD-AEE7-4C33-BC07-2F036E0C8ED9@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On Nov 20, 2009, at 2:04 AM, Joshua Tolley wrote:

> On Wed, Nov 18, 2009 at 12:38:00PM +0200, Alexey Klyukin wrote:
>> Yes, current_call_data can't be allocate in the SPI memory context, since it's used to extract the result after SPI_finish is called, although it doesn't lead to problems here since no result is returned. Anyway, I'd move SPI_connect after the current_call_data initialization.
>>
>> I also noticed that no error context is set in the inline handler, not sure whether it really useful except for the sake of consistency, but in case it is - here is the patch:
>
> Makes sense on both counts. Thanks for the help. How does the attached look?

These two problems seem to be fixed now, thank you.

--
Alexey Klyukin http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc


From: Alexey Klyukin <alexk(at)commandprompt(dot)com>
To: Tim Bunce <Tim(dot)Bunce(at)pobox(dot)com>
Cc: Joshua Tolley <eggyknap(at)gmail(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-20 14:40:14
Message-ID: ED9C64F7-3263-4103-8DB9-0C25136B9EC3@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On Nov 20, 2009, at 3:50 PM, Tim Bunce wrote:

>
> When does the reference held by desc.reference get freed?
> At the moment it looks like this would leak memory for each DO.

Isn't it also the case with the existing plperl code ? I've noticed that free(prodesc) is called when it's no longer used (i.e. in plperl_compile_callback:1636), but refcount of desc->reference is never decremented.

--
Alexey Klyukin http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc


From: "Ross J(dot) Reedstrom" <reedstrm(at)rice(dot)edu>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: plruby code and postgres ?
Date: 2009-11-20 16:36:23
Message-ID: 20091120163623.GB21786@rice.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Nov 19, 2009 at 05:15:05PM -0700, u235sentinel wrote:
> Does anyone have a link for pl/ruby? I found a link under the postgres
> documentation and found a web site from there talking about the code.
> However when I clicked on the link to download it I noticed ftp wouldn't
> respond on their site.

Debian's got a copy of the original tarball or the most recently release
version:

http://packages.debian.org/source/lenny/postgresql-plruby

And links there in.

Ross
--
Ross Reedstrom, Ph.D. reedstrm(at)rice(dot)edu
Systems Engineer & Admin, Research Scientist phone: 713-348-6166
The Connexions Project http://cnx.org fax: 713-348-3665
Rice University MS-375, Houston, TX 77005
GPG Key fingerprint = F023 82C8 9B0E 2CC6 0D8E F888 D3AE 810E 88F0 BEDE


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Tim Bunce <Tim(dot)Bunce(at)pobox(dot)com>
Cc: Joshua Tolley <eggyknap(at)gmail(dot)com>, Alexey Klyukin <alexk(at)commandprompt(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-21 02:30:24
Message-ID: 9958FC3F-862E-406C-A8C5-51FFCC154C94@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Nov 20, 2009, at 10:50 PM, Tim Bunce wrote:

> I'd suggest:
>
> ...; PL/Perl functions created with CREATE FUNCTION are called in a
> scalar context, so can't return a list. You can return more complex
> structures (arrays, records, and sets) by returning a reference, as
> discussed below.
>
> That only mentions "functions created with CREATE FUNCTION" though.
> Perhaps it needs to be generalized to cover DO as well.

FWIW, DO is run in a VOID context. Return values are ignored (or perhaps trigger an exception?).

Best,

David


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexey Klyukin <alexk(at)commandprompt(dot)com>
Cc: Tim Bunce <Tim(dot)Bunce(at)pobox(dot)com>, Joshua Tolley <eggyknap(at)gmail(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-29 02:40:34
Message-ID: 2733.1259462434@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alexey Klyukin <alexk(at)commandprompt(dot)com> writes:
> On Nov 20, 2009, at 3:50 PM, Tim Bunce wrote:
>> When does the reference held by desc.reference get freed?
>> At the moment it looks like this would leak memory for each DO.

> Isn't it also the case with the existing plperl code ? I've noticed that free(prodesc) is called when it's no longer used (i.e. in plperl_compile_callback:1636), but refcount of desc->reference is never decremented.

I've been experimenting with this and confirmed that there is a leak;
not only in the DO patch but in the pre-existing code, if a plperl
function is redefined repeatedly.

Is this the correct way to release the SV* reference?

if (reference)
SvREFCNT_dec(reference);

regards, tom lane


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: Alexey Klyukin <alexk(at)commandprompt(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-29 03:15:40
Message-ID: 3725.1259464540@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Joshua Tolley <eggyknap(at)gmail(dot)com> writes:
> Makes sense on both counts. Thanks for the help. How does the attached look?

Applied with minor corrections, mainly around the state save/restore
logic. I also put in some code to fix the memory leak noted by Tim Bunce,
but am waiting for some confirmation that it's right before
back-patching the pre-existing bug of the same ilk.

regards, tom lane


From: Alexey Klyukin <alexk(at)commandprompt(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Tim Bunce <Tim(dot)Bunce(at)pobox(dot)com>, Joshua Tolley <eggyknap(at)gmail(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-29 09:04:39
Message-ID: AAA71B8A-8900-478E-9CFF-72E32C9A32A4@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On Nov 29, 2009, at 4:40 AM, Tom Lane wrote:

> Alexey Klyukin <alexk(at)commandprompt(dot)com> writes:
>
>> Isn't it also the case with the existing plperl code ? I've noticed that free(prodesc) is called when it's no longer used (i.e. in plperl_compile_callback:1636), but refcount of desc->reference is never decremented.
>
> I've been experimenting with this and confirmed that there is a leak;
> not only in the DO patch but in the pre-existing code, if a plperl
> function is redefined repeatedly.
>
> Is this the correct way to release the SV* reference?
>
> if (reference)
> SvREFCNT_dec(reference);

Yes. In fact this only decreases the reference count, making the interpreter free the memory referred to when it becomes 0, but since prodesc->reference has refcount of 1 this would do the right thing.

--
Alexey Klyukin http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc


From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Alexey Klyukin <alexk(at)commandprompt(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: plperl and inline functions -- first draft
Date: 2009-11-29 22:29:08
Message-ID: 20091129222908.GN3803@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, Nov 28, 2009 at 10:15:40PM -0500, Tom Lane wrote:
> Joshua Tolley <eggyknap(at)gmail(dot)com> writes:
> > Makes sense on both counts. Thanks for the help. How does the attached look?
>
> Applied with minor corrections, mainly around the state save/restore
> logic. I also put in some code to fix the memory leak noted by Tim Bunce,
> but am waiting for some confirmation that it's right before
> back-patching the pre-existing bug of the same ilk.
>
> regards, tom lane

Yay, and thanks. For the record, I'm can't claim to know whether your fix is
the Right Thing or not, so I'm witholding comment.

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com