PG9.4官方英文文档:html
关于file_fdw:http://www.postgresql.org/docs/9.4/static/file-fdw.htmlsql
关于日志配置:http://www.postgresql.org/docs/9.4/static/runtime-config-logging.htmlshell
一、 配置postgresql.conf文件中关于日志的参数数据库
log_destination = 'csvlog' #生成日csv格式的日志文件 logging_collector = on log_directory = 'pg_log' #日志文件存储在./data/pg_log目录 log_filename = 'postgresql-%Y-%m-%d' #定义日志文件名字为postgresql-2013-12-17.csv log_truncate_on_rotation = off log_rotation_age = 1d #设置日志文件生成的频率为1天 log_rotation_size = 0 log_error_verbosity = verbose #设置日志文件中的错误信息为详细 log_statement = all #设置全部语句均计入日志
2、配置file_fdw并建立外部表
session
检查pg安装目录的lib/postgresql目录下是否存在file_fdw.so文件,确保安装时已经编译安装了file_fdw组件。app
如下须要以数据库postgres用户链接到postgres数据库进行操做: post
建立扩展:spa
postgres=# create extension file_fdw;
建立外部服务:
日志
postgres=# create server pglog FOREIGN DATA WRAPPER file_fdw;
建立外部表:postgresql
postgres=# CREATE FOREIGN TABLE pglog ( 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 ) SERVER pglog OPTIONS (filename '/user/data/pg_log/postgresql-2013-12-17.csv',format 'csv');
注意:以上filename中的文件路径要求是绝对路径。
能够用如下SQL删除外部表:
postgres=# drop foreign table pglog;
3、使用外部表读取日志信息
postgres=# select log_time,connection_from,user_name,database_name,query,application_name from pglog where query is not null;
实际应用中能够根据须要为SQL语句添加过滤条件。