RDS PostgreSQL提供log_fdw插件,可以查詢數據庫日志。
您可以加入RDS PostgreSQL插件交流釘釘群(103525002795),進行咨詢、交流和反饋,獲取更多關于插件的信息。
前提條件
該插件僅支持RDS PostgreSQL 11,如果無法創建插件,請參見升級內核小版本。
背景信息
log_fdw插件提供如下兩個函數,幫助您查詢數據庫日志。
list_postgres_log_files()
:列出所有的csvlog文件。create_foreign_table_for_log_file(IN table_name text, IN log_server text, IN log_file text)
:創建一個與特定csvlog文件相對應的外部表。
操作步驟
創建log_fdw插件。
postgres=> create extension log_fdw; CREATE EXTENSION
創建log服務器定義。
postgres=> create server <server名稱> foreign data wrapper log_fdw;
示例
postgres=> create server log_server foreign data wrapper log_fdw; CREATE SERVER
調用
list_postgres_log_files()
函數,列出所有的csvlog文件。postgres=> select * from list_postgres_log_files() order by 1; file_name | file_size_bytes ----------------------------------+----------------- postgresql-2020-01-10_095546.csv | 3794 postgresql-2020-01-10_100336.csv | 318318 postgresql-2020-01-11_000000.csv | 198437 postgresql-2020-01-11_083546.csv | 4775 postgresql-2020-01-13_030618.csv | 3347
調用
create_foreign_table_for_log_file(IN table_name text, IN log_server text, IN log_file text)
函數,創建一個與特定csvlog文件相對應的外部表。postgres=> select create_foreign_table_for_log_file('<外部表名稱>', '<log服務器名稱>', '<csvlog文件名稱>');
示例
postgres=> select create_foreign_table_for_log_file('ft1', 'log_server', 'postgresql-2020-01-13_030618.csv'); create_foreign_table_for_log_file ----------------------------------- t (1 row)
查詢外部表即可查詢到日志內容。
postgres=> select log_time, message from <外部表名稱> order by log_time desc limit 2;
示例
postgres=> select log_time, message from ft1 order by log_time desc limit 2; log_time | message ----------------------------+------------------------------------------------------------------- 2020-01-13 03:35:00.003+00 | cron job 1 completed: INSERT 0 1 1 2020-01-13 03:35:00+00 | cron job 1 starting: INSERT INTO cron_test VALUES ('Hello World') (2 rows)
外部表結構
postgres=> \d+ ft1
Foreign table "public.ft1"
Column | Type | Collation | Nullable | Default | FDW options | Storage | Stats target | Description
------------------------+-----------------------------+-----------+----------+---------+-------------+----------+--------------+-------------
log_time | timestamp(3) with time zone | | | | | plain | |
user_name | text | | | | | extended | |
database_name | text | | | | | extended | |
process_id | integer | | | | | plain | |
connection_from | text | | | | | extended | |
session_id | text | | | | | extended | |
session_line_num | bigint | | | | | plain | |
command_tag | text | | | | | extended | |
session_start_time | timestamp with time zone | | | | | plain | |
virtual_transaction_id | text | | | | | extended | |
transaction_id | bigint | | | | | plain | |
error_severity | text | | | | | extended | |
sql_state_code | text | | | | | extended | |
message | text | | | | | extended | |
detail | text | | | | | extended | |
hint | text | | | | | extended | |
internal_query | text | | | | | extended | |
internal_query_pos | integer | | | | | plain | |
context | text | | | | | extended | |
query | text | | | | | extended | |
query_pos | integer | | | | | plain | |
location | text | | | | | extended | |
application_name | text | | | | | extended | |
Server: log_server
FDW options: (filename 'postgresql-2020-01-13_030618.csv')
文檔內容是否對您有幫助?