Printing run time messages in postgres

前端 未结 4 2107
粉色の甜心
粉色の甜心 2021-02-12 16:29

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

相关标签:
4条回答
  • 2021-02-12 16:51

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

    0 讨论(0)
  • 2021-02-12 17:00

    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;
    
    0 讨论(0)
  • 2021-02-12 17:02

    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)
    
    0 讨论(0)
  • 2021-02-12 17:10

    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.

    0 讨论(0)
提交回复
热议问题