postgresql 日志管理-外部表

84593973 2020-04-09

postgresql的日志管理比较灵活,可以设置多种格式
log_destination=‘csvlog‘
;调成csvlog后log_line_prefix无效;然后自己写个脚本就可以在数据库中方便的调用了,我这里用的是外部表方式,得装file_fdw扩展;
如果不装就可以考虑直接copy到数据库里去;
#!/bin/bash
#export PGDATA=/ssd/database/data5433
source ~/.bash_profile
if [[ $1 = "" || $2 = "" ]]; 
then 
echo "usage create_log.sh databasename port"
exit 1
else
echo $1 $2
log_dir=`grep -i  ^log_directory $PGDATA/postgresql.conf|awk ‘{print $3}‘`
log_dir=`echo ${log_dir//\‘/}`
logfile=`ls $PGDATA/$log_dir/*.csv|sort -nr |head -n 1`
psql -d $1 -c ‘drop  FOREIGN table pg_fdw_log‘ -p $2 -U postgres -h 127.0.0.1
echo $logfile
echo -e "
create foreign table pg_fdw_log
(
  log_time timestamp(3) with time zone,
  user_name text,
  database_name text,
  process_id integer,
  connection_from text,
  session_id text,
  session_line_num bigint,
  command_tag text,
  session_start_time timestamp with time zone,
  virtual_transaction_id text,
  transaction_id bigint,
  error_severity text,
  sql_state_code text,
  message text,
  detail text,
  hint text,
  internal_query text,
  internal_query_pos integer,
  context text,
  query text,
  query_pos integer,
  location text,
  application_name text
 -- PRIMARY KEY (session_id, session_line_num)
)SERVER file_fdw_server
OPTIONS (format ‘csv‘,header ‘true‘,filename ‘$logfile‘, delimiter ‘,‘, null ‘‘);
" > create_pg_log.sql
psql -d $1 -f create_pg_log.sql -p $2 -U postgres -h 127.0.0.1
psql -d $1 -c ‘select count(*) from pg_fdw_log‘ -p $2 -U postgres -h 127.0.0.1
fi

相关推荐

84593973 / 0评论 2020-06-14