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

42.4k views Asked by At
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?

1

There are 1 answers

0
khampson On

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