[Reprint] ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

Ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

https://www.toutiao.com/i6715390855772897800/

 

Gregg said that the original operation and maintenance 2019-07-26 00:03:00

Outline

Today article from the log files, parameter files, control files, data files, redo log (WAL), a background process that six to introduce PostgreSQL architecture.


Architecture

PostgreSQL's main structure is as follows:

Ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

 

 


A log file

1, the type of log file

1) $ PGDATA / log running log (before pg10 is $ PGDATA / pg_log)

2) $ PGDATA / pg_wal redo log before (pg10 is $ PGDATA / pg_xlog)

3) $ PGDATA / pg_xact transaction commit log (before pg10 is $ PGDATA / pg_clog)

4) server logs, you can specify at boot time, such as pg_ctl start -l ./alert.log

2. Run Log

The main parameters related to the operation of the log follows, not enabled by default if no log directory, will be automatically generated after opening.

Ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

 

Ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

 

 

3, the transaction log pg_xact

pg_xact a transaction commit log record metadata transaction. Enabled by default. Content can not be directly read.

4, server logs

If you do not specify the start time pg_ctl -l parameter to specify the server log, error may be output to the cmd reception. Server logs record important information to the database.

lsof may be able to filter out the write log files

$ Lsof -c Postgres | grip REG | grep -v / usr | grep -v / dev | grep -v / SYS

 


Second, the parameter file

1、 postgresql.conf

pg的主要参数文件,有很详细的说明和注释,和Oracle的pfile,MySQL的my.cnf类似。默认在$PGDATA下。很多参数修改后都需要重启。9.6之后支持了alter system来修改,修改后的会存在$PGDATA/postgresql.auto.conf下,可以reload或者 restart来使之生效。

主要的参数如下:

Ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

 

Ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

 

 

2、pg_hba.conf

这个是黑白名单的设置。文件里有详细的参数说明,默认参数如下:

type 列有local,host,hostssl,hostnossl四种。local是本地认证

database 可以是all,或者指定的数据库

user列可以是all,或者具体的用户

address 可以是ip或者网段

method比较重要,有"trust", "reject", "md5", "password", "scram-sha-256", "gss", "sspi", "ident", "peer", "pam", "ldap", "radius" or "cert"这么多可选。trust是免密登录;reject是黑名单拒绝;md5是加密的密码;password是没有加密的密码;ident是Linux下PostgreSQL默认的local认证方式,凡是能正确登录服务器的操作系统用户(注:不是数据库用户)就能使用本用户映射的数据库用户不需密码登录数据库

3、 pg_ident.conf

pg_ident.con是用户映射配置文件。结合pg_hba.connf中,method为ident可以用特定的操作系统用户和指定的数据库用户登录数据库。如下:

pg_ident.conf如下:

Ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

 

pg_hba.conf如下:

Ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

 


三、控制文件

1、 控制文件位置

$PGDATA/global/pg_control

控制文件在数据库目录的global目录下。控制文件记录了数据库的重要信息。

2、 查看控制文件

pg_controldata可以查看控制文件的内容

$ pg_controldata $PGDATA
pg_control version number: 1100
Catalog version number: 201809051
Database system identifier: 6684270596680436587 #dbid
Database cluster state: in production # primary
pg_control last modified: Thu 16 May 2019 02:26:37 PM CST
Latest checkpoint location: 0/48812A0
Latest checkpoint's REDO location: 0/4881268 #redo 位置
Latest checkpoint's REDO WAL file: 000000010000000000000001 #wal文件号
Latest checkpoint's TimeLineID: 1
Latest checkpoint's PrevTimeLineID: 1
Latest checkpoint's full_page_writes: on
Latest checkpoint's NextXID: 0:572 #下一个事务id
Latest checkpoint's NextOID: 16388 #下一个OID
.....

controlfile记录了数据库运行的一些信息,比如数据库id,是否open,wal的位置,checkpoint的位置,等等。controlfile是很重要的文件,数据库部署和调整。


四、数据文件

1、page

pg中,每个索引和表都是一个单独的文件,pg中叫做page。默认是每个大于1G的page会被分割pg_class.relfilenode.1这样的文件。

Page默认大小为8KB,最大32KB,一个数据块中可存放多行的数据。块中的结构如下图:

Ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

 

块头记录了如下信息:

  • 块的checksum值
  • 空闲空间的起始位置和结束位置
  • 特殊数据的起始位置
  • 其他一些信息
  • 行指针是一个32bit的数字,具体结构如下:
  • 行内容的偏移量,占15bit;
  • 指针的标记,占2bit;
  • 行内容的长度,占15bit。

行指针中表示行内容的偏移量是15bit,能表示的最大偏移量是2^15=32768,因此块的最大大小是32768,即32KB。

2、page物理位置

page的物理位置在$PGDATA/BASE/DATABASE_OID/PG_CLASS.RELFILENODE

需要注意的是,pg_class.relfilenode类似dba_objects.data_object_id,truncate表之后relfilenode会变。对应的物理文件名字也会变。


五、WAL日志

1、wal位置

wal在$PGDATA/pg_wal下。10之前为pg_xlog

2、wal命名格式

文件名称为16进制的24个字符组成,每8个字符一组,每组的意义如下

Ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

 

3、手动切换WAL日志的命令

在PG10之前:

highgo=# select pg_switch_xlog();
pg_switch_xlog
----------------
0/B000C48
(1 row)

在PG10之后:

highgo=# select pg_switch_wal();
pg_switch_wal
----------------
0/B000C48
(1 row)

Sixth, a background process

pg background processes as follows:

Ultra-detailed summary of the PostgreSQL system architecture, worthy of collection

 

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11620212.html