postgres file_fdw如何查询日志

创建file_fdw

CREATE EXTENSION file_fdw;
CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;

创建外部表

CREATE FOREIGN TABLE pglog (
  log_time text,
  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 text,
  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,
  backend_type text
) SERVER pglog
OPTIONS ( filename 'log/postgresql-Mon.csv', format 'csv' );
其中pg13多了一个字段backend_type

同时查看多个csv文件


#根据program查询最近7天的csv日志
find $PGDATA/log -type f -name "*.csv" -mtime -7 -exec cat {} \;

CREATE FOREIGN TABLE pglog (
  log_time text,
  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 text,
  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,
  backend_type text
) SERVER pglog
OPTIONS ( program 'find $PGDATA/log -type f -name "*.csv" -mtime -7 -exec cat {} \;', format 'csv' );


调整日志级别

postgres=# show log_min_messages;
 log_min_messages 
------------------
 notice
(1 row)
postgres=# show client_min_messages ;
 client_min_messages 
---------------------
 notice
(1 row)


log_destination = 'csvlog'    
logging_collector = on    
log_directory = 'log'    
# 保留一周,每天一个文件,当ROTATE时间周期到达后,重复使用同一个文件前truncate文件内容    
log_filename = 'postgresql-%a.log'    
log_truncate_on_rotation = on    
log_rotation_age = 1d     
  
# 如果文件达到100MB,切换到下一个文件(如果文件名与当前已有文件名同名,则APPEND,而不会truncate这个文件)    
# 所以以上配置,1天内文件大小可以超过100MB。   
log_rotation_size = 100MB

存储过程调用

do $$               
begin
insert into t values(1);
raise notice '%','测试===============';
perform pg_sleep(50);
insert into t values(2);
raise notice '%','测试10===============';
end;
$$
;

查看日志

# 日志在第一个insert后已经写入到日志中
postgres=# select * from pglog where message like '%测试%' order by log_time desc;
          log_time           | user_name | database_name | process_id | connection_from |   session_id   | session_line_num | command_tag |   session_start_time    | virtual_transa
ction_id | transaction_id | error_severity | sql_state_code |        message        | detail | hint | internal_query | internal_query_pos |                       context           
            | query | query_pos | location | application_name |  backend_type  
-----------------------------+-----------+---------------+------------+-----------------+----------------+------------------+-------------+-------------------------+---------------
---------+----------------+----------------+----------------+-----------------------+--------+------+----------------+--------------------+-----------------------------------------
------------+-------+-----------+----------+------------------+----------------
 2022-03-07 17:34:35.989 CST | pg13      | postgres      |      72919 | [local]         | 6225cc4f.11cd7 |                7 | DO          | 2022-03-07 17:11:43 CST | 3/5           
         |          20335 | NOTICE         | 00000          | 测试===============   |        |      |                |                    | PL/pgSQL function inline_code_block line
 4 at RAISE |       |           |          | psql             | client backend
 2022-03-07 17:18:22.519 CST | pg13      | postgres      |      72919 | [local]         | 6225cc4f.11cd7 |                6 | DO          | 2022-03-07 17:11:43 CST | 3/4           
         |          20332 | NOTICE         | 00000          | 测试10=============== |        |      |                |                    | PL/pgSQL function inline_code_block line
 7 at RAISE |       |           |          | psql             | client backend

链接:http://postgres.cn/docs/13/file-fdw.html

你可能感兴趣的:(数据库,postgresql)