log_fdw 拡張機能は、PostgreSQL の CSV ログファイルを外部テーブルにマップします。これにより、シェルアクセスを必要とせずに、標準 SQL を使用してデータベースログのクエリを実行できます。この機能を使用して、低速クエリの分析、特定の期間のアクティビティの監査、または重大度、ユーザー、セッションによるログのフィルター処理が可能です。
前提条件
作業を開始する前に、以下を確認してください。
ご利用の RDS インスタンスで PostgreSQL 11 が実行されていること。
拡張が作成できない場合は、マイナーエンジンバージョンを更新する インスタンス
仕組み
log_fdw は、次の 2 つの関数を提供します。
| 関数 | 説明 |
|---|---|
list_postgres_log_files() | 利用可能なすべての .csv ログファイルとそのサイズを一覧表示します。 |
create_foreign_table_for_log_file(IN table_name text, IN log_server text, IN log_file text) | 特定の .csv ログファイルにマップされた外部テーブルを作成します。 |
外部テーブルが作成されると、通常のテーブルと同様にクエリを実行してログデータを取得できます。
log_fdw を使用したログのクエリ
ステップ 1:拡張機能の作成
postgres=> create extension log_fdw;
CREATE EXTENSIONステップ 2:ログサーバーの定義
postgres=> create server log_server foreign data wrapper log_fdw;
CREATE SERVERlog_server を、ログサーバーに割り当てる名前に置き換えてください。
ステップ 3:利用可能なログファイルの一覧表示
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ステップ 4:ログファイル用の外部テーブルの作成
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)3 つのパラメーターは、外部テーブル名、ログサーバー名、ログファイル名です。
ステップ 5:外部テーブルへのクエリ実行
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)外部テーブルのスキーマ
create_foreign_table_for_log_file() によって作成される外部テーブルには、次の列があります。
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')ログ分析の主要な列:
| 列 | 型 | 説明 |
|---|---|---|
log_time | timestamp(3) with time zone | ログエントリのタイムスタンプ |
user_name | text | イベントをトリガーしたデータベースユーザー |
database_name | text | イベントが発生したデータベース |
process_id | integer | PostgreSQL バックエンドの OS プロセス ID |
error_severity | text | ログレベル:DEBUG、INFO、NOTICE、WARNING、ERROR、LOG、FATAL、または PANIC |
sql_state_code | text | 5 文字の SQLSTATE エラーコード (SQL 標準に準拠) |
message | text | プライマリログメッセージ |
detail | text | セカンダリ詳細メッセージ (存在する場合) |
hint | text | エラーを解決するための推奨アクション (提供されている場合) |
internal_query | text | エラーをトリガーした内部クエリ (該当する場合) |
internal_query_pos | integer | エラーが発生した internal_query 内の文字オフセット |
context | text | ログエントリ時点でのコールスタックのコンテキスト |
query | text | ユーザーが指定したクエリ文字列 (該当する場合) |
query_pos | integer | エラーが発生した query 内の文字オフセット |
command_tag | text | ログエントリに関連付けられた SQL コマンドタイプ (例:SELECT、INSERT) |
session_id | text | データベースセッションの一意の識別子 |
session_start_time | timestamp with time zone | データベースセッションが開始された時刻 |
session_line_num | bigint | セッションのログ出力内の行番号 |
virtual_transaction_id | text | 実際のトランザクション ID が割り当てられる前に割り当てられた仮想トランザクション ID |
transaction_id | bigint | 割り当てられた実際のトランザクション ID |
connection_from | text | クライアントのホストアドレスとポート |
location | text | ログエントリが生成された PostgreSQL サーバーコード内のソースファイルの場所 |
application_name | text | クライアントアプリケーションの名前 (設定されている場合) |