Re: python modul pre-import to avoid importing each time

From: Rémi Cura <remi(dot)cura(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>, PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: python modul pre-import to avoid importing each time
Date: 2014-06-27 08:18:08
Message-ID: CAJvUf_vMooVeLfM7XWr7TqGenM2oiQBV7AgxXg8WUc9GwkcHcA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hey,
thanks, now we have good information:

the python package are really loaded once per connection, so no
optimization is needed.
Unlike plperl or plR there is no easy way to preload packages.
There may be some solutions to make this import at connection start but it
would involve C modification (found no trace of python file or hackable sql
script in postgres source and install directory)

After that,
further optimization is possible by avoiding the useless 'import' (because
it is already loaded) (see the trick here
<http://stackoverflow.com/questions/15023080/how-are-import-statements-in-plpython-handled>
)
,however benefits are not proven.

My use case is simple geometry manipulation functions. It is easier to use
plpython rather than plpgsql thanks to numpy for vector manipulation.
Usually the functions are called inside complex query with many CTE, and
execute over 100k of rows. Total execution time is in the order of minutes.
(exemple of querry at the end)

Thanks everybody,
Rémi

Example of querry
CREATE TABLE holding_result AS
WITH the_geom AS (
SELECT gid, geom
FROM my_big_table --200k rows
)
SELECT gid, my_python_function(geom) AS result
FROM the_geom;

2014-06-27 4:27 GMT+02:00 Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>:

> On 06/26/2014 02:14 AM, Rémi Cura wrote:
>
>> Hey,
>> thanks for your answer !
>>
>> Yep you are right, the function I would like to test are going to be
>> called a lot (100k times), so even 15 ms per call matters.
>>
>>
> I got to thinking about this.
>
> 100K over what time frame?
>
> How is it being called?
>
>
>
>
> --
> Adrian Klaver
> adrian(dot)klaver(at)aklaver(dot)com
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message James Le Cuirot 2014-06-27 09:28:03 Re: Alternative to psql -c ?
Previous Message hubert depesz lubaczewski 2014-06-27 07:15:23 Re: Repeated semop calls