A database recovery method and device
By reading the full-write page record from the WAL log file of the database and recovering the data when the database is closed, the problem in the existing technology of being unable to recover accidentally deleted data at any time is solved, and efficient and reliable data recovery is achieved. Data consistency and integrity are ensured.
Patent Information
- Application Number
- CN202410882983.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-03
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2044-07-03
AI Technical Summary
Existing database recovery methods cannot achieve accurate recovery of accidentally deleted data at any time, especially in an offline state, and must be performed before a VACUUM operation, which affects recovery efficiency and data integrity.
By reading the all-write page record of the accidentally deleted table from the WAL log file of the database, the target data page is determined, a data recovery environment is created when the database is closed, and the database is restarted to write the accidentally deleted data into the original data table.
It achieves accurate recovery of accidentally deleted data in specified tables offline, avoids the impact of VACUUM operations, improves recovery efficiency and reliability, and ensures data consistency and integrity.
Smart Images

Figure CN118409905B
Abstract
Description
Technical Field
[0001] The embodiments of the present specification relate to the field of database storage technology, and in particular to a database recovery method. Background Art
[0002] Data security is a key concern for technical personnel, among which the recovery of accidentally deleted data is of utmost importance.
[0003] The existing database data recovery method relies on online status and time point recovery after data is accidentally deleted, that is, backup and recovery is performed according to time, that is, a time point is specified and the data is restored to the position of the event point. This method has obvious disadvantages. All data will be restored to the position of the specified time, and it is not possible to specify the recovery of specified data of a certain table. When data of a table is accidentally deleted but before VACUUM is executed, according to the mvcc mechanism, when a piece of data is deleted, the data will be marked as a dead tuple first, and will not be deleted immediately. Only after VACUUM is executed will the data be completely cleared. Therefore, the deleted data can be re-read before VACUUM, and then the missing rows can be copied to a new table, and then injected into the original table. The disadvantage of this method is that it must be performed before VACUUM is executed, because VACUUM will actually clear the dead tuples, and the recovery method must be online.
[0004] In view of this, how to propose a database recovery method to realize the recovery of online data or offline data at any time after the data is accidentally deleted has become a technical problem that needs to be solved urgently. Summary of the invention
[0005] In view of this, an embodiment of the present specification provides a database recovery method. One or more embodiments of the present specification also relate to a database recovery device, a computing device, and a computer-readable storage medium to solve the technical defect in the prior art that the data cannot be deleted after one or more data are mistakenly deleted in a traditional database.
[0006] According to a first aspect of an embodiment of this specification, a database recovery method is provided, including:
[0007] Reading a full-write page record corresponding to the erroneous deletion table from the WAL log file of the database, and determining a target data page corresponding to the erroneous deletion table based on the full-write page record, wherein the full-write page record is a data page written into the WAL log when the database operation instruction is first executed;
[0008] Creating a data recovery environment based on the target data page corresponding to the full-write page record;
[0009] The database is restarted, and based on the data recovery environment, the mistakenly deleted data is determined, and the mistakenly deleted data is written into an original data table corresponding to the mistakenly deleted data.
[0010] According to a second aspect of an embodiment of this specification, there is provided a database recovery device, including:
[0011] A reading module is configured to read a full-write page record corresponding to the erroneous deletion table from a WAL log file of the database, and determine a target data page corresponding to the erroneous deletion table based on the full-write page record, wherein the full-write page record is a data page written into the WAL log when the database operation instruction is first executed;
[0012] A creation module configured to create a data recovery environment based on the target data page corresponding to the full-write page record;
[0013] The determination module is configured to restart the database, determine the mistakenly deleted data based on the data recovery environment, and write the mistakenly deleted data into the original data table corresponding to the mistakenly deleted data.
[0014] According to a third aspect of an embodiment of this specification, a computing device is provided, including:
[0015] Memory and processor;
[0016] The memory is used to store computer executable instructions, and the processor is used to execute the computer executable instructions. When the computer executable instructions are executed by the processor, the steps of the above database recovery method are implemented.
[0017] According to a fourth aspect of the embodiments of this specification, a computer-readable storage medium is provided, which stores computer-executable instructions, and when the instructions are executed by a processor, the steps of the above-mentioned database recovery method are implemented.
[0018] The present specification provides a database recovery method, comprising: first, reading a full-write page record corresponding to an erroneous deletion table from a WAL log file of a database, and determining a target data page corresponding to the erroneous deletion table based on the full-write page record, wherein the full-write page record is a data page written into the WAL log when a database operation instruction is first executed; then, based on the target data page corresponding to the full-write page record, creating a data recovery environment; finally, restarting the database, determining erroneous deletion data based on the data recovery environment, and writing the erroneous deletion data into an original data table corresponding to the erroneous deletion data.
[0019] By applying the database recovery method provided in this manual, by parsing the full-write page records, accurate recovery of accidentally deleted data in the specified table can be achieved in an offline state, ensuring data consistency and integrity, avoiding the impact of VACUUM operations, and improving recovery efficiency and reliability. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Figure 1 It is a flowchart of a database recovery method provided by an embodiment of this specification;
[0021] Figure 2 It is a structural schematic diagram of a database recovery device provided by an embodiment of this specification;
[0022] Figure 3 It is a structural block diagram of a computing device provided by an embodiment of this specification. DETAILED DESCRIPTION
[0023] Many specific details are described in the following description to facilitate a full understanding of this specification. However, this specification can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of this specification, so this specification is not limited to the specific implementation disclosed below.
[0024] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in one or more embodiments of this specification are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0025] In this specification, a database recovery method is provided. This specification also relates to a database recovery device, a computing device, and a computer-readable storage medium, which are described in detail one by one in the following embodiments.
[0026] See also Figure 1 , Figure 1 FIG. 1 is a flowchart of a database recovery method provided by an embodiment of this specification. Figure 1 As shown, the specific steps include the following steps.
[0027] Step S102: Read the full-write page record corresponding to the erroneous deletion table from the WAL log file of the database, and determine the target data page corresponding to the erroneous deletion table based on the full-write page record, wherein the full-write page record is the data page written into the WAL log when the database operation instruction is executed for the first time.
[0028] In the embodiment of this specification, the method for determining the target data page corresponding to the wrong deletion table specifically includes:
[0029] Read all log records from the WAL log file of the database, and parse the log records to obtain relevant information of the database operation carried in the log records, wherein the relevant information includes: data insertion information, data update information, and data deletion information;
[0030] Determine target information corresponding to the erroneously deleted table based on the relevant information of the database operation carried by the log record, wherein the target information corresponding to the erroneously deleted table is determined based on the data deletion information;
[0031] Determine, based on the target information corresponding to the erroneous deletion table, a full-write page record corresponding to the erroneous deletion table;
[0032] Determine a target data page based on the OIDs corresponding to the tables in the full-write page record and the page identifiers corresponding to the data pages;
[0033] The target data page is saved separately in a target folder, and the format of the target data page is processed to determine that the format of the target data page is consistent with the format of the original data page, wherein the target file only includes the complete content of the target data page corresponding to the error deletion table.
[0034] It should be noted that the relevant information of the database operation here may be the data information corresponding to the data insertion operation, the data information corresponding to the data update operation, or the data information corresponding to the data deletion operation. Those skilled in the art should know that the data information generated by the execution of corresponding operations on the database may be included in the relevant information of the database operation mentioned in the embodiments of this specification.
[0035] Here, the log records are parsed to obtain detailed information corresponding to each log record.
[0036] In the embodiment of the present specification, the method for determining the target data page is to match the specific target data page by combining the OID of the table in the log record and the page identifier of the data page.
[0037] In addition, the embodiment of this specification also extracts the parsed full-write page records and stores them separately in a file, wherein the file only contains the complete content of the data page of the mistaken deletion table to ensure the integrity and consistency of the data.
[0038] When processing the format of the target data page, the embodiment of this specification writes the target data page into a binary file to retain the original page format.
[0039] Step S104: creating a data recovery environment based on the target data page corresponding to the full-write page record.
[0040] In an embodiment of the present specification, a method for creating a data recovery environment includes: based on the target data page corresponding to the full-write page record, creating a new table with the same structure as the incorrectly deleted table in the database, wherein the fields and data types of the new table are consistent with those of the incorrectly deleted table; determining the data file path corresponding to the new table through a target built-in function; and when the database is closed, writing the data carried by the target data page into the data file corresponding to the new table through a target operation instruction.
[0041] It should be noted that the target built-in function here is a built-in function in PostgreSQL, which is a function that can determine the data file path.
[0042] In the embodiment of this specification, closing the database is to determine the consistency of the data writing process and avoid interference, so the database needs to be temporarily closed.
[0043] In the embodiment of this specification, the target operation instruction is used to write into the data file corresponding to the new table, which means that the data of the extracted full-write page record is directly written into the data file of the new table through low-level file operations.
[0044] Step S106: restarting the database, determining the mistakenly deleted data based on the data recovery environment, and writing the mistakenly deleted data into the original data table corresponding to the mistakenly deleted data.
[0045] In an embodiment of the present specification, a specific implementation method for writing the accidentally deleted data into the original data table includes: when the database is restarted, loading the data file corresponding to the new table, and reading the new table data carried by the new table by dirty reading; analyzing the new table data to determine the accidentally deleted data and the accidentally deleted data row corresponding to the accidentally deleted data, and writing the accidentally deleted data into the original data table corresponding to the accidentally deleted data.
[0046] In the embodiment of this specification, restarting the database is to ensure that the data file of the new table is correctly loaded.
[0047] By applying the database recovery method provided in the embodiments of this specification, on the one hand, the data recovery process can be carried out in an offline state, ensuring the continuity and security of database operation, that is, having offline recovery capabilities; on the other hand, it can accurately locate and recover specified data in a specific table without affecting the integrity of other data tables, thereby improving the reliability and efficiency of recovery, that is, having the ability to accurately recover the corresponding data of a specific table.
[0048] Specifically, the database recovery method provided by the embodiment of this specification has the following advantages: 1. Efficient data recovery: This method performs data recovery by parsing the full-page write records in the WAL log, without relying on additional backups, saving time and resources. By combining and matching the OID of the table and the page identifier of the data page, the accidentally deleted data can be quickly found and restored, improving the recovery efficiency; 2. Avoiding data loss: Since the recovery process depends on the WAL log file, even if the VACUUM operation clears the dead tuples, the recovery process can still be carried out, ensuring that the data will not be lost due to VACUUM; 3. Accurate recovery of specific data: This method allows the specified recovery of the data of a specific table without affecting the data of other tables in the database, avoiding the complexity and waste of resources caused by the full database recovery; 4. Offline operation: The recovery operation can be performed when the database is offline, which will not affect the normal operation of the database, ensuring the security and stability of the recovery process. At the same time, the offline recovery method avoids large-scale operations when the database is operating normally, reducing the interruption time of the business system; 5. Data consistency: By writing records to the entire page, the integrity and consistency of the data page are ensured, and the data can be seamlessly restored to the state before the accidental deletion after recovery; 6. Low cost: By using the WAL log file for recovery, the reliance on frequent full backups is reduced, and the cost of backup storage and management is reduced. The recovery process mainly relies on the database's own functions and log files, and does not require additional software or hardware support, which reduces the recovery cost.
[0049] Corresponding to the above method embodiment, this specification also provides a database recovery device embodiment, Figure 2 FIG. 1 is a schematic diagram of a database recovery device provided by an embodiment of the present specification. Figure 2 As shown, the device comprises:
[0050] The reading module 202 is configured to read the full-write page record corresponding to the erroneous deletion table from the WAL log file of the database, and determine the target data page corresponding to the erroneous deletion table based on the full-write page record, wherein the full-write page record is the data page written into the WAL log when the database operation instruction is executed for the first time;
[0051] A creation module 204, configured to create a data recovery environment based on the target data page corresponding to the full-write page record;
[0052] The determination module 206 is configured to determine the mistakenly deleted data based on the data recovery environment, and write the mistakenly deleted data into the original data table corresponding to the mistakenly deleted data.
[0053] In an optional embodiment, the reading module 202 is further configured to:
[0054] Read all log records from the WAL log file of the database, and parse the log records to obtain relevant information of the database operation carried in the log records, wherein the relevant information includes: data insertion information, data update information, and data deletion information;
[0055] Determine target information corresponding to the erroneously deleted table based on the relevant information of the database operation carried by the log record, wherein the target information corresponding to the erroneously deleted table is determined based on the data deletion information;
[0056] Determine, based on the target information corresponding to the erroneous deletion table, a full-write page record corresponding to the erroneous deletion table;
[0057] Determine a target data page based on the OIDs corresponding to the tables in the full-write page record and the page identifiers corresponding to the data pages;
[0058] The target data page is saved separately in a target folder, and the format of the target data page is processed to determine that the format of the target data page is consistent with the format of the original data page, wherein the target file only includes the complete content of the target data page corresponding to the error deletion table.
[0059] In an optional embodiment, the creation module 204 is further configured to:
[0060] Based on the target data page corresponding to the full-write page record, a new table having the same structure as the incorrectly deleted table is created in the database, wherein the fields and data types of the new table are consistent with those of the incorrectly deleted table;
[0061] Determine the data file path corresponding to the new table through the target built-in function;
[0062] When the database is closed, the data carried by the target data page is written into the data file corresponding to the new table through the target operation instruction.
[0063] In an optional embodiment, the determining module 206 is further configured to:
[0064] When the database is restarted, the data file corresponding to the new table is loaded, and the new table data carried by the new table is read by dirty reading;
[0065] The new table data is analyzed to determine the mistakenly deleted data and the mistakenly deleted data row corresponding to the mistakenly deleted data, and the mistakenly deleted data is written into the original data table corresponding to the mistakenly deleted data.
[0066] By applying the database recovery device provided in the embodiments of this specification, on the one hand, the data recovery process can be carried out in an offline state, ensuring the continuity and security of database operation, that is, having offline recovery capabilities; on the other hand, it can accurately locate and recover specified data in a specific table without affecting the integrity of other data tables, thereby improving the reliability and efficiency of recovery, that is, having the ability to accurately recover the corresponding data of a specific table.
[0067] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the database recovery device, since it is basically similar to the database recovery method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the database recovery method embodiment.
[0068] Figure 3 3 is a block diagram of a computing device provided by an embodiment of the present specification. The components of the computing device 300 include but are not limited to a memory 310 and a processor 320. The processor 320 is connected to the memory 310 via a bus 330, and the database 350 is used to store data.
[0069] The computing device 300 also includes an access device 340 that enables the computing device 300 to communicate via one or more networks 360. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 340 may include one or more of any type of network interface (e.g., a network interface card (NIC)) of wired or wireless, such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a world-wide interoperability for microwave access (Wi-MAX) interface, an Ethernet interface, a universal serial bus (USB) interface, a cellular network interface, a Bluetooth interface, and a near field communication (NFC).
[0070] In one embodiment of the present specification, the above components of the computing device 300 and Figure 3 Other components not shown in the figure may also be connected to each other, for example, via a bus. It should be understood that Figure 3 The computing device structure block diagram shown is only for the purpose of illustration, and is not intended to limit the scope of this specification. Those skilled in the art can add or replace other components as needed.
[0071] The computing device 300 may be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smart phone), a wearable computing device (e.g., a smart watch, smart glasses, etc.), or other types of mobile devices, or a stationary computing device such as a desktop computer or a personal computer (PC). The computing device 300 may also be a mobile or stationary server.
[0072] The processor 320 is used to execute the following computer executable instructions, which implement the steps of the above database recovery method when executed by the processor.
[0073] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the computing device embodiment, since it is basically similar to the database recovery method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the database recovery method embodiment.
[0074] An embodiment of the present specification further provides a computer-readable storage medium storing computer-executable instructions, which can implement the steps of the above-mentioned database recovery method when executed by a processor.
[0075] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the computer-readable storage medium embodiment, since it is basically similar to the database recovery method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the database recovery method embodiment.
[0076] An embodiment of the present specification further provides a computer program, wherein when the computer program is executed in a computer, the computer is caused to execute the steps of the above-mentioned database recovery method.
[0077] The preferred embodiments of this specification disclosed above are only used to help explain this specification. The optional embodiments do not describe all the details in detail, nor do they limit the invention to the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of the embodiments of this specification. This specification selects and specifically describes these embodiments in order to better explain the principles and practical applications of the embodiments of this specification, so that technicians in the relevant technical field can understand and use this specification well.
Claims
1. A database recovery method, characterized in that: include: Reading a full-write page record corresponding to the erroneous deletion table from the WAL log file of the database, and determining a target data page corresponding to the erroneous deletion table based on the OIDs corresponding to each table in the full-write page record and the page identifiers corresponding to each data page, wherein the full-write page record is a data page written into the WAL log when the data page executes the database operation instruction for the first time; Creating a data recovery environment based on the target data page corresponding to the full-write page record; Restarting the database, determining the mistakenly deleted data based on the data recovery environment, and writing the mistakenly deleted data into the original data table corresponding to the mistakenly deleted data; The step of creating a data recovery environment based on the target data page corresponding to the full-write page record includes: Based on the target data page corresponding to the full-write page record, a new table having the same structure as the incorrectly deleted table is created in the database, wherein the fields and data types of the new table are consistent with those of the incorrectly deleted table; Determine the data file path corresponding to the new table through the target built-in function; When the database is closed, the data carried by the target data page is written into the data file corresponding to the new table through the target operation instruction; The restarting of the database, determining the mistakenly deleted data based on the data recovery environment, and writing the mistakenly deleted data into the original data table corresponding to the mistakenly deleted data, comprises: When the database is restarted, the data file corresponding to the new table is loaded, and the new table data carried by the new table is read by dirty reading; The new table data is analyzed to determine the mistakenly deleted data and the mistakenly deleted data row corresponding to the mistakenly deleted data, and the mistakenly deleted data is written into the original data table corresponding to the mistakenly deleted data.
2. The method according to claim 1, characterized in that The step of reading the full-write page record corresponding to the erroneous deletion table from the WAL log file of the database, and determining the target data page corresponding to the erroneous deletion table based on the full-write page record includes: Read all log records from the WAL log file of the database, and parse the log records to obtain relevant information of the database operation carried in the log records, wherein the relevant information includes: data insertion information, and / or data update information, and / or data deletion information; Determine target information corresponding to the erroneously deleted table based on the relevant information of the database operation carried by the log record, wherein the target information corresponding to the erroneously deleted table is determined based on the data deletion information; Determine, based on the target information corresponding to the erroneous deletion table, a full-write page record corresponding to the erroneous deletion table; Determine a target data page based on the OIDs corresponding to the tables in the full-write page record and the page identifiers corresponding to the data pages; The target data page is saved separately in a target file, and the format of the target data page is processed to determine that the format of the target data page is consistent with the format of the original data page, wherein the target file only includes the complete content of the target data page corresponding to the error deletion table.
3. A database recovery device, characterized in that: include: A reading module is configured to read a full-write page record corresponding to an erroneous deletion table from a WAL log file of a database, and determine a target data page corresponding to the erroneous deletion table based on an OID corresponding to each table in the full-write page record and a page identifier corresponding to each data page, wherein the full-write page record is a data page written into the WAL log when the data page executes a database operation instruction for the first time; A creation module configured to create a data recovery environment based on the target data page corresponding to the full-write page record; a determination module configured to restart the database, determine the mistakenly deleted data based on the data recovery environment, and write the mistakenly deleted data into an original data table corresponding to the mistakenly deleted data; Wherein, the creation module is further configured as follows: Based on the target data page corresponding to the full-write page record, a new table having the same structure as the incorrectly deleted table is created in the database, wherein the fields and data types of the new table are consistent with those of the incorrectly deleted table; Determine the data file path corresponding to the new table through the target built-in function; When the database is closed, the data carried by the target data page is written into the data file corresponding to the new table through the target operation instruction; The restarting of the database, determining the mistakenly deleted data based on the data recovery environment, and writing the mistakenly deleted data into the original data table corresponding to the mistakenly deleted data, comprises: When the database is restarted, the data file corresponding to the new table is loaded, and the new table data carried by the new table is read by dirty reading; The new table data is analyzed to determine the mistakenly deleted data and the mistakenly deleted data row corresponding to the mistakenly deleted data, and the mistakenly deleted data is written into the original data table corresponding to the mistakenly deleted data.
4. A computer device comprising: Memory and processor; The memory is used to store computer executable instructions, and the processor is used to execute the computer executable instructions. When the computer executable instructions are executed by the processor, the steps of the database recovery method described in any one of claims 1 to 2 are implemented.
5. A computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the steps of the database recovery method according to any one of claims 1 to 2.
Citation Information
Patent Citations
Method for realizing recovery of distributed database cluster at any time point based on WAL
CN112925676A
Data offline recovery method, storage medium and equipment
CN116643913A
Database data recovery method, storage medium and equipment
CN117271221A
Data recovery method, storage medium, equipment and computer program product
CN118069422A
Point in Time Recovery on a Database
US20160342484A1
