static void CheckConnection(void) { /* Be sure that a backend does not use a postmaster connection */ if (IsUnderPostmaster && GTMPQispostmaster(conn) == 1) { InitGTM(); return; } if (GTMPQstatus(conn) != CONNECTION_OK) InitGTM(); }
int PrepareTranGTM(GlobalTransactionId gxid) { int ret; if (!GlobalTransactionIdIsValid(gxid)) return 0; CheckConnection(); #ifdef XCP ret = -1; if (conn) #endif ret = prepare_transaction(conn, gxid); /* * If something went wrong (timeout), try and reset GTM connection. * We will close the transaction locally anyway, and closing GTM will force * it to be closed on GTM. */ if (ret < 0) { CloseGTM(); InitGTM(); #ifdef XCP if (conn) ret = prepare_transaction(conn, gxid); #endif } currentGxid = InvalidGlobalTransactionId; return ret; }
int CommitTranGTM(GlobalTransactionId gxid) { int ret; if (!GlobalTransactionIdIsValid(gxid)) return 0; CheckConnection(); ret = commit_transaction(conn, gxid); /* * If something went wrong (timeout), try and reset GTM connection. * We will close the transaction locally anyway, and closing GTM will force * it to be closed on GTM. */ if (ret < 0) { CloseGTM(); InitGTM(); } /* Close connection in case commit is done by autovacuum worker or launcher */ if (IsAutoVacuumWorkerProcess() || IsAutoVacuumLauncherProcess()) CloseGTM(); return ret; }
/* * UnRegister Given Node * Connection for registering is just used once then closed */ int UnregisterGTM(GTM_PGXCNodeType type) { int ret; CheckConnection(); if (!conn) return EOF; ret = node_unregister(conn, type, PGXCNodeName); /* If something went wrong, retry once */ if (ret < 0) { CloseGTM(); InitGTM(); if (conn) ret = node_unregister(conn, type, PGXCNodeName); } /* * If node is unregistered cleanly, cut the connection. * and Node shuts down smoothly. */ CloseGTM(); return ret; }
int GetGIDDataGTM(char *gid, GlobalTransactionId *gxid, GlobalTransactionId *prepared_gxid, char **nodestring) { int ret = 0; CheckConnection(); ret = get_gid_data(conn, GTM_ISOLATION_RC, gid, gxid, prepared_gxid, nodestring); /* * If something went wrong (timeout), try and reset GTM connection. * We will abort the transaction locally anyway, and closing GTM will force * it to end on GTM. */ if (ret < 0) { CloseGTM(); InitGTM(); } return ret; }
GlobalTransactionId BeginTranGTM(GTM_Timestamp *timestamp) { GlobalTransactionId xid = InvalidGlobalTransactionId; CheckConnection(); // TODO Isolation level if (conn) xid = begin_transaction(conn, GTM_ISOLATION_RC, timestamp); /* If something went wrong (timeout), try and reset GTM connection * and retry. This is safe at the beginning of a transaction. */ if (!TransactionIdIsValid(xid)) { CloseGTM(); InitGTM(); if (conn) xid = begin_transaction(conn, GTM_ISOLATION_RC, timestamp); } #ifdef XCP if (xid) IsXidFromGTM = true; #endif currentGxid = xid; return xid; }
int RollbackTranGTM(GlobalTransactionId gxid) { int ret = -1; if (!GlobalTransactionIdIsValid(gxid)) return 0; CheckConnection(); if (conn) ret = abort_transaction(conn, gxid); /* * If something went wrong (timeout), try and reset GTM connection. * We will abort the transaction locally anyway, and closing GTM will force * it to end on GTM. */ if (ret < 0) { CloseGTM(); InitGTM(); } currentGxid = InvalidGlobalTransactionId; return ret; }
int StartPreparedTranGTM(GlobalTransactionId gxid, char *gid, char *nodestring) { int ret = 0; if (!GlobalTransactionIdIsValid(gxid)) return 0; CheckConnection(); ret = start_prepared_transaction(conn, gxid, gid, nodestring); /* * If something went wrong (timeout), try and reset GTM connection. * We will abort the transaction locally anyway, and closing GTM will force * it to end on GTM. */ if (ret < 0) { CloseGTM(); InitGTM(); } return ret; }
GetNextValGTM(char *seqname) #endif { GTM_Sequence ret = -1; GTM_SequenceKeyData seqkey; #ifdef XCP char *coordName = IS_PGXC_COORDINATOR ? PGXCNodeName : MyCoordName; int coordPid = IS_PGXC_COORDINATOR ? MyProcPid : MyCoordPid; int status; #endif CheckConnection(); seqkey.gsk_keylen = strlen(seqname) + 1; seqkey.gsk_key = seqname; #ifdef XCP if (conn) status = get_next(conn, &seqkey, coordName, coordPid, range, &ret, rangemax); else status = GTM_RESULT_COMM_ERROR; /* retry once */ if (status == GTM_RESULT_COMM_ERROR) { CloseGTM(); InitGTM(); if (conn) status = get_next(conn, &seqkey, coordName, coordPid, range, &ret, rangemax); } if (status != GTM_RESULT_OK) ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("%s", GTMPQerrorMessage(conn)))); #else if (conn) ret = get_next(conn, &seqkey); if (ret < 0) { CloseGTM(); InitGTM(); } #endif return ret; }
GTM_Snapshot GetSnapshotGTM(GlobalTransactionId gxid, bool canbe_grouped) { GTM_Snapshot ret_snapshot = NULL; CheckConnection(); if (conn) ret_snapshot = get_snapshot(conn, gxid, canbe_grouped); if (ret_snapshot == NULL) { CloseGTM(); InitGTM(); } return ret_snapshot; }
/* * Get the next sequence value */ GTM_Sequence GetNextValGTM(char *seqname) { GTM_Sequence ret = -1; GTM_SequenceKeyData seqkey; CheckConnection(); seqkey.gsk_keylen = strlen(seqname) + 1; seqkey.gsk_key = seqname; if (conn) ret = get_next(conn, &seqkey); if (ret < 0) { CloseGTM(); InitGTM(); } return ret; }
/* * Register Given Node * Connection for registering is just used once then closed */ int RegisterGTM(GTM_PGXCNodeType type, GTM_PGXCNodePort port, char *datafolder) { int ret; CheckConnection(); if (!conn) return EOF; ret = node_register(conn, type, port, PGXCNodeName, datafolder); /* If something went wrong, retry once */ if (ret < 0) { CloseGTM(); InitGTM(); if (conn) ret = node_register(conn, type, port, PGXCNodeName, datafolder); } return ret; }
/* * For a prepared transaction, commit the gxid used for PREPARE TRANSACTION * and for COMMIT PREPARED. */ int CommitPreparedTranGTM(GlobalTransactionId gxid, GlobalTransactionId prepared_gxid) { int ret = 0; if (!GlobalTransactionIdIsValid(gxid) || !GlobalTransactionIdIsValid(prepared_gxid)) return ret; CheckConnection(); ret = commit_prepared_transaction(conn, gxid, prepared_gxid); /* * If something went wrong (timeout), try and reset GTM connection. * We will close the transaction locally anyway, and closing GTM will force * it to be closed on GTM. */ if (ret < 0) { CloseGTM(); InitGTM(); } return ret; }
GlobalTransactionId BeginTranAutovacuumGTM(void) { GlobalTransactionId xid = InvalidGlobalTransactionId; CheckConnection(); // TODO Isolation level if (conn) xid = begin_transaction_autovacuum(conn, GTM_ISOLATION_RC); /* * If something went wrong (timeout), try and reset GTM connection and retry. * This is safe at the beginning of a transaction. */ if (!TransactionIdIsValid(xid)) { CloseGTM(); InitGTM(); if (conn) xid = begin_transaction_autovacuum(conn, GTM_ISOLATION_RC); } return xid; }
/* * Get the next sequence value */ GTM_Sequence GetNextValGTM(char *seqname) { GTM_Sequence ret = -1; GTM_SequenceKeyData seqkey; int status; CheckConnection(); seqkey.gsk_keylen = strlen(seqname) + 1; seqkey.gsk_key = seqname; if (conn) status = get_next(conn, &seqkey, &ret); if (status != GTM_RESULT_OK) { CloseGTM(); InitGTM(); ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("%s", GTMPQerrorMessage(conn)))); } return ret; }
int CommitTranGTM(GlobalTransactionId gxid, int waited_xid_count, GlobalTransactionId *waited_xids) { int ret; if (!GlobalTransactionIdIsValid(gxid)) return 0; CheckConnection(); #ifdef XCP ret = -1; if (conn) #endif ret = commit_transaction(conn, gxid, waited_xid_count, waited_xids); /* * If something went wrong (timeout), try and reset GTM connection. * We will close the transaction locally anyway, and closing GTM will force * it to be closed on GTM. */ if (ret < 0) { CloseGTM(); InitGTM(); #ifdef XCP if (conn) ret = commit_transaction(conn, gxid, waited_xid_count, waited_xids); #endif } /* Close connection in case commit is done by autovacuum worker or launcher */ if (IsAutoVacuumWorkerProcess() || IsAutoVacuumLauncherProcess()) CloseGTM(); currentGxid = InvalidGlobalTransactionId; return ret; }
/* * For a prepared transaction, commit the gxid used for PREPARE TRANSACTION * and for COMMIT PREPARED. */ int CommitPreparedTranGTM(GlobalTransactionId gxid, GlobalTransactionId prepared_gxid, int waited_xid_count, GlobalTransactionId *waited_xids) { int ret = 0; if (!GlobalTransactionIdIsValid(gxid) || !GlobalTransactionIdIsValid(prepared_gxid)) return ret; CheckConnection(); #ifdef XCP ret = -1; if (conn) #endif ret = commit_prepared_transaction(conn, gxid, prepared_gxid, waited_xid_count, waited_xids); /* * If something went wrong (timeout), try and reset GTM connection. * We will close the transaction locally anyway, and closing GTM will force * it to be closed on GTM. */ if (ret < 0) { CloseGTM(); InitGTM(); #ifdef XCP if (conn) ret = commit_prepared_transaction(conn, gxid, prepared_gxid, waited_xid_count, waited_xids); #endif } currentGxid = InvalidGlobalTransactionId; return ret; }