WALs
In a system using Write-ahead logs (WALs or Xlogs), all data file modifications are written to a log before they are applied. That is, corresponding logs must be written into a permanent device before a data file is modified. You can use WALs to restore the cluster if the system crashes. WALs can be recorded at different levels, which are specified by the wal_level parameter.
Log Running Mechanism
When GaussDB is started, a memory segment is allocated to cache logs generated during database running. The buffer size is specified by the wal_buffers parameter. To ensure transaction persistence, logs in wal_buffers must be flushed to disks.
WALs are written to disks in the following scenarios:
- Commit a transaction using COMMIT. The system calls the fdatasync function to write WAL data in wal_buffers to WAL files. (wal_sync_method specifies the function called during commit. The default value is fdatasync.)
- WAL writer periodically writes data. The WAL writer process periodically writes WAL data in wal_buffers to WAL files. The wal_writer_delay parameter specifies the interval at which the WAL writer process works. The default value is 200, in ms. A longer delay might lead to insufficient WAL buffer and a shorter delay leads to continuously writing of the WALs, thereby increasing the load of disk I/O.
Log Storage Directory
The path is Data directory/pg_xlog.
- The WAL storage path of CNs is /<CN data directory>/pg_xlog. The default size of a WAL file is 16 MB, which is specified by the wal_segment_size parameter. Generally, you do not need to change the size.
gaussdb=# SHOW wal_segment_size; wal_segment_size ----------------- 16MB (1 row) - The following uses one DN as an example. You can run the following command to query the data directory: /opt/gaussdb/data/dn/db_6001 indicates the data directory of the cluster node. Xlogs on the current node are stored in the /opt/gaussdb/data/dn/db_6001/pg_xlog directory.
$ cm_ctl query -Cvd [ CMServer State ] node instance state ------------------------------------------------------------------------ 1 192.168.10.182 1 /opt/gaussdb/data/cm/cm_server Primary [ Cluster State ] cluster_state : Normal redistributing : No balanced : Yes current_az : AZ_ALL [ Datanode State ] node instance state -------------------------------------------------------------------------- 1 192.168.10.182 6001 /opt/gaussdb/data/dn/dn_6001 P Primary Normal
Log Naming Rules
WALs are saved as segment files. Each segment is 16 MB and is divided into multiple 8 KB pages. The name of a segment file consists of 24 hexadecimal characters. The name of a WAL file consists of the timeline, log file identifier, and log file segment identifier. For details, see Table 1.
The upper limit of the segment identifier of a WAL file is FF. If there is another WAL file, add 1 to the file identifier and reset the segment identifier to 0. For example, the next file of 0000000100000000000000FF is 000000010000000100000000. The timeline starts from 1, and the log file identifier and the segment identifier of the file both start from 0.
| Column | Description |
|---|---|
| Timeline | Time line The value is an 8-digit hexadecimal number. |
| wal_file_num | Identifier of a WAL file The value is an 8-digit hexadecimal number. |
| wal_segment_num | Identifier of a WAL file segment The value is an 8-digit hexadecimal number. |
The size of each WAL file is 16 MB. Typical WAL file names are as follows:
-rW----- 1 gaussdb d gaussdb d 16777216 Jun 11 12:33 0000000100000001000000F2 -rW----- 1 gaussdb d gaussdb d 16777216 Jun 11 18:10 0000000100000001000000F3 -rW----- 1 gaussdb d gaussdb d 16777216 Jun 11 22:18 0000000100000001000000F4 -rW----- 1 gaussdb d gaussdb d 16777216 Jun 12 03:58 0000000100000001000000F5 -rW----- 1 gaussdb d gaussdb d 16777216 Jun 12 07:59 0000000100000001000000F6 -rW----- 1 gaussdb d gaussdb d 16777216 Jun 12 13:45 0000000100000001000000F7 -rW----- 1 gaussdb d gaussdb d 16777216 Jun 12 17:30 0000000100000001000000F8 -rW----- 1 gaussdb d gaussdb d 16777216 Jun 12 23:10 0000000100000001000000F9 -rW----- 1 gaussdb d gaussdb d 16777216 Jun 13 03:10 0000000100000001000000FA -rW----- 1 gaussdb d gaussdb d 16777216 Jun 13 08:39 0000000100000001000000FB -rW----- 1 gaussdb d gaussdb d 16777216 Jun 13 12:43 0000000100000001000000FC -rW----- 1 gaussdb d gaussdb d 16777216 Jun 13 17:08 0000000100000001000000FD -rW----- 1 gaussdb d gaussdb d 16777216 Jun 13 16:32 0000000100000001000000FE
The numbers in the WAL file name are used in ascending order in succession. Exhausting all available numbers takes a long time, and the numbers will start from zero again after they reach the maximum.
Log Content Description
The content of WALs depends on the types of recorded transactions. WALs can be used to restore a system after the system breaks down.
By default, GaussDB reads WALs for system restoration during each startup.
Log Application Scenario
Database recovery: When a database crashes unexpectedly, database changes have been recorded in Xlogs. After the database is restarted, the Xlogs after the checkpoint can be replayed to flush the modified data pages (recorded in the Xlogs but not modified in the data files) to disks, ensuring data integrity. This process is automatically completed after the database is started.
Data synchronization between the primary and standby DNs: The standby DN of GaussDB receives Xlogs from the primary DN and applies Xlogs to ensure data consistency between the primary and standby DNs. Data synchronization between the primary and standby databases can also be implemented using Xlogs.
Database backup and restoration: When GaussRoach is used to back up GaussDB, you need to copy data files and Xlogs generated during the backup to form a complete backup. You can use a full backup or an incremental backup and Xlogs to restore data to a specified point in time, that is, perform point-in-time recovery (PITR).
Logical replication: Logical replication tools, such as DRS, parse Xlogs to obtain data streams for incremental data synchronization.
Xlog Recycling
During database running, Xlogs are continuously generated. GaussDB clears Xlogs using the checkpoint mechanism (also called Xlog recycling). For details about the parameters related to Xlog recycling, see Table 2. For details about how to set parameters, see Setting Parameters.
| Parameter | Description |
|---|---|
| Sets the maximum time between automatic checkpoints. Specifically, this is the interval for the next automatic checkpoint after the last automatic checkpoint. The default value is 15 minutes. If the number of Xlogs accumulated between two automatic checkpoints is greater than the value of checkpoint_segments, an automatic checkpoint is triggered. | |
| Maximum number of WAL files that can be retained within the interval specified by checkpoint_timeout. The size of each log file is 16 MB. | |
| Specifies whether to enable incremental checkpointing. The default value is on. | |
| Specifies the maximum interval between automatic checkpoints when the incremental checkpoint (enable_incremental_checkpoint) is enabled. The default value is 1 minute. That is, an incremental checkpoint is started every minute, and whether Xlogs need to be cleared is checked. | |
| Specifies the minimum number of Xlog files that can be retained in the pg_xlog directory. The standby node obtains the Xlogs from the primary node to perform streaming replication. The default value is 128. Generally, when executing a checkpoint, the database checks whether the number of Xlogs in the pg_xlog directory is greater than the value of wal_keep_segments. If the number is greater than the value of wal_keep_segments, the database deletes the earliest Xlogs until the number of Xlogs is equal to the value of wal_keep_segments. | |
| Specifies whether the primary node recycles logs when any standby node is disconnected and the size of Xlogs exceeds the value of max_size_for_xlog_prune. The value on indicates that the functionality is enabled. When a standby node is disconnected, Xlogs between the primary and standby DNs are not cleared to ensure that Xlogs are continuous. However, if enable_xlog_prune is enabled and the accumulated Xlog size is greater than the value of max_size_for_xlog_prune (generally 256 GB), Xlogs are cleared. | |
| Maximum number of Xlogs retained on the primary node when the standby node is faulty. This parameter takes effect when enable_xlog_prune is enabled. The mechanism is as follows:
|
Maintenance Suggestion
WALs are important for database restoration. You are advised to routinely back up WALs.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot