暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL数据库体系架构之进程结构

DBA随笔记 2024-09-11
55

PostgreSQL进程可以分为三类:后台进程后端进程或者服务器进程客户端进程或用户进程。

1. 进程结构介绍

a. 后台进程:

    [postgres@pgserver pgdata]$ id
    uid=2000(postgres) gid=2000(postgres) groups=2000(postgres)


    [postgres@pgserver pgdata]$ ps -ef|grep postgres
    root 2672 2576 0 19:54 pts/0 00:00:00 su - postgres
    postgres 2673 2672 0 19:54 pts/0 00:00:00 -bash
    postgres 2721 1 0 19:54 ? 00:00:00 postgresql/pg16/bin/postgres
    postgres 2722 2721 0 19:54 ? 00:00:00 postgres: logger
    postgres 2723 2721 0 19:54 ? 00:00:00 postgres: checkpointer
    postgres 2724 2721 0 19:54 ? 00:00:00 postgres: background writer
    postgres 2726 2721 0 19:54 ? 00:00:00 postgres: walwriter
    postgres 2727 2721 0 19:54 ? 00:00:00 postgres: autovacuum launcher
    postgres 2728 2721 0 19:54 ? 00:00:00 postgres: archiver
    postgres 2729 2721 0 19:54 ? 00:00:00 postgres: logical replication launcher
    postgres 2906 2673 0 20:08 pts/0 00:00:00 ps -ef
    postgres 2907 2673 0 20:08 pts/0 00:00:00 grep --color=auto postgres


    [postgres@pgserver pgdata]$ pstree -p 2721
    postgres(2721)─┬─postgres(2722)
    ├─postgres(2723)
    ├─postgres(2724)
    ├─postgres(2726)
    ├─postgres(2727)
    ├─postgres(2728)
    └─postgres(2729)


    其中,/postgresql/pg16/bin/postgres是数据库服务器的master进程,其它诸如checkpoint,background writer,walwrite,autovacuum launcher,archiver, logical replication launcher都是由它fork的子进程。当然数据库运行模式不同,配置不同,也可能有其它后台进程。

    postgres:PostgreSQL数据库核心进程,也是整个cluster的父进程,该进程出现问题,整个cluster就over了,该进程由操作系统的守护进程进程派生。

    checkpointer:检查点进程,等价于Oracle的CKPT进程,负责完成数据库的检查点,通知数据库的写进程DBWR将内存中的脏数据写出到磁盘。

    background writer :等价于Oracle的DBWR进程,负责将内存中的脏数据写出到磁盘。

    walwriter :等价于Oracle的LGWR进程,负责将日志缓冲区中的记录关于数据库的修改的日志写出到日志文件中去,确保数据的修改不会丢失,用于恢复使用。

    autovacuum launcher:自动清理工作进程。由于PostgreSQL不像Oracle那样有undo的机制,将数据被修改前的信息写入到undo,然后修改数据。PostgreSQL采取的是在原数据块上进行保留旧的数据,并作标记,等到将来修改提交生效之后,旧的数据(dead tuple 死元组)不需要的话,就得清理,由该进程来完成。

    stats collector:统计信息收集进程。用于及时的更新数据库中的统计信息,如表、index有多少条记录,数据分布等,给优化器提供最新的信息,便于优化器选择最优的执行计划。避免统计信息不准确,导致优化器选择错误的执行计划,导致SQL性能下降或偏差。

    logical replication launcher: 逻辑复制进程。用于完成逻辑复制的工作。

    archiver:归档进程,等价于Oracle的ARCH进程,用于完成数据库日志文件的归档。当数据库配置了归档模式之后,可以看到该进程。


    b. 后端进程(backend)或服务器进程:

    当我们的应用程序和图形界面的客户端工具,连接到PostgreSQL数据库服务器时。

    master进程会为该应用程序创建1个服务器进程,用于处理和响应该客户端应用程序的请求。

      [postgres@pgserver pgdata]$  psql
      psql (16.4)
      Type "help" for help.


      postgres=# select pg_backend_pid();
      pg_backend_pid
      ----------------
      3055
      (1 row)


      postgres=# \! ps -ef|grep postgres
      root 2672 2576 0 19:54 pts/0 00:00:00 su - postgres
      postgres 2673 2672 0 19:54 pts/0 00:00:00 -bash
      postgres 2721 1 0 19:54 ? 00:00:00 postgresql/pg16/bin/postgres
      postgres 2722 2721 0 19:54 ? 00:00:00 postgres: logger
      postgres 2723 2721 0 19:54 ? 00:00:00 postgres: checkpointer
      postgres 2724 2721 0 19:54 ? 00:00:00 postgres: background writer
      postgres 2726 2721 0 19:54 ? 00:00:00 postgres: walwriter
      postgres 2727 2721 0 19:54 ? 00:00:00 postgres: autovacuum launcher
      postgres 2728 2721 0 19:54 ? 00:00:00 postgres: archiver
      postgres 2729 2721 0 19:54 ? 00:00:00 postgres: logical replication launcher
      postgres 3054 2673 0 20:21 pts/0 00:00:00 psql
      postgres 3055 2721 0 20:21 ? 00:00:00 postgres: postgres postgres [local] idle
      postgres 3067 3054 0 20:23 pts/0 00:00:00 sh -c ps -ef|grep postgres
      postgres 3068 3067 0 20:23 pts/0 00:00:00 ps -ef
      postgres 3069 3067 0 20:23 pts/0 00:00:00 grep postgres


      如上,可以看到服务器进程号是3055,通过select pg_backend_pid()查询。同时,看到服务器上该进程的父进程是2721,由/postgresql/pg16/bin/postgres这个主进程派生。

      后端进程或服务器进程的数量由max_connections参数决定。

      每一个后端进程一次只能访问一个数据库。它和客户端进程进行TCP通信,客户端断开之后,该进程自动回收消失。客户端重新连接或发起新连接时重新创建新的后端进程。由于进程的创建或回收,比较消耗操作系统的资源,因此,多数情况下,应用系统都会通过连接池的方式和数据库建立连接。

      我们查看当前会话所在的后端进程号或者叫服务器进程的时候,我们调用的是pg_backend_pid()函数,杀会话所在进程时,调用的是pg_terminate_backend()

        查看当前会话所在的后端进程号
        postgres=# select pg_backend_pid();
        pg_backend_pid
        ----------------
        3055
        (1 row)


        杀当前会话所在进程
        postgres=# select pg_terminate_backend(3055);
        FATAL: terminating connection due to administrator command
        server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
        The connection to the server was lost. Attempting reset: Succeeded.
        postgres=# \c
        You are now connected to database "postgres" as user "postgres"

        c. 用户进程或客户端进程:

        指的是连接数据库服务器的应用程序或者客户端工具等

        每个用户进程或者客户端进程对应一个服务端进程。

        2. 数据库服务器启动流程

        当我们通过pg_ctl工具来启动PostgreSQL数据库时,先在操作系统上创建1个master进程,然后该进程派生出一系列的后台进程,同时该进程监听$PGDATA/postgresql.conf配置文件中指定的端口。并且,向操作系统申请内存,用于数据库的正常运行操作,处理客户端的连接请求操作处理。最后,数据库可以正常对外提供服务。

          [postgres@pgserver pgdata]$ netstat -anp|grep 5432
          (Not all processes could be identified, non-owned process info
          will not be shown, you would have to be root to see it all.)
          tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 2721/postgres
          tcp6 0 0 :::5432 :::* LISTEN 2721/postgres
          unix 2 [ ACC ] STREAM LISTENING 41439 2721/postgres postgresql/pgdata/.s.PGSQL.5432
          [postgres@pgserver pgdata]$


          $PGDATA/postgresql.conf配置文件中指定的端口是5432, listen_address=*


          3. 进程结构小结

          PostgreSQL数据库服务器的进程结构,类似于ORACLE 数据库的进程结构:守护进程,后台进程,服务器进程,用户进程等。



          文章转载自DBA随笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

          评论