How do I print to the console in SQL?

I am using PostgreSQL

AND I want to print a message to the console.

If I use plpythonu

, I use plpy.notice

If I use plpgsql

, I useraise notice

but how can I print when the function is clean SQL

? using SELECT 'string'

doesn't help me as it prints line in column and if the message is in the middle of the code it doesn't display it. I need something like raise notice

/ plpy.notice

for SQL

.

CREATE OR REPLACE FUNCTION A()
  RETURNS VOID AS
$BODY$ 
                how do i print 'hello world' here?
 $BODY$
  LANGUAGE sql VOLATILE

      

if it was plpgsql

I would do:

CREATE OR REPLACE FUNCTION A()
  RETURNS VOID AS
$BODY$ 
        Raise Notice 'hello world'
 $BODY$
  LANGUAGE plpgsql VOLATILE

      

I am looking for an equivalent in LANGUAGE SQL

+3


source to share


2 answers


There is a command in plain SQL NOTIFY

. This command sends the payload to the channel. Any string can be a pipe and payload.

CREATE OR REPLACE FUNCTION A() RETURNS void AS $BODY$ 
  NOTIFY msg, 'hello world';
$BODY$ LANGUAGE sql;

      

and then:



LISTEN msg;
SELECT A();

Asynchronous notification "msg" with payload "hello world" received from server process with PID 3275.

UNLISTEN msg;

      

The message will also contain information about where it came from. If you really only want the string (payload), you'll have to parse that from the full string.

+2


source


you can only select it in SQL



db=# CREATE FUNCTION b() RETURNS text AS                                                                                                                                         
$BODY$                                                                                                                                                                            
select 'Hello there'::text;                                                                                                                                                     
$BODY$                                                                                                                                                                          
 LANGUAGE sql VOLATILE;
CREATE FUNCTION
db=# select b()::text;                                                                                                                                                                
b
-------------
 Hello there
(1 row)

      

+3


source







All Articles