Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing run time messages in postgres

Tags:

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

like image 417
RAFIQ Avatar asked Mar 19 '14 14:03

RAFIQ


People also ask

How do I get the current time in PostgreSQL?

Postgresql now() The NOW() function in Postgresql is used to get the current date and time. The return type of the NOW() function is the timestamp with the time zone. We can fetch the current date and time by using the PostgreSQL NOW() function. This function has a return type i.e. the timestamp with the time zone.

What command turns on timing in PostgreSQL?

You can use \timing only with the command line client psql , since this is a psql command. It is a switch that turns execution time reporting on and off: test=> \timing Timing is on.

What is raise notice in PostgreSQL?

RAISE is used to raise errors and report messages, PostgreSQL provides various parameters to report an error, warning, and information at a detailed level.


2 Answers

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';

like image 23
Gaurav Koradiya Avatar answered Oct 26 '22 12:10

Gaurav Koradiya


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; 
like image 150
Rahul Avatar answered Oct 26 '22 12:10

Rahul