Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "RAISE INFO, RAISE LOG, RAISE DEBUG” to track log in PostgreSQL function?

CREATE OR REPLACE FUNCTION mover(src text, dst text, cpquery text, conname text, ifbin boolean) returns void as
$$
        DECLARE
                cnt integer;
                dlcnt integer;
                del_count integer;
                ret text;

        BEGIN
                SELECT  pg_catalog.dblink_copy_open(conname, dst, ifbin) INTO ret ;
                RAISE LOG 'dblink_open %',ret;

                execute 'SELECT  1 as check FROM ' || src ||' limit 1' into cnt;
                IF cnt=0 THEN
                        PERFORM pg_sleep(2);
                END IF;

                IF ifbin=true THEN
                        RAISE DEBUG 'Start to Copy data with binary';
                        execute 'COPY (' || cpquery || '  ) to function pg_catalog.dblink_copy_write with binary';
                        RAISE DEBUG 'Finish Copy data';
                ELSE
                        RAISE DEBUG 'Start to Copy data without binary';
                        execute 'COPY (' || cpquery || '  ) to function pg_catalog.dblink_copy_write';
                        RAISE DEBUG 'Finish Copy data';
                END IF;

                execute 'DELETE FROM ' || src;

                GET DIAGNOSTICS del_count=ROW_COUNT;
                RAISE INFO 'DELETE % rows',del_count;

                SELECT  pg_catalog.dblink_copy_end() INTO ret;
                RAISE LOG 'dblink_end %',ret;
        END;
$$
language plpgsql;

As code, I want to put some message into log by using RAISE, but where is the location of my log file ? and where RAISE DEBUG output?

like image 234
Simon Su Avatar asked Nov 17 '14 07:11

Simon Su


People also ask

What is the use of 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.

How do I debug a Postgres function in Pgadmin?

To set a breakpoint at the first line of a program, right-click the name of the object you would like to debug, and select Set breakpoint from the Debugging sub-menu. The debugger window will open, waiting for another session to invoke the program.

What is the command enable debug in PostgreSQL?

The command enable-debug is used to enable the compilation of all the applications and libraries. The execution of this procedure usually impedes the system, but it also amplifies the binary file size.

Which command helps with creating functions in PostgreSQL?

The Syntax for PostgreSQL CREATE Function command is as follows: CREATE [OR REPLACE] FUNCTION function_name (arguments) RETURNS return_datatype. LANGUAGE plpgsql.


1 Answers

They can either be output to the Postgres log, reported back to the client, or both. These are controlled by server-side settings, log_min_messages and client_min_messages.

See the following doc for more details:

http://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html

http://www.postgresql.org/docs/current/static/runtime-config-logging.html

As @a_horse_with_no_name suggested: These parameters can also be set via the SET command from the client.

It can be set via the SQL: set client_min_messages to 'debug';

like image 118
khampson Avatar answered Oct 16 '22 18:10

khampson