Advertisement
Guest User

function WRITE_MESSAGE_TO_TABLE

a guest
Mar 30th, 2018
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. \echo Start functions/WRITE_MESSAGE_TO_TABLE.pg_sql
  2.  
  3. /** Write given parameters into the logging table.
  4.  *  It is meant to be called in an autonomous transaction to and best done
  5.  *  in a separate process to reduce impact on main transaction as much as
  6.  *  possible. As colateral the ID of written record is returned.
  7.  *
  8.  *  @function WRITE_MESSAGE_TO_TABLE
  9.  *  @return bigint ID of written record
  10.  *  @param in text I_FUNCTION Value for LOG.FUNCTION.
  11.  *  @param in text I_MESSAGE Value for LOG.MESSAGE.
  12.  *  @param in text I_LEVEL Value for LOG.LEVEL.
  13.  *  @param in name I_PRESENT_USER Value for LOG.PRESENT_USER.
  14.  *  @param in name I_SESSION_USER Value for LOG.SESS_USER.
  15.  *  @param in timestamp_with_timezone I_TRANSACTION_TIMESTAMP Value for LOG.TRANSACTION_TIMESTAMP.
  16.  *  @param in bigint I_TRANSACTION_ID Value for LOG.TRANSACTION_ID.
  17.  *  @param in bigint I_SERVER_PID Value for LOG.SERVER_PID.
  18.  *  @param in inet I_REMOTE_ADDRESS Value for LOG.REMOTE_ADDRESS.
  19.  *  @param in int I_REMOTE_PORT Value for LOG.REMOTE_PORT.
  20.  *  @private
  21.  *  @info This function is meant to be private. Its use outside pglogger is
  22.  *        very limited at most. That's why it is marked as private eventhough
  23.  *        it is visible to the outside. All of this is due to the fact that
  24.  *        PostgreSQL 10 and earlier does not support packages.
  25.  *  @version $Id: WRITE_MESSAGE_TO_TABLE.pg_sql 4 2018-03-30 16:39:10Z thiemo $
  26.  *  @todo Once PostgreSQL supports packages, integrate this function into one.
  27.  *        For the time being, it should get installed within a schema on its
  28.  *        own and no execution grants should be given on it.
  29.  *  @todo Once HyperSQL supports a license tag, convert the corresponding
  30.  *        info tag into one.
  31.  *  @copyright Thiemo Kellner, 2018 -
  32.  *  @info License LGPLv3
  33.  *  @webpage https://www.sourceforge.net/projects/pglogger
  34.  */
  35. create or replace function WRITE_MESSAGE_TO_TABLE(
  36.     I_FUNCTION text,
  37.     I_MESSAGE text,
  38.     I_LEVEL text,
  39.     I_PRESENT_USER name,
  40.     I_SESSION_USER name,
  41.     I_TRANSACTION_TIMESTAMP timestamp with time zone,
  42.     I_TRANSACTION_ID bigint,
  43.     I_SERVER_PID bigint,
  44.     I_REMOTE_ADDRESS inet,
  45.     I_REMOTE_PORT int
  46. )
  47.   returns bigint
  48.   language plpgsql
  49.   volatile as
  50. $body$
  51.     declare
  52.         V_ID bigint;
  53.     begin
  54.         insert into LOG(
  55.             FUNCTION,
  56.             MESSAGE,
  57.             LEVEL,
  58.             PRESENT_USER,
  59.             SESS_USER,
  60.             TRANSACTION_TIMESTAMP,
  61.             TRANSACTION_ID,
  62.             SERVER_PID,
  63.             REMOTE_ADDRESS,
  64.             REMOTE_PORT
  65.         ) values (
  66.             I_FUNCTION,
  67.             I_MESSAGE,
  68.             I_LEVEL,
  69.             I_PRESENT_USER,
  70.             I_SESSION_USER,
  71.             I_TRANSACTION_TIMESTAMP,
  72.             I_TRANSACTION_ID,
  73.             I_SERVER_PID,
  74.             I_REMOTE_ADDRESS,
  75.             I_REMOTE_PORT
  76.         ) returning ID into V_ID;
  77.         return V_ID;
  78.     end;
  79. $body$;
  80.  
  81. comment on function WRITE_MESSAGE_TO_TABLE(
  82.     I_FUNCTION text,
  83.     I_MESSAGE text,
  84.     I_LEVEL text,
  85.     I_PRESENT_USER name,
  86.     I_SESSION_USER name,
  87.     I_TRANSACTION_TIMESTAMP timestamp with time zone,
  88.     I_TRANSACTION_ID bigint,
  89.     I_SERVER_PID bigint,
  90.     I_REMOTE_ADDRESS inet,
  91.     I_REMOTE_PORT int
  92. ) is
  93.   'Write given parameters into the logging table. It is meant to be called in an autonomous transaction to and best done in a separate process to reduce impact on main transaction as much as possible.
  94. As colateral the ID of written record is returned.
  95. $Header: svn+ssh://thiemo@svn.code.sf.net/p/pglogger/code/functions/WRITE_MESSAGE_TO_TABLE.pg_sql 4 2018-03-30 16:39:10Z thiemo $';
  96.  
  97. commit; -- unlike Oracle not all ddl commit implicitly
  98.  
  99. \echo End functions/WRITE_MESSAGE_TO_TABLE.pg_sql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement