Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
onurctirtir committed Apr 19, 2023
1 parent 381f968 commit b335134
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions src/backend/distributed/commands/utility_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static void PostStandardProcessUtility(Node *parsetree);
static void DecrementUtilityHookCountersIfNecessary(Node *parsetree);
static bool IsDropSchemaOrDB(Node *parsetree);
static bool ShouldCheckUndistributeCitusLocalTables(void);
static bool ShouldAddNewTableToMetadata(Node *parsetree);
static bool ShouldAddNewTableToMetadata(Oid relationId);

/*
* ProcessUtilityParseTree is a convenience method to create a PlannedStmt out of
Expand Down Expand Up @@ -344,26 +344,43 @@ multi_ProcessUtility(PlannedStmt *pstmt,
}
ResetConstraintDropped();

/*
* We're only interested in top-level CREATE TABLE commands
* to create a Citus managed table.
*/
Oid createdRelationId = InvalidOid;
if (context == PROCESS_UTILITY_TOPLEVEL &&
ShouldAddNewTableToMetadata(parsetree))
(IsA(parsetree, CreateStmt) ||
IsA(parsetree, CreateForeignTableStmt)))
{
CreateStmt *baseCreateTableStmt =
IsA(parsetree, CreateStmt) ? (CreateStmt *) parsetree :
(CreateStmt *) &(((CreateForeignTableStmt *) parsetree)->base);

/*
* Here we need to increment command counter so that next command
* can see the new table.
*/
CommandCounterIncrement();
CreateStmt *createTableStmt = (CreateStmt *) parsetree;
Oid relationId = RangeVarGetRelid(createTableStmt->relation,
NoLock, false);
bool missingOk = false;
createdRelationId = RangeVarGetRelid(baseCreateTableStmt->relation,
NoLock, missingOk);
}

if (!OidIsValid(createdRelationId))
{
/* no tables were created by this command, or it wasn't a top-level one */
}
else if (ShouldAddNewTableToMetadata(createdRelationId))
{
/*
* Here we set autoConverted to false, since the user explicitly
* wants these tables to be added to metadata, by setting the
* GUC use_citus_managed_tables to true.
*/
bool autoConverted = false;
bool cascade = true;
CreateCitusLocalTable(relationId, cascade, autoConverted);
CreateCitusLocalTable(createdRelationId, cascade, autoConverted);
}
}

Expand Down Expand Up @@ -1061,36 +1078,17 @@ ShouldCheckUndistributeCitusLocalTables(void)


/*
* ShouldAddNewTableToMetadata takes a Node* and returns true if we need to add a
* ShouldAddNewTableToMetadata takes a relationId and returns true if we need to add a
* newly created table to metadata, false otherwise.
* This function checks whether the given Node* is a CREATE TABLE statement.
* For partitions and temporary tables, ShouldAddNewTableToMetadata returns false.
* For other tables created, returns true, if we are on a coordinator that is added
* as worker, and ofcourse, if the GUC use_citus_managed_tables is set to on.
*/
static bool
ShouldAddNewTableToMetadata(Node *parsetree)
ShouldAddNewTableToMetadata(Oid relationId)
{
CreateStmt *createTableStmt;

if (IsA(parsetree, CreateStmt))
{
createTableStmt = (CreateStmt *) parsetree;
}
else if (IsA(parsetree, CreateForeignTableStmt))
{
CreateForeignTableStmt *createForeignTableStmt =
(CreateForeignTableStmt *) parsetree;
createTableStmt = (CreateStmt *) &(createForeignTableStmt->base);
}
else
{
/* if the command is not CREATE [FOREIGN] TABLE, we can early return false */
return false;
}

if (createTableStmt->relation->relpersistence == RELPERSISTENCE_TEMP ||
createTableStmt->partbound != NULL)
if (get_rel_persistence(relationId) == RELPERSISTENCE_TEMP ||
PartitionTableNoLock(relationId))
{
/*
* Shouldn't add table to metadata if it's a temp table, or a partition.
Expand Down

0 comments on commit b335134

Please sign in to comment.