程序员的知识教程库

网站首页 > 教程分享 正文

PostgreSql系列二:PG的架构体系及权限体系

henian88 2024-10-22 11:49:48 教程分享 9 ℃ 0 评论

上一篇我们学习了PG的安装配置启动.这一篇学习理解PG的物理体系及逻辑体系还有权限体系.

1 PG的物理体系介绍

进入数据目录
[root@slowquery data]# pwd
/pgsql/data

查看PG数据目录下边的文件
[root@slowquery data]# ll
总用量 64
drwx------. 6 postgres postgres    54 7月   1 19:52 base
-rw-------. 1 postgres postgres    30 7月   1 00:00 current_logfiles
drwx------. 2 postgres postgres  4096 6月  30 20:21 global
drwx------. 2 postgres postgres    58 7月   1 00:00 log
drwx------. 2 postgres postgres     6 6月  30 20:08 pg_commit_ts
drwx------. 2 postgres postgres     6 6月  30 20:08 pg_dynshmem
-rw-------. 1 postgres postgres  4792 6月  30 20:20 pg_hba.conf
-rw-------. 1 postgres postgres  1636 6月  30 20:08 pg_ident.conf
drwx------. 4 postgres postgres    68 7月   1 19:57 pg_logical
drwx------. 4 postgres postgres    36 6月  30 20:08 pg_multixact
drwx------. 2 postgres postgres    18 6月  30 20:20 pg_notify
drwx------. 2 postgres postgres     6 6月  30 20:08 pg_replslot
drwx------. 2 postgres postgres     6 6月  30 20:08 pg_serial
drwx------. 2 postgres postgres     6 6月  30 20:08 pg_snapshots
drwx------. 2 postgres postgres     6 6月  30 20:20 pg_stat
drwx------. 2 postgres postgres    63 7月   1 19:59 pg_stat_tmp
drwx------. 2 postgres postgres    18 6月  30 20:08 pg_subtrans
drwx------. 2 postgres postgres     6 6月  30 20:08 pg_tblspc
drwx------. 2 postgres postgres     6 6月  30 20:08 pg_twophase
-rw-------. 1 postgres postgres     3 6月  30 20:08 PG_VERSION
drwx------. 3 postgres postgres    60 6月  30 20:08 pg_wal
drwx------. 2 postgres postgres    18 6月  30 20:08 pg_xact
-rw-------. 1 postgres postgres    88 6月  30 20:08 postgresql.auto.conf
-rw-------. 1 postgres postgres 26664 6月  30 20:18 postgresql.conf
-rw-------. 1 postgres postgres    47 6月  30 20:20 postmaster.opts
-rw-------. 1 postgres postgres    85 6月  30 20:20 postmaster.pid

主要文件理解:
drwx------. 6 postgres postgres    54 7月   1 19:52 base
base目录下边是什么呢?就是存放表数据的
我们现查看当前目录下边的内容
[root@slowquery data]# ll base/
总用量 48
drwx------. 2 postgres postgres 8192 6月  30 20:08 1
drwx------. 2 postgres postgres 8192 6月  30 20:08 14186
drwx------. 2 postgres postgres 8192 6月  30 20:21 14187
drwx------. 2 postgres postgres 8192 7月   1 19:52 16387
我们在数据库内创建一张表,然后查看表在数据库目录中的位置
postgres=# CREATE TABLE test (
postgres(#      did    integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
postgres(#      name   varchar(40) NOT NULL CHECK (name <> '')
postgres(# );
CREATE TABLE
postgres=# select pg_relation_filepath('test') ;
 pg_relation_filepath 
----------------------
 base/14187/16390

同时由此我们可以看到PG的物理结构是:表空间--database--table 这么一个结构 对应到目录就是
base  14187(database)  16390(table)
其他文件解释:
-rw-------. 1 postgres postgres    30 7月   1 00:00 current_logfiles  当前使用的日志
drwx------. 2 postgres postgres  4096 6月  30 20:21 global         系统元数据信息
drwx------. 2 postgres postgres    58 7月   1 00:00 log       日志目录
drwx------. 2 postgres postgres     6 6月  30 20:08 pg_commit_ts  包含事务提交时间戳数据的子目录
-rw-------. 1 postgres postgres  4792 6月  30 20:20 pg_hba.conf    实例防火墙配置文件
drwx------. 3 postgres postgres    60 6月  30 20:08 pg_wal    redolog目录
-rw-------. 1 postgres postgres    88 6月  30 20:08 postgresql.auto.conf   动态参数修改保存的文件(优先级高于配置文件)
-rw-------. 1 postgres postgres 26664 6月  30 20:18 postgresql.conf       pg配置文件


2 PG的逻辑结构

PG的逻辑架构理解:
PG和其他关系型数据库不一样.比如MySQL MySQL就是实例---库--表
PG是实例---库---模式--表
PG的一个实例中可以有多个database 客户端连接的时候需要指定database
MySQL则不需要
PG的模式是介于database和table之间的一个逻辑分割
在PG中一个database可以有多个schema 默认情况下每个database下边都有一个public模式
postgres=# create database test;
CREATE DATABASE
postgres=# \c test
您现在已经连接到数据库 "test",用户 "postgres".
test=# \dn
   架构模式列表
  名称  |  拥有者  
--------+----------
 public | postgres
(1 行记录)

用户可以创建模式
test=# create schema test;
CREATE SCHEMA
查看test下的所有模式
test=# \dn
   架构模式列表
  名称  |  拥有者  
--------+----------
 lzm    | postgres
 public | postgres
 test   | postgres
(3 行记录)
在一个database下的不同模式中可以创建相同名字的表
test=# create table test.tt(id int8);
CREATE TABLE
test=# create table lzm.tt(id int8);
CREATE TABLE

查看对应的table时候需要加上database和模式名
test=# \dt test.lzm.tt;
              关联列表
 架构模式 | 名称 |  类型  |  拥有者  
----------+------+--------+----------
 lzm      | tt   | 数据表 | postgres

删除模式
test=# drop schema test cascade ;
注意:  递归删除 表 test.tt
DROP SCHEMA
PG的逻辑是一个多进程的.主要由一个postmaster监听其他进程工作
postmaster  所有数据库的进程的主进程(负责监听和fork子进程)
startup  主要用于数据库恢复的进程
syslogger  记录系统日志
pgstat  收集统计信息
pgarch  如果开启了归档那么postmaster就会fork一个归档进程
checkpointer  负责检查点的进程
bgwriter 负责把shared buffer中的脏数据写入磁盘的进程
autovacuum worker  负责回收垃圾数据的worker进程,是lanucher进程fork出来的
autovacuum lanucher  负责回收垃圾数据的进程,如果开启了autovacuum的话,那么postmaster会fork这个进程
bgworker  分为很多种worker,例如logical replication worker launcher.parallerl worker.replication worker等
wal sender 逻辑复制 流式物理复制的wal发送进程
wal receiver 逻辑复制 流式物理复制的wal 接收进程
work process 工作进程,东台fork 例如并行计算的进程

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表