개요

개요도

Shared Memory

Shared Buffer

WAL Buffer

개요

DB의 데이터 변경
$\to$ WAL 버퍼와 Data 버퍼 쓰기
$\to$ COMMIT 발생
$\to$ WAL 버퍼의 데이터를 디스크로 flush
$\to$ CHECKPOINT 발생
$\to$ 모든 데이터 버퍼를 디스크로 flush

CHECKPOINT

checkpoint_timeout 조회
postgres=# SHOW checkpoint_timeout;
 checkpoint_timeout
--------------------
 5min
(1 )
checkpoint_completion_target

max_wal_size 조회

postgres=# SHOW max_wal_size;
 max_wal_size
--------------
 1GB
(1 )

Dynamic WAL Switching

단일 WAL 파일이 가득 차고(기본 16MB) 남은 디스크 공간이 부족해질 때까지 WAL log 쓰기
$\to$ 다음 쓰기를 위해 오래된 WAL 파일 사용

프로세스

[root@vultr ~]# systemctl status postgresql-13
 postgresql-13.service - PostgreSQL 13 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-07-22 23:26:23 KST; 2s ago
     Docs: https://www.postgresql.org/docs/13/static/
  Process: 3794307 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 3794313 (postmaster)
    Tasks: 8 (limit: 49532)
   Memory: 30.6M
   CGroup: /system.slice/postgresql-13.service
           ├─3794313 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
           ├─3794314 postgres: logger
           ├─3794316 postgres: checkpointer
           ├─3794317 postgres: background writer
           ├─3794318 postgres: walwriter
           ├─3794319 postgres: autovacuum launcher
           ├─3794320 postgres: stats collector
           └─3794321 postgres: logical replication launcher

[root@vultr ~]# pstree -p 3794313
postmaster(3794313)─┬─postmaster(3794314)
                    ├─postmaster(3794316)
                    ├─postmaster(3794317)
                    ├─postmaster(3794318)
                    ├─postmaster(3794319)
                    ├─postmaster(3794320)
                    └─postmaster(3794321)

Postmaster

[root@vultr ~]# ll /usr/pgsql-13/bin/postmaster
lrwxrwxrwx. 1 root root 8  5 18 22:23 /usr/pgsql-13/bin/postmaster -> postgres

postgres

postgres가 하는 일?

Background

logger

checkpointer

background writer

wal writer

autovacuum launcer

archiver

stats collector

Backend

work_mem 파라미터

maintenance_work_mem 파라미터

temp_buffers 파라미터

Client

참고 링크

데이터베이스 구조

데이터베이스 관련 사항

template0, template1?

테이블스페이스 관련 사항

pg_default

pg_global

사용자 테이블 스페이스

테이블 관련 사항

테이블 데이터 저장 파일

테이블 여유 공간 관리 파일

테이블 블록 visibility 관리 파일

template0, template1 postgres 데이터베이스

SELECT
  oid,
  datname,
  datistemplate,
  datallowconn
FROM pg_database
ORDER BY 1;

  oid  |  datname  | datistemplate | datallowconn
-------+-----------+---------------+--------------
     1 | template1 | t             | t
 13437 | template0 | t             | f
 13438 | postgres  | f             | t
(3 )
SELECT
  a.oid,
  a.datname,
  a.datistemplate,
  a.datallowconn,
  a.datconnlimit,
  a.dattablespace,
  b.spcname
FROM pg_database a
JOIN pg_tablespace b
  ON b.oid = a.dattablespace;

  oid  |  datname  | datistemplate | datallowconn | datconnlimit | dattablespace |  spcname
-------+-----------+---------------+--------------+--------------+---------------+------------
 13438 | postgres  | f             | t            |           -1 |          1663 | pg_default
     1 | template1 | t             | t            |           -1 |          1663 | pg_default
 13437 | template0 | t             | f            |           -1 |          1663 | pg_default
# systemctl show -p Environment postgresql-13.service
Environment=PGDATA=/var/lib/pgsql/13/data/ PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj PG_OOM_ADJUST_VALUE=0

[root@vultr ~]# ll /var/lib/pgsql/13/data/base/
합계 12
drwx------. 2 postgres postgres 4096  7월 21 21:31 1
drwx------. 2 postgres postgres 4096  7월 21 21:31 13437
drwx------. 2 postgres postgres 4096  7월 24 01:07 13438
 tmplate0       tmplate1          사용자db
[template0]    [template0]      [template1]
               [템플릿추가]   [사용자 오브젝트]

사용자 DB 생성

template 데이터베이스에 접속하여 사용자 테이블 t1을 생성

postgres=# \c template1
접속정보: 데이터베이스="template1", 사용자="postgres".
template1=# CREATE TABLE t1 (c1 INTEGER);
CREATE TABLE
template1=# \d+ t1
                               "public.t1" 테이블
 필드명 |  종류   | Collation | NULL허용 | 초기값 | 스토리지 | 통계수집량 | 설명
--------+---------+-----------+----------+--------+----------+------------+------
 c1     | integer |           |          |        | plain    |            |
접근 방법: heap
postgres=# CREATE DATABASE db_aimpugn;
CREATE DATABASE
postgres=# \c db_aimpugn
접속정보: 데이터베이스="db_aimpugn", 사용자="postgres".
db_aimpugn=# \dt+
                     릴레이션(relation) 목록
 스키마 | 이름 |  종류  |  소유주  | Persistence |  크기   | 설명
--------+------+--------+----------+-------------+---------+------
 public | t1   | 테이블 | postgres | permanent   | 0 bytes |
(1 )

pg_global 테이블스페이스

사용자 테이블스페이스 생성

[root@vultr home]# mkdir /home/data
[root@vultr home]# chown -R postgres:postgres /home/data
-- 경로는 절대경로 지정해야 한다
postgres=# CREATE TABLESPACE tspc_aimpugn LOCATION '/home/data';
CREATE TABLESPACE
[root@vultr home]# ll $PGDATA/pg_tblspc
합계 0
lrwxrwxrwx. 1 postgres postgres 10  7월 25 17:38 16396 -> /home/data
사용자 테이블스페이스와 사용자 테이블
db_aimpugn=# \c postgres
접속정보: 데이터베이스="postgres", 사용자="postgres".
postgres=# CREATE TABLE t1 (c1 INTEGER) TABLESPACE tspc_aimpugn;
CREATE TABLE
postgres=# SELECT oid FROM pg_class WHERE relname = 't1';
  oid
-------
 16398
(1 )
-- 이미 t1 테이블 있음
db_aimpugn=# CREATE TABLE t1 (c1 INTEGER) TABLESPACE tspc_aimpugn;
오류:  "t1" 이름의 릴레이션(relation) 이미 있습니다
db_aimpugn=# \d+ t1
                               "public.t1" 테이블
 필드명 |  종류   | Collation | NULL허용 | 초기값 | 스토리지 | 통계수집량 | 설명
--------+---------+-----------+----------+--------+----------+------------+------
 c1     | integer |           |          |        | plain    |            |
접근 방법: heap

db_aimpugn=# DROP TABLE t1;
DROP TABLE
db_aimpugn=# CREATE TABLE t1 (c1 INTEGER) TABLESPACE tspc_aimpugn;
CREATE TABLE
db_aimpugn=# \d+ t1
                               "public.t1" 테이블
 필드명 |  종류   | Collation | NULL허용 | 초기값 | 스토리지 | 통계수집량 | 설명
--------+---------+-----------+----------+--------+----------+------------+------
 c1     | integer |           |          |        | plain    |            |
테이블스페이스: "tspc_aimpugn"
접근 방법: heap

-- OID 확인
db_aimpugn=# SELECT oid FROM pg_class WHERE relname = 't1';
  oid
-------
 16401
(1 )
[root@vultr data]# tree
.
└── PG_13_202007201
    ├── 13438 // postgres
    │   └── 16398 // t1
    └── 16397 // db_aimpugn
        └── 16401 // t1
 oid  |  datname   | datistemplate | datallowconn | datconnlimit | dattablespace |  spcname
-------+------------+---------------+--------------+--------------+---------------+------------
 13438 | postgres   | f             | t            |           -1 |          1663 | pg_default
     1 | template1  | t             | t            |           -1 |          1663 | pg_default
 13437 | template0  | t             | f            |           -1 |          1663 | pg_default
 16397 | db_aimpugn | f             | t            |           -1 |          1663 | pg_default

테이블스페이스 LOCATION 변경 방법

VACUUM

하는 일?

  1. 테이블 및 인덱스 통계 정보 수집(일반 DBMS 작업)
  2. 테이블 재구성(일반 DBMS 작업)
  3. 테이블 및 인덱스 DEAD 블록 정리(pgsql MVCC 특징 때문에 필요한 작업)
  4. XID Wraparound 방지 위한 레코드별 XID Frozen(pgsql MVCC 특징 때문에 필요한 작업)

ORACLE vs PostgreSQL

항목 ORACLE PostgreSQL
MVCC 모델 구현 방식 UNDO 세그먼트 블록 내에 이전 레코드 저장
Shared Pool 존재 여부 존재 존재하지 않음

MVCC 모델의 차이점

Shared Pool 존재 여부