Database Reference
In-Depth Information
In standard python, this would look like this:
def fact(x):
f = 1
while (x > 0):
f = f * x
x = x - 1
print 'f:%d, x:%d' % (f, x)
return f
It will print out all intermediate values for
f
and
x
as it runs:
>>> fact(3)
f:3, x:2
f:6, x:1
f:6, x:0
6
If you try to use print in a PL/Python function, you will discover that nothing is printed.
In fact, there is no single logical place to print to when running a pluggable language
inside a PostgreSQL server.
The closest thing to print in PL/Python is the function
plpy.notice()
, which
sends a PostgreSQL
NOTICE
to the client and also to the server log if
log_min_messages
is set to value
notice
or smaller.
CREATE FUNCTION fact(x int) RETURNS int
AS $$
global x
f = 1
while (x > 0):
f = f * x
x = x - 1
plpy.notice('f:%d, x:%d' % (f, x))
return f
$$ LANGUAGE plpythonu;