Fix PR comments

niupre-reuse-connections-for-logical-ref-fkeys
Nitish Upreti 2022-09-14 14:56:35 -07:00
parent a5aaa08a20
commit 17ae71fa8f
7 changed files with 73 additions and 49 deletions

View File

@ -16,7 +16,7 @@
#include "miscadmin.h" #include "miscadmin.h"
#include "safe_lib.h" #include "safe_lib.h"
#include "postmaster/postmaster.h"
#include "access/hash.h" #include "access/hash.h"
#include "commands/dbcommands.h" #include "commands/dbcommands.h"
#include "distributed/backend_data.h" #include "distributed/backend_data.h"
@ -244,6 +244,27 @@ GetNodeUserDatabaseConnection(uint32 flags, const char *hostname, int32 port,
} }
/*
* GetLocalConnectionForSubtransaction establishes a localhost connection.
* To avoid creating excessive connections, we try to reuse an existing connection.
*/
MultiConnection*
GetLocalConnectionForSubtransactionAsUser(char *userName)
{
int connectionFlag = OUTSIDE_TRANSACTION;
MultiConnection *connection = GetNodeUserDatabaseConnection(connectionFlag,
LocalHostName,
PostPortNumber,
userName,
get_database_name(
MyDatabaseId));
/* Don't cache connection for the lifetime of the entire session. */
connection->forceCloseAtTransactionEnd = true;
return connection;
}
/* /*
* StartNodeUserDatabaseConnection() initiates a connection to a remote node. * StartNodeUserDatabaseConnection() initiates a connection to a remote node.
* *

View File

@ -670,9 +670,8 @@ InsertCleanupRecordInSubtransaction(CleanupObject objectType,
nodeGroupId, nodeGroupId,
policy); policy);
SendCommandListToWorkerOutsideTransaction(LocalHostName, MultiConnection *connection = GetLocalConnectionForSubtransactionAsUser(CitusExtensionOwnerName());
PostPortNumber, SendCommandListToWorkerOutsideTransactionWithConnection(connection,
CitusExtensionOwnerName(),
list_make1(command->data)); list_make1(command->data));
} }
@ -691,9 +690,8 @@ DeleteCleanupRecordByRecordIdOutsideTransaction(uint64 recordId)
PG_DIST_CLEANUP, PG_DIST_CLEANUP,
recordId); recordId);
SendCommandListToWorkerOutsideTransaction(LocalHostName, MultiConnection *connection = GetLocalConnectionForSubtransactionAsUser(CitusExtensionOwnerName());
PostPortNumber, SendCommandListToWorkerOutsideTransactionWithConnection(connection,
CitusExtensionOwnerName(),
list_make1(command->data)); list_make1(command->data));
} }
@ -756,9 +754,8 @@ TryDropShardOutsideTransaction(OperationId operationId, char *qualifiedTableName
dropQuery->data); dropQuery->data);
/* remove the shard from the node */ /* remove the shard from the node */
bool success = SendOptionalCommandListToWorkerOutsideTransaction(nodeName, MultiConnection *connection = GetLocalConnectionForSubtransactionAsUser(CitusExtensionOwnerName());
nodePort, bool success = SendOptionalCommandListToWorkerOutsideTransactionWithConnection(connection,
NULL,
dropCommandList); dropCommandList);
return success; return success;

View File

@ -2245,14 +2245,7 @@ GetNextShardIdForSplitChild()
appendStringInfo(nextValueCommand, "SELECT nextval(%s);", quote_literal_cstr( appendStringInfo(nextValueCommand, "SELECT nextval(%s);", quote_literal_cstr(
"pg_catalog.pg_dist_shardid_seq")); "pg_catalog.pg_dist_shardid_seq"));
int connectionFlag = FORCE_NEW_CONNECTION; MultiConnection *connection = GetLocalConnectionForSubtransactionAsUser(CitusExtensionOwnerName());
MultiConnection *connection = GetNodeUserDatabaseConnection(connectionFlag,
LocalHostName,
PostPortNumber,
CitusExtensionOwnerName(),
get_database_name(
MyDatabaseId));
PGresult *result = NULL; PGresult *result = NULL;
int queryResult = ExecuteOptionalRemoteCommand(connection, nextValueCommand->data, int queryResult = ExecuteOptionalRemoteCommand(connection, nextValueCommand->data,
&result); &result);
@ -2269,7 +2262,8 @@ GetNextShardIdForSplitChild()
} }
shardId = SafeStringToUint64(PQgetvalue(result, 0, 0 /* nodeId column*/)); shardId = SafeStringToUint64(PQgetvalue(result, 0, 0 /* nodeId column*/));
CloseConnection(connection); PQclear(result);
ForgetResults(connection);
return shardId; return shardId;
} }

View File

@ -1162,7 +1162,7 @@ CreateForeignKeyConstraints(List *logicalRepTargetList)
list_make1("SET LOCAL citus.skip_constraint_validation TO ON;"), list_make1("SET LOCAL citus.skip_constraint_validation TO ON;"),
commandList); commandList);
SendCommandListToWorkerInSeparateTransaction( SendCommandListToWorkerOutsideTransactionWithConnection(
target->superuserConnection, target->superuserConnection,
commandList); commandList);
@ -1542,7 +1542,7 @@ DropUser(MultiConnection *connection, char *username)
* The DROP USER command should not propagate, so we temporarily disable * The DROP USER command should not propagate, so we temporarily disable
* DDL propagation. * DDL propagation.
*/ */
SendCommandListToWorkerInSeparateTransaction( SendCommandListToWorkerOutsideTransactionWithConnection(
connection, connection,
list_make2( list_make2(
"SET LOCAL citus.enable_ddl_propagation TO OFF;", "SET LOCAL citus.enable_ddl_propagation TO OFF;",
@ -1728,7 +1728,7 @@ CreateSubscriptions(MultiConnection *sourceConnection,
* create a user with SUPERUSER permissions and then alter it to NOSUPERUSER. * create a user with SUPERUSER permissions and then alter it to NOSUPERUSER.
* This prevents permission escalations. * This prevents permission escalations.
*/ */
SendCommandListToWorkerInSeparateTransaction( SendCommandListToWorkerOutsideTransactionWithConnection(
target->superuserConnection, target->superuserConnection,
list_make2( list_make2(
"SET LOCAL citus.enable_ddl_propagation TO OFF;", "SET LOCAL citus.enable_ddl_propagation TO OFF;",
@ -1787,7 +1787,7 @@ CreateSubscriptions(MultiConnection *sourceConnection,
* The ALTER ROLE command should not propagate, so we temporarily * The ALTER ROLE command should not propagate, so we temporarily
* disable DDL propagation. * disable DDL propagation.
*/ */
SendCommandListToWorkerInSeparateTransaction( SendCommandListToWorkerOutsideTransactionWithConnection(
target->superuserConnection, target->superuserConnection,
list_make2( list_make2(
"SET LOCAL citus.enable_ddl_propagation TO OFF;", "SET LOCAL citus.enable_ddl_propagation TO OFF;",

View File

@ -340,24 +340,15 @@ SendCommandListToWorkerOutsideTransaction(const char *nodeName, int32 nodePort,
nodeName, nodePort, nodeName, nodePort,
nodeUser, NULL); nodeUser, NULL);
MarkRemoteTransactionCritical(workerConnection); SendCommandListToWorkerOutsideTransactionWithConnection(workerConnection,
RemoteTransactionBegin(workerConnection); commandList);
/* iterate over the commands and execute them in the same connection */
const char *commandString = NULL;
foreach_ptr(commandString, commandList)
{
ExecuteCriticalRemoteCommand(workerConnection, commandString);
}
RemoteTransactionCommit(workerConnection);
CloseConnection(workerConnection); CloseConnection(workerConnection);
} }
/* /*
* SendCommandListToWorkerInSeparateTransaction sends the command list over the * SendCommandListToWorkerOutsideTransactionWithConnection sends the command list
* connection in a separate connection. This opens a new transaction on the * over the connection in a separate connection. This opens a new transaction on the
* connection, thus it's important that no transaction is currently open on the * connection, thus it's important that no transaction is currently open on the
* given connection. This function is mainly useful to avoid opening an closing * given connection. This function is mainly useful to avoid opening an closing
* connections excessively by allowing reusing a single connection to send * connections excessively by allowing reusing a single connection to send
@ -365,7 +356,7 @@ SendCommandListToWorkerOutsideTransaction(const char *nodeName, int32 nodePort,
* any of the queries fail. * any of the queries fail.
*/ */
void void
SendCommandListToWorkerInSeparateTransaction(MultiConnection *workerConnection, SendCommandListToWorkerOutsideTransactionWithConnection(MultiConnection *workerConnection,
List *commandList) List *commandList)
{ {
MarkRemoteTransactionCritical(workerConnection); MarkRemoteTransactionCritical(workerConnection);
@ -458,21 +449,15 @@ SendMetadataCommandListToWorkerListInCoordinatedTransaction(List *workerNodeList
/* /*
* SendOptionalCommandListToWorkerOutsideTransaction sends the given command * SendOptionalCommandListToWorkerOutsideTransactionWithConnection sends the
* list to the given worker in a single transaction that is outside of the * given command list to the given worker in a single transaction that is outside
* coordinated tranaction. If any of the commands fail, it rollbacks the * of the coordinated tranaction. If any of the commands fail, it rollbacks the
* transaction, and otherwise commits. * transaction, and otherwise commits.
*
*/ */
bool bool
SendOptionalCommandListToWorkerOutsideTransaction(const char *nodeName, int32 nodePort, SendOptionalCommandListToWorkerOutsideTransactionWithConnection(MultiConnection *workerConnection, List *commandList)
const char *nodeUser, List *commandList)
{ {
int connectionFlags = FORCE_NEW_CONNECTION;
bool failed = false;
MultiConnection *workerConnection = GetNodeUserDatabaseConnection(connectionFlags,
nodeName, nodePort,
nodeUser, NULL);
if (PQstatus(workerConnection->pgConn) != CONNECTION_OK) if (PQstatus(workerConnection->pgConn) != CONNECTION_OK)
{ {
return false; return false;
@ -480,6 +465,7 @@ SendOptionalCommandListToWorkerOutsideTransaction(const char *nodeName, int32 no
RemoteTransactionBegin(workerConnection); RemoteTransactionBegin(workerConnection);
/* iterate over the commands and execute them in the same connection */ /* iterate over the commands and execute them in the same connection */
bool failed = false;
const char *commandString = NULL; const char *commandString = NULL;
foreach_ptr(commandString, commandList) foreach_ptr(commandString, commandList)
{ {
@ -499,6 +485,28 @@ SendOptionalCommandListToWorkerOutsideTransaction(const char *nodeName, int32 no
RemoteTransactionCommit(workerConnection); RemoteTransactionCommit(workerConnection);
} }
CloseRemoteTransaction(workerConnection);
return !failed;
}
/*
* SendOptionalCommandListToWorkerOutsideTransaction sends the given command
* list to the given worker in a single transaction that is outside of the
* coordinated tranaction. If any of the commands fail, it rollbacks the
* transaction, and otherwise commits.
*/
bool
SendOptionalCommandListToWorkerOutsideTransaction(const char *nodeName, int32 nodePort,
const char *nodeUser, List *commandList)
{
int connectionFlags = FORCE_NEW_CONNECTION;
MultiConnection *workerConnection = GetNodeUserDatabaseConnection(connectionFlags,
nodeName, nodePort,
nodeUser, NULL);
bool failed = SendOptionalCommandListToWorkerOutsideTransactionWithConnection(workerConnection,
commandList);
CloseConnection(workerConnection); CloseConnection(workerConnection);
return !failed; return !failed;

View File

@ -289,6 +289,7 @@ extern MultiConnection * StartNodeConnection(uint32 flags, const char *hostname,
extern MultiConnection * GetNodeUserDatabaseConnection(uint32 flags, const char *hostname, extern MultiConnection * GetNodeUserDatabaseConnection(uint32 flags, const char *hostname,
int32 port, const char *user, int32 port, const char *user,
const char *database); const char *database);
extern MultiConnection* GetLocalConnectionForSubtransactionAsUser(char* userName);
extern MultiConnection * StartNodeUserDatabaseConnection(uint32 flags, extern MultiConnection * StartNodeUserDatabaseConnection(uint32 flags,
const char *hostname, const char *hostname,
int32 port, int32 port,

View File

@ -60,6 +60,9 @@ extern bool SendOptionalCommandListToWorkerOutsideTransaction(const char *nodeNa
int32 nodePort, int32 nodePort,
const char *nodeUser, const char *nodeUser,
List *commandList); List *commandList);
extern bool SendOptionalCommandListToWorkerOutsideTransactionWithConnection(
MultiConnection *workerConnection,
List *commandList);
extern bool SendOptionalMetadataCommandListToWorkerInCoordinatedTransaction(const extern bool SendOptionalMetadataCommandListToWorkerInCoordinatedTransaction(const
char *nodeName, char *nodeName,
int32 nodePort, int32 nodePort,
@ -75,7 +78,7 @@ extern void SendCommandListToWorkerOutsideTransaction(const char *nodeName,
int32 nodePort, int32 nodePort,
const char *nodeUser, const char *nodeUser,
List *commandList); List *commandList);
extern void SendCommandListToWorkerInSeparateTransaction( extern void SendCommandListToWorkerOutsideTransactionWithConnection(
MultiConnection *workerConnection, MultiConnection *workerConnection,
List *commandList); List *commandList);
extern void SendMetadataCommandListToWorkerListInCoordinatedTransaction( extern void SendMetadataCommandListToWorkerListInCoordinatedTransaction(