PostgreSQL 9.1.2 general control walsender. c source code analysis

Source: Internet
Author: User

I. Some procedures

1. The master server operates in a continuous archiving mode, while the slave server reads data from the WAL file of the master server in a continuous recovery mode.

2. the archive process pcompetition is responsible for copying the fully-filled redo log files to the archive log files after switching the redo log files to prevent them from being overwritten when the redo log files are repeatedly written. Therefore, only when the database is running in the archive mode will the pcompetition process be started.

3. Log transmission is asynchronous. WAL records are transferred after the transaction is committed.

4. The secondary server can read WAL records from the WAL archive or master server directory through TCP connections (so-called stream replication.

5. If you want to use stream replication, you must set the corresponding permissions on the master server to allow the stream replication connection from the backup server. You need to configure this in the pg_cmd.conf file. The max_wal_senders variable in this Code is also configured in this file to provide a large enough value to store the number of slave servers.

Ii. Detailed description of main functions of walsender. c

Walsender. all functions in the c program are prepared to send WAL records for the walsender process, includes the walsender process entry function, initialization function, XlogRead function for reading WAL records, XlogSend function, and walsender process processing signal function.

1. Important data structures of the walsender Process

(1) State enumeration marking the walsender Process

In the life cycle of the walsender process, the process has four states: startup, backup, exception, and termination. The definition is as follows:

[Cpp]
  1. Typedef EnumWalSndState
  2. {
  3. WALSNDSTATE_STARTUP = 0,/* Startup status */
  4. WALSNDSTATE_BACKUP,/* Backup status */
  5. WALSNDSTATE_CATCHUP,/* Get the exception status */
  6. WALSNDSTATE_STREAMING/* Close */
  7. } WalSndState;

The following describes the four states of the walsender process and when the process is in a specific State:

① It was in the startup status when it was just created;

② During the log sending process, the log is always in the backup state;

③ If the walsender process receives abnormal signals during sleep, the status of the walsender process is marked as catchup, some exceptions that cannot be handled during the process of communicating with walcycler are marked. For example, the file requested by the walcycler process does not exist, and the communication link between the walsender process and the walcycler process is disconnected.

④ When the walsender process receives some abnormal signals during sleep, it will be converted from the catchup state to the termination state. In this state, the process generally needs to handle related exception handling, for example, send some WAL log files that have not been sent out in the buffer zone, and exit the process.

(2) store the information data structure of each walsender Process

In the postgreSQL database system, multiple walsender processes may be used to send request WAL records for the walsener process. If each walsender process is not marked, the entire system will be messy, the data structure used to store walsender process information is defined in src \ include \ replication \ walsender. h file. Its structure is as follows:

[Cpp]
  1. Typedef StructWalSnd
  2. {
  3. Pid_t pid;/* ID of the walsender process */
  4. WalSndState state/* Walsender status */
  5. XLogRecPtr sentPtr;/* WAL sending point */
  6. XLogRecPtr write;/* Write position */
  7. XLogRecPtr flush;/* Refresh location */
  8. XLogRecPtr apply;/* Location of the application */
  9. Slock_t mutex;// Mutex semaphores to protect the above shared Variables
  10. Latch latch;
  11. IntSync_standby_priority;
  12. } WalSnd;

In the WalSnd struct, the ID number of the stored walsender process is defined. In the system, the idnumber of each process is different. In this way, the process number is used to mark the WalSnd structure in the shared memory, it will bring great convenience. The content defined in the WalSnd struct is described as follows:

①. Pid: the ID number of the walsender process;

② State: stores the state of the walsender process, which has been stated above;

③ Write, flush, and apply: records the locations where xlog is written, refreshed, and applied on the secondary server. If the secondary server has not assigned a value for them, they are invalid;

④ Mutex: mutex semaphores are int type, used to control some critical resources, so that the critical resources can only be read and written by one process at a time;

⑤ Latch: used to protect the three variables in ③. Its function is equivalent to a lock;

6. sync_standby_priority: Priority of the secondary server. If the value is 0, the secondary server is not ranked and protected by the synchronous replication lock SyncRepLock.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.