LoginSignup
3
0

【PostgreSQL】ファイルについて

Last updated at Posted at 2023-12-04

初めに

PostgresQLではデータベースクラスタと呼ばれるディレクトリは以下に使用するファイルが作成される。
今回はそれらのディレクトリやファイルについて理解を深めていきます。

データベースクラスタの構成

データベースクラスタ内は以下のような構成でできている

-bash-4.2$ pwd
/var/lib/pgsql/11/data
-bash-4.2$ ll
合計 64
-rw-------. 1 postgres postgres     3  1月 18  2023 PG_VERSION
drwx------. 6 postgres postgres    58  6月 13 17:26 base
-rw-------. 1 postgres postgres    30 12月  4 14:07 current_logfiles
drwx------. 2 postgres postgres  4096 12月  4 14:07 global
drwx------. 2 postgres postgres   188  1月 24  2023 log
drwx------. 2 postgres postgres     6  1月 18  2023 pg_commit_ts
drwx------. 2 postgres postgres     6  1月 18  2023 pg_dynshmem
-rw-------. 1 postgres postgres  4267  1月 23  2023 pg_hba.conf
-rw-------. 1 postgres postgres  1636  1月 18  2023 pg_ident.conf
drwx------. 4 postgres postgres    68 12月  4 14:07 pg_logical
drwx------. 4 postgres postgres    36  1月 18  2023 pg_multixact
drwx------. 2 postgres postgres    18 12月  4 14:07 pg_notify
drwx------. 2 postgres postgres     6  1月 18  2023 pg_replslot
drwx------. 2 postgres postgres     6  1月 18  2023 pg_serial
drwx------. 2 postgres postgres     6  1月 18  2023 pg_snapshots
drwx------. 2 postgres postgres     6 11月 16 14:44 pg_stat
drwx------. 2 postgres postgres    25 12月  4 14:08 pg_stat_tmp
drwx------. 2 postgres postgres    18  1月 18  2023 pg_subtrans
drwx------. 2 postgres postgres     6  1月 11  1957 pg_tblspc
drwx------. 2 postgres postgres     6  1月 18  2023 pg_twophase
drwx------. 3 postgres postgres  4096 10月 18 16:24 pg_wal
drwx------. 2 postgres postgres    18  1月 18  2023 pg_xact
-rw-------. 1 postgres postgres    88  1月 18  2023 postgresql.auto.conf
-rw-------. 1 postgres postgres 24007 11月 16 14:44 postgresql.conf
-rw-------. 1 postgres postgres    58 12月  4 14:07 postmaster.opts
-rw-------. 1 postgres postgres   103 12月  4 14:07 postmaster.pid

PGDATAでデータディレクトに移動できる

# /var/lib/pgsql/data
cd $PGDATA

ディレクトリとファイルについて

ディレクトリとファイルにはそれぞれ何のために使用されるかを纏めていきます。

ディレクトリについて

  • baseディレクトリ
    PostgreSQLが管理する各データベースの物理的なデータファイルが格納される
    各データベースは、baseディレクトリ内の独自のサブディレクトリにデータを保持している。サブディレクトリは、データベースのOID(オブジェクト識別子)によって命名される。以下のようなファイルを格納する。

    • テーブルファイル
    • インデックスファイル
    • TOASTデータ
    • システムカタログ
  • globalディレクトリ
    データベースクラスタ全体に共通のシステム情報を格納する。以下のファイルが格納される。

    • システムカタログ
    • トランザクションログ
    • 設定ファイル
  • pg_walディレクトリ
    WALファイルを格納するディレクトリ

  • pg_xactディレクトリ
    トランザクションのコミットログを格納する

  • pg_tblspcディレクトリ
    テーブルスペースへのシンボリックシンクを格納する
    テーブルスペースとは、データベースオブジェクトを保存するための物理的な場所を定義する概念。

ファイルについて

  • PG_VERSIONファイル
    PostgreSQLのメジャーバージョン番号が書き込まれているテキストファイル。
cat PG_VERSION 
11

  • テーブルファイル
    テーブルデータの実態が格納されているファイル
    8192バイトのページで管理されている
ll base/13881/
合計 15184
-rw-------. 1 postgres postgres    8192  1月 18  2023 112
-rw-------. 1 postgres postgres    8192  1月 18  2023 113
-rw-------. 1 postgres postgres   98304 10月 18 14:53 1247
-rw-------. 1 postgres postgres   24576 10月 18 14:18 1247_fsm
-rw-------. 1 postgres postgres    8192  1月 24  2023 1247_vm
  • インデックスファイル
    インデックスの情報が格納されているファイル
    8192バイトのページで管理されている

  • TOASTファイル
    長大な行を格納する場合に生成される特殊なファイル。

  • Free Space Mapファイル
    空き領域を追跡するための情報が格納されたファイル。

ll base/13881/
合計 15184
-rw-------. 1 postgres postgres    8192  1月 18  2023 112
-rw-------. 1 postgres postgres    8192  1月 18  2023 113
-rw-------. 1 postgres postgres   98304 10月 18 14:53 1247
-rw-------. 1 postgres postgres   24576 10月 18 14:18 1247_fsm # Free Space Mapファイル
-rw-------. 1 postgres postgres    8192  1月 24  2023 1247_vm
  • Visibility Mapファイル
    テーブルの可視性を管理するファイル
ll base/13881/
合計 15184
-rw-------. 1 postgres postgres    8192  1月 18  2023 112
-rw-------. 1 postgres postgres    8192  1月 18  2023 113
-rw-------. 1 postgres postgres   98304 10月 18 14:53 1247
-rw-------. 1 postgres postgres   24576 10月 18 14:18 1247_fsm
-rw-------. 1 postgres postgres    8192  1月 24  2023 1247_vm # Visibility Mapファイル
  • WALファイル
    PostgresQLに対して行われた更新操作を記録するファイル。pg_walディレクトリは以下に格納される。
  • postgresql.confとpg_hba.conf
    PostgreSQLの動作を設定するファイル。
  • postmaster.pid
    PostgreSQLの稼働中に作成されるロックファイルで、データベースクラスタは以下に格納される。
cat postmaster.pid
1210
/var/lib/pgsql/11/data
1701666445
5432
/var/run/postgresql
localhost
  5432001         0
ready   

まとめ

ファイル・ディレクトリ構成を理解することで、データベースの管理・メンテナンスの効率化に繋げることができるので是非とも理解を深めておきましょう。

参考

内部構造から学ぶPostgreSQL―設計・運用計画の鉄則

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0