2

When executing multiple SQL commands, such as in the pgAdmin SQL Editor, how can I print some strings to the console (output pane in pgAdmin). I want to monitor execution.

UPDATE x
SET a = b 
WHERE a IS NULL
;

Print to console --> Finished x.

UPDATE y
SET a = b 
WHERE a IS NULL
;

Print to console --> Finished y.

My question is the same as this StackOverflow Question. Or, as Craig Ringer interpreted that Question:

Do you wish to observe SQL queries that are run by PgAdmin-III by printing them to standard error, viewed by launching PgAdmin-III on a cmd.exe interactive console?


I have seen remarks about \echo 'some_text'. But that gives me the error:

ERROR: syntax error at or near "\"
SQL state: 42601
2
  • SELECT 'some text'; \echo of a psql command, you cannot use it in pgAdmin. If you want an error-like message (like a warning or such), you can create a pl/pgsql function that emits a message of the desired level using an incoming parameter, like the one I used here. Commented May 20, 2015 at 8:02
  • Have you got any solution for this, yet? I have a very long evolution script, which I execute inside a transaction (with ROLLBACK for testing purposes) and I try to know the content of a table mid-way through... Commented Nov 18, 2015 at 18:00

1 Answer 1

5

The only way I have found is through a stored procedure

CREATE OR REPLACE FUNCTION print_notice(msg text) 
  RETURNS integer AS 
$$ 
DECLARE 
BEGIN 
    RAISE NOTICE USING MESSAGE = msg;
    RETURN null;
END; 
$$ 
LANGUAGE 'plpgsql' IMMUTABLE; 

So, executing SELECT print_notice('Foo ' || someText || ' !');

Will produce (for example) :

NOTICE:  Foo whatever !
query result with 1 row discarded.

I'm not sure if this can be done without the SELECT and the query ... row discarded message. But it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.