Hi All,

Greetings for the day.

Would like to discuss on below feature here.

Feature:
    Having an SQL function, to write messages to log destination.

Justification:
    As of now, we don't have an SQL function to write custom/application
messages to log destination. We have "RAISE" clause which is controlled by
log_ parameters. If we have an SQL function which works irrespective of log
settings, that would be a good for many log parsers. What i mean is, in DBA
point of view, if we route all our native OS stats to log files in a proper
format, then we can have our log reporting tools to give most effective
reports. Also, Applications can log their own messages to postgres log
files, which can be monitored by DBAs too.

Implementation:
    Implemented a new function "pg_report_log" which takes one argument as
text, and returns void. I took, "LOG" prefix for all the reporting
messages.I wasn't sure to go with new prefix for this, since these are
normal LOG messages. Let me know, if i am wrong here.

Here is the attached patch.

Regards,
Dinesh
manojadinesh.blogspot.com
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 76f77cb..b2fc4cd 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -17850,6 +17850,15 @@ postgres=# SELECT * FROM 
pg_xlogfile_name_offset(pg_stop_backup());
         Return information about a file.
        </entry>
       </row>
+      <row>
+       <entry>
+        <literal><function>pg_report_log(<parameter>message</> 
<type>text</type>])</function></literal>
+       </entry>
+       <entry><type>void</type></entry>
+       <entry>
+        Write message into log file.
+       </entry>
+      </row>
      </tbody>
     </tgroup>
    </table>
@@ -17918,6 +17927,18 @@ SELECT (pg_stat_file('filename')).modification;
 </programlisting>
    </para>
 
+   <indexterm>
+    <primary>pg_report_log</primary>
+   </indexterm>
+   <para>
+    <function>pg_report_log</> is useful to write custom messages
+    into current log destination and returns <type>void</type>.
+    Typical usages include:
+<programlisting>
+SELECT pg_report_log('Message');
+</programlisting>
+   </para>
+
   </sect2>
 
   <sect2 id="functions-advisory-locks">
diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c
index c0495d9..6c54f3a 100644
--- a/src/backend/utils/adt/misc.c
+++ b/src/backend/utils/adt/misc.c
@@ -76,6 +76,23 @@ current_query(PG_FUNCTION_ARGS)
 }
 
 /*
+ * pg_report_log()
+ *
+ * Printing custom log messages in log file.
+ */
+
+Datum
+pg_report_log(PG_FUNCTION_ARGS)
+{
+
+       ereport(MESSAGE,
+                       (errmsg("%s", text_to_cstring(PG_GETARG_TEXT_P(0))),
+                       errhidestmt(true)));
+
+       PG_RETURN_VOID();
+}
+
+/*
  * Send a signal to another backend.
  *
  * The signal is delivered if the user is either a superuser or the same
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 088c714..2e8f547 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -302,7 +302,7 @@ errstart(int elevel, const char *filename, int lineno,
                                                                elevel == INFO);
        }
 
-       /* Skip processing effort if non-error message will not be output */
+       /* Skip processing effort if non-error,custom message will not be 
output */
        if (elevel < ERROR && !output_to_server && !output_to_client)
                return false;
 
@@ -2062,6 +2062,7 @@ write_eventlog(int level, const char *line, int len)
                case DEBUG3:
                case DEBUG2:
                case DEBUG1:
+               case MESSAGE:
                case LOG:
                case COMMERROR:
                case INFO:
@@ -2917,6 +2918,7 @@ send_message_to_server_log(ErrorData *edata)
                        case DEBUG1:
                                syslog_level = LOG_DEBUG;
                                break;
+                       case MESSAGE:
                        case LOG:
                        case COMMERROR:
                        case INFO:
@@ -3547,6 +3549,7 @@ error_severity(int elevel)
                case DEBUG5:
                        prefix = _("DEBUG");
                        break;
+               case MESSAGE:
                case LOG:
                case COMMERROR:
                        prefix = _("LOG");
@@ -3666,6 +3669,9 @@ is_log_level_output(int elevel, int log_min_level)
        /* Neither is LOG */
        else if (elevel >= log_min_level)
                return true;
+       /* If elevel is MESSAGE, then ignore log settings */
+       else if (elevel == MESSAGE)
+               return true;
 
        return false;
 }
diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h
index 6fd1278..62c619a 100644
--- a/src/include/catalog/pg_proc.h
+++ b/src/include/catalog/pg_proc.h
@@ -5344,6 +5344,11 @@ DESCR("tsm_bernoulli_reset(internal)");
 DATA(insert OID = 3346 (  tsm_bernoulli_cost           PGNSP PGUID 12 1 0 0 0 
f f f f t f v 7 0 2278 "2281 2281 2281 2281 2281 2281 2281" _null_ _null_ 
_null_ _null_ _null_ tsm_bernoulli_cost _null_ _null_ _null_ ));
 DESCR("tsm_bernoulli_cost(internal)");
 
+/* Logging function */
+
+DATA(insert OID = 6015 (  pg_report_log                PGNSP PGUID 12 1 0 0 0 
f f f f t f v 1 0 2278 "1043" _null_ _null_ _null_ _null_ _null_ pg_report_log 
_null_ _null_ _null_ ));
+DESCR("write message to log file");
+
 /*
  * Symbolic values for provolatile column: these indicate whether the result
  * of a function is dependent *only* on the values of its explicit arguments,
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index fcb0bf0..3a2164b 100644
--- a/src/include/utils/builtins.h
+++ b/src/include/utils/builtins.h
@@ -495,6 +495,7 @@ extern Datum pg_typeof(PG_FUNCTION_ARGS);
 extern Datum pg_collation_for(PG_FUNCTION_ARGS);
 extern Datum pg_relation_is_updatable(PG_FUNCTION_ARGS);
 extern Datum pg_column_is_updatable(PG_FUNCTION_ARGS);
+extern Datum pg_report_log(PG_FUNCTION_ARGS);
 
 /* oid.c */
 extern Datum oidin(PG_FUNCTION_ARGS);
diff --git a/src/include/utils/elog.h b/src/include/utils/elog.h
index 7684717..3054d3c 100644
--- a/src/include/utils/elog.h
+++ b/src/include/utils/elog.h
@@ -16,6 +16,9 @@
 
 #include <setjmp.h>
 
+/* Custom log message */
+#define MESSAGE                9                       /* Custom messages to 
log file*/
+
 /* Error level codes */
 #define DEBUG5         10                      /* Debugging messages, in 
categories of
                                                                 * decreasing 
detail. */
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to