Since Postgres-xl is based on Postgres-xc, the data of Postgres-xc is used here to analyze its GTM components. GTM is a key component of Postgres-XC to provide consistent transaction management and tuple visibility control. First, we will introduce how PostgreSQL manages transactions and database updates.
In PostgreSQL, each transaction is assigned a unique ID, called transaction ID (or XID). XIDs are given in ascending order to determine which transaction is older/newer.
When a transaction tries to read a tuple, each tuple has a set of XIDs to indicate the transaction that created and deleted the tuple. Therefore, if the target tuple is created by an active transaction, it will not be committed or aborted, and the read transaction should ignore such tuples. In this way (in practice, this is done by the tqual.c module in the PostgreSQL core), if we give each transaction a unique transaction in the entire system (not only in a single server but in all servers) Id and maintain a snapshot (which transaction is active) , even if the server accepts new statements from other transactions that only run on other servers, we can maintain global consistent visibility of each tuple.
Globalization of transaction management
In Postgres-XC, GTM provides the following functions for transaction management:
- Assign a global XID ( Global Transaction ID GXID, Global Transaction ID) to the transaction . Using GXID, transactions can be globally identified. If a transaction is written to multiple nodes, we can track such writes.
- Provide a snapshot . GTM collects the status of all transactions (running, committed, aborted, etc.) to provide a global snapshot (global snapshot). Please note that the global snapshot includes the GXID given to other servers, as shown in Figure 1.8. Because some older transactions may access the new server, in this case, if the GXID of such a transaction is not included in the snapshot, the transaction may be considered "old enough" and may be read uncommitted Line. If the GXID of such a transaction is included in the snapshot from the beginning, this inconsistency will not occur.
GTM has four roles: Master, Slave, Proxy and client.
The GTM process and the postmaster process in Postgres-xc are independent of each other, have independent binary files, configuration files, log files and pid files, and are started separately from the postmaster. src/include/gtm is the header file of the GTM function, src/gtm/client is the library function of the GTM client, src/gtm/config is the scan function of the configuration file, src/gtm/main is the GTM main program, src/gtm/ Proxy is the main program of GTM Proxy, src/gtm/recovery is the registration function of PGXC node on GTM and Proxy and the tool function of GTM standby global variables, src/gtm/common is the general function of GTM, GTM Proxy, GTM client, src /gtm/libpq is the libpq protocol function, and src/gtm/path is the path processing function.
The GTM source code structure of Postgres-xl is as follows