Re: Status of plperl inter-sp calling

From: Tim Bunce <Tim(dot)Bunce(at)pobox(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Tim Bunce <Tim(dot)Bunce(at)pobox(dot)com>
Subject: Re: Status of plperl inter-sp calling
Date: 2010-01-05 22:31:19
Message-ID: 20100105223119.GK2505@timac.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Ok, Plan B...

Consider this (hypothetical) example:

CREATE OR REPLACE FUNCTION test() ... LANGUAGE plperl AS $$

use SP foo_int => 'foo(int)';
use SP foo_text => 'foo(text)', -cached;

foo_int(42);
foo_text(42);
...
$$

Here the user is importing into their function, at load/compile-time,
aliases for specific stored procedures with specific type signatures.

The importer builds and imports a custom closure.
At its most basic it would be something like:

my $h = spi_prepare('select foo($1)', 'text');
return sub { spi_exec_prepared($h, @_)->{rows} }

or perhaps, with added lazy smartness:

my $mk = sub { spi_prepare('select foo($1)', 'text') };
my $h; # initialized on first use
record_handle_for_later_freeing_if_needed(\$h);
return sub { spi_exec_prepared($h ||= $mk->(), @_)->{rows} }

As much as possible has been pre-computed. All foo_text() does is
call spi_exec_prepared and do something (to be decided) with the results.

That's likely to be fast enough to negate much of the desire for
caching. It'll also work for all functions in all languages.
I added an example with -cached above to indicate how extra attributes
could be specified to influence the behaviour of the import-time code
builder.

The code builder only needs to handle a few simple cases initially.
Enough to cover at least nargs and type polymorphism. I'd guess that
VARADIC won't be too hard, but I'll probably skip OUT & INOUT.
There probably won't be explicit support for DEFAULT args - just
import another alias that has the default arg missing.

The only question I have at the moment, before I try implementing this,
is the the need for freeing the plan. When would that be needed?
(Note that this scheme will only generate a fixed set of plans,
one per specific function name and type signature.)
Can someone give me some real-world examples? For example, does a plan
become 'broken' if an object it references gets dropped and recreated?
Assuming it does, or there's some other need to free/recreate plans,
then I can add a function call to do that (by recording a reference
to the $h's in the example above and using that to undef them).

Does the above sound workable? Anything I've missed?

Tim.

p.s. My earlier plperl feature patch enabled the use of 'use' within
plperl stored procedures - but only for modules that have been explicitly
configured and pre-loaded.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Arie Bikker 2010-01-05 23:09:32 xpath improvement suggestion
Previous Message Simon Riggs 2010-01-05 22:07:34 Re: Somebody has broken autovacuum's abort path