84593973 2020-04-09
#!/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