PostgreSQL 9.1.2总控walsender.c源码分析
PostgreSQL9.1.2总控walsender.c源码分析,walsender.c程序中的函数都是为walsender进程发送WAL记录而准备的,包括了walsender进
一、程序涉及到的一些知识
1、主服务器是以连续的归档模式操作的,而备用服务器是以连续的恢复模式从主服务器的WAL文件中读取数据。
2、归档进程pgarch就是负责在重做日志文件切换后将已经写满的重做日志文件复制到归档日志文件中,以防止循环写入重做日志文件时将其覆盖。所以说,只有数据库运行在归档模式时,这个pgarch进程才会被启动。
3、日志传送是异步进行的。WAL记录的传送是在事务提交之后进行的。
4、辅助服务器可以通过TCP连接(所谓的流复制)从WAL归档或者主服务器的目录中读取WAL记录。
5、如果想用流复制,必须在主服务器中设置相应的权限,以使来自备用服务器的流复制连接得到许可。这个需要在pg_hba.conf文件中做相应的配置。本代码中的max_wal_senders变量也是在这个文件做配置的,以提供足够大的值来存储备用服务器的数量。
二、walsender.c主要函数功能详解
walsender.c程序中的函数都是为walsender进程发送WAL记录而准备的,包括了walsender进程的入口函数、初始化函数、读取WAL记录的XlogRead函数、XlogSend函数以及walsender进程处理相关的信号函数等。
1、walsender进程重要的数据结构
(1)、标记walsender进程的状态枚举
在walsender进程的生命周期中,进程存在四个状态,即:启动状态、备份状态、获取异常状态以及终止状态。其定义如下所示:
[cpp]
下面就walsender进程的四个状态以及何时处于一种特定的状态进行说明:
①、在刚刚创建的时候是处在启动状态;
②、在日志的发送过程中,一直都是处在备份的状态;
③、如果walsender进程在休眠的时候接收到一些异常信号的时,walsender进程的状态标记为catchup态,标记出进程在和 walreceiver通信的过程中出现一些不可以处理的异常,比如:walreceiver进程请求的文件不存在、walsender进程和walreceiver进程的通信链接断开等等。
④、walsender进程在休眠的时候接收到一些异常信号的时候,会由catchup态转换为终止态,在这个状态的时候,进程一般需要处理相关的异常处理,比如发送出一些留在缓冲区中尚未发送出去的WAL日志文件、进程退出等。
(2)、存储每一个walsender进程的信息数据结构
在postgreSQL数据库系统中,可能存在多个walsender进程用来为walreceiver进程发送请求的WAL记录,如果不对每个walsender进程加以标记,整个系统将会很混乱,这个用来存储walsender进程信息的数据结构定义在src\include\replication\walsender.h文件中。其结构如下所示:
[cpp]
在WalSnd结构体中,定义了存储walsender进程的ID号,在系统中每一个进程的ID号是不一样的,这样用进程号来标记共享内存中的WalSnd结构,将会带来很大的方便。在WalSnd结构体中定义的内容分别简述如下:
①、pid:存储walsender进程的ID号;
②、state:存储walsender进程的状态,,其状态含义在上面陈述过;
③、write、flush以及apply:分别记录辅助服务器中xlog被写、刷新和应用的位置,如果辅助服务器还没有为它们赋值,则它们是无效的;
④、mutex:互斥信号量,是int类型的,用来控制一些临界资源,使得临界资源一次只能被一个进程所读、写等操作;
⑤、latch:用来保护③中的三个变量,其作用相当于一把锁;
⑥、sync_standby_priority:辅助服务器的优先级顺序,为0则表示没有被排列,被同步复制锁SyncRepLock保护。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL and PostgreSQL: Best Practices in Web Development Introduction: In the modern world of web development, databases are an essential component. When choosing a database, common choices are MySQL and PostgreSQL. This article will cover best practices for using MySQL and PostgreSQL in web development and provide some code examples. 1. Applicable scenarios MySQL is suitable for most web applications, especially those that require high performance, scalability and ease of use.

MySQL and PostgreSQL: Performance Comparison and Optimization Tips When developing web applications, the database is an indispensable component. When choosing a database management system, MySQL and PostgreSQL are two common choices. They are both open source relational database management systems (RDBMS), but there are some differences in performance and optimization. This article will compare the performance of MySQL and PostgreSQL and provide some optimization tips. Performance comparison comparing two database management

MySQL and PostgreSQL: Data Security and Backup Strategies Introduction: In modern society, data has become an indispensable part of business and personal life. For database management systems, data security and backup strategies are crucial, both to protect data from loss or damage and to ensure the reliability and integrity of recovered data. This article will focus on the data security and backup strategies of two mainstream relational database systems, MySQL and PostgreSQL. 1. Data security: (1) User rights

Learn the database functions in the Go language and implement the addition, deletion, modification, and query operations of PostgreSQL data. In modern software development, the database is an indispensable part. As a powerful programming language, Go language provides a wealth of database operation functions and toolkits, which can easily implement addition, deletion, modification and query operations of the database. This article will introduce how to learn database functions in Go language and use PostgreSQL database for actual operations. Step 1: Install the database driver in Go language for each database

With the development of database technology, database management systems also present a variety of choices. Developers can choose the most suitable database according to their needs and preferences. As an advanced open source relational database system, PostgreSQL is attracting more and more attention and use by developers. So, how to use PostgreSQL database in PHP programming? 1. Install and configure the PostgreSQL database. Before using PostgreSQL, you need to install and configure it. first

Go language is a fast and efficient programming language suitable for building web services and back-end applications. PostgreSQL is an open source relational database management system that promises to provide higher reliability, scalability and data security. In this article, we’ll take a deep dive into using PostgreSQL with Go and provide some practical code examples and tips. Installing and setting up PostgreSQL First, we need to install and set up PostgreSQL. Can be found on the official website

With the development of the Internet, the amount of data continues to grow, and the need for data management becomes increasingly urgent. Relational database is an important way of data management, among which PostgreSQL is popular for its flexibility, scalability and security. This article introduces how to use the PHP language to implement an open source PostgreSQL relational database. I hope it will be helpful to developers with corresponding needs. Overview PostgreSQL is a powerful relational database system that follows the SQL standard and has many

MySQL and PostgreSQL: How to optimize database query performance? Overview: Database query performance is an important consideration when developing applications. Good query performance improves application responsiveness and user experience. This article will introduce some methods to optimize database query performance, focusing on two commonly used databases, MySQL and PostgreSQL. Optimization of database indexes: Database indexes are an important factor in improving query performance. Indexes can speed up data search and reduce query scanning time.
