Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PostgreSQL, where does plpython(3)u output from `print` go?

When I create the following function in PostgreSQL:

create function log( value variadic text[] )
  returns void
  language plpython3u
  as $$
    print( ' '.join( value ) + '\n' )
    $$;

do $$ begin perform log( ( 42 + 108 )::text ); end; $$;

the ouput does not appear in the terminal (using psql -f ...). Where does it go instead?

Also, isn't there any simple way (maybe an extension) that gives me an easy-to-use output to stdout / stderr? I do not want to use select since that surrounds all output with table decorations, which you can turn off, but only using psql tricks that do not work in functions. Likewise, I do not want to use \echo, as that won't work within function definitions.

Edit I know about plpy.notice(), but that function surrounds its output with a whole lot of clutter, too.


BTW my solution right now is to write to a file:

create function log( value variadic text[] )
  returns void
  language plpython3u
  as $$
    with open( '/tmp/psql-output', 'a' ) as o:
      o.write( ' '.join( value ) + '\n' )
    $$;

and have a tail running in the background (that uses ANSI colors, yay!):

tail -f /tmp/psql-output | sed 's/^.*$/\x1b[38;05;214m\0\x1b[0m/g' &

but the disadvantage is that I need external code to set up this thing.

like image 613
John Frazer Avatar asked Aug 23 '17 12:08

John Frazer


1 Answers

You can use the function of plpy to print the message.

plpy.debug(msg, **kwargs)
plpy.log(msg, **kwargs)
plpy.info(msg, **kwargs)
plpy.notice(msg, **kwargs)
plpy.warning(msg, **kwargs)

plpy.log() is always output if log_destination is "stderr", and others functions are according to log_min_messages value(warning is default).

like image 135
always stuck Avatar answered Oct 23 '22 14:10

always stuck