原文: https://www.enmotech.com/web/detail/1/798/1.html
鼓樓網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),鼓樓網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為鼓樓成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的鼓樓做網(wǎng)站的公司定做!
導(dǎo)讀:本文主要介紹PostgreSQL的日志文件參數(shù)及注意事項(xiàng),從csv日志中載入數(shù)據(jù)庫(kù)。通過靈活的數(shù)據(jù)加載方式,讓SQL在處理很多問題上更加簡(jiǎn)捷便利。
運(yùn)行日志參數(shù)
1.1 運(yùn)行日志主要參數(shù)
運(yùn)行日志主要相關(guān)的參數(shù)如下,默認(rèn)沒有開啟的話沒有l(wèi)og目錄,開啟后會(huì)自動(dòng)生成。
1.2 注意事項(xiàng)
設(shè)置csv格式日志的話一定要設(shè)置logging_collector 為 on
pg10版本的運(yùn)行日志一般在$PGDATA/log目錄下
log目錄是開啟運(yùn)行日志后自動(dòng)生成的
可以通過log_rotation_age來(lái)設(shè)置多久重新生成一個(gè)日志文件
可以通過log_rotation_size來(lái)設(shè)置多大的日志來(lái)重新生成日志文件
上面兩個(gè)都需要配合log_truncate_on_rotation 為 on來(lái)使用
可以開啟log_duration來(lái)記錄sql執(zhí)行時(shí)間
可以開啟log_statement來(lái)記錄數(shù)據(jù)庫(kù)ddl
1.3 csv日志載入數(shù)據(jù)庫(kù)
Oracle有外部表,pg也有fdw。oracle可以用外部表的方式將alert日志載入到數(shù)據(jù)庫(kù)中用SQL來(lái)查看。PG可以用copy命令將csv日志載入到數(shù)據(jù)庫(kù)中用SQL來(lái)查看。這種方式都可以很方便得用sql來(lái)查詢想要的日志內(nèi)容。這種方式的有點(diǎn)是顯而易見的,就是可以很容易得用SQL來(lái)查詢和過濾日志,pg的日志文件可以截?cái)喾指畛扇舾尚∥募梢暂d入自己需要的日志。而Oracle的alert通常會(huì)很大。
缺點(diǎn)也是顯而易見的,如果數(shù)據(jù)庫(kù)掛了就不能用這種方式來(lái)查看日志。而且pg的csv日志不容易直接閱讀。
1.3.1 創(chuàng)建日志表
創(chuàng)建了一個(gè)數(shù)據(jù)庫(kù)和新的表來(lái)載入日志
postgres=# create database test;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "pg12".
test=# CREATE TABLE pg_log
test-# (
test(# log_time timestamp(3) with time zone,
test(# user_name text,
test(# database_name text,
test(# process_id integer,
test(# connection_from text,
test(# session_id text,
test(# session_line_num bigint,
test(# command_tag text,
test(# session_start_time timestamp with time zone,
test(# virtual_transaction_id text,
test(# transaction_id bigint,
test(# error_severity text,
test(# sql_state_code text,
test(# message text,
test(# detail text,
test(# hint text,
test(# internal_query text,
test(# internal_query_pos integer,
test(# context text,
test(# query text,
test(# query_pos integer,
test(# location text,
test(# application_name text,
test(# PRIMARY KEY (session_id, session_line_num)
test(# );CREATE TABLE
test=#
1.3.2 查看日志文件名字
[pg12@whf307 ~]$ cd $PGDATA/log
[pg12@whf307 log]$ ls -rtl
total 24
-rw------- 1 pg12 pg12 166 May 30 13:32 postgresql-2019-05-30_133202.log
-rw------- 1 pg12 pg12 496 May 30 13:32 postgresql-2019-05-30_133202.csv
-rw------- 1 pg12 pg12 0 May 30 13:32 postgresql-2019-05-30_133254.log
-rw------- 1 pg12 pg12 170 May 30 13:32 postgresql-2019-05-30_133254.csv
-rw------- 1 pg12 pg12 166 May 30 13:33 postgresql-2019-05-30_133324.log
-rw------- 1 pg12 pg12 6566 May 30 16:16 postgresql-2019-05-30_133324.csv
-rw------- 1 pg12 pg12 0 May 31 00:00 postgresql-2019-05-31_000000.log
-rw------- 1 pg12 pg12 0 May 31 00:00 postgresql-2019-05-31_000000.csv
[pg12@whf307 log]$[pg12@whf307 log]$ pwd
/soft/pg_data/log
[pg12@whf307 log]$
1.3.3 載入到數(shù)據(jù)庫(kù)
[pg12@whf307 log]$ psql test
psql (12beta1)
Type "help" for help.test=# \d
List of relations
Schema | Name | Type | Owner
--------+--------+-------+-------
public | pg_log | table | pg12
(1 row)test=# copy pg_log from '/soft/pg_data/log/postgresql-2019-05-30_133324.csv' with csv;
COPY 32
1.3.4 查看日志
這樣就可以用sql來(lái)查看了。執(zhí)行一個(gè)普通查詢
test=# select relfilenode from pg_class where relname='pg_log';
relfilenode
-------------
16385
(1 row)
載入最新的日志。這里可以重復(fù)載入,不會(huì)覆蓋之前的數(shù)據(jù)。
[pg12@whf307 log]$ ls -rtl
total 32
-rw------- 1 pg12 pg12 166 May 30 13:32 postgresql-2019-05-30_133202.log
-rw------- 1 pg12 pg12 496 May 30 13:32 postgresql-2019-05-30_133202.csv
-rw------- 1 pg12 pg12 0 May 30 13:32 postgresql-2019-05-30_133254.log
-rw------- 1 pg12 pg12 170 May 30 13:32 postgresql-2019-05-30_133254.csv
-rw------- 1 pg12 pg12 166 May 30 13:33 postgresql-2019-05-30_133324.log
-rw------- 1 pg12 pg12 6566 May 30 16:16 postgresql-2019-05-30_133324.csv
-rw------- 1 pg12 pg12 0 May 31 00:00 postgresql-2019-05-31_000000.log
-rw------- 1 pg12 pg12 4545 May 31 00:37 postgresql-2019-05-31_000000.csv
[pg12@whf307 log]$ psql test
psql (12beta1)
Type "help" for help.test=# copy pg_log from '/soft/pg_data/log/postgresql-2019-05-31_000000.csv' with csv;
COPY 28
再次查看日志
test=# SELECT COUNT(*) FROM PG_LOG;count
-------
60
(1 row)test=# select log_time at time zone 'UTC' ,database_name,connection_from,query from pg_log where log_time>to_timestamp('2019-05-31 14:35:00','yyyy-mm-dd hh34:mi:ss');
timezone | database_name | connection_from | query
-------------------------+---------------+-----------------+-----------------------------------------------------------
2019-05-31 06:35:42.843 | test | [local] |
2019-05-31 06:35:57.582 | test | [local] |
2019-05-31 06:36:54.369 | test | [local] | selectt relfilenode from pg_class where relname='pg_log';
2019-05-31 06:36:58.002 | test | [local] |
2019-05-31 06:37:00.192 | test | [local] |
2019-05-31 06:37:11.651 | | [local] |2019-05-31 06:37:11.651 | test | [local] |(7 rows)
可以看到記錄數(shù)變成了60,之前的記錄沒有被覆蓋,我們可以一直使用該表,可以用sql來(lái)查看sql,數(shù)據(jù)庫(kù),登錄時(shí)間等等的所有日志。
查看日志起始結(jié)束時(shí)間:
test=# select min(log_time) at time zone 'UTC',max(log_time) at time zone 'UTC' from pg_log;
timezone | timezone
-------------------------+-------------------------
2019-05-30 19:33:24.892 | 2019-05-31 06:37:11.651
(1 row)
有了靈活的數(shù)據(jù)加載方式,讓SQL處理很多問題更加簡(jiǎn)捷便利。
想了解更多關(guān)于數(shù)據(jù)庫(kù)、云技術(shù)的內(nèi)容嗎?
快來(lái)關(guān)注“數(shù)據(jù)和云”公眾號(hào)、“云和恩墨”官方網(wǎng)站,我們期待與大家一同學(xué)習(xí)和進(jìn)步!
(掃描上方二維碼,關(guān)注“數(shù)據(jù)和云”公眾號(hào),即可查看更多科技文章)
當(dāng)前文章:PostgreSQL的日志文件和數(shù)據(jù)加載
URL分享:http://www.chinadenli.net/article24/jdhhce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站收錄、標(biāo)簽優(yōu)化、品牌網(wǎng)站制作、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)