postgresql的日志信息

前端之家收集整理的这篇文章主要介绍了postgresql的日志信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

postgresql的操作日志默认是关闭的,需要对数据库进行一定的配置后才可看到日志信息。日志存放目录一般在/Postgresql/9.2/data/pg_log

Postgresql有3种日志,分别是pg_log(数据库运行日志)、pg_xlog(WAL 日志,即重做日志)、pg_clog(事务提交日志,记录的是事务的元数据)

pg_log默认是关闭的,需要设置参数启用此日志。pg_xlog和pg_clog都是强制打开的,无法关闭

修改postgresql配置文件postgresql.conf 存放目录:Postgresql/9.2/data

1.启用pg_log并配置日志参数
log_destination = 'csvlog'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_rotation_age = 1d
log_rotation_size = 100MB
log_min_messages = info
# 记录执行慢的sql
log_min_duration_statement = 60
log_checkpoints = on
log_connections = on
log_disconnections = on
log_duration = on
log_line_prefix = '%m'
# 监控数据库中长时间的锁
log_lock_waits = on
# 记录DDL操作
log_statement = 'ddl'


2.重启Postgresql即可在$PGDATA/pg_log/ 下看到新生成的日志。

pg_ctl restart -m fast


--日志文件目录
日志的目录可以通过参数 log_directory 来设置,下面是我的参数设置。
log_directory = '/var/applog/pg_log'

[postgres@pg_log]$ ll /var/applog/pg_log
-rw------- 1 postgres postgres 4.8M Mar 14 23:57 postgresql-2011-03-14_000000.csv
-rw------- 1 postgres postgres 0 Mar 14 00:00 postgresql-2011-03-14_000000.log
-rw------- 1 postgres postgres 294K Mar 15 15:10 postgresql-2011-03-15_000000.csv
-rw------- 1 postgres postgres 0 Mar 15 00:00 postgresql-2011-03-15_000000.log

--CSV日志文件内容
2011-03-15 00:07:03.513 CST,"wapportal","wapportal_216",4137,"172.16.3.43:59356",4d7e361f.1029,3,"idle",2011-03-14 23:37:03 CST,LOG,00000,"disconnection: session time: 0:30:00.086 user=wapportal database=wapportal_216 host=172.16.3.43 port=59356",""
2011-03-15 00:07:03.514 CST,5173,"",4d7e3d27.1435,1,2011-03-15 00:07:03 CST,"connection received: host=172.16.3.43 port=51135",""

上面两条是 postgresql-2011-03-15_000000.csv 日志文件的部分内容,由于日志文件的可读性
较差,于是可以通过下面方法将CSV日志导入到数据库表里。详细如下(我用命令未成功导入)

将CSV日志导入数据库表里
1--调整参数
log_destination = 'csvlog'
logging_collector = on

这两个参数修改后,PG SERVER 需要重启。

2--创建日志记录表

CREATE TABLE "soc"."postgres_log" (
"logId" int8 DEFAULT nextval('postgres_log_seq'::regclass) NOT NULL,
"log_time" timestamptz(3),
"user_name" text COLLATE "default",
"database_name" text COLLATE "default",
"process_id" int4,
"connection_from" text COLLATE "default",
"session_id" text COLLATE "default",
"session_line_num" int8,
"command_tag" text COLLATE "default",
"session_start_time" timestamptz(6),
"virtual_transaction_id" text COLLATE "default",
"transaction_id" int8,
"error_severity" text COLLATE "default",
"sql_state_code" text COLLATE "default",
"message" text COLLATE "default",
"detail" text COLLATE "default",
"hint" text COLLATE "default",
"internal_query" text COLLATE "default",
"internal_query_pos" int4,
"context" text COLLATE "default",
"query" text COLLATE "default",
"query_pos" int4,
"location" text COLLATE "default",
"application_name" text COLLATE "default"
)
WITH (OIDS=FALSE)

;

CREATE SEQUENCE "soc"."NewSequence"
INCREMENT 1
MINVALUE 1
MAXVALUE 999999999999999999
START 1
CACHE 1;

ALTER TABLE "soc"."NewSequence" OWNER TO "soc";

SELECT setval('"soc"."NewSequence"',true);

备注:创建日志表 postgres_log 用来保存 CSV日志数据,创建自增序列。

3--导入操作系统 csv 日志到表 postgres_log 表
skytf=# copy skytf.postgres_log from '/var/applog/pg_log/postgresql-2011-03-14_000000.csv' with csv;
COPY 26031

skytf=# copy skytf.postgres_log from '/var/applog/pg_log/postgresql-2011-03-15_000000.csv' with csv;
COPY 1297

备注:文件形式导入导出数据需要以超级用户 postgres 连接到目标库。

4--常用日志分析sql
skytf=# select min(log_time),max(log_time) from skytf.postgres_log;
min | max
----------------------------+----------------------------
2011-03-14 14:04:07.275+08 | 2011-03-16 05:04:34.427+08
(1 row)

skytf=> select log_time,database_name,user_name,application_name,message from postgres_log where message like '%duration%'; log_time | database_name | user_name | application_name | mess age ----------------------------+---------------+-----------+------------------+-------------------------------------------------------- ------------------------------------------------------- 2011-03-15 00:23:38.957+08 | db_lbs | lbs | | duration: 1297.440 ms execute <unnamed>: SELECT cit yname,province,the_geom as the_geom FROM china_city ....... 为了显示方便,上面只取一条记录。

原文链接:https://www.f2er.com/postgresql/194482.html

猜你在找的Postgre SQL相关文章