1.postgres 相关进程
BgWriter:数据文件写入进程
PgStat:统计信息进程
SysLogger:系统日志进程
checkpoint :检查点
AutoVacumm :自动清理碎片
Archiver :归档进程
WalWriter :重做日志文件写入进程。
共享内存区:类似SGA;
shared_buffers
wal_buffers
clog_buffers
other buffers
进程私用内存区:类似PGA
temp_buffers
work_mem
maintenance_work_mem
数据文件
WAL文件
参数文件:postgresql.auto.conf,postgresql.conf,pg_hba.conf
2.客户连接发出SQL后的请求顺序
1).客户端提出连接请求
2).postgres产生后台进程服务客户端请求
3).postgres是PG主要守护进程:
守护:
background writer
wal writer
checkpoint
auto vacuum
stat collector
archiver
4).后台进程通过shared memory进行交流
shared memory:包括:
shared buffers
commit log
wal buffer
5).后台进程有各自的local memory 做运算
Local Buffer包括:
temp buffer
maintenance_work_mem
work_mem
6).磁盘数据的读取也是经过shared memory;
Disk:
datafiles
wal logs
arhives
config files
commit log files
pg_control file
3.PostgreSQL架构模型
PostgreSQL使用一种客户端/服务器、进程架构模型。
整体上包括以下几个部分:
内存区域
配置文件
服务进程
磁盘存储
4.PostgreSQL服务器
PostgreSQL服务器本身是由多个进程组合而成的
这些进程全部是被(postmaster的软链接)postgres这个守护进程维护着
PG运用了传统的客户端和服务器架构来让用户访问数据库内容。
客户端包含了PG自带的'psql'客户端
或是用户可以利用PG提供的'libpq'代码库来编写自己的客户端
PG和磁盘之间隔了一层 shared memory(共享内存),大多数的数据都会被放在shared memory里面
,因为访问速度快。PG后台的 checkpointer 进程就是负责定期的把数据从 shared memory 存到磁盘上。
5.共享内存区
shared_buffers
wal_buffers
clog_buffers
other buffers
(1)shared_buffers ---共享缓冲区
它表示数据缓冲区中的数据块的个数,每个数据块的大小是8KB。
数据缓冲区位于数据库的共享内存中,它越大越好,不能小于128KB。
这个参数只有在启动数据库时,才能被设置。
默认值是128MB。
推荐值:1/4 主机物理内存
(2)wal_buffers --- 日志缓存区的大小
可以降低IO,如果遇上比较多的并发短事务,应该和commit_delay一起用
存放WAL数据的内存空间大小,系统默认值是64K
6.进程私有内存区
temp_buffers
work_mem
maintenance_work_mem
(1)temp_buffers---临时缓冲区
用于存放数据库会话访问临时表数据,系统默认值为8M。
可以在单独的 session 中对该参数进行设置,尤其是需要访问比较大的临时表时,将会有显著的性能提升
(2)work_mem --- 工作内存或者操作内存。
其负责内部的sort和hash操作,合适的work_mem大小能够保证这些操作在内存中进行。
(3)maintenance_work_mem ---维护工作内存
主要是针对数据库的维护操作或者语句。
主要针对 VACUUM,CREATE INDEX,ALTER TABLE ADD FOREIGN KEY等操作。
在对整个数据库进行VACUUM或者较大的index进行重建时,适当的调整该参数非常必要
postresql文档提示在启用了 autoacuum 功能的情况下,该参数不能配置的过大
7.主要配置文件
主要配置文件:postgresql.auto.conf,postgresql.conf,pg_hba.conf
pg_hba.conf :存储着和连接相关的权限配置,文件存放在Data目录下。
文件中每一行为一条配置,每条配置中包括连接类型、数据库、用户、客户端地址和认证方式几个字段
postgresql.conf:Pgsql的配置参数是在这个文件中集中管理的,文件存放在Data目录下。
每个参数配置项的格式都是"参数名 = 参数值"
postgresql.auto.conf 和postgresql.conf 的格式相同,但不允许手动编辑。
这个文件保存了通过 ALTER SYSTEM 命令设置的参数。
在读取postgresql.conf 时自动读取postgresql.auto.conf文件,并且它的设置以相同的方式生效。
设置会覆盖postgresql.conf 中的设置。
8.进程通信-建立会话
建立会话的过程
阶段一:客户端发起请求
阶段二:该阶段由主服务进程(postmaster的软链接)postgres负责
服务器是否接受客户端的host通信认证
服务器对客户端进行身份鉴别
阶段三:客户端与postgres进程建立通信连接,由postgres进程负责后续所有的客户端请求操作,
同时postgres作为后台守护进程。
9.物理结构
Datafiles;
Table
Toast(大字段)
Index
Sequence
Controlfile
Archived
WALs
10.物理结构-数据存储方式
目录结构
默认表空间的数据文件存放在base目录
在pg_xlog保存WAL日志,只能通过软链改变它的位置(10版本之后命名为pg_wal)
段(Segments)
每一个表和索引都存放到单独的数据文件中
文件名是表或索引的文件结点(filenode)编号
如果表或索引超过 1 GB 就会被分割为多个段
第一个段以文件结点(filenode)编号命名,第二个以及之后的段以 filenode.1, filenode.2 形式命名
Page(Block)和 Buffer
在磁盘中称为page,内存中称为buffer
默认为8k,可以在编译时指定block_size参数改变大小
Relation:表示表或索引
Tuple=row 表中的行
11.目录结构的查看方法
postgres=# show data_directory;
data_directory
--------------------
/postgresql/pgdata
select name,setting from pg_settings where category='File Locations';
postgres=# select name,setting from pg_settings where category='File Locations';
name | setting
-------------------+------------------------------------
config_file | /postgresql/pgdata/postgresql.conf
data_directory | /postgresql/pgdata
external_pid_file |
hba_file | /postgresql/pgdata/pg_hba.conf
ident_file | /postgresql/pgdata/pg_ident.conf
12.物理结构-目录含义
对象 类型 说 明
base 目录 存储数据库及数据文件
global 目录 存储cluster(实例)级的系统表
###pg_clog 目录 存储事务提交状态(pg12.2没有这个目录)
pg_commit_ts 目录 存储事务提交时间:pg12.2 里面是空的。
pg_dynshmem 目录 存储动态共享内存子系统的相关文件:pg12.2 里面是空的。
pg_log 目录 缺省的数据库日志存储目录:启动日志。
pg_logical 目录 存储逻辑解码的状态
pg_multixact 目录 存储并发事务的状态(用于共享行锁)
pg_notify 目录 存储 listen/notify 的状态
pg_replslot 目录 存储复制槽/位数据:单机时为空间。
pg_serial 目录 存储关于序列化事务的信息:默认空。
pg_snapshots 目录 存储导出快照:默认空。
pg_stat 目录 存储用于统计子系统的持久性文件:默认为空
pg_stat_tmp 目录 存储用于统计子系统的临时文件
pg_subtrans 目录 存储子事务的状态
pg_tblspc 目录 存储指向实际表空间的软连接
pg_twophase 目录 存储关于预提交事务的状态:默认为空。
pg_xlog 目录 存储联机事务日志
#######文件部分。
PG_VERSION 文件 该cluster所属的数据库版本文件
postgresql.conf 文件 数据库参数配置文件
postgresql.auto.conf 文件 数据库参数配置文件
pg_hba.conf 文件 访问配置文件,侧重访问方法
pg_ident.conf 文件 访问配置文件,侧重身份鉴别、用户映射
postmaster.opts 文件 该cluster最后一次启动时使用的命令行选项
postmaster.pid 文件 该cluster启动后,生成的锁文件
##recovery.conf 文件 恢复或复制中需要的配置文件:pg12没有这个。
13.文件目录树
[pgsql@postgresql:/postgresql]$tree -L 3
.
|-- 1.txt
|-- archive
|-- backup
|-- pg12
| |-- bin
| | |-- clusterdb
| | |-- createdb
| | |-- createuser
| | |-- dropdb
| | |-- dropuser
| | |-- ecpg
| | |-- initdb
| | |-- pg_archivecleanup
| | |-- pg_basebackup
| | |-- pgbench
| | |-- pg_checksums
| | |-- pg_config
| | |-- pg_controldata
| | |-- pg_ctl
| | |-- pg_dump
| | |-- pg_dumpall
| | |-- pg_isready
| | |-- pg_receivewal
| | |-- pg_recvlogical
| | |-- pg_resetwal
| | |-- pg_restore
| | |-- pg_rewind
| | |-- pg_test_fsync
| | |-- pg_test_timing
| | |-- pg_upgrade
| | |-- pg_waldump
| | |-- postgres
| | |-- postmaster -> postgres
| | |-- psql
| | |-- reindexdb
| | `-- vacuumdb
| |-- include
| |-- lib
| `-- share
| |-- errcodes.txt
| |-- extension
| |-- information_schema.sql
| |-- pg_hba.conf.sample
| |-- pg_ident.conf.sample
| |-- pg_service.conf.sample
| |-- postgres.bki
| |-- postgres.description
| |-- postgresql.conf.sample
| |-- postgres.shdescription
| |-- psqlrc.sample
| |-- snowball_create.sql
| |-- sql_features.txt
| |-- system_views.sql
| |-- timezone
| |-- timezonesets
| `-- tsearch_data
|-- pgdata
| |-- base
| | |-- 1
| | |-- 13592
| | |-- 13593
| | `-- 16385
| |-- current_logfiles
| |-- global
| | |-- 1136
| | |-- 1136_fsm
| | |-- 1136_vm
| | |-- 1137
| | |-- 1213
| | |-- 1213_fsm
| | |-- 1213_vm
| | |-- 1214
| | |-- 1214_fsm
| | |-- 1214_vm
| | |-- 1232
| | |-- 1233
| | |-- 1260
| | |-- 1260_fsm
| | |-- 1260_vm
| | |-- 1261
| | |-- 1261_fsm
| | |-- 1261_vm
| | |-- 1262
| | |-- 1262_fsm
| | |-- 1262_vm
| | |-- 2396
| | |-- 2396_fsm
| | |-- 2396_vm
| | |-- 2397
| | |-- 2671
| | |-- 2672
| | |-- 2676
| | |-- 2677
| | |-- 2694
| | |-- 2695
| | |-- 2697
| | |-- 2698
| | |-- 2846
| | |-- 2847
| | |-- 2964
| | |-- 2965
| | |-- 2966
| | |-- 2967
| | |-- 3592
| | |-- 3593
| | |-- 4060
| | |-- 4061
| | |-- 4175
| | |-- 4176
| | |-- 4177
| | |-- 4178
| | |-- 4179
| | |-- 4180
| | |-- 4181
| | |-- 4182
| | |-- 4183
| | |-- 4184
| | |-- 4185
| | |-- 4186
| | |-- 6000
| | |-- 6001
| | |-- 6002
| | |-- 6100
| | |-- 6114
| | |-- 6115
| | |-- pg_control
| | |-- pg_filenode.map
| | `-- pg_internal.init
| |-- log
| | |-- postgresql-2023-03-23_080736.csv
| | |-- postgresql-2023-03-23_080736.log
| | |-- postgresql-2023-03-23_081149.csv
| | `-- postgresql-2023-03-23_081149.log
| |-- pg_commit_ts
| |-- pg_dynshmem
| |-- pg_hba.conf
| |-- pg_ident.conf
| |-- pg_log
| | |-- postgresql-2022-12-22_053850.log
| | |-- postgresql-2022-12-22_054258.log
| | |-- postgresql-2022-12-22_054418.log
| | |-- postgresql-2022-12-22_054714.log
| | |-- postgresql-2022-12-22_054739.log
| | |-- postgresql-2022-12-22_055002.log
| | |-- postgresql-2022-12-22_055452.log
| | |-- postgresql-2022-12-22_055941.log
| | |-- postgresql-2022-12-22_060338.log
| | |-- postgresql-2022-12-22_060546.log
| | |-- postgresql-2022-12-22_060654.log
| | |-- postgresql-2023-02-17_150135.log
| | |-- postgresql-2023-03-03_053926.log
| | |-- postgresql-2023-03-03_054248.log
| | |-- postgresql-2023-03-03_091820.log
| | |-- postgresql-2023-03-04_011734.log
| | |-- postgresql-2023-03-23_072237.log
| | `-- postgresql-2023-03-23_073641.log
| |-- pg_logical
| | |-- mappings
| | |-- replorigin_checkpoint
| | `-- snapshots
| |-- pg_multixact
| | |-- members
| | `-- offsets
| |-- pg_notify
| | `-- 0000
| |-- pg_replslot
| |-- pg_serial
| |-- pg_snapshots
| |-- pg_stat
| |-- pg_stat_tmp
| | |-- db_0.stat
| | |-- db_13593.stat
| | `-- global.stat
| |-- pg_subtrans
| | `-- 0000
| |-- pg_tblspc
| |-- pg_twophase
| |-- PG_VERSION
| |-- pg_wal
| | |-- 000000010000000000000001
| | `-- archive_status
| |-- pg_xact
| | `-- 0000
| |-- postgresql.auto.conf
| |-- postgresql.conf
| |-- postgresql.conf.20221221
| |-- postmaster.opts
| `-- postmaster.pid
14.后台进程
pgsql 2354 1 0 16:11 ? 00:00:00 /postgresql/pg12/bin/postgres
pgsql 2355 2354 0 16:11 ? 00:00:00 postgres: logger
pgsql 2357 2354 0 16:11 ? 00:00:00 postgres: checkpointer
pgsql 2358 2354 0 16:11 ? 00:00:00 postgres: background writer
pgsql 2359 2354 0 16:11 ? 00:00:00 postgres: walwriter
pgsql 2360 2354 0 16:11 ? 00:00:00 postgres: autovacuum launcher
pgsql 2361 2354 0 16:11 ? 00:00:00 postgres: archiver
pgsql 2362 2354 0 16:11 ? 00:00:00 postgres: stats collector
pgsql 2363 2354 0 16:11 ? 00:00:00 postgres: logical replication launcher
pgsql 2364 2354 0 16:12 ? 00:00:00 postgres: postgres postgres [local] idle
postgres是postmaster的软连接。
15.查看进程树的方法。
[pgsql@postgresql:/home/pgsql]$pstree -p 2354
postgres(2354)─┬─postgres(2355)
├─postgres(2357)
├─postgres(2358)
├─postgres(2359)
├─postgres(2360)
├─postgres(2361)
├─postgres(2362)
├─postgres(2363)
└─postgres(2364)
16.总结
无