Cache the current database name

Purely for performance reasons.
pull/2633/head
Marco Slot 2019-03-04 13:38:15 +01:00 committed by Onder Kalaci
parent 0ea4e52df5
commit 5ff1821411
7 changed files with 38 additions and 5 deletions

View File

@ -251,7 +251,7 @@ StartNodeUserDatabaseConnection(uint32 flags, const char *hostname, int32 port,
} }
else else
{ {
strlcpy(key.database, get_database_name(MyDatabaseId), NAMEDATALEN); strlcpy(key.database, CurrentDatabaseName(), NAMEDATALEN);
} }
if (CurrentCoordinatedTransactionState == COORD_TRANS_NONE) if (CurrentCoordinatedTransactionState == COORD_TRANS_NONE)

View File

@ -871,7 +871,7 @@ TrackerConnectPoll(TaskTracker *taskTracker)
{ {
char *nodeName = taskTracker->workerName; char *nodeName = taskTracker->workerName;
uint32 nodePort = taskTracker->workerPort; uint32 nodePort = taskTracker->workerPort;
char *nodeDatabase = get_database_name(MyDatabaseId); char *nodeDatabase = CurrentDatabaseName();
char *nodeUser = taskTracker->userName; char *nodeUser = taskTracker->userName;
int32 connectionId = MultiClientConnectStart(nodeName, nodePort, int32 connectionId = MultiClientConnectStart(nodeName, nodePort,

View File

@ -507,7 +507,7 @@ ExplainTaskPlacement(ShardPlacement *taskPlacement, List *explainOutputList,
StringInfo nodeAddress = makeStringInfo(); StringInfo nodeAddress = makeStringInfo();
char *nodeName = taskPlacement->nodeName; char *nodeName = taskPlacement->nodeName;
uint32 nodePort = taskPlacement->nodePort; uint32 nodePort = taskPlacement->nodePort;
char *nodeDatabase = get_database_name(MyDatabaseId); char *nodeDatabase = CurrentDatabaseName();
ListCell *explainOutputCell = NULL; ListCell *explainOutputCell = NULL;
int rowIndex = 0; int rowIndex = 0;

View File

@ -24,6 +24,7 @@
#include "catalog/pg_namespace.h" #include "catalog/pg_namespace.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "citus_version.h" #include "citus_version.h"
#include "commands/dbcommands.h"
#include "commands/extension.h" #include "commands/extension.h"
#include "commands/trigger.h" #include "commands/trigger.h"
#include "distributed/colocation_utils.h" #include "distributed/colocation_utils.h"
@ -137,6 +138,8 @@ typedef struct MetadataCacheData
Oid unavailableNodeRoleId; Oid unavailableNodeRoleId;
Oid pgTableIsVisibleFuncId; Oid pgTableIsVisibleFuncId;
Oid citusTableIsVisibleFuncId; Oid citusTableIsVisibleFuncId;
bool databaseNameValid;
char databaseName[NAMEDATALEN];
} MetadataCacheData; } MetadataCacheData;
@ -2102,6 +2105,33 @@ CitusTableVisibleFuncId(void)
} }
/*
* CurrentDatabaseName gets the name of the current database and caches
* the result.
*
* Given that the database name cannot be changed when there is at least
* one session connected to it, we do not need to implement any invalidation
* mechanism.
*/
char *
CurrentDatabaseName(void)
{
if (!MetadataCache.databaseNameValid)
{
char *databaseName = get_database_name(MyDatabaseId);
if (databaseName == NULL)
{
return NULL;
}
strlcpy(MetadataCache.databaseName, databaseName, NAMEDATALEN);
MetadataCache.databaseNameValid = true;
}
return MetadataCache.databaseName;
}
/* /*
* CitusExtensionOwner() returns the owner of the 'citus' extension. That user * CitusExtensionOwner() returns the owner of the 'citus' extension. That user
* is, amongst others, used to perform actions a normal user might not be * is, amongst others, used to perform actions a normal user might not be

View File

@ -368,7 +368,7 @@ CreateTask(uint64 jobId, uint32 taskId, char *taskCallString)
{ {
WorkerTask *workerTask = NULL; WorkerTask *workerTask = NULL;
uint32 assignmentTime = 0; uint32 assignmentTime = 0;
char *databaseName = get_database_name(MyDatabaseId); char *databaseName = CurrentDatabaseName();
char *userName = CurrentUserName(); char *userName = CurrentUserName();
/* increase task priority for cleanup tasks */ /* increase task priority for cleanup tasks */

View File

@ -248,7 +248,7 @@ ReceiveRegularFile(const char *nodeName, uint32 nodePort, const char *nodeUser,
} }
/* we use the same database name on the master and worker nodes */ /* we use the same database name on the master and worker nodes */
nodeDatabase = get_database_name(MyDatabaseId); nodeDatabase = CurrentDatabaseName();
/* connect to remote node */ /* connect to remote node */
connectionId = MultiClientConnect(nodeName, nodePort, nodeDatabase, nodeUser); connectionId = MultiClientConnect(nodeName, nodePort, nodeDatabase, nodeUser);

View File

@ -167,4 +167,7 @@ extern Oid BinaryCopyFormatId(void);
extern Oid CitusExtensionOwner(void); extern Oid CitusExtensionOwner(void);
extern char * CitusExtensionOwnerName(void); extern char * CitusExtensionOwnerName(void);
extern char * CurrentUserName(void); extern char * CurrentUserName(void);
extern char * CurrentDatabaseName(void);
#endif /* METADATA_CACHE_H */ #endif /* METADATA_CACHE_H */