mirror of
https://github.com/postgres/postgres.git
synced 2025-11-04 20:11:56 +03:00
Allow altering of two_phase option of a SUBSCRIPTION.
The two_phase option is controlled by both the publisher (as a slot option) and the subscriber (as a subscription option), so the slot option must also be modified. Changing the 'two_phase' option for a subscription from 'true' to 'false' is permitted only when there are no pending prepared transactions corresponding to that subscription. Otherwise, the changes of already prepared transactions can be replicated again along with their corresponding commit leading to duplicate data or errors. To avoid data loss, the 'two_phase' option for a subscription can only be changed from 'false' to 'true' once the initial data synchronization is completed. Therefore this is performed later by the logical replication worker. Author: Hayato Kuroda, Ajin Cherian, Amit Kapila Reviewed-by: Peter Smith, Hou Zhijie, Amit Kapila, Vitaly Davydov, Vignesh C Discussion: https://postgr.es/m/8fab8-65d74c80-1-2f28e880@39088166
This commit is contained in:
@@ -2681,3 +2681,82 @@ LookupGXact(const char *gid, XLogRecPtr prepare_end_lsn,
|
||||
LWLockRelease(TwoPhaseStateLock);
|
||||
return found;
|
||||
}
|
||||
|
||||
/*
|
||||
* TwoPhaseTransactionGid
|
||||
* Form the prepared transaction GID for two_phase transactions.
|
||||
*
|
||||
* Return the GID in the supplied buffer.
|
||||
*/
|
||||
void
|
||||
TwoPhaseTransactionGid(Oid subid, TransactionId xid, char *gid_res, int szgid)
|
||||
{
|
||||
Assert(OidIsValid(subid));
|
||||
|
||||
if (!TransactionIdIsValid(xid))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
||||
errmsg_internal("invalid two-phase transaction ID")));
|
||||
|
||||
snprintf(gid_res, szgid, "pg_gid_%u_%u", subid, xid);
|
||||
}
|
||||
|
||||
/*
|
||||
* IsTwoPhaseTransactionGidForSubid
|
||||
* Check whether the given GID (as formed by TwoPhaseTransactionGid) is
|
||||
* for the specified 'subid'.
|
||||
*/
|
||||
static bool
|
||||
IsTwoPhaseTransactionGidForSubid(Oid subid, char *gid)
|
||||
{
|
||||
int ret;
|
||||
Oid subid_from_gid;
|
||||
TransactionId xid_from_gid;
|
||||
char gid_tmp[GIDSIZE];
|
||||
|
||||
/* Extract the subid and xid from the given GID */
|
||||
ret = sscanf(gid, "pg_gid_%u_%u", &subid_from_gid, &xid_from_gid);
|
||||
|
||||
/*
|
||||
* Check that the given GID has expected format, and at least the subid
|
||||
* matches.
|
||||
*/
|
||||
if (ret != 2 || subid != subid_from_gid)
|
||||
return false;
|
||||
|
||||
/*
|
||||
* Reconstruct a temporary GID based on the subid and xid extracted from
|
||||
* the given GID and check whether the temporary GID and the given GID
|
||||
* match.
|
||||
*/
|
||||
TwoPhaseTransactionGid(subid, xid_from_gid, gid_tmp, sizeof(gid_tmp));
|
||||
|
||||
return strcmp(gid, gid_tmp) == 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* LookupGXactBySubid
|
||||
* Check if the prepared transaction done by apply worker exists.
|
||||
*/
|
||||
bool
|
||||
LookupGXactBySubid(Oid subid)
|
||||
{
|
||||
bool found = false;
|
||||
|
||||
LWLockAcquire(TwoPhaseStateLock, LW_SHARED);
|
||||
for (int i = 0; i < TwoPhaseState->numPrepXacts; i++)
|
||||
{
|
||||
GlobalTransaction gxact = TwoPhaseState->prepXacts[i];
|
||||
|
||||
/* Ignore not-yet-valid GIDs. */
|
||||
if (gxact->valid &&
|
||||
IsTwoPhaseTransactionGidForSubid(subid, gxact->gid))
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
LWLockRelease(TwoPhaseStateLock);
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user