From faee21bff9601b527a85143fe7d1d5c3edf13497 Mon Sep 17 00:00:00 2001 From: Ahmet Gedemenli Date: Mon, 25 Oct 2021 18:44:05 +0300 Subject: [PATCH] Create new function AnyConnectedRelationIsNotAutoConverted --- src/backend/distributed/commands/table.c | 65 ++++++++++++++++-------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/src/backend/distributed/commands/table.c b/src/backend/distributed/commands/table.c index 27f96646e..fe0dfe131 100644 --- a/src/backend/distributed/commands/table.c +++ b/src/backend/distributed/commands/table.c @@ -79,6 +79,8 @@ static bool RelationIdListContainsCitusTableType(List *relationIdList, static bool RelationIdListContainsPostgresTable(List *relationIdList); static void ConvertPostgresLocalTablesToCitusLocalTables( AlterTableStmt *alterTableStatement); +static bool AnyConnectedRelationIsNotAutoConverted(List *relationRangeVarList, + AlterTableStmt *alterTableStatement); static int CompareRangeVarsByOid(const void *leftElement, const void *rightElement); static List * GetAlterTableAddFKeyRightRelationIdList( AlterTableStmt *alterTableStatement); @@ -1296,35 +1298,17 @@ ConvertPostgresLocalTablesToCitusLocalTables(AlterTableStmt *alterTableStatement relationRangeVarList = SortList(relationRangeVarList, CompareRangeVarsByOid); bool autoConverted = true; - RangeVar *relationRangeVar; - foreach_ptr(relationRangeVar, relationRangeVarList) + + if (AnyConnectedRelationIsNotAutoConverted(relationRangeVarList, alterTableStatement)) { - /* - * Here we iterate the relation list, and if at least one of the relations - * is marked as not-auto-converted, we should mark all of them as - * not-auto-converted. In that case, we set the local variable autoConverted - * to false here, to later use it when converting relations. - */ - List *commandList = alterTableStatement->cmds; - LOCKMODE lockMode = AlterTableGetLockLevel(commandList); - bool missingOk = alterTableStatement->missing_ok; - Oid relationId = RangeVarGetRelid(relationRangeVar, lockMode, missingOk); - if (OidIsValid(relationId) && IsCitusTable(relationId) && - IsCitusTableType(relationId, CITUS_LOCAL_TABLE)) - { - CitusTableCacheEntry *entry = GetCitusTableCacheEntry(relationId); - if (!entry->autoConverted) - { - autoConverted = false; - break; - } - } + autoConverted = false; } /* * Here we should operate on RangeVar objects since relations oid's would * change in below loop due to CreateCitusLocalTable. */ + RangeVar *relationRangeVar; foreach_ptr(relationRangeVar, relationRangeVarList) { List *commandList = alterTableStatement->cmds; @@ -1417,6 +1401,43 @@ ConvertPostgresLocalTablesToCitusLocalTables(AlterTableStmt *alterTableStatement } +/* + * AnyConnectedRelationIsNotAutoConverted takes a list of relations and returns true + * if any of these relations is marked as auto-converted = false. Returns true otherwise. + * This function also takes the current alterTableStatement command, to obtain the + * necessary locks. + */ +static bool +AnyConnectedRelationIsNotAutoConverted(List *relationRangeVarList, + AlterTableStmt *alterTableStatement) +{ + RangeVar *relationRangeVar; + foreach_ptr(relationRangeVar, relationRangeVarList) + { + /* + * Here we iterate the relation list, and if at least one of the relations + * is marked as not-auto-converted, we should mark all of them as + * not-auto-converted. In that case, we return true here. + */ + List *commandList = alterTableStatement->cmds; + LOCKMODE lockMode = AlterTableGetLockLevel(commandList); + bool missingOk = alterTableStatement->missing_ok; + Oid relationId = RangeVarGetRelid(relationRangeVar, lockMode, missingOk); + if (OidIsValid(relationId) && IsCitusTable(relationId) && + IsCitusTableType(relationId, CITUS_LOCAL_TABLE)) + { + CitusTableCacheEntry *entry = GetCitusTableCacheEntry(relationId); + if (!entry->autoConverted) + { + return true; + } + } + } + + return false; +} + + /* * CompareRangeVarsByOid is a comparison function to sort RangeVar object list. */