Re: BUG #14583: plpythonu : subprocess not working in function

From: Kieran McCusker <kieran(dot)mccusker(at)gmail(dot)com>
To: paulo(dot)nuno(dot)leite(at)gmail(dot)com
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #14583: plpythonu : subprocess not working in function
Date: 2017-03-09 18:14:49
Message-ID: CAGgUQ6EhhhKEEorodgAh3dmXYAxjmpJDbd-ZgbysqSNXmoLEOg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 8 March 2017 at 17:51, <paulo(dot)nuno(dot)leite(at)gmail(dot)com> wrote:

> The following bug has been logged on the website:
>
> Bug reference: 14583
> Logged by: Paulo B
> Email address: paulo(dot)nuno(dot)leite(at)gmail(dot)com
> PostgreSQL version: 9.5.6
> Operating system: Windows
> Description:
>
> I have the following function that runs a command:
>
>
> CREATE OR REPLACE FUNCTION public.classify_test()
> RETURNS void AS
> $BODY$
> import os
> from subprocess import Popen, PIPE
> import subprocess
> import os
> from subprocess import Popen, PIPE
>
> echo=Popen(('echo', '123'), shell=True)
>
> p = subprocess.Popen(['python.exe', '-u','test.py'], stdin = echo.stdout,
> universal_newlines = True, shell = True)
>
> $BODY$
> LANGUAGE plpythonu VOLATILE
> COST 100;
> ALTER FUNCTION public.classify_test()
> OWNER TO postgres;
>
> if I run this fuction as select classify_test() it will not run the python
> script. However, if I run it in the command line it will work. It works as
> well in SublimeText2 IDE. I have been trying to find what the bug or
> problem
> is, but had no success. I am using python2.7, I have psycopg2 installed.
>
> Why doesn't postgresql execute the python script?
>
>
>
>
>
> --
> 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

Hi

I had the same problem migrating to python3. My python3 code now looks like

$BODY$
import subprocess

plpy.execute('drop server if exists fi_server_{} cascade'.format(id))

try:
subprocess.check_output(['/usr/local/bin/ogr_fdw_info',
'-s', 'fi_server_{}'.format(id),
'-t', 'import.t{0}_{1}'.format(id, worksheet),
'-d', filename,
'-w', str(worksheet)
])

except subprocess.CalledProcessError, e:
if e.returncode == 1:
plpy.execute(e.output)
return None
else:
return e.output

Hope this helps

Kieran

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message markus 2017-03-10 08:00:45 BUG #14586: Permissions of recovery.conf are different in plain and tar-format
Previous Message Michael Meskes 2017-03-09 17:45:05 Re: BUG #14582: ecpg crashes on SQL input