mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Logical replication support for initial data copy
Add functionality for a new subscription to copy the initial data in the tables and then sync with the ongoing apply process. For the copying, add a new internal COPY option to have the COPY source data provided by a callback function. The initial data copy works on the subscriber by receiving COPY data from the publisher and then providing it locally into a COPY that writes to the destination table. A WAL receiver can now execute full SQL commands. This is used here to obtain information about tables and publications. Several new options were added to CREATE and ALTER SUBSCRIPTION to control whether and when initial table syncing happens. Change pg_dump option --no-create-subscription-slots to --no-subscription-connect and use the new CREATE SUBSCRIPTION ... NOCONNECT option for that. Author: Petr Jelinek <petr.jelinek@2ndquadrant.com> Tested-by: Erik Rijkers <er@xs4all.nl>
This commit is contained in:
@ -499,51 +499,32 @@ SnapBuildBuildSnapshot(SnapBuild *builder, TransactionId xid)
|
||||
}
|
||||
|
||||
/*
|
||||
* Export a snapshot so it can be set in another session with SET TRANSACTION
|
||||
* SNAPSHOT.
|
||||
* Build the initial slot snapshot and convert it to normal snapshot that
|
||||
* is understood by HeapTupleSatisfiesMVCC.
|
||||
*
|
||||
* For that we need to start a transaction in the current backend as the
|
||||
* importing side checks whether the source transaction is still open to make
|
||||
* sure the xmin horizon hasn't advanced since then.
|
||||
*
|
||||
* After that we convert a locally built snapshot into the normal variant
|
||||
* understood by HeapTupleSatisfiesMVCC et al.
|
||||
* The snapshot will be usable directly in current transaction or exported
|
||||
* for loading in different transaction.
|
||||
*/
|
||||
const char *
|
||||
SnapBuildExportSnapshot(SnapBuild *builder)
|
||||
Snapshot
|
||||
SnapBuildInitalSnapshot(SnapBuild *builder)
|
||||
{
|
||||
Snapshot snap;
|
||||
char *snapname;
|
||||
TransactionId xid;
|
||||
TransactionId *newxip;
|
||||
int newxcnt = 0;
|
||||
|
||||
Assert(!FirstSnapshotSet);
|
||||
Assert(XactIsoLevel = XACT_REPEATABLE_READ);
|
||||
|
||||
if (builder->state != SNAPBUILD_CONSISTENT)
|
||||
elog(ERROR, "cannot export a snapshot before reaching a consistent state");
|
||||
elog(ERROR, "cannot build an initial slot snapshot before reaching a consistent state");
|
||||
|
||||
if (!builder->committed.includes_all_transactions)
|
||||
elog(ERROR, "cannot export a snapshot, not all transactions are monitored anymore");
|
||||
elog(ERROR, "cannot build an initial slot snapshot, not all transactions are monitored anymore");
|
||||
|
||||
/* so we don't overwrite the existing value */
|
||||
if (TransactionIdIsValid(MyPgXact->xmin))
|
||||
elog(ERROR, "cannot export a snapshot when MyPgXact->xmin already is valid");
|
||||
|
||||
if (IsTransactionOrTransactionBlock())
|
||||
elog(ERROR, "cannot export a snapshot from within a transaction");
|
||||
|
||||
if (SavedResourceOwnerDuringExport)
|
||||
elog(ERROR, "can only export one snapshot at a time");
|
||||
|
||||
SavedResourceOwnerDuringExport = CurrentResourceOwner;
|
||||
ExportInProgress = true;
|
||||
|
||||
StartTransactionCommand();
|
||||
|
||||
Assert(!FirstSnapshotSet);
|
||||
|
||||
/* There doesn't seem to a nice API to set these */
|
||||
XactIsoLevel = XACT_REPEATABLE_READ;
|
||||
XactReadOnly = true;
|
||||
elog(ERROR, "cannot build an initial slot snapshot when MyPgXact->xmin already is valid");
|
||||
|
||||
snap = SnapBuildBuildSnapshot(builder, GetTopTransactionId());
|
||||
|
||||
@ -578,7 +559,9 @@ SnapBuildExportSnapshot(SnapBuild *builder)
|
||||
if (test == NULL)
|
||||
{
|
||||
if (newxcnt >= GetMaxSnapshotXidCount())
|
||||
elog(ERROR, "snapshot too large");
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
|
||||
errmsg("initial slot snapshot too large")));
|
||||
|
||||
newxip[newxcnt++] = xid;
|
||||
}
|
||||
@ -589,9 +572,43 @@ SnapBuildExportSnapshot(SnapBuild *builder)
|
||||
snap->xcnt = newxcnt;
|
||||
snap->xip = newxip;
|
||||
|
||||
return snap;
|
||||
}
|
||||
|
||||
/*
|
||||
* Export a snapshot so it can be set in another session with SET TRANSACTION
|
||||
* SNAPSHOT.
|
||||
*
|
||||
* For that we need to start a transaction in the current backend as the
|
||||
* importing side checks whether the source transaction is still open to make
|
||||
* sure the xmin horizon hasn't advanced since then.
|
||||
*/
|
||||
const char *
|
||||
SnapBuildExportSnapshot(SnapBuild *builder)
|
||||
{
|
||||
Snapshot snap;
|
||||
char *snapname;
|
||||
|
||||
if (IsTransactionOrTransactionBlock())
|
||||
elog(ERROR, "cannot export a snapshot from within a transaction");
|
||||
|
||||
if (SavedResourceOwnerDuringExport)
|
||||
elog(ERROR, "can only export one snapshot at a time");
|
||||
|
||||
SavedResourceOwnerDuringExport = CurrentResourceOwner;
|
||||
ExportInProgress = true;
|
||||
|
||||
StartTransactionCommand();
|
||||
|
||||
/* There doesn't seem to a nice API to set these */
|
||||
XactIsoLevel = XACT_REPEATABLE_READ;
|
||||
XactReadOnly = true;
|
||||
|
||||
snap = SnapBuildInitalSnapshot(builder);
|
||||
|
||||
/*
|
||||
* now that we've built a plain snapshot, use the normal mechanisms for
|
||||
* exporting it
|
||||
* now that we've built a plain snapshot, make it active and use the
|
||||
* normal mechanisms for exporting it
|
||||
*/
|
||||
snapname = ExportSnapshot(snap);
|
||||
|
||||
|
Reference in New Issue
Block a user