BUG #6511: calling spi_exec_query from non-main package, results in: couldn't fetch $_TD

Lists: pgsql-bugs
From: marko(at)kobaz(dot)net
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #6511: calling spi_exec_query from non-main package, results in: couldn't fetch $_TD
Date: 2012-03-03 19:19:48
Message-ID: E1S3uUe-0005ER-RD@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 6511
Logged by: Mark Murawski
Email address: marko(at)kobaz(dot)net
PostgreSQL version: 9.1.3
Operating system: Linux - Debian Squeeze postgres 9.1 from backports
Description:

create table mytable ( id integer );

--
CREATE OR REPLACE FUNCTION mytrigger() RETURNS trigger AS $BODY$
return undef
$BODY$ LANGUAGE plperlu VOLATILE COST 100;

--
CREATE TRIGGER "insert" BEFORE INSERT ON mytable FOR EACH ROW EXECUTE
PROCEDURE mytrigger();

--
CREATE OR REPLACE FUNCTION myfunc() RETURNS text AS $BODY$
package foo;
sub foo {
main::spi_exec_query(q{INSERT INTO mytable VALUES (1) RETURNING id});
};

package main;
foo::foo();
return;
$BODY$ LANGUAGE plperlu VOLATILE COST 100;

--

pbx=# select * from myfunc();
ERROR: couldn't fetch $_TD at line 4.
CONTEXT: PL/Perl function "myfunc"

-- this works:

CREATE OR REPLACE FUNCTION myfunc() RETURNS text AS $BODY$
package foo;
sub foo {
package main;
spi_exec_query(q{INSERT INTO mytable VALUES (1) RETURNING id});
};

package main;
foo::foo();
return;
$BODY$ LANGUAGE plperlu VOLATILE COST 100;

select * from myfunc();
myfunc
--------

(1 row)

drop table mytable;
drop function mytrigger();
drop function myfunc();


From: Alex Hunsaker <badalex(at)gmail(dot)com>
To: marko(at)kobaz(dot)net
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #6511: calling spi_exec_query from non-main package, results in: couldn't fetch $_TD
Date: 2012-03-07 04:08:25
Message-ID: CAFaPBrQRwr7OevWjeFwU2+E34Vt+wfy1_GmhNm4=Yw0_dPLL_w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Sat, Mar 3, 2012 at 12:19, <marko(at)kobaz(dot)net> wrote:
> The following bug has been logged on the website:
>
> Bug reference:      6511
> Logged by:          Mark Murawski
> Email address:      marko(at)kobaz(dot)net
> PostgreSQL version: 9.1.3
> Operating system:   Linux - Debian Squeeze postgres 9.1 from backports
> Description:
>
> CREATE OR REPLACE FUNCTION myfunc()  RETURNS text AS $BODY$
> package foo;
> sub foo {
>  main::spi_exec_query(q{INSERT INTO mytable VALUES (1) RETURNING id});
> };
>
> package main;
> foo::foo();
> return;
> $BODY$  LANGUAGE plperlu VOLATILE  COST 100;
>
> --
>
> pbx=# select * from myfunc();
> ERROR:  couldn't fetch $_TD at line 4.
> CONTEXT:  PL/Perl function "myfunc"

[ Calling a plperl trigger function from a plperl function ]

Yeah, there were some optimization done for 9.1 to try and make calls
a bit faster. The problem is we are fetching "_TD" not "main::_TD",
which means we try to find and use $_TD from whatever the current
package is. This should only happen from a nested plperl to plperl
trigger where the outer call was in a different package, otherwise the
package is always main.

The attached fixes it for me, It would be great if you could confirm that.

Thanks for the report!

Attachment Content-Type Size
plperl_nested_pkg_trigger.patch text/x-patch 419 bytes

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Alex Hunsaker <badalex(at)gmail(dot)com>
Cc: marko(at)kobaz(dot)net, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #6511: calling spi_exec_query from non-main package, results in: couldn't fetch $_TD
Date: 2012-03-15 22:13:51
Message-ID: 20120315221351.GB20113@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Tue, Mar 06, 2012 at 09:08:25PM -0700, Alex Hunsaker wrote:
> [ Calling a plperl trigger function from a plperl function ]
>
> Yeah, there were some optimization done for 9.1 to try and make calls
> a bit faster. The problem is we are fetching "_TD" not "main::_TD",
> which means we try to find and use $_TD from whatever the current
> package is. This should only happen from a nested plperl to plperl
> trigger where the outer call was in a different package, otherwise the
> package is always main.
>
> The attached fixes it for me, It would be great if you could confirm that.
>
> Thanks for the report!

So, should this be applied?

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

> *** a/src/pl/plperl/plperl.c
> --- b/src/pl/plperl/plperl.c
> ***************
> *** 2062,2068 **** plperl_call_perl_trigger_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo,
> ENTER;
> SAVETMPS;
>
> ! TDsv = get_sv("_TD", 0);
> if (!TDsv)
> elog(ERROR, "couldn't fetch $_TD");
>
> --- 2062,2068 ----
> ENTER;
> SAVETMPS;
>
> ! TDsv = get_sv("main::_TD", 0);
> if (!TDsv)
> elog(ERROR, "couldn't fetch $_TD");
>

>
> --
> Sent via pgsql-bugs mailing list (pgsql-bugs(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-bugs

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +


From: Alex Hunsaker <badalex(at)gmail(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: marko(at)kobaz(dot)net, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #6511: calling spi_exec_query from non-main package, results in: couldn't fetch $_TD
Date: 2012-03-16 17:48:24
Message-ID: CAFaPBrTXmwj+iAMe2g7RsGg5qBm5KGvNJVy7Nezo0mZyjwYqhA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Thu, Mar 15, 2012 at 16:13, Bruce Momjian <bruce(at)momjian(dot)us> wrote:
> On Tue, Mar 06, 2012 at 09:08:25PM -0700, Alex Hunsaker wrote:
>> [ Calling a plperl trigger function from a plperl function ]
>>
>> Yeah, there were some optimization done for 9.1 to try and make calls
>> a bit faster. The problem is we are fetching "_TD" not "main::_TD",
>> which means we try to find and use $_TD from whatever the current
>> package is. This should only happen from a nested plperl to plperl
>> trigger where the outer call was in a different package, otherwise the
>> package is always main.
>>
>> The attached fixes it for me, It would be great if you could confirm that.
>>
>> Thanks for the report!
>
> So, should this be applied?

Yes, but I would like to get conformation that it fixes the issue.
Either way Ill probably just end up submitting it to the next commit
fest. (I know most commiters are still busy with the current one).


From: "Mark M(dot)" <marko(at)kobaz(dot)net>
To: Alex Hunsaker <badalex(at)gmail(dot)com>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #6511: calling spi_exec_query from non-main package, results in: couldn't fetch $_TD
Date: 2012-03-18 04:11:48
Message-ID: 4F656084.2050207@kobaz.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On 03/16/12 13:48, Alex Hunsaker wrote:
> On Thu, Mar 15, 2012 at 16:13, Bruce Momjian<bruce(at)momjian(dot)us> wrote:
>> On Tue, Mar 06, 2012 at 09:08:25PM -0700, Alex Hunsaker wrote:
>>> [ Calling a plperl trigger function from a plperl function ]
>>>
>>> Yeah, there were some optimization done for 9.1 to try and make calls
>>> a bit faster. The problem is we are fetching "_TD" not "main::_TD",
>>> which means we try to find and use $_TD from whatever the current
>>> package is. This should only happen from a nested plperl to plperl
>>> trigger where the outer call was in a different package, otherwise the
>>> package is always main.
>>>
>>> The attached fixes it for me, It would be great if you could confirm that.
>>>
>>> Thanks for the report!
>> So, should this be applied?
> Yes, but I would like to get conformation that it fixes the issue.
> Either way Ill probably just end up submitting it to the next commit
> fest. (I know most commiters are still busy with the current one).

Fixes the problem. Thanks. I always like one line fixes.


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Alex Hunsaker <badalex(at)gmail(dot)com>
Cc: marko <marko(at)kobaz(dot)net>, Pg Bugs <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #6511: calling spi_exec_query from non-main package, results in: couldn't fetch $_TD
Date: 2012-03-19 20:40:37
Message-ID: 1332189577-sup-8497@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


Excerpts from Alex Hunsaker's message of mié mar 07 01:08:25 -0300 2012:
> On Sat, Mar 3, 2012 at 12:19, <marko(at)kobaz(dot)net> wrote:

> Yeah, there were some optimization done for 9.1 to try and make calls
> a bit faster. The problem is we are fetching "_TD" not "main::_TD",
> which means we try to find and use $_TD from whatever the current
> package is. This should only happen from a nested plperl to plperl
> trigger where the outer call was in a different package, otherwise the
> package is always main.
>
> The attached fixes it for me, It would be great if you could confirm that.

Thanks -- applied to 9.1 and master.

--
Álvaro Herrera <alvherre(at)commandprompt(dot)com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Alex Hunsaker <badalex(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: marko <marko(at)kobaz(dot)net>, Pg Bugs <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #6511: calling spi_exec_query from non-main package, results in: couldn't fetch $_TD
Date: 2012-03-21 18:16:19
Message-ID: CAFaPBrTL5YYR1LyDvNvJJZep=x=tqXaEPdE8tW5et+8W-i+3vw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Mon, Mar 19, 2012 at 14:40, Alvaro Herrera
<alvherre(at)commandprompt(dot)com> wrote:
>
> Excerpts from Alex Hunsaker's message of mié mar 07 01:08:25 -0300 2012:

>> The attached fixes it for me, It would be great if you could confirm that.
>
> Thanks -- applied to 9.1 and master.

Thanks!