3

If I want to write a message to the Postgres log I can do this:

DO $$
BEGIN
  RAISE LOG 'Justin Is Awesome';
END $$;

However, it logs the statement as well so I get 5 lines of output instead of one:

2016-07-12 16:38:13 UTC LOG:  Justin Is Awesome
2016-07-12 16:38:13 UTC STATEMENT:  DO $$
  BEGIN
    RAISE LOG 'Justin Is Awesome';
  END $$

I can reduce it to two lines with the following:

2016-07-12 16:36:04 UTC LOG:  Justin Is Awesome
2016-07-12 16:36:04 UTC STATEMENT:  DO $$ BEGIN RAISE LOG 'Justin Is Awesome'; END $$

However is it possible to reduce it down to one? Is there a way to send a line of text to the Postgres log without postgres logging the statement that generated the log?

2 Answers 2

4

If you want to suppress the "STATEMENT" information, then you could set log_min_error_statement = fatal (or panic).

If you want to keep the STATEMENT information but compress it into a single line with the main entry, then you could set log_destination = 'csvlog'. Technically it will still be multiple lines, but the internal newlines will be escaped by inclusion within double quotes, which any good csv parser will handle for you.

0

Controlling the log_min_error_statement will prevent the statements being logged in the log_destination file. Essentially, you are logging all messages of FATAL and PANIC levels when this parameter is set to 'FATAL'.

Here is the excerpt from Postgresql Documentation https://www.postgresql.org/docs/9.5/static/runtime-config-logging.html

log_min_error_statement (enum) Controls which SQL statements that cause an error condition are recorded in the server log. The current SQL statement is included in the log entry for any message of the specified severity or higher. Valid values are DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1, INFO, NOTICE, WARNING, ERROR, LOG, FATAL, and PANIC. The default is ERROR, which means statements causing errors, log messages, fatal errors, or panics will be logged. To effectively turn off logging of failing statements, set this parameter to PANIC. Only superusers can change this setting.

1. log_min_error_statement='error'

Terminal 1

postgres=# show log_min_error_statement;
 log_min_error_statement 
-------------------------
 error
(1 row)

postgres=# DO $$                        
BEGIN
  RAISE LOG 'Justin Is Awesome';
END $$;
DO

Terminal 2

-bash-4.1$ tail -f  pg_log/postgresql-2016-07-13_112940.log 

2016-07-13 11:50:01 PDT LOG:  Justin Is Awesome
2016-07-13 11:50:01 PDT STATEMENT:  DO $$
    BEGIN
      RAISE LOG 'Justin Is Awesome';
    END $$;

2. log_min_error_statement='fatal'

Terminal 1

postgres=# set log_min_error_statement='FATAL';
SET
postgres=# DO $$                               
BEGIN
  RAISE LOG 'Justin Is Awesome';
END $$;
DO

Terminal 2:

-bash-4.1$ tail -f  pg_log/postgresql-2016-07-13_112940.log 
2016-07-13 11:51:15 PDT LOG:  Justin Is Awesome
2
  • log_statement is already set to none, context super user. Commented Jul 13, 2016 at 12:37
  • I just saw somebody else answered it as well
    – Sathish
    Commented Jul 13, 2016 at 19:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.