Some refactor as a preparation

pull/4539/head
Onur Tirtir 2021-01-18 11:41:43 +03:00
parent 253c19062a
commit 8e02375aa3
1 changed files with 20 additions and 18 deletions

View File

@ -114,9 +114,10 @@ static void EnsureLocalTableEmptyIfNecessary(Oid relationId, char distributionMe
static bool ShouldLocalTableBeEmpty(Oid relationId, char distributionMethod, bool static bool ShouldLocalTableBeEmpty(Oid relationId, char distributionMethod, bool
viaDeprecatedAPI); viaDeprecatedAPI);
static void EnsureCitusTableCanBeCreated(Oid relationOid); static void EnsureCitusTableCanBeCreated(Oid relationOid);
static List * GetFKeyCreationCommandsRelationInvolved(Oid relationId); static List * GetFKeyCreationCommandsRelationInvolvedWithTableType(Oid relationId,
int tableTypeFlag);
static Oid DropFKeysAndUndistributeTable(Oid relationId); static Oid DropFKeysAndUndistributeTable(Oid relationId);
static void DropFKeysRelationInvolved(Oid relationId); static void DropFKeysRelationInvolvedWithTableType(Oid relationId, int tableTypeFlag);
static bool LocalTableEmpty(Oid tableId); static bool LocalTableEmpty(Oid tableId);
static void CopyLocalDataIntoShards(Oid relationId); static void CopyLocalDataIntoShards(Oid relationId);
static List * TupleDescColumnNameList(TupleDesc tupleDescriptor); static List * TupleDescColumnNameList(TupleDesc tupleDescriptor);
@ -354,12 +355,13 @@ CreateDistributedTable(Oid relationId, Var *distributionColumn, char distributio
* given relation is involved, then we undistribute the relation and finally * given relation is involved, then we undistribute the relation and finally
* we re-create dropped foreign keys at the end of this function. * we re-create dropped foreign keys at the end of this function.
*/ */
List *fKeyCreationCommandsRelationInvolved = NIL; List *originalForeignKeyRecreationCommands = NIL;
if (IsCitusTableType(relationId, CITUS_LOCAL_TABLE)) if (IsCitusTableType(relationId, CITUS_LOCAL_TABLE))
{ {
/* store foreign key creation commands that relation is involved */ /* store foreign key creation commands that relation is involved */
fKeyCreationCommandsRelationInvolved = originalForeignKeyRecreationCommands =
GetFKeyCreationCommandsRelationInvolved(relationId); GetFKeyCreationCommandsRelationInvolvedWithTableType(relationId,
INCLUDE_ALL_TABLE_TYPES);
relationId = DropFKeysAndUndistributeTable(relationId); relationId = DropFKeysAndUndistributeTable(relationId);
} }
@ -472,27 +474,27 @@ CreateDistributedTable(Oid relationId, Var *distributionColumn, char distributio
* we can skip the validation of the foreign keys. * we can skip the validation of the foreign keys.
*/ */
bool skip_validation = true; bool skip_validation = true;
ExecuteForeignKeyCreateCommandList(fKeyCreationCommandsRelationInvolved, ExecuteForeignKeyCreateCommandList(droppedFKeyCreationCommands, skip_validation);
skip_validation);
} }
/* /*
* GetFKeyCreationCommandsRelationInvolved returns a list of DDL commands to * GetFKeyCreationCommandsRelationInvolvedWithTableType returns a list of DDL
* recreate the foreign keys that relation with relationId is involved. * commands to recreate the foreign keys that relation with relationId is involved
* with given table type.
*/ */
static List * static List *
GetFKeyCreationCommandsRelationInvolved(Oid relationId) GetFKeyCreationCommandsRelationInvolvedWithTableType(Oid relationId, int tableTypeFlag)
{ {
int referencingFKeysFlag = INCLUDE_REFERENCING_CONSTRAINTS | int referencingFKeysFlag = INCLUDE_REFERENCING_CONSTRAINTS |
INCLUDE_ALL_TABLE_TYPES; tableTypeFlag;
List *referencingFKeyCreationCommands = List *referencingFKeyCreationCommands =
GetForeignConstraintCommandsInternal(relationId, referencingFKeysFlag); GetForeignConstraintCommandsInternal(relationId, referencingFKeysFlag);
/* already captured self referencing foreign keys, so use EXCLUDE_SELF_REFERENCES */ /* already captured self referencing foreign keys, so use EXCLUDE_SELF_REFERENCES */
int referencedFKeysFlag = INCLUDE_REFERENCED_CONSTRAINTS | int referencedFKeysFlag = INCLUDE_REFERENCED_CONSTRAINTS |
EXCLUDE_SELF_REFERENCES | EXCLUDE_SELF_REFERENCES |
INCLUDE_ALL_TABLE_TYPES; tableTypeFlag;
List *referencedFKeyCreationCommands = List *referencedFKeyCreationCommands =
GetForeignConstraintCommandsInternal(relationId, referencedFKeysFlag); GetForeignConstraintCommandsInternal(relationId, referencedFKeysFlag);
return list_concat(referencingFKeyCreationCommands, referencedFKeyCreationCommands); return list_concat(referencingFKeyCreationCommands, referencedFKeyCreationCommands);
@ -510,7 +512,7 @@ GetFKeyCreationCommandsRelationInvolved(Oid relationId)
static Oid static Oid
DropFKeysAndUndistributeTable(Oid relationId) DropFKeysAndUndistributeTable(Oid relationId)
{ {
DropFKeysRelationInvolved(relationId); DropFKeysRelationInvolvedWithTableType(relationId, INCLUDE_ALL_TABLE_TYPES);
/* store them before calling UndistributeTable as it changes relationId */ /* store them before calling UndistributeTable as it changes relationId */
char *relationName = get_rel_name(relationId); char *relationName = get_rel_name(relationId);
@ -535,20 +537,20 @@ DropFKeysAndUndistributeTable(Oid relationId)
/* /*
* DropFKeysRelationInvolved drops all foreign keys that relation with * DropFKeysRelationInvolvedWithTableType drops foreign keys that relation
* relationId is involved. * with relationId is involved with given table type.
*/ */
static void static void
DropFKeysRelationInvolved(Oid relationId) DropFKeysRelationInvolvedWithTableType(Oid relationId, int tableTypeFlag)
{ {
int referencingFKeysFlag = INCLUDE_REFERENCING_CONSTRAINTS | int referencingFKeysFlag = INCLUDE_REFERENCING_CONSTRAINTS |
INCLUDE_ALL_TABLE_TYPES; tableTypeFlag;
DropRelationForeignKeys(relationId, referencingFKeysFlag); DropRelationForeignKeys(relationId, referencingFKeysFlag);
/* already captured self referencing foreign keys, so use EXCLUDE_SELF_REFERENCES */ /* already captured self referencing foreign keys, so use EXCLUDE_SELF_REFERENCES */
int referencedFKeysFlag = INCLUDE_REFERENCED_CONSTRAINTS | int referencedFKeysFlag = INCLUDE_REFERENCED_CONSTRAINTS |
EXCLUDE_SELF_REFERENCES | EXCLUDE_SELF_REFERENCES |
INCLUDE_ALL_TABLE_TYPES; tableTypeFlag;
DropRelationForeignKeys(relationId, referencedFKeysFlag); DropRelationForeignKeys(relationId, referencedFKeysFlag);
} }