Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FREEZE Option to COPY Command and Explicit Transaction Management for Shard Moves #7687

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
Oid relationId = PG_GETARG_OID(0);
uint32_t targetNodeId = PG_GETARG_INT32(1);

if (IsCitusTable(relationId))
{
char *qualifiedRelationName = generate_qualified_relation_name(relationId);
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),

Check warning on line 46 in src/backend/distributed/operations/worker_copy_table_to_node_udf.c

View check run for this annotation

Codecov / codecov/patch

src/backend/distributed/operations/worker_copy_table_to_node_udf.c#L45-L46

Added lines #L45 - L46 were not covered by tests
errmsg("table %s is a Citus table, only copies of "
"shards or regular postgres tables are supported",
qualifiedRelationName)));
}

Oid schemaOid = get_rel_namespace(relationId);
char *relationSchemaName = get_namespace_name(schemaOid);
char *relationName = get_rel_name(relationId);
Expand Down
85 changes: 72 additions & 13 deletions src/backend/distributed/operations/worker_shard_copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
static void LocalCopyToShard(ShardCopyDestReceiver *copyDest, CopyOutState
localCopyOutState);
static void ConnectToRemoteAndStartCopy(ShardCopyDestReceiver *copyDest);
static StringInfo ConstructShardTruncateStatement(
List *destinationShardFullyQualifiedName);


static bool
Expand All @@ -108,12 +110,35 @@
NULL /* database (current) */);
ClaimConnectionExclusively(copyDest->connection);


RemoteTransactionBeginIfNecessary(copyDest->connection);
/* Begin the remote transaction */
RemoteTransactionBegin(copyDest->connection);

SetupReplicationOriginRemoteSession(copyDest->connection);

/* Handle TRUNCATE or any setup commands */
StringInfo truncateStatement = ConstructShardTruncateStatement(
copyDest->destinationShardFullyQualifiedName);

if (!SendRemoteCommand(copyDest->connection, truncateStatement->data))
{
ReportConnectionError(copyDest->connection, ERROR);
RemoteTransactionAbort(copyDest->connection);
ResetRemoteTransaction(copyDest->connection);

Check warning on line 126 in src/backend/distributed/operations/worker_shard_copy.c

View check run for this annotation

Codecov / codecov/patch

src/backend/distributed/operations/worker_shard_copy.c#L124-L126

Added lines #L124 - L126 were not covered by tests
}

PGresult *truncateResult = GetRemoteCommandResult(copyDest->connection, true);
if (!IsResponseOK(truncateResult))
{
ReportResultError(copyDest->connection, truncateResult, ERROR);
PQclear(truncateResult);
ForgetResults(copyDest->connection);
RemoteTransactionAbort(copyDest->connection);
ResetRemoteTransaction(copyDest->connection);

Check warning on line 136 in src/backend/distributed/operations/worker_shard_copy.c

View check run for this annotation

Codecov / codecov/patch

src/backend/distributed/operations/worker_shard_copy.c#L132-L136

Added lines #L132 - L136 were not covered by tests
}
PQclear(truncateResult);
ForgetResults(copyDest->connection);

/* Construct and send the COPY statement with FREEZE */
StringInfo copyStatement = ConstructShardCopyStatement(
copyDest->destinationShardFullyQualifiedName,
copyDest->copyOutState->binary,
Expand All @@ -122,16 +147,18 @@
if (!SendRemoteCommand(copyDest->connection, copyStatement->data))
{
ReportConnectionError(copyDest->connection, ERROR);
RemoteTransactionAbort(copyDest->connection);
ResetRemoteTransaction(copyDest->connection);

Check warning on line 151 in src/backend/distributed/operations/worker_shard_copy.c

View check run for this annotation

Codecov / codecov/patch

src/backend/distributed/operations/worker_shard_copy.c#L150-L151

Added lines #L150 - L151 were not covered by tests
}

PGresult *result = GetRemoteCommandResult(copyDest->connection,
true /* raiseInterrupts */);
if (PQresultStatus(result) != PGRES_COPY_IN)
PGresult *copyResult = GetRemoteCommandResult(copyDest->connection,
true /* raiseInterrupts */);
if (PQresultStatus(copyResult) != PGRES_COPY_IN)
{
ReportResultError(copyDest->connection, result, ERROR);
ReportResultError(copyDest->connection, copyResult, ERROR);
}

PQclear(result);
PQclear(copyResult);
}


Expand Down Expand Up @@ -329,7 +356,7 @@
/* check whether there were any COPY errors */
PGresult *result = GetRemoteCommandResult(copyDest->connection,
true /* raiseInterrupts */);
if (PQresultStatus(result) != PGRES_COMMAND_OK)
if (!IsResponseOK(result))
{
ReportCopyError(copyDest->connection, result);
}
Expand All @@ -339,6 +366,21 @@

ResetReplicationOriginRemoteSession(copyDest->connection);

/* End the transaction by sending a COMMIT command */
if (!SendRemoteCommand(copyDest->connection, "COMMIT"))
{
HandleRemoteTransactionConnectionError(copyDest->connection, true);

Check warning on line 372 in src/backend/distributed/operations/worker_shard_copy.c

View check run for this annotation

Codecov / codecov/patch

src/backend/distributed/operations/worker_shard_copy.c#L372

Added line #L372 was not covered by tests
}

PGresult *commitResult = GetRemoteCommandResult(copyDest->connection, true);
if (!IsResponseOK(result))
{
ereport(ERROR, (errcode(ERRCODE_TRANSACTION_RESOLUTION_UNKNOWN),

Check warning on line 378 in src/backend/distributed/operations/worker_shard_copy.c

View check run for this annotation

Codecov / codecov/patch

src/backend/distributed/operations/worker_shard_copy.c#L378

Added line #L378 was not covered by tests
errmsg("Failed to commit transaction")));
}

PQclear(commitResult);

CloseConnection(copyDest->connection);
}
}
Expand Down Expand Up @@ -424,6 +466,23 @@
}


/*
* ConstructShardTruncateStatement constructs the text of a TRUNCATE statement
* for the destination shard.
*/
static StringInfo
ConstructShardTruncateStatement(List *destinationShardFullyQualifiedName)
{
StringInfo command = makeStringInfo();

appendStringInfo(command, "TRUNCATE %s.%s;",
quote_identifier(linitial(destinationShardFullyQualifiedName)),
quote_identifier(lsecond(destinationShardFullyQualifiedName)));

return command;
}


/*
* ConstructShardCopyStatement constructs the text of a COPY statement
* for copying into a result table
Expand All @@ -436,22 +495,22 @@
char *destinationShardSchemaName = linitial(destinationShardFullyQualifiedName);
char *destinationShardRelationName = lsecond(destinationShardFullyQualifiedName);


StringInfo command = makeStringInfo();

const char *columnList = CopyableColumnNamesFromTupleDesc(tupleDesc);

appendStringInfo(command, "COPY %s.%s (%s) FROM STDIN",
quote_identifier(destinationShardSchemaName), quote_identifier(
destinationShardRelationName), columnList);
quote_identifier(destinationShardSchemaName),
quote_identifier(destinationShardRelationName),
columnList);

if (useBinaryFormat)
{
appendStringInfo(command, " WITH (format binary);");
appendStringInfo(command, " WITH (format binary, FREEZE);");
}
else
{
appendStringInfo(command, ";");
appendStringInfo(command, " WITH (FREEZE);");
}

return command;
Expand Down
Loading