Re: [GENERAL] PLPythonu for production server
Thanks ! Cheers, Rémi-C 2016-03-05 0:38 GMT+01:00 Adrian Klaver: > On 03/04/2016 01:46 AM, Rémi Cura wrote: > >> Thanks for the answer guys. >> >> I should have mentionned that I had read the doc, >> and was looking for non explicit knowledge, >> like : >> - what is the reputation of plpython for a dba? >> - are there actual production system that use it >> - what would be the recommended usage perimeter ? >>(only administration script like function, advanced processing, etc >> ...) >> > > An example: > > http://bonesmoses.org/2016/03/04/pg-phriday-being-a-tattletale/ > > > >> Cheers, >> Rémi-C >> >> > > -- > Adrian Klaver > adrian.kla...@aklaver.com >
Re: [GENERAL] PLPythonu for production server
On 03/04/2016 01:46 AM, Rémi Cura wrote: Thanks for the answer guys. I should have mentionned that I had read the doc, and was looking for non explicit knowledge, like : - what is the reputation of plpython for a dba? - are there actual production system that use it - what would be the recommended usage perimeter ? (only administration script like function, advanced processing, etc ...) An example: http://bonesmoses.org/2016/03/04/pg-phriday-being-a-tattletale/ Cheers, Rémi-C -- Adrian Klaver adrian.kla...@aklaver.com -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
Re: [GENERAL] PLPythonu for production server
On 4 March 2016 at 10:46, Rémi Curawrote: > Thanks for the answer guys. > > I should have mentionned that I had read the doc, > and was looking for non explicit knowledge, > like : > - what is the reputation of plpython for a dba? > Dunno. - are there actual production system that use it > I used it in an SMS processing system and it worked like a charm. That system never made it to the production phase, though. > - what would be the recommended usage perimeter ? > (only administration script like function, advanced processing, etc ...) > I would say, in general, use untrusted languages only if the trusted ones are not capable or practical for your goals. (Reading/writing/processing data outside of the database, using third party tools for processing, etc.) Regards, Sándor
Re: [GENERAL] PLPythonu for production server
Thanks for the answer guys. I should have mentionned that I had read the doc, and was looking for non explicit knowledge, like : - what is the reputation of plpython for a dba? - are there actual production system that use it - what would be the recommended usage perimeter ? (only administration script like function, advanced processing, etc ...) Cheers, Rémi-C 2016-03-03 20:55 GMT+01:00 David G. Johnston: > On Thu, Mar 3, 2016 at 12:35 PM, Adrian Klaver > wrote: > >> On 03/03/2016 10:09 AM, Rémi Cura wrote: >> >>> Hey List, >>> >>> would it be considered safe to use plpythonu for a production database? >>> What would be the limitations/ dangers? >>> >> >> They are explained here: >> >> http://www.postgresql.org/docs/9.5/interactive/plpython.html >> >> "PL/Python is only available as an "untrusted" language, meaning it does >> not offer any way of restricting what users can do in it and is therefore >> named plpythonu. A trusted variant plpython might become available in the >> future if a secure execution mechanism is developed in Python. The writer >> of a function in untrusted PL/Python must take care that the function >> cannot be used to do anything unwanted, since it will be able to do >> anything that could be done by a user logged in as the database >> administrator. Only superusers can create functions in untrusted languages >> such as plpythonu." >> > > See also: > > http://www.postgresql.org/docs/9.5/interactive/sql-grant.html > > GRANT { USAGE | ALL [ PRIVILEGES ] } > ON LANGUAGE lang_name [, ...] > TO role_specification [, ...] [ WITH GRANT OPTION ] > > and > > GRANT { EXECUTE | ALL [ PRIVILEGES ] } > ON { FUNCTION function_name ( [ [ argmode ] [ arg_name ] arg_type [, > ...] ] ) [, ...] > | ALL FUNCTIONS IN SCHEMA schema_name [, ...] } > TO role_specification [, ...] [ WITH GRANT OPTION ] > > David J. > >
Re: [GENERAL] PLPythonu for production server
On Thu, Mar 3, 2016 at 12:35 PM, Adrian Klaverwrote: > On 03/03/2016 10:09 AM, Rémi Cura wrote: > >> Hey List, >> >> would it be considered safe to use plpythonu for a production database? >> What would be the limitations/ dangers? >> > > They are explained here: > > http://www.postgresql.org/docs/9.5/interactive/plpython.html > > "PL/Python is only available as an "untrusted" language, meaning it does > not offer any way of restricting what users can do in it and is therefore > named plpythonu. A trusted variant plpython might become available in the > future if a secure execution mechanism is developed in Python. The writer > of a function in untrusted PL/Python must take care that the function > cannot be used to do anything unwanted, since it will be able to do > anything that could be done by a user logged in as the database > administrator. Only superusers can create functions in untrusted languages > such as plpythonu." > See also: http://www.postgresql.org/docs/9.5/interactive/sql-grant.html GRANT { USAGE | ALL [ PRIVILEGES ] } ON LANGUAGE lang_name [, ...] TO role_specification [, ...] [ WITH GRANT OPTION ] and GRANT { EXECUTE | ALL [ PRIVILEGES ] } ON { FUNCTION function_name ( [ [ argmode ] [ arg_name ] arg_type [, ...] ] ) [, ...] | ALL FUNCTIONS IN SCHEMA schema_name [, ...] } TO role_specification [, ...] [ WITH GRANT OPTION ] David J.
Re: [GENERAL] PLPythonu for production server
On 03/03/2016 10:09 AM, Rémi Cura wrote: Hey List, would it be considered safe to use plpythonu for a production database? What would be the limitations/ dangers? They are explained here: http://www.postgresql.org/docs/9.5/interactive/plpython.html "PL/Python is only available as an "untrusted" language, meaning it does not offer any way of restricting what users can do in it and is therefore named plpythonu. A trusted variant plpython might become available in the future if a secure execution mechanism is developed in Python. The writer of a function in untrusted PL/Python must take care that the function cannot be used to do anything unwanted, since it will be able to do anything that could be done by a user logged in as the database administrator. Only superusers can create functions in untrusted languages such as plpythonu." Thanks, Cheers, Rémi-C -- Adrian Klaver adrian.kla...@aklaver.com -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
[GENERAL] PLPythonu for production server
Hey List, would it be considered safe to use plpythonu for a production database? What would be the limitations/ dangers? Thanks, Cheers, Rémi-C
Re: [GENERAL] plpythonu memory leak
Alex Hunsaker writes: FYI if I don't use a slice copy here I can't get it to leak. ( find my test case at the end ) I don't know enough about python to know if thats a pl/python issue or python doing what its told-- having never really wrote any python myself. --- -- leaks big time CREATE or replace FUNCTION pygaps_leak() RETURNS void LANGUAGE plpythonu AS $$ results = plpy.execute(select generate_series(0, 100)) prev = results[0] for curr in results[1:]: prev = curr return $$; -- does not leak CREATE or replace FUNCTION pygaps_no_leak() RETURNS void LANGUAGE plpythonu AS $$ results = plpy.execute(select generate_series(0, 100)) prev = results[0] for curr in range(1, len(results)): prev = curr return $$; Alex, Great find! Yes, it's definitely leaking when taking a slice. Something is hanging on to the reference to the slice object and/or the reference count is not properly managed: I modified your leak function and added explicit calls to the python garbage collector with no result. I'll hunt around in the source for the leak. Regardless of my findings, I'll submit a bug. Thanks! Dan Popowich -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
Re: [GENERAL] plpythonu memory leak
I found the bug and it has been reported. Bug #5842. Details here: http://archives.postgresql.org/pgsql-bugs/2011-01/msg00134.php Dan Popowich -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
Re: [GENERAL] plpythonu memory leak
On Fri, Jan 14, 2011 at 19:14, Daniel Popowich danielpopow...@gmail.com wrote: [ snip ] CREATE FUNCTION pygaps(start_ts timestamp without time zone, end_ts timestamp without time zone, gap_length interval) RETURNS SETOF timerange LANGUAGE plpythonu AS $$ # because pg passes date/time to python as strings I'm using pg to # recompute values as seconds so I have numbers to do math gap = plpy.execute(select extract(epoch from '%s'::interval) as sec % gap_length)[0]['sec'] results = plpy.execute(select ts, extract(epoch from ts) as epoch from timeseries where ts between '%s' and '%s' % (start_ts, end_ts)) if results.nrows() 2: return # prime the well by setting prev(ious) to the first tic and # iterate starting with the second... prev = results[0] for curr in results[1:]: FYI if I don't use a slice copy here I can't get it to leak. ( find my test case at the end ) I don't know enough about python to know if thats a pl/python issue or python doing what its told-- having never really wrote any python myself. --- -- leaks big time CREATE or replace FUNCTION pygaps_leak() RETURNS void LANGUAGE plpythonu AS $$ results = plpy.execute(select generate_series(0, 100)) prev = results[0] for curr in results[1:]: prev = curr return -- does not leak CREATE or replace FUNCTION pygaps_no_leak() RETURNS void LANGUAGE plpythonu AS $$ results = plpy.execute(select generate_series(0, 100)) prev = results[0] for curr in range(1, len(results)): prev = curr return -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
[GENERAL] plpythonu memory leak
I am working with very large sets of time-series data. Imagine a table with a timestamp as the primary key. One question I need to ask of my data is: Are there gaps of time greater than some interval between consecutive rows? I wrote a function in plpgsql to answer this question and it worked great. Being a python zealot I decided to rewrite the function in plpythonu to compare performance. While initial comparisons seemed inconclusive, after testing on large queries (over a million records) I discovered ever-increasing time to complete the exact same query and massive memory growth in my postgres process to the point of memory starvation in under 15 queries. I've reduced my my schema to one table with one timestamp column, one type and two functions in a schema named plpythonu_bug and saved with: `pg_dump -n plpythonu_bug -s -O bug.sql`. It is attached. Here are some statistics on two separate psql sessions, one where I ran this plpgsql function several times: EXPLAIN ANALYZE SELECT count(*) from gaps('2008-01-01', '2010-01-01', '1 min'); Then a second session running the exact same query but with the plpythonu function, pygaps. Note: I had over 273,000 rows in my table. The function returned 5103 rows each run. Memory usage is from `top` output. Milliseconds, from output of explain analyze. This is on an Ubuntu 10.04 system w/ 2GB RAM, postgres 8.4.6, python 2.6.5. plpgsql function Run # Virt Resms before 101m 3500 n/a 1 103m 17m584 2 104m 17m561 3 104m 18m579 ...etc... (virtually no movement over several runs) plpythonu function -- Run # Virt Resms before 101m 3492 n/a 1 213m 122m 1836 2 339m 246m 1784 3 440m 346m 2178 ...and so on, about 100m or so increase with each run such that in a dozen or so runs I had 1.5g in resident memory and single calls to the function taking over 45 seconds. My schema is attached. Thanks for any help and insight, Dan Popowich -- -- PostgreSQL database dump -- SET statement_timeout = 0; SET client_encoding = 'UTF8'; SET standard_conforming_strings = off; SET check_function_bodies = false; SET client_min_messages = warning; SET escape_string_warning = off; -- -- Name: plpythonu_bug; Type: SCHEMA; Schema: -; Owner: - -- CREATE SCHEMA plpythonu_bug; SET search_path = plpythonu_bug, pg_catalog; -- -- Name: timerange; Type: TYPE; Schema: plpythonu_bug; Owner: - -- CREATE TYPE timerange AS ( begin_ts timestamp without time zone, end_ts timestamp without time zone ); -- -- Name: gaps(timestamp without time zone, timestamp without time zone, interval); Type: FUNCTION; Schema: plpythonu_bug; Owner: - -- CREATE FUNCTION gaps(start_ts timestamp without time zone, end_ts timestamp without time zone, gap_length interval) RETURNS SETOF timerange LANGUAGE plpgsql AS $$ DECLARE prev timestamp; curr timestamp; tr timerange; BEGIN FOR curr IN SELECT ts FROM timeseries WHERE ts BETWEEN start_ts AND end_ts ORDER BY ts LOOP IF curr - prev gap_length THEN tr.begin_ts := prev; tr.end_ts := curr; RETURN NEXT tr; END IF; prev := curr; END LOOP; RETURN; END; $$; -- -- Name: pygaps(timestamp without time zone, timestamp without time zone, interval); Type: FUNCTION; Schema: plpythonu_bug; Owner: - -- CREATE FUNCTION pygaps(start_ts timestamp without time zone, end_ts timestamp without time zone, gap_length interval) RETURNS SETOF timerange LANGUAGE plpythonu AS $$ # because pg passes date/time to python as strings I'm using pg to # recompute values as seconds so I have numbers to do math gap = plpy.execute(select extract(epoch from '%s'::interval) as sec % gap_length)[0]['sec'] results = plpy.execute(select ts, extract(epoch from ts) as epoch from timeseries where ts between '%s' and '%s' % (start_ts, end_ts)) if results.nrows() 2: return # prime the well by setting prev(ious) to the first tic and # iterate starting with the second... prev = results[0] for curr in results[1:]: # yield timestamp pairs for gaps of timestamps greater than gap if curr['epoch'] - prev['epoch'] gap: yield dict(begin_ts=prev['ts'], end_ts=curr['ts']) prev = curr return $$; SET default_tablespace = ''; SET default_with_oids = false; -- -- Name: timeseries; Type: TABLE; Schema: plpythonu_bug; Owner: -; Tablespace: -- CREATE TABLE timeseries ( ts timestamp without time zone ); -- -- PostgreSQL database dump complete -- --
[GENERAL] plpythonu / using pg as an application server
PG 8.2 I am using plpythonu to add application server functionality to my postgresql database. For example, I have triggers and functions that FTP files, sends email, processes files, etc.. Is there any good reason not to include this functionality directly in the database? (Too much parallel processing, engine not equipped for that kind of processing, threading issues...) Thanks Sim -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
Re: [GENERAL] plpythonu / using pg as an application server
2010/6/1 Sim Zacks s...@compulab.co.il PG 8.2 I am using plpythonu to add application server functionality to my postgresql database. For example, I have triggers and functions that FTP files, sends email, processes files, etc.. Is there any good reason not to include this functionality directly in the database? (Too much parallel processing, engine not equipped for that kind of processing, threading issues...) Thanks Sim The problem is that such a trigger can last very long and makes some non transactional operations. When you perform some insert or update, and the trigger sends an email, the insert/update lasts much longer while blocking other transactions. As as result the overall database efficiency is much worse. Another problem is that sometimes sending an email can fail, should then be made rollback of the insert/update operation? I'd rather use some message queue so the trigger just inserts an email info to a table `emails` instead of sending it. Another trigger would just insert some information to a table `ftpsites` to indicate some ftp address to download. There should also be some process at the background that will select the information from those tables and send emails, process the ftp sites and so on. regards Szymon Guz
Re: [GENERAL] plpythonu / using pg as an application server
2010/6/1 Sim Zacks s...@compulab.co.il: PG 8.2 I am using plpythonu to add application server functionality to my postgresql database. For example, I have triggers and functions that FTP files, sends email, processes files, etc.. Is there any good reason not to include this functionality directly in the database? (Too much parallel processing, engine not equipped for that kind of processing, threading issues...) there are some issues still * missing integrated scheduler * missing autonomous transaction if these isn't issue for you, then you can use rich database without bigger problems. Regards Pavel Stehule Thanks Sim -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
Re: [GENERAL] plpythonu / using pg as an application server
On 6/1/2010 11:12 AM, Szymon Guz wrote: 2010/6/1 Sim Zacks s...@compulab.co.il mailto:s...@compulab.co.il PG 8.2 I am using plpythonu to add application server functionality to my postgresql database. For example, I have triggers and functions that FTP files, sends email, processes files, etc.. Is there any good reason not to include this functionality directly in the database? (Too much parallel processing, engine not equipped for that kind of processing, threading issues...) Thanks Sim The problem is that such a trigger can last very long and makes some non transactional operations. When you perform some insert or update, and the trigger sends an email, the insert/update lasts much longer while blocking other transactions. As as result the overall database efficiency is much worse. Another problem is that sometimes sending an email can fail, should then be made rollback of the insert/update operation? I'd rather use some message queue so the trigger just inserts an email info to a table `emails` instead of sending it. Another trigger would just insert some information to a table `ftpsites` to indicate some ftp address to download. There should also be some process at the background that will select the information from those tables and send emails, process the ftp sites and so on. I am actually using a number of methods. Triggers are only used when the function does have to be completed as part of the transaction or it is considered an error. Also a big advantage of the plpythonu is that you can use try..except blocks so that if something fails you can process the failure and still allow the transaction to complete. For all other functions, such as email and FTP I am using either queues or the Listen/Notify mechanism. The queues run in a cron job that call a database function to complete the task. I have a database function called SendQueuedEmails which runs a loop on the emaildetails table and sends each email one by one. Any errors are written to the errors table and a bounce email is sent to the user. I use Listen/Notify for what I call asynchronous triggers. Something that I want to happen immediately upon a specific transaction, but I don't want to wait for the result and the transaction is not dependent on. Errors are written to an error table so I can review them later and a bounce email is sent when relevant. I just prefer to have all the functionality in the database, so I have a single location for all server code and a single standard method of calling those functions. Sim -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
Re: [GENERAL] plpythonu / using pg as an application server
Is there any good reason not to include this functionality directly in the database? (Too much parallel processing, engine not equipped for that kind of processing, threading issues...) there are some issues still * missing integrated scheduler * missing autonomous transaction if these isn't issue for you, then you can use rich database without bigger problems. These are very real issues. As workarounds, I am using cron on the server to call database functions. I am planning on installing pgAgent, but still haven't gotten around to it. For autonomous transactions or what we might call asynchronous triggers, I use the listen/notify mechanism and the server calls the function when the Notify call is made. Sim -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
Re: [GENERAL] plpythonu / using pg as an application server
As an example of a filesystem access that is transaction dependent: When I create a new supplier in the database, I need a set of directories built on the file system. If the directories are not there, it will cause a lot of problems when dealing with this supplier. When creating the supplier, I use a trigger to build the directories, and if it can't then it will error out, roll back the supplier insert and give an error to the user that they cannot build the directory structure and to please speak with IT to resolve the issue. Sim -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
Re: [GENERAL] plpythonu
Sim Zacks wrote: I'm a fan of unsecured languages in the database. Obviously they should only be used by people who understand the difference. I wouldn't want python locked down. That would prevent so many different external functions that can now be integrated into the database. Among other things that I currently do with plpythonu is a function that reads a template file, populates it with data and saves the file to the server. It then FTPs the file to another server. It certainly can all be done outside of the database, but it is so much easier (and less maintenance) to include it as a function. Having the trusted language does not mean you lose the untrusted one. We have both plperl and plperlu, for example. -- Alvaro Herrerahttp://www.CommandPrompt.com/ The PostgreSQL Company - Command Prompt, Inc. ---(end of broadcast)--- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq
Re: [GENERAL] plpythonu
I'm a fan of unsecured languages in the database. Obviously they should only be used by people who understand the difference. I wouldn't want python locked down. That would prevent so many different external functions that can now be integrated into the database. Among other things that I currently do with plpythonu is a function that reads a template file, populates it with data and saves the file to the server. It then FTPs the file to another server. It certainly can all be done outside of the database, but it is so much easier (and less maintenance) to include it as a function. Sim Erik Jones wrote: On Jan 18, 2008, at 7:48 AM, Stuart Bishop wrote: plpython !=3D plpythonu. plpython was the 'secure' sandboxed version. The Python devs gave up supporting any sort of sandboxing feature in Python declaring it impossib= le. Someone should definitely take a look at this: http://sayspy.blogspot.com/2007/05/i-have-finished-securing-python.html That guy claims he's locked down the python interpreter there. Erik Jones DBA | Emma® [EMAIL PROTECTED] 800.595.4401 or 615.292.5888 615.292.0777 (fax) Emma helps organizations everywhere communicate market in style. Visit us online at http://www.myemma.com ---(end of broadcast)--- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq ---(end of broadcast)--- TIP 5: don't forget to increase your free space map settings
Re: [GENERAL] plpythonu
On Jan 18, 2008, at 7:48 AM, Stuart Bishop wrote: plpython !=3D plpythonu. plpython was the 'secure' sandboxed version. The Python devs gave up supporting any sort of sandboxing feature in Python declaring it impossib= le. Someone should definitely take a look at this: http:// sayspy.blogspot.com/2007/05/i-have-finished-securing-python.html That guy claims he's locked down the python interpreter there. Erik Jones DBA | Emma® [EMAIL PROTECTED] 800.595.4401 or 615.292.5888 615.292.0777 (fax) Emma helps organizations everywhere communicate market in style. Visit us online at http://www.myemma.com ---(end of broadcast)--- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq
Re: [GENERAL] plpythonu
Alexandre da Silva wrote: Hello, someone can tell me if is secure to create external python modules and import them to functions/procedures/triggers to use? Its fine as long as you trust the users with write access to your PYTHONP= ATH. Another question is that I have read in some discussion list (old message year 2003) the possibility of plpython be removed from postgresql, this information is valid yet? plpython !=3D plpythonu. plpython was the 'secure' sandboxed version. The Python devs gave up supporting any sort of sandboxing feature in Python declaring it impossib= le. plpythonu is unrestricted, so if you have the ability to create plpythonu= stored procedures you effectively have full filesystem access on your database server as the user your database is running as. So don't put open('/etc/passwd','w') in your plpythonu code. --=20 Stuart Bishop [EMAIL PROTECTED] http://www.stuartbishop.net/ signature.asc Description: OpenPGP digital signature
Re: [GENERAL] plpythonu
On 1/18/08, Erik Jones [EMAIL PROTECTED] wrote: On Jan 18, 2008, at 7:48 AM, Stuart Bishop wrote: plpython !=3D plpythonu. plpython was the 'secure' sandboxed version. The Python devs gave up supporting any sort of sandboxing feature in Python declaring it impossib= le. Someone should definitely take a look at this: http:// sayspy.blogspot.com/2007/05/i-have-finished-securing-python.html That guy claims he's locked down the python interpreter there. Interesting. But the problem has never been in locking down the interpreter vX.Y, but locking down interpreter vX.Y+1, when previously work was done on vX.Y. Without upstream developers cooperation this has been too painful. So the interesting thing in the posting is not that he succeeded locking Python down, but that he is pushing the patch to core. -- marko ---(end of broadcast)--- TIP 1: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to [EMAIL PROTECTED] so that your message can get through to the mailing list cleanly
[GENERAL] plpythonu
Hello, someone can tell me if is secure to create external python modules and import them to functions/procedures/triggers to use? I am doing the following: function/procedure/trigger: CREATE OR REPLACE FUNCTION tabela_be_i_tg_fx() RETURNS trigger AS $body$ from dbfunctions.postgres.pg_trigger import TestTrigger as fx fe = fx() return fe.execute(args=TD[args], event=TD[event], when=TD[when], level=TD[level], name=TD[name], relid=TD[relid], new=TD[new], old=TD[old], pl_py=plpy) $body$ LANGUAGE plpythonu; at python module I have: class TestTrigger(object): def execute(self, args, event, when, level, name, relid, new, old, pl_py): new[group_name]='__modified__' return 'MODIFY' all this works properly (on windows and linux), but I don't know if is the correct way to use plpythonu, and if I will have future problems. Another question is that I have read in some discussion list (old message year 2003) the possibility of plpython be removed from postgresql, this information is valid yet? sorry bad English Thank's for all -- Alexandre da Silva Analista de Sistemas - Bacharel em Sistemas de Informação (2003-2007) ---(end of broadcast)--- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match
[GENERAL] plpythonu under OS X 10.4 -- a brief HOWTO
Hi all, I just got plpythonu working under OS X, and I'm posting my HOWTO notes here in case I (or anyone else) needs them. The install was pretty straightforward for me once I'd Googled the proper magic commands. I'm on OS X Tiger (10.4.10) which comes with Python 2.3 installed by default. I wanted to have Python 2.5 available, so my first step was to download and build that. One must build Python with the --enabled-shared flag in order to be able to use plpythonu, and I don't know if the stock OS X install of Python has that flag. So even if you're satisfied with the stock Python install version, you might need to build a new version anyway. Here's how I made Python: export MACOSX_DEPLOYMENT_TARGET=10.4 ./configure --enable-shared --enable-framework make sudo make frameworkinstall That installed into /Library/Frameworks/Python.framework/Versions/ 2.5. (Note that OS X's python lives in /System/Library/Frameworks/ Python.framework/Versions/2.3, so the 2.5 install lives alongside of the system install rather than replacing it. This is good -- OS X uses Python and you don't want to monkey around with something on which OS X relies.) The installer helpfully symlinked /usr/local/bin/ python to /Library/Frameworks/Python.framework/Versions/2.5/bin/ python, so putting /usr/local/bin in my path before /usr/bin ensures that I invoke Python 2.5 automatically. After that, I rebuilt postgres (I'm using 8.1.4 and building from / usr/local/src) with the following commands. I don't know if the -- with-includes and --with-libraries options are necessary for plpythonu, but I needed them to get readline support in psql. The 'make clean' step was necessary for me to resolve a linker error. ./configure --with-includes=/usr/local/include --with-libraries=/usr/ local/lib --with-python --enable-nls='en' sudo make clean sudo make install I didn't back up my data or config files before reinstalling and do not appear to have lost anything. YMMV. You should now have plpythonu available as a language in Postgres. In order to make it available in database my_stuff, start psql as superuser and execute these commands: \c my_stuff CREATE LANGUAGE plpythonu; Since plpythonu is an untrusted language (that's what the 'u' on the end stands for), only superusers can create functions in that language. So as superuser you can create a function like so: CREATE OR REPLACE FUNCTION show_python_version() RETURNS TEXT AS $$ import sys return sys.version $$ LANGUAGE plpythonu; Then invoke psql as a normal user and try out your function: select * from show_python_version(); show_python_version 2.5.1 (r251:54863, Nov 17 2007, 21:19:53) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] (1 row) That's all! Enjoy Philip Semanchuk first name @ last name.com ---(end of broadcast)--- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq
[GENERAL] plpythonu and PYTHONPATH/sys.path
Hi, I want to use python modules I have written in plpythonu. I can load these modules in functions within postgres if they are installed in the official python module directories (e.g. /blah/site-packages etc.) However, I want to use modules which are in my home file space, but I can't get this to work. I've tried setting PYTHONPATH to the directory containing the modules, and even tried explicitly appending the directory to sys.path within the plpythonu interpretter (i.e. at the beginning of the database function). I always get an import error, though - the module cannot be found. Why doesn't the embedded python interpretter use sys.path (fully?) for locating modules? How can I use python modules in plpythonu which are located in non-standard directories? (I use Fedora Core 6, Postgres v. 8.1.8, Python 2.4.4) Thanks, Korin -- = Dr. Korin Richmond Centre for Speech Technology Research 2 Buccleuch Pl, Edinburgh, EH8 9LW Tel: +44 (0)131 651 1769 http://www.cstr.ed.ac.uk/ssi/people/korin.html = ---(end of broadcast)--- TIP 5: don't forget to increase your free space map settings
Re: [GENERAL] plpythonu and PYTHONPATH/sys.path
Korin Richmond wrote: Hi, I want to use python modules I have written in plpythonu. I can load these modules in functions within postgres if they are installed in the official python module directories (e.g. /blah/site-packages etc.) However, I want to use modules which are in my home file space, but I can't get this to work. Does user postgres have access to your home directory? -- Richard Huxton Archonet Ltd ---(end of broadcast)--- TIP 4: Have you searched our list archives? http://archives.postgresql.org/
Re: [GENERAL] plpythonu and PYTHONPATH/sys.path
On Tue, 6 Mar 2007, Richard Huxton wrote: Korin Richmond wrote: Hi, I want to use python modules I have written in plpythonu. I can load these modules in functions within postgres if they are installed in the official python module directories (e.g. /blah/site-packages etc.) However, I want to use modules which are in my home file space, but I can't get this to work. Does user postgres have access to your home directory? Sorry, yes, I forgot to mention I've checked it's not just a permissions problem by putting the module in /tmp, readable by everyone, and appending /tmp to sys.path in the function in the database. Still complains that the module can't be found though :( (If I fire up python as user postgres, I can import the module from /tmp.) Korin -- = Dr. Korin Richmond Centre for Speech Technology Research 2 Buccleuch Pl, Edinburgh, EH8 9LW Tel: +44 (0)131 651 1769 http://www.cstr.ed.ac.uk/ssi/people/korin.html = ---(end of broadcast)--- TIP 2: Don't 'kill -9' the postmaster
Re: [GENERAL] plpythonu and PYTHONPATH/sys.path
On Tue, 6 Mar 2007, Korin Richmond wrote: On Tue, 6 Mar 2007, Richard Huxton wrote: Korin Richmond wrote: Hi, I want to use python modules I have written in plpythonu. I can load these modules in functions within postgres if they are installed in the official python module directories (e.g. /blah/site-packages etc.) However, I want to use modules which are in my home file space, but I can't get this to work. Does user postgres have access to your home directory? Sorry, yes, I forgot to mention I've checked it's not just a permissions problem by putting the module in /tmp, readable by everyone, and appending /tmp to sys.path in the function in the database. Still complains that the module can't be found though :( (If I fire up python as user postgres, I can import the module from /tmp.) Arrgh - apologies, I've just realised SELinux was the problem! Sorry to answer my own query - but thanks for your mail Richard, which set me on the right track again! regards, Korin -- = Dr. Korin Richmond Centre for Speech Technology Research 2 Buccleuch Pl, Edinburgh, EH8 9LW Tel: +44 (0)131 651 1769 http://www.cstr.ed.ac.uk/ssi/people/korin.html = ---(end of broadcast)--- TIP 6: explain analyze is your friend
Re: [GENERAL] plpythonu array parameter
Anyone interested in arrays and plpython might find this interesting. Based on the conversation below I put these functions in a library (pg_stuff.py): def arr2list(a): a = a.replace({,[).replace(},]) pylist = eval(a) return pylist def list2arr(a): parm = `a` parm=parm.replace([,{).replace(],}) return parm Then I was able to call them like this: CREATE TYPE int_triple AS( x int, y int, z int); CREATE FUNCTION py_explode_4 (a int[], b int[]) returns setof int_triple AS $$ import sys sys.path.append('/Users/wsprague/lib') import pg_stuff x_list = pg_stuff.arr2list(a) y_list = pg_stuff.arr2list(b) for x in x_list: for y in y_list: yield(x, y, x+y) $$ LANGUAGE plpythonu; Yielding the following in postgres: or_gis=# select * from py_explode_4(array[1,2], array[10,20]); x | y | z ---++ 1 | 10 | 11 1 | 20 | 21 2 | 10 | 12 2 | 20 | 22 (4 rows) Not exactly earth shattering, but still cool. My next hack will be to convert an array to a matrix and find the eigenvalues and convert back... It does seem like array to list conversion should happen automatically, though. Sim Zacks wrote: I ran into an interesting issue trying to pass an array to a plpythonu function (Postgresql 8.03). When I googled the issue I found a number of people asking similar questions and they haven't found an answer. The problem is that there is no type mapped from a postgresql array to a python list. These conversion functions will map between a postgresql array and a python list and back. Is there a way to stick this in the integration code somewhere, so that every time an array is passed to/from a python function it converts it transparently? In the python function itself this type of code will work, what I don't know is how to change the internals so that when an array variable is passed in it automatically puts it through this code and hands off a python list variable. The second function would work as is, and return a postgresql array. Of course both would have to work with any type of array and not just text[] create or replace function pgarray_to_pylist(text[]) returns {python list} as $$ parm=args[0] parm=parm.replace({,[).replace(},]) pylist=eval(parm) return pylist $$ language 'plpythonu' create or replace function pylist_to_pgarray({python list}) return text[] as $$ parm=`args[0]` parm=parm.replace([,{).replace(],}) return parm $$ language 'plpythonu' ---(end of broadcast)--- TIP 1: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to [EMAIL PROTECTED] so that your message can get through to the mailing list cleanly
[GENERAL] plpythonu array parameter
I ran into an interesting issue trying to pass an array to a plpythonu function (Postgresql 8.03). When I googled the issue I found a number of people asking similar questions and they haven't found an answer. The problem is that there is no type mapped from a postgresql array to a python list. These conversion functions will map between a postgresql array and a python list and back. Is there a way to stick this in the integration code somewhere, so that every time an array is passed to/from a python function it converts it transparently? In the python function itself this type of code will work, what I don't know is how to change the internals so that when an array variable is passed in it automatically puts it through this code and hands off a python list variable. The second function would work as is, and return a postgresql array. Of course both would have to work with any type of array and not just text[] create or replace function pgarray_to_pylist(text[]) returns {python list} as $$ parm=args[0] parm=parm.replace({,[).replace(},]) pylist=eval(parm) return pylist $$ language 'plpythonu' create or replace function pylist_to_pgarray({python list}) return text[] as $$ parm=`args[0]` parm=parm.replace([,{).replace(],}) return parm $$ language 'plpythonu' ---(end of broadcast)--- TIP 6: explain analyze is your friend
[GENERAL] plpythonu and type record
I'm trying to write a stored procedure in python with postgresql 7.4, but I cannot return a record or a setof record. I get this error: ERROR: cannot accept a value of type record Is possible to return a record or am I trying to do something impossible? Thank you signature.asc Description: OpenPGP digital signature
Re: [GENERAL] plpythonu and return void
On Monday 08 August 2005 08:02 pm, Tom Lane wrote: Adrian Klaver [EMAIL PROTECTED] writes: I recently migrated a database from Postgres 7.4.1 to Postgres 8.03. The only problem I have run into is that a plpythonu function that returns void will not run under 8.03. The error message states that a plpython function cannot return void. I got around it by having it return text and not actually returning anything. I have searched for an explanation and can't seem to locate one. The explanation is doubtless here: 2004-09-19 19:38 tgl * src/pl/plpython/plpython.c: Add defenses against plpython functions being declared to take or return pseudotypes. Not sure why I neglected to add these checks at the same time I added them to the other PLs, but it seems I did. It's certainly possible to weaken this test to allow void again, but shouldn't there be corresponding changes elsewhere to ensure that the behavior is actually sensible? regards, tom lane Thanks for the explanation. One of these days I will remember that the source is my friend. -- Adrian Klaver [EMAIL PROTECTED] ---(end of broadcast)--- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match
[GENERAL] plpythonu and return void
I recently migrated a database from Postgres 7.4.1 to Postgres 8.03. The only problem I have run into is that a plpythonu function that returns void will not run under 8.03. The error message states that a plpython function cannot return void. I got around it by having it return text and not actually returning anything. I have searched for an explanation and can't seem to locate one. Obviously I have missed something I just don't know what? Any suggestions would be appreciated. -- Adrian Klaver [EMAIL PROTECTED] ---(end of broadcast)--- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq
Re: [GENERAL] plpythonu and return void
Adrian Klaver [EMAIL PROTECTED] writes: I recently migrated a database from Postgres 7.4.1 to Postgres 8.03. The only problem I have run into is that a plpythonu function that returns void will not run under 8.03. The error message states that a plpython function cannot return void. I got around it by having it return text and not actually returning anything. I have searched for an explanation and can't seem to locate one. The explanation is doubtless here: 2004-09-19 19:38 tgl * src/pl/plpython/plpython.c: Add defenses against plpython functions being declared to take or return pseudotypes. Not sure why I neglected to add these checks at the same time I added them to the other PLs, but it seems I did. It's certainly possible to weaken this test to allow void again, but shouldn't there be corresponding changes elsewhere to ensure that the behavior is actually sensible? regards, tom lane ---(end of broadcast)--- TIP 4: Have you searched our list archives? http://archives.postgresql.org
Re: [GENERAL] plpythonu and bytea
On Sat, Jul 02, 2005 at 04:49:23PM -0400, Greg Steffensen wrote: Hey, I'm trying to write some plpython procedures that read binary data from images on the disk and store it in bytea fields. I'm basically trying to write a plpython procedure that accepts a varchar and returns a bytea, with these procedure contents: data = file(args[0]).read() return data (The actual procedure will have more in it, but that's the tricky part). But the returned data is always severely truncated. Is returning a bytea from plpython impossible, or is there some way I should escape the data string? I think the return value is a cstring that's cast to whatever type the function is declared to return; I'd guess it's being truncated because it contains NUL (\000) values. Example: CREATE FUNCTION foo() RETURNS bytea AS $$ data = '\001\002\000\003\004' return data $$ LANGUAGE plpythonu; SELECT foo(), length(foo()); foo| length --+ \001\002 | 2 (1 row) The function should work if the data is escaped. I don't know the best Python way to do that, but the following appears to work: CREATE FUNCTION foo() RETURNS bytea AS $$ data = '\001\002\000\003\004' return ''.join(['\\%03o' % ord(x) for x in data]) $$ LANGUAGE plpythonu; SELECT foo(), length(foo()); foo | length --+ \001\002\000\003\004 | 5 (1 row) It seems like there ought to be a better way than the list comprehension shown; maybe you or somebody else with better Python skills can improve on it. -- Michael Fuhr http://www.fuhr.org/~mfuhr/ ---(end of broadcast)--- TIP 7: don't forget to increase your free space map settings
[GENERAL] plpythonu and bytea
Hey, I'm trying to write some plpython procedures that read binary data from images on the disk and store it in bytea fields. I'm basically trying to write a plpython procedure that accepts a varchar and returns a bytea, with these procedure contents: data = ""> return data (The actual procedure will have more in it, but that's the tricky part). But the returned data is always severely truncated. Is returning a bytea from plpython impossible, or is there some way I should escape the data string? I've tried using the built in encode and decode functions, but they don't seem to help. If worse comes to worse, I can store the base64 encoded version, of course, but I'd rather not do that. Any ideas? Greg
[GENERAL] plpythonu and booleans
When accessing Postgres boolean fields (in the TD dictionaries) in plpythonu, their values are '0' or '1'. IIRC, in PHP their values would be 't' or 'f'. How are boolean values actually stored in Postgres? CSN Discover Yahoo! Stay in touch with email, IM, photo sharing and more. Check it out! http://discover.yahoo.com/stayintouch.html ---(end of broadcast)--- TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq
[GENERAL] plpythonu and dollar quoting
Can dollar quoting be used with plpythonu (like with plperl - http://www.postgresql.org/docs/8.0/interactive/plperl.html#PLPERL-FUNCS)? When trying to create a function I get this error: 'syntax error at or near $' If I use single quotes, then escape all quotes and (a lot of) backslashes in the python code then the function gets created fine. CSN __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ---(end of broadcast)--- TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]
Re: [GENERAL] plpythonu and dollar quoting
Nevermind, I see dollar-quoting was added in 8.0. --- CSN [EMAIL PROTECTED] wrote: Can dollar quoting be used with plpythonu (like with plperl - http://www.postgresql.org/docs/8.0/interactive/plperl.html#PLPERL-FUNCS)? When trying to create a function I get this error: 'syntax error at or near $' If I use single quotes, then escape all quotes and (a lot of) backslashes in the python code then the function gets created fine. CSN __ Do you Yahoo!? Make Yahoo! your home page http://www.yahoo.com/r/hs ---(end of broadcast)--- TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq
Re: [GENERAL] plpythonu and dollar quoting
On Tue, May 03, 2005 at 02:46:04PM -0700, CSN wrote: Can dollar quoting be used with plpythonu (like with plperl - Yes, if you're using PostgreSQL 8.0 or later. Dollar quotes have nothing to do with the function's language -- they're just another way to quote a string. http://www.postgresql.org/docs/8.0/interactive/sql-syntax.html#SQL-SYNTAX-DOLLAR-QUOTING When trying to create a function I get this error: 'syntax error at or near $' This is the error you'd get if you tried to use dollar quotes in PostgreSQL 7.4; you'd get a similar error in earlier versions. What version are you using? What does SELECT version(); show? -- Michael Fuhr http://www.fuhr.org/~mfuhr/ ---(end of broadcast)--- TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]
[GENERAL] plpythonu on 7.4
On a 7.4 installation built --with-python... Python installed is 2.3.4. On createlang, I get the following error: createlang -U postgres 'plpythonu' template1 createlang: language installation failed: ERROR: could not load library /usr/local/pgsql74/lib/plpython.so: /usr/local/pgsql74/lib/plpython.so: undefined symbol: PyDict_Copy Anyone have any hints? thanks, [EMAIL PROTECTED] ---(end of broadcast)--- TIP 2: you can get off all lists at once with the unregister command (send unregister YourEmailAddressHere to [EMAIL PROTECTED])
Re: [GENERAL] plpythonu on 7.4
On Fri, Jan 21, 2005 at 09:16:27PM -0800, elein wrote: createlang: language installation failed: ERROR: could not load library /usr/local/pgsql74/lib/plpython.so: /usr/local/pgsql74/lib/plpython.so: undefined symbol: PyDict_Copy What does ldd /usr/local/pgsql74/lib/plpython.so show? -- Michael Fuhr http://www.fuhr.org/~mfuhr/ ---(end of broadcast)--- TIP 9: the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match
Re: [GENERAL] plpythonu on 7.4
Is this on fedora or redhat linux? If so a separate rpm must be installed for plpython to function. If you built from source the config script must also include building python. On Fri, Jan 21, 2005 at 10:34:51PM -0700, Michael Fuhr wrote: On Fri, Jan 21, 2005 at 09:16:27PM -0800, elein wrote: createlang: language installation failed: ERROR: could not load library /usr/local/pgsql74/lib/plpython.so: /usr/local/pgsql74/lib/plpython.so: undefined symbol: PyDict_Copy What does ldd /usr/local/pgsql74/lib/plpython.so show? -- Michael Fuhr http://www.fuhr.org/~mfuhr/ ---(end of broadcast)--- TIP 9: the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match ---(end of broadcast)--- TIP 8: explain analyze is your friend
Re: [GENERAL] plpythonu on 7.4
[EMAIL PROTECTED] (elein) writes: createlang -U postgres 'plpythonu' template1 createlang: language installation failed: ERROR: could not load library /usr/local/pgsql74/lib/plpython.so: /usr/local/pgsql74/lib/plpython.so: undefined symbol: PyDict_Copy I'd bet on a python version issue, viz plpython expecting a newer version of python than you have installed. regards, tom lane ---(end of broadcast)--- TIP 9: the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match
Re: [GENERAL] plpythonu on 7.4
On Sat, Jan 22, 2005 at 12:57:37AM -0500, Tom Lane wrote: [EMAIL PROTECTED] (elein) writes: createlang -U postgres 'plpythonu' template1 createlang: language installation failed: ERROR: could not load library /usr/local/pgsql74/lib/plpython.so: /usr/local/pgsql74/lib/plpython.so: undefined symbol: PyDict_Copy I'd bet on a python version issue, viz plpython expecting a newer version of python than you have installed. H...Python 2.3.4 isn't very old, and the source code does contain a PyDict_Copy() function. The only newer version is 2.4, and it was released in late November. I was wondering if it was a dynamic linking issue, like plpython.so not finding libpython*. Does PostgreSQL's configure script check versions for Perl, Python, Tcl, etc.? If the version matters then configure ought to complain about it. -- Michael Fuhr http://www.fuhr.org/~mfuhr/ ---(end of broadcast)--- TIP 2: you can get off all lists at once with the unregister command (send unregister YourEmailAddressHere to [EMAIL PROTECTED])