48

Can we use RAISE NOTICE in postgres as equivalent of RAISERROR 'message to display' WITH NOWAIT in SQL Server, or is there a better way to print intermediate messages while postgres queries are running? Please suggest if there is better way to print run time messages in postgres.

INSERT INTO tbl1 (col1) values (val1);
DO $$
begin
raise notice 'insert tbl1 done!';
end;
$$;
UPDATE tbl2 set col2='val2' where ...;
DO $$
begin
raise notice 'update tbl2 done!';
end;
$$;

I apologize if this code is too bad to comment, pls do suggest a better way to do it, Thanks

5 Answers 5

76

Yes, you can use RAISE NOTICE like below. It's correct the way you are doing.

RAISE NOTICE 'i want to print % and %', var1,var2;

See here for more information https://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html

EDIT:

begin
INSERT INTO tbl1 (col1) values (val1);
raise notice 'insert tbl1 done!';
end;
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, Rahul, Ok but i was hoping if we can simplify the syntax around RAISE NOTICE just like how we run INSERT and UPDATE is it possible?
Not sure what's your Q. Give an example? But yes it's. With RAISE NOTICE you give the msg you want to print (like the way you are already doing); those % are placeholders.
RAISE is a PL/PgSQL statement, not an SQL statement. So you cannot use it in top-level SQL, only within procedures. You can always create a trivial procedure that you can call as raise_notice('message') though.
Where can I see these messages after the function has run?
@olivmir - It is a late response. I hope you found where it is. For those who are still looking for the output, it is written into "Database Server Output log" window. If you are using DBeaver, you can open that window by clicking on 'Show server output' icon on the left side of the SQL Editor window/pane".
|
14

you can use very simple statement in function everywhere.

DO $$ begin raise notice '%',now(); end; $$;

function for reference:

create or replace function test() RETURNS bool AS '
begin
raise notice ''%'',now();
for i IN 0..50000000  loop
     end loop
     raise notice ''%'',now();
     return true;
end;

LANGUAGE 'plpgsql';

Comments

4

You could also do normal selects without the DO block.

INSERT INTO tbl1 (col1) values (val1);

SELECT 'insert tbl1 done!' as msg;

UPDATE tbl2 set col2='val2' where ...;

SELECT 'update tbl2 done!' as msg;

The tradeoff is that it does add extra clutter to the output, like

UPDATE 1
      msg
-----------------
update tbl2 done!
(1 row)

Comments

2

RAISE NOTICE is part of PL/pgSQL so it's only legal in a function or an anonymous DO block. I guess you could make a function that raises the notice and call that.

1 Comment

Thanks for comment, yes function may be a better approach if we want to print many messages, but currently I am sticking to DO blcok
0

Not sure if this is what you are looking for, but it is useful anyway. Print to the SQL output can be done using \qecho.

Example:

\qecho '\nDrop Trigger Functions:'
drop function if exists trade_tx_before();
drop function if exists trade_tx_after();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.