2015-12-21 46 views

回答

1

正如PostgreSQL Manual建议您可以使用Postgres的解析自己的日志:

  • 创建临时表;
  • 将日志文件导入到它;
  • 选择所需数据。

这里是简单的bash脚本来自动化过程。

#!/usr/bin/env bash 

psql -t << EOF 
CREATE TEMPORARY TABLE postgres_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) 
); 

COPY postgres_log FROM '$1' WITH csv; 

SELECT query from postgres_log; 
EOF 

您可以运行它:./fetch-queries.sh /full/path/to/logfile.csv