Method for creating partition table and automatically expanding partition based on distributed database
By introducing the global object identifier (GOID) and the global transaction manager (GTM) into the distributed database to manage partition information, the problem of inconsistent partition information between the coordination node and the data node is solved, and global consistency and high efficiency of automatic partition creation are achieved. performance, reducing system complexity and resource waste.
Patent Information
- Application Number
- CN202410598259.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-15
- Publication Date
- 2024-08-27
- Estimated Expiration
- 2044-05-15
AI Technical Summary
In a distributed database, the partition information of the coordination node and the data node is inconsistent, resulting in increased system complexity and reduced performance. The existing technology relies on external scheduled tasks for partition auto-increment, which affects the order and efficiency of data insertion.
The Global Object Identifier (GOID) is introduced to manage and store partition information by the Global Transaction Manager (GTM). Partitions are automatically created within the SQL engine through distributed transactions to ensure the global consistency of partition information.
It achieves globally consistent storage of partition information, improves the accuracy of SQL execution and the performance of coordination nodes, reduces system complexity and operation and maintenance costs, and avoids waste of resources and insertion failures.
Smart Images

Figure CN118193541B
Abstract
Description
Technical Field
[0001] The present application belongs to the field of database technology, and more specifically, relates to a method for creating a partition table based on a distributed database and automatically expanding partitions. Background Art
[0002] Traditional centralized databases (using Oracle as an example) support the automatic partition feature. When a data node detects that the data inserted by the user falls outside the partition, a new partition will be automatically created.
[0003] For example, the following table is the definition information of a partitioned table. The partition column is col2. The created table has two initial partitions p1 and p2. When the newly inserted data col2 is 2023-02-15, this value is not in partitions p1 and p2. Therefore, the storage engine detects this situation and automatically creates a new partition, such as p3 less than ('2023-03-02').
[0004] create table tb(
[0005] col1 int,
[0006] col2timestamp
[0007] ) partion by range(col2) (
[0009] partition p1 values less than('2023-01-02'),
[0010] partition p2 values less than('2023-02-02') );
[0012] Directly applying a partition auto-increment solution similar to Oracle's centralized database to a distributed database may cause inconsistency in the partition metadata between the CN node and the DN node of the distributed database. The current distributed database partition auto-increment solution in the industry uses an additional scheduled task outside the database SQL engine to decide whether to create a new partition based on the currently created partition and the partition where data is inserted. This solution will increase the complexity of the system. In addition, the partitions of each data node are different, and the partition information of each node needs to be recorded on the CN. The metadata on the CN will expand with the number of DNs, resulting in a decrease in CN performance. Summary of the invention
[0013] In view of the defects of the prior art, the purpose of this application is to provide a method for creating a partition table and automatically expanding partitions based on a distributed database, aiming to solve the problem of inconsistent partition information between the distributed database coordination node and each data node in the prior art.
[0014] To achieve the above-mentioned purpose, in a first aspect, the present application provides a method for creating a partition table based on a distributed database, wherein the distributed database includes a coordination node CN, a plurality of data nodes DN and a global transaction manager GTM;
[0015] The method for creating the partition table comprises the following steps:
[0016] Step S101, the CN receives an SQL statement for establishing a partition table sent by a client, and sends partition information of the partition table to the GTM based on the SQL statement for establishing the partition table;
[0017] Step S102, GTM allocates a corresponding GOID to the partition table based on the received partition information of the partition table, performs local persistent storage and management on the GOID and the partition information of the corresponding partition table to ensure the global consistency of the partition information, and returns the GOID to CN; the GOID is an identifier of the partition table shared by CN, DN and GTM;
[0018] Step S103, after receiving the GOID, the CN sends the GOID and the SQL statement for establishing the partition table to each DN;
[0019] Step S104, after receiving the GOID and the SQL statement, each DN requests the GTM for the partition information corresponding to the GOID, creates a partition table based on the partition information corresponding to the GOID and the SQL statement returned by the GTM, and stores the GOID locally at the same time;
[0020] Step S105: After each DN establishes a partition table, the CN establishes a partition table based on the partition information corresponding to the GOID and stores the GOID locally at the same time.
[0021] In an optional example, the CN specifically stores the GOID locally by storing the correspondence between the GOID and the OID in a system table; the OID is an identifier of the partition table independently allocated and used by the CN, so that the CN can obtain the corresponding partition table column information based on the OID.
[0022] In an optional example, the GTM specifically stores the GOID and the partition information of the corresponding partition table in a key-value storage manner, wherein the GOID is a keyword and the partition information of the partition table is a value corresponding to the keyword.
[0023] In an optional example, step S104 also includes each DN locally caching the partition information corresponding to the GOID; step S105 also includes CN locally caching the partition information corresponding to the GOID.
[0024] In an optional example, the distributed database specifically adopts the PGXC distributed database.
[0025] In a second aspect, the present application provides a partition automatic expansion method based on a distributed database, the distributed database comprising a coordination node CN, a plurality of data nodes DN and a global transaction manager GTM;
[0026] The partition automatic expansion method comprises the following steps:
[0027] Step S201, the CN receives a first SQL statement for inserting data sent by the client, converts the first SQL statement into a second SQL statement corresponding to the DN, and sends the second SQL statement to the DN corresponding to the data;
[0028] Step S202: the DN corresponding to the data parses the received second SQL statement, searches for the corresponding partition according to the value of the data, and if not found, obtains the GOID of the partition table corresponding to the data and the partition information corresponding to the GOID, determines the information of the newly added partition according to the partition information and the data, and encapsulates the GOID and the information of the newly added partition into a message and sends it to the GTM; the partition table is established based on the method for creating a partition table based on a distributed database described in the first aspect;
[0029] Step S203: After receiving the message, the GTM modifies the partition information corresponding to the GOID in the local persistent storage, and sends the GOID and the modified partition information corresponding to the GOID to the CN and each DN, so that the CN and each DN update the partition information corresponding to the GOID.
[0030] Step S204: After the DN corresponding to the data updates the partition information corresponding to the GOID, the data is inserted into the newly added partition.
[0031] In an optional example, the partition information includes the partition increment interval, and the name, start value, and end value of each partition;
[0032] The information of the newly added partition includes the number, name, start value, and end value of the newly added partition.
[0033] In a third aspect, the present application provides a distributed database, including a coordination node CN, a plurality of data nodes DN and a global transaction manager GTM, wherein:
[0034] CN is used to receive the SQL statement for establishing the partition table sent by the client, and send the partition information of the partition table to GTM based on the SQL statement for establishing the partition table;
[0035] The GTM is used to allocate a corresponding GOID to the partition table based on the partition information of the received partition table, locally persistently store and manage the GOID and the partition information of the corresponding partition table to ensure the global consistency of the partition information, and return the GOID to the CN; the GOID is an identifier of the partition table shared by the CN, DN and GTM;
[0036] After receiving the GOID, CN sends the GOID and the SQL statement for establishing the partition table to each DN;
[0037] After receiving the GOID and SQL statement, each DN is used to request the partition information corresponding to the GOID from the GTM, establish a partition table based on the partition information corresponding to the GOID and the SQL statement returned by the GTM, and store the GOID locally at the same time;
[0038] After each DN establishes a partition table, CN is used to establish a partition table based on the partition information corresponding to the GOID and store the GOID locally at the same time.
[0039] In an optional example, the CN is further used to receive a first SQL statement for inserting data sent by a client, convert the first SQL statement into a second SQL statement corresponding to the DN, and send the second SQL statement to the DN corresponding to the data;
[0040] The DN corresponding to the data is also used to parse the received second SQL statement, and search for the corresponding partition according to the value of the data. If the partition is not found, the GOID of the partition table corresponding to the data and the partition information corresponding to the GOID are obtained, and the information of the newly added partition is determined according to the partition information and the data, and the GOID and the information of the newly added partition are encapsulated as a message and sent to the GTM;
[0041] The GTM is also used to modify the partition information corresponding to the GOID in the local persistent storage after receiving the message, and send the GOID and the partition information corresponding to the modified GOID to the CN and each DN, so that the CN and each DN update the partition information corresponding to the GOID;
[0042] The DN corresponding to the data is also used to insert data into the newly added partition after updating the partition information corresponding to the GOID.
[0043] In a fourth aspect, the present application provides an electronic device, comprising: at least one memory for storing programs; and at least one processor for executing the programs stored in the memory. When the program stored in the memory is executed, the processor is used to execute the method described in any possible implementation of the first aspect or the second aspect.
[0044] In a fifth aspect, the present application provides a computer-readable storage medium, which stores a computer program. When the computer program runs on a processor, the processor executes the method described in any possible implementation of the first aspect or the second aspect.
[0045] In a sixth aspect, the present application provides a computer program product. When the computer program product runs on a processor, the processor executes the method described in any possible implementation of the first aspect or the second aspect.
[0046] It can be understood that the beneficial effects of the third to sixth aspects mentioned above can be found in the relevant descriptions in the first or second aspect mentioned above, and will not be repeated here.
[0047] In general, the above technical solutions conceived by this application have the following beneficial effects compared with the prior art:
[0048] The present application provides a partition table creation and partition automatic expansion method based on a distributed database. On the basis of creating a table through distributed transactions in a distributed database, the concept of GOID global object identification is proposed. GTM, as a global transaction manager, allocates a global GOID to the partitions of the partition table, and GTM is used to store and manage the partition information. The partition information of the partition table is stored in a globally consistent manner in a distributed architecture by combining GOID with GTM to store partition information, thereby ensuring the accuracy of SQL execution and the high performance of CN, and thus ensuring the global consistency of partition information for newly added partitions. BRIEF DESCRIPTION OF THE DRAWINGS
[0049] Figure 1 It is a flowchart of a method for creating a partition table based on a distributed database provided in an embodiment of the present application;
[0050] Figure 2 It is a flowchart of the partition automatic expansion method provided in an embodiment of the present application;
[0051] Figure 3 It is a flow chart of the distributed database provided by the prior art to realize automatic creation of partitions;
[0052] Figure 4 This is a flowchart of a client connecting to a CN to send an SQL statement for establishing a partition table, provided in an embodiment of the present application;
[0053] Figure 5 This is an example diagram of the correspondence between the GOID of the system table on the CN and the partition information on the GTM provided in the embodiment of the present application;
[0054] Figure 6 This is a flow chart of a distributed database implementing automatic creation of partitions provided by an embodiment of the present application;
[0055] Figure 7 is a schematic diagram of a distributed database provided in an embodiment of the present application;
[0056] Figure 8 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0057] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0058] In the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.
[0059] In the description of the embodiments of the present application, unless otherwise specified, "multiple" means two or more than two. For example, multiple data nodes refer to two or more than two data nodes, etc.
[0060] First, the technical terms involved in the embodiments of the present application are introduced.
[0061] Partition (P): partition. Divide the data of a table into multiple blocks. Logically, it is just one table, but the underlying layer is composed of multiple physical block files. The partition auto-increment described in this application mainly refers to the time type range partition. The range partition mainly consists of three parts: name, start value, and end value.
[0062] For example: partition name p1, start value 2024-01-01, end value 2024-02-01.
[0063] Datanode: A data node in a distributed database. Each data node independently manages a data shard. In a distributed database system, there are usually multiple data nodes. The collection of data managed by these data nodes constitutes the data set managed by the database system.
[0064] Coordinator: the coordination node of the distributed database. As the SQL access layer, this node responds to various requests sent by the client; it stores the metadata information of the table internally and sends the SQL sent by the user to the corresponding DN node.
[0065] GTM: The Global Transaction Manager assigns global transaction numbers to distributed database transactions and provides globally consistent snapshots.
[0066] DDL: Data definition language.
[0067] CN:abbreviation for coordinator.
[0068] DN: abbreviation for datanode.
[0069] OID: Object identifier, used to uniquely identify a database object. Different data objects of the same type (tables, indexes, etc.) must have different OIDs. In the classic PGXC distributed architecture, the OIDs of each CN and DN node are independently assigned and have no necessary connection.
[0070] GOID: The global object identifier proposed in this application is globally unique and increments compared to the traditional OID. CN and DN share GOID. The purpose of GOID is to achieve global consistent storage of certain metadata information in a distributed environment.
[0071] System table: A table that stores metadata information of user tables, such as column metadata and partition metadata.
[0072] PGXC: An open source distributed database based on the Postgres open source relational database. PGXC mainly includes three roles: CN, DN, and GTM.
[0073] Next, the technical solutions provided in the embodiments of the present application are introduced.
[0074] If you directly apply the centralized implementation, there will be the following problems:
[0075] 1. In a distributed database, both the coordination node and the data nodes store the metadata of the table. The data nodes automatically perform partition addition, which may lead to inconsistencies in the partition information between the CN and the DN. If this occurs, it may result in an incorrect execution plan generated on the CN, leading to problems with the SQL execution results.
[0076] 2. The data in a distributed database is distributed to different data nodes according to a certain strategy, and the data volumes of different data nodes are different. If the DN automatically partitions according to the inserted data, then the partitions of each data node will be different. How to record the different partitions on each node? If the CN independently records a copy of the partition information of each node, the metadata on the CN will expand with the number of DNs, resulting in a decline in the performance of the CN.
[0077] To address the above issues, in a distributed database, the industry mainly uses scheduled tasks to implement automatic partitioning. Traditional distributed databases (taking Doris as an example) usually deploy multiple data nodes (datanode) and multiple coordination nodes (coordinator). The coordination node serves as the access layer for clients and provides services externally. When a client connects to the coordination node to create a partitioned table, the coordination node automatically creates a scheduled task that runs at a certain time interval for this table. Each time this scheduled task is executed, it checks the insertion situation of the data in the partitions of the table to determine whether a new partition needs to be created. The specific process is as follows:
[0078] 1. Obtain the partition information of the table from the coordination node;
[0079] 2. Obtain the largest partition Pn of the table;
[0080] 3. Query the distribution of the table in the current partition from the data nodes and obtain the partition Pm where data has been inserted currently;
[0081] 4. If n - m < k, then automatically create a new partition (k is the threshold for creating a partition).
[0082] The method of implementing automatic creation of new partitions based on scheduled tasks mainly has the following problems:
[0083] 1. It is necessary to introduce a scheduled task outside the database engine to complete the creation of partitions, increasing the system complexity and the cost of operation and maintenance;
[0084] 2. When the inserted partition keys are not in ascending order, it cannot ensure the normal insertion of data. For example, if a value with a particularly large partition key is inserted at this time, the insertion will必然 fail.
[0085] In this regard, this application proposes the concept of GOID based on the traditional PGXC distributed database that creates tables through distributed transactions. GTM, as a global transaction manager, allocates a global GOID and stores partition information. The GOID+GTM method of storing partition information is used to achieve global consistency storage of partition information of the partition table under a distributed architecture, so that automatic creation of partitions can be achieved only through the SQL engine itself without relying on external scheduled tasks.
[0086] The embodiment of the present application provides a method for creating a partition table based on a distributed database, wherein the distributed database includes a coordination node CN, multiple data nodes DN and a global transaction manager GTM;
[0087] Figure 1 is a flow chart of a method for creating a partition table based on a distributed database provided in an embodiment of the present application, such as Figure 1 As shown, the method for creating a partition table includes the following steps:
[0088] Step S101, the CN receives an SQL statement for establishing a partition table sent by a client, and sends partition information of the partition table to the GTM based on the SQL statement for establishing the partition table;
[0089] Step S102, GTM allocates a corresponding GOID to the partition table based on the received partition information of the partition table, performs local persistent storage and management on the GOID and the partition information of the corresponding partition table to ensure the global consistency of the partition information, and returns the GOID to CN; the GOID is an identifier of the partition table shared by CN, DN and GTM;
[0090] Step S103, after receiving the GOID, the CN sends the GOID and the SQL statement for establishing the partition table to each DN;
[0091] Step S104, after receiving the GOID and the SQL statement, each DN requests the GTM for the partition information corresponding to the GOID, creates a partition table based on the partition information corresponding to the GOID and the SQL statement returned by the GTM, and stores the GOID locally at the same time;
[0092] Step S105: After each DN establishes a partition table, the CN establishes a partition table based on the partition information corresponding to the GOID and stores the GOID locally at the same time.
[0093] It should be noted that the SQL statement for establishing the partition table sent by the CN to each DN in step S103 is generated by equivalently converting the SQL statement sent by the client by the CN, and is used to establish the partition table for each DN. In addition, after establishing the partition table, each DN can return the execution result to the CN. After receiving the execution result of each DN, the CN can perform subsequent partition table establishment operations.
[0094] In traditional distributed databases, the CN node creates a partition table and broadcasts it to other CNs and DNs. Other nodes will generate OIDs for this partition table separately. The OIDs of each node are different. When insert is executed, DN finds that the partition does not exist when inserting data, and creates a partition, which will cause the local partition and CN partition to be different. In this application, the partition storage and modification are uniformly performed by GTM. If GTM modifies, CN can also obtain it, because CN, DN and GTM all use the same GOID, and the partition information corresponding to the GOID can be directly queried to ensure the global consistency of the partition information. Optionally, the partition is specifically for the range partition of the time type.
[0095] When creating a partition table, the embodiment of the present application sets the initial partition to improve the success rate and efficiency of subsequent data insertion. In addition, the embodiment of the present application separates the partition information of the partition table from the metadata on the CN and DN and stores it in the GTM. As a global transaction manager, the GTM can ensure the consistency of the partitions. Only the GOID of the partition and the cache of the partition are stored on the CN and DN. This ensures the global uniqueness of the partition information, laying a foundation for the global consistency of the partition information when adding new partitions later.
[0096] The method provided in the embodiment of the present application proposes the concept of GOID global object identification based on the creation of tables through distributed transactions in a distributed database. GTM, as a global transaction manager, allocates global GOIDs to partitions of the partition table, and GTM is used to store and manage partition information. The partition information of the partition table is stored in a globally consistent manner in a distributed architecture by combining GOID with GTM to store partition information, thereby ensuring the accuracy of SQL execution and the high performance of CN, and laying a foundation for ensuring the global consistency of partition information for subsequent newly added partitions.
[0097] Based on the above embodiment, in step S101, after receiving the SQL statement for establishing a partition table sent by the client, the CN first requests the global transaction number GXID from the GTM, and then performs subsequent operations after receiving the global transaction number returned by the GTM, so as to globally identify and track the transaction.
[0098] Based on any of the above embodiments, the CN specifically stores the GOID locally by storing the correspondence between the GOID and the OID in a system table; the OID is an identifier of the partition table independently allocated and used by the CN, so that the CN can obtain the column information of the corresponding partition table based on the OID.
[0099] It should be noted that the way DN stores GOID locally is similar to CN, which is also the correspondence between the stored GOID and the OID generated independently by DN.
[0100] It should be noted that the partition table information includes the partition table column information and partition information. Once the partition table column is created, the DN will not be modified, only the CN will be modified, and then the CN will broadcast the modification to the DN, so there will be no inconsistency problem; while the partition DN may be modified, but the CN will not perceive it, which will cause inconsistency problems. Therefore, the partition table identifier GOID shared by CN, DN and GTM is required to ensure the global consistency of partition information.
[0101] Based on any of the above embodiments, step S104 also includes each DN locally caching the partition information corresponding to the GOID; step S105 also includes CN locally caching the partition information corresponding to the GOID.
[0102] It should be noted that, considering that if CN or DN goes to GTM to check partition information every time, the performance will be slow, in order to solve this problem, in addition to storing the GOID of the partition table, CN and DN in the embodiment of the present application can also cache the partition information locally.
[0103] Based on any of the above embodiments, the GTM specifically uses a key-value storage (K, V structure) method to store the GOID and the partition information of the corresponding partition table, wherein the GOID is the keyword K, and the partition information of the partition table is the value V corresponding to the keyword.
[0104] Based on any of the above embodiments, in order to improve the reliability and stability of the distributed database, the distributed database described in the embodiments of the present application specifically adopts a PGXC (Postgresql-XC) distributed database.
[0105] Based on any of the above embodiments, the present application embodiment provides a partition automatic expansion method based on a distributed database. Figure 2 is a flow chart of the partition automatic expansion method provided in the embodiment of the present application, such as Figure 2 As shown, the distributed database includes a coordination node CN, multiple data nodes DN and a global transaction manager GTM;
[0106] The partition automatic expansion method comprises the following steps:
[0107] Step S201, the CN receives a first SQL statement for inserting data sent by the client, converts the first SQL statement into a second SQL statement corresponding to the DN, and sends the second SQL statement to the DN corresponding to the data;
[0108] Step S202: the DN corresponding to the data parses the received second SQL statement, searches for the corresponding partition according to the value of the data, and if not found, obtains the GOID of the partition table corresponding to the data and the partition information corresponding to the GOID, determines the information of the newly added partition according to the partition information and the data, and encapsulates the GOID and the information of the newly added partition into a message and sends it to the GTM; the partition table is established based on the method for creating a partition table based on a distributed database described in any of the above embodiments;
[0109] Step S203: After receiving the message, the GTM modifies the partition information corresponding to the GOID in the local persistent storage, and sends the GOID and the modified partition information corresponding to the GOID to the CN and each DN, so that the CN and each DN update the partition information corresponding to the GOID.
[0110] Step S204: After the DN corresponding to the data updates the partition information corresponding to the GOID, the data is inserted into the newly added partition.
[0111] It is understandable that CN can decide which DN to insert the data into based on the distribution key, and then DN decides which partition to insert the data into based on the partition key. The first SQL statement received by CN is sent by the client, and CN needs to perform an equivalent conversion on SQL to obtain the second SQL statement executed by DN, and then send it to DN. The first SQL statement and the second SQL statement can be executed by the SQL engines on CN and DN respectively. The process of the DN corresponding to the data updating the partition information corresponding to the GOID realizes the automatic expansion of the partition of the partition table corresponding to the GOID.
[0112] It should be noted that when a scheduled task is used to automatically create new partitions, partitions will be created at intervals regardless of whether there is currently any data to be inserted. Therefore, more partitions than the actual number will be created. In the embodiment of the present application, a new partition will only be created when data needs to be inserted and the partition corresponding to the data does not exist, thus avoiding waste of resources.
[0113] In addition, the method of automatically creating new partitions using a scheduled task cannot guarantee normal data insertion if the inserted partition keys are not in order. For example, if a particularly large partition key value is inserted, the insertion will inevitably fail. In the embodiment of the present application, the automatic creation of partitions does not depend on the order of the partition keys of the inserted data. For example, if there are only partitions 1-3 and you want to insert data from partition 10, create new partitions 4-10.
[0114] The method provided in the embodiment of the present application, when executing the INSERT statement through the DN node, if it is found that the distribution does not exist, the partition that needs to be added is calculated, and the GOID of the partition and the partition information that needs to be added are sent to the GTM. The GTM modifies the partition information according to the GOID and broadcasts it to other CNs and DNs to update the local cache, thereby further ensuring the global consistency of the partition; and, the mechanism of automatic creation of partitions based on GOID of the present application is completely embedded in the distributed execution framework, and its correctness is guaranteed by distributed transactions. As long as the data is inserted successfully, the partition will be successfully expanded; it does not rely on the scheduled tasks of a third party for scheduling, thereby reducing the complexity of the system and the complexity of operation and maintenance; the automatic creation of partitions does not depend on the order of the partition keys of the inserted data, thereby ensuring the success of data insertion.
[0115] Based on any of the above embodiments, the partition information includes the interval of partition self-increment, and the name, start value and end value of each partition;
[0116] The information of the newly added partition includes the number, name, start value, and end value of the newly added partition.
[0117] It is understandable that the number of newly added partitions is one or more, and the specific calculation needs to be performed based on the partition information and the data to be inserted. Considering the situation where a particularly large value of the partition key will be inserted, if the embodiment of the present application calculates that a newly added partition still cannot cover the value of the data to be inserted, the information of the next newly added partition can be calculated to ensure the success of inserting the data.
[0118] The names of the partitions may be incremented in sequence, such as p_sys1, p_sys2, and so on.
[0119] Based on any of the above embodiments, in step S201, after receiving the first SQL statement for inserting data sent by the client, the CN may first request the global transaction number GXID from the GTM, and then perform subsequent operations after receiving the global transaction number returned by the GTM, so as to globally identify and track the transaction.
[0120] Based on any of the above embodiments, the process of the present application is divided into two stages: stage one: globally consistent storage of partition information of the partition table under the PGXC distributed architecture; stage two: the SQL engine automatically increments the partition when the user performs an insert operation.
[0121] The process of phase 1 is as follows, of which 3-7 are proposed and implemented by this application, and the other processes are consistent with the process of creating a table under the classic PGXC distributed architecture. In this process, this application proposes the concept of GOID global object identifier, assigns GOID to the partition of the partition table, and obtains GOID from GTM application. CN and DN no longer store the partition information of the partition table. The partition information is only stored on GTM. CN and DN only store the GOID of the partition and the cache of the partition information corresponding to the GOID.
[0122] 1. The client sends the SQL statement for creating a partition table to the CN node;
[0123] 2. The CN node requests the global transaction number from GTM;
[0124] 3. The CN node sends the partition information to the GTM to request the allocation of a GOID;
[0125] 4. GTM assigns a globally unique and increasing integer GOID to the partition, associates the GOID with the partition, stores it in memory and persists it to the local disk, and returns the GOID and the corresponding partition information to the CN node;
[0126] 5. After receiving the partition GOID returned by GTM, the CN node sends the SQL for creating the partition table and the partition GOID after equivalent conversion to each DN node;
[0127] 6. Each DN node creates a partition table based on SQL and GOID, and returns the execution result to CN;
[0128] 7. The CN node creates a partition table and stores the GOID in the local metadata.
[0129] The process of phase 2 is as follows, of which 4), 5), and 6) are proposed and implemented in this application, and the other processes are consistent with the process of executing INSERT statements under the classic PGXC architecture. In this process, when the DN node executes the INSERT statement, if it finds that the distribution does not exist, it calculates the partition that needs to be added, and sends the GOID and partition information of the partition to the GTM. The GTM modifies the partition information according to the GOID and broadcasts it to other CNs and DNs to update the local cache.
[0130] 1) The client sends an insert SQL statement to the CN node;
[0131] 2) The CN node parses the SQL and sends it to the corresponding DN node;
[0132] 3) The DN node SQL engine executes SQL statements;
[0133] 4) The DN node finds the corresponding partition according to the value of the partition column of the inserted data. If it does not exist, the global GOID of the partition is obtained from the metadata of the partition table. The startValue, endValue, and partition name of the next partition are calculated according to the interval value of the partition table (the names of the partitions automatically created by the system are generally p_sys1, p_sys2, etc.). If a newly added partition still cannot include the value of the inserted data, the startValue, endValue, and partition name of the next partition are calculated. The GOID, startValue, endValue, and partition name are encapsulated into a message and sent to the GTM.
[0134] 5) GTM modifies the partition information in the local metadata cache according to the GOID and persists it to the disk, broadcasts the GOID and the corresponding partition information to other CN and DN nodes, and updates the cache of the partition information corresponding to the GOID;
[0135] 6) The DN node inserts data into the new partition;
[0136] 7) The CN node returns an INSERT execution success message to the client.
[0137] Based on any of the above embodiments, Figure 3 It is a flow chart of the distributed database provided by the prior art to realize automatic creation of partitions, such as Figure 3 As shown, a scheduled task is used to automatically query the partition information and data distribution of the table to determine whether to create a new partition.
[0138] Figure 4 This is a flowchart of the client connecting to the CN to send SQL to establish a partition table provided by the embodiment of the present application, such as Figure 4 As shown in the figure, GTM opens a K, V structure storage space to store partition information, where K is the partition GOID and V is the partition information. A table partition corresponds to a unique GOID. Once the partition table is created successfully, the partition GOID assigned by GTM cannot be changed, realizing global consistent storage of partitions. Table 1 shows the correspondence between GOID and Partition of two partition tables.
[0139] Table 1 Correspondence between GOID and Partition in the partition table
[0140]
[0141] Figure 5This is an example diagram of the correspondence between the GOID of the system table on the CN and the partition information on the GTM provided in the embodiment of the present application, such as Figure 5 As shown, each cylindrical border in CN represents a row of records in the system table of CN, and each cylindrical border in GTM represents a partition record in GTM. From the system table records of CN, we can see that there are three tables, whose OIDs are 1, 2, and 3. The table with OID=1 and GOID=0 indicates that the table has no partition information and is a non-partitioned table; the table with OID=2 and GOID=4 indicates that the table is a partitioned table, and its partition information corresponds to the partition record with GOID=4 on GTM; the table with OID=3 and GOID=5 indicates that the table is a partitioned table, and its partition information corresponds to the partition record with GOID=4 on GTM.
[0142] Figure 6 : is a flowchart of the distributed database provided by the embodiment of the present application to realize automatic creation of partitions, such as Figure 6 As shown in the figure, after the partition information is stored globally consistently, once CN sends the INSERT statement to DN, if DN finds that the partition does not exist, it only needs to calculate the new partition information and send it to GTM together with the partition's GOID. GTM updates the local cache and persists the partition information to the local disk, and broadcasts it to other nodes to update the partition information corresponding to the GOID in their partition cache.
[0143] Based on any of the above embodiments, the following is a specific implementation case, which is divided into two situations. One is the implementation steps when establishing a partition table, and the other is the implementation process of the client connecting to the CN to execute the INSERT statement to trigger the automatic creation of partitions.
[0144] 1. Implementation steps for creating a partition table
[0145] 1. The client connects to the CN node;
[0146] 2. The client sends a SQL statement to create a partition table to the CN. The SQL statement contains the interval for partition auto-increment.
[0147] 3. The CN node sends a message to the GTM requesting to register the partition;
[0148] 4. GTM uses the local maximum GOID value as the partition's GOID, refreshes the memory cache of the partition information and persists it to the local disk, and then returns the GOID to the CN. Here, the local maximum GOID value is the value of the last allocated GOID plus one.
[0149] 5.CN sends the GOID and the equivalently converted table creation SQL to each DN node;
[0150] 6. Each DN requests the partition information corresponding to the GOID from GTM to create a partition table locally, write the GOID into the system table, update the local GOID and partition information cache, and return success to CN;
[0151] 7. CN creates a partition table locally and stores the GOID in the OID field of the partition system table;
[0152] 8. CN returns success to the client.
[0153] 2. Implementation steps for the client to execute SQL statements to trigger automatic partition creation
[0154] 1. The client sends an INSERT SQL statement to CN;
[0155] 2. The coordination node requests the global transaction number from GTM and sends the converted SQL statement to DN;
[0156] 3. When DN executes SQL, if it is found that the partition where the inserted data is located does not exist, that is, a new partition needs to be created, a message is sent to GTM to request the creation of the partition. The message contains the GOID of the partition and the partition information;
[0157] 4. After receiving the message, GTM parses the GOID value and retrieves the partition information corresponding to the GOID from the local partition cache. If the partition corresponding to the GOID is not found in the cache, it returns to the CN that the GOID is an invalid value. Otherwise, it modifies the local partition, returns to the DN that the partition is successfully created, and broadcasts the GOID to other CN and DN nodes, notifying these nodes to modify the partition corresponding to the GOID.
[0158] 5. DN inserts data into the partition and returns success to CN;
[0159] 6.CN returns success to the client.
[0160] Based on any of the above embodiments, the present application provides a distributed database. Figure 7 is a schematic diagram of a distributed database provided in an embodiment of the present application, such as Figure 7 As shown, the distributed database includes a global transaction manager GTM710, a coordination node CN720 and multiple data nodes DN730, wherein:
[0161] CN720 is used to receive the SQL statement for establishing the partition table sent by the client, and send the partition information of the partition table to GTM710 based on the SQL statement for establishing the partition table;
[0162] GTM710 is used to assign a corresponding GOID to the partition table based on the partition information of the received partition table, locally persistently store and manage the GOID and the partition information of the corresponding partition table to ensure the global consistency of the partition information, and return the GOID to CN720; the GOID is an identifier of the partition table shared by CN720, DN730 and GTM710;
[0163] After receiving the GOID, CN720 sends the GOID and the SQL statement for establishing the partition table to each DN730;
[0164] After receiving the GOID and SQL statement, each DN 730 requests the partition information corresponding to the GOID from the GTM 710, establishes a partition table based on the partition information corresponding to the GOID and the SQL statement returned by the GTM 710, and stores the GOID locally at the same time;
[0165] After each DN730 establishes a partition table, CN720 is used to establish a partition table based on the partition information corresponding to the GOID and store the GOID locally at the same time.
[0166] Based on any of the above embodiments, CN720 is further used to receive a first SQL statement for inserting data sent by a client, convert the first SQL statement into a second SQL statement corresponding to DN730, and send the second SQL statement to DN730 corresponding to the data;
[0167] The DN 730 corresponding to the data is also used to parse the received second SQL statement, search for the corresponding partition according to the value of the data, and if not found, obtain the GOID of the partition table corresponding to the data and the partition information corresponding to the GOID, determine the information of the newly added partition according to the partition information and the data, and encapsulate the GOID and the information of the newly added partition into a message and send it to the GTM 710;
[0168] GTM710 is also used to modify the partition information corresponding to the GOID in the local persistent storage after receiving the message, and send the GOID and the partition information corresponding to the modified GOID to CN720 and each DN730, so that CN720 and each DN730 update the partition information corresponding to the GOID;
[0169] The DN730 corresponding to the data is also used to insert data into the newly added partition after updating the partition information corresponding to the GOID.
[0170] It should be understood that the implementation principles and technical effects of GTM, CN and DN in the above distributed database are similar to those described in the above method and will not be repeated here.
[0171] Based on the method in the above embodiment, an embodiment of the present application provides an electronic device. Figure 8 is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application, such as Figure 8 As shown, the electronic device may include: a processor 810, a communication interface 820, a memory 830 and a communication bus 840, wherein the processor 810, the communication interface 820 and the memory 830 communicate with each other through the communication bus 840. The processor 810 may call the logic instructions in the memory 830 to execute the method in the above embodiment.
[0172] In addition, the logic instructions in the above-mentioned memory 830 can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application.
[0173] Based on the method in the above embodiment, an embodiment of the present application provides a computer-readable storage medium, which stores a computer program. When the computer program runs on a processor, the processor executes the method in the above embodiment.
[0174] Based on the method in the above embodiment, an embodiment of the present application provides a computer program product. When the computer program product runs on a processor, the processor executes the method in the above embodiment.
[0175] It is understandable that the processor in the embodiment of the present application may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), field programmable gate arrays (FPGA) or other programmable logic devices, transistor logic devices, hardware components or any combination thereof. The general-purpose processor may be a microprocessor or any conventional processor.
[0176] The method steps in the embodiments of the present application can be implemented by hardware or by a processor executing software instructions. The software instructions can be composed of corresponding software modules, and the software modules can be stored in random access memory (RAM), flash memory, read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), registers, hard disks, mobile hard disks, CD-ROMs, or any other form of storage medium known in the art. An exemplary storage medium is coupled to the processor so that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can be located in an ASIC.
[0177] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium or transmitted through the computer-readable storage medium. The computer instructions may be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid state disk (SSD)), etc.
[0178] It should be understood that the various numerical numbers involved in the embodiments of the present application are only used for the convenience of description and are not used to limit the scope of the embodiments of the present application.
[0179] It will be easily understood by those skilled in the art that the above description is only a preferred embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present application shall be included in the scope of protection of the present application.
Claims
1. A method for creating a partition table based on a distributed database. It is characterized in that The distributed database includes a coordination node CN, multiple data nodes DN and a global transaction manager GTM; The method for creating the partition table comprises the following steps: Step S101, the CN receives an SQL statement for establishing a partition table sent by a client, and sends partition information of the partition table to the GTM based on the SQL statement for establishing the partition table; Step S102, GTM allocates a corresponding GOID to the partition table based on the received partition information of the partition table, performs local persistent storage and management on the GOID and the partition information of the corresponding partition table to ensure the global consistency of the partition information, and returns the GOID to CN; the GOID is an identifier of the partition table shared by CN, DN and GTM; Step S103, after receiving the GOID, the CN sends the GOID and the SQL statement for establishing the partition table to each DN; Step S104, after receiving the GOID and the SQL statement, each DN requests the GTM for the partition information corresponding to the GOID, establishes a partition table based on the partition information corresponding to the GOID and the SQL statement returned by the GTM, and stores the GOID locally at the same time, and the DN stores the GOID locally by locally storing the corresponding relationship between the GOID and the OID; Step S105, after each DN establishes a partition table, the CN establishes a partition table based on the partition information corresponding to the GOID and stores the GOID locally at the same time. The CN specifically stores the GOID locally by storing the corresponding relationship between the GOID and the OID in the system table.
2. The method for creating a partition table according to claim 1, It is characterized in that The OID is an identifier of the partition table independently allocated and used by the CN, so that the CN can obtain column information of the corresponding partition table based on the OID.
3. The method for creating a partition table according to claim 1, It is characterized in that The GTM specifically stores the GOID and the partition information of the corresponding partition table in a key-value storage manner, wherein the GOID is a keyword and the partition information of the partition table is a value corresponding to the keyword.
4. The method for creating a partition table according to claim 1, It is characterized in that Step S104 also includes each DN locally caching the partition information corresponding to the GOID; step S105 also includes CN locally caching the partition information corresponding to the GOID.
5. The method for creating a partition table according to claim 1, It is characterized in that The distributed database specifically adopts the PGXC distributed database.
6. A partition automatic expansion method based on distributed database, It is characterized in that The distributed database includes a coordination node CN, multiple data nodes DN and a global transaction manager GTM; The partition automatic expansion method comprises the following steps: Step S201, the CN receives a first SQL statement for inserting data sent by the client, converts the first SQL statement into a second SQL statement corresponding to the DN, and sends the second SQL statement to the DN corresponding to the data; Step S202: the DN corresponding to the data parses the received second SQL statement, searches for the corresponding partition according to the value of the data, and if not found, obtains the GOID of the partition table corresponding to the data and the partition information corresponding to the GOID, determines the information of the newly added partition according to the partition information and the data, and encapsulates the GOID and the information of the newly added partition into a message and sends it to the GTM; the partition table is established based on the method for creating a partition table based on a distributed database as described in any one of claims 1 to 5; Step S203: After receiving the message, the GTM modifies the partition information corresponding to the GOID in the local persistent storage, and sends the GOID and the modified partition information corresponding to the GOID to the CN and each DN, so that the CN and each DN update the partition information corresponding to the GOID. Step S204: After the DN corresponding to the data updates the partition information corresponding to the GOID, the data is inserted into the newly added partition.
7. The partition automatic expansion method according to claim 6, It is characterized in that Partition information includes the interval of partition increment, as well as the name, start value and end value of each partition; The information of the newly added partition includes the number, name, start value, and end value of the newly added partition.
8. A distributed database, It is characterized in that It includes a coordination node CN, multiple data nodes DN and a global transaction manager GTM, where: The CN is used to receive the SQL statement for establishing the partition table sent by the client, and send the partition information of the partition table to the GTM based on the SQL statement for establishing the partition table; The GTM is used to allocate a corresponding GOID to the partition table based on the partition information of the received partition table, locally persistently store and manage the GOID and the partition information of the corresponding partition table to ensure the global consistency of the partition information, and return the GOID to the CN; the GOID is an identifier of the partition table shared by the CN, DN and GTM; After receiving the GOID, CN sends the GOID and the SQL statement for establishing the partition table to each DN; After receiving the GOID and the SQL statement, each DN is used to request the partition information corresponding to the GOID from the GTM, establish a partition table based on the partition information corresponding to the GOID and the SQL statement returned by the GTM, and store the GOID locally at the same time. The DN stores the GOID locally by locally storing the corresponding relationship between the GOID and the OID; After each DN establishes a partition table, the CN is used to establish a partition table based on the partition information corresponding to the GOID and to store the GOID locally at the same time. The CN specifically stores the GOID locally by storing the corresponding relationship between the GOID and the OID in the system table.
9. The distributed database according to claim 8, It is characterized in that The CN is also used to receive a first SQL statement for inserting data sent by the client, convert the first SQL statement into a second SQL statement corresponding to the DN, and send the second SQL statement to the DN corresponding to the data; The DN corresponding to the data is also used to parse the received second SQL statement, and search for the corresponding partition according to the value of the data. If the partition is not found, the GOID of the partition table corresponding to the data and the partition information corresponding to the GOID are obtained, and the information of the newly added partition is determined according to the partition information and the data, and the GOID and the information of the newly added partition are encapsulated as a message and sent to the GTM; The GTM is also used to modify the partition information corresponding to the GOID in the local persistent storage after receiving the message, and send the GOID and the partition information corresponding to the modified GOID to the CN and each DN, so that the CN and each DN update the partition information corresponding to the GOID; The DN corresponding to the data is also used to insert data into the newly added partition after updating the partition information corresponding to the GOID.
10. An electronic device, It is characterized in that include: at least one memory for storing a computer program; At least one processor is used to execute the program stored in the memory. When the program stored in the memory is executed, the processor is used to execute the partition table creation method as described in any one of claims 1-5, or execute the partition automatic expansion method as described in any one of claims 6-7.
