From 5acbd735ca85a12e94d8e334ddbcea211fe44d21 Mon Sep 17 00:00:00 2001 From: aykut-bozkurt <51649454+aykut-bozkurt@users.noreply.github.com> Date: Tue, 13 Jun 2023 11:43:48 +0300 Subject: [PATCH] Move 2 functions to correct files (#7000) Followup item from https://github.com/citusdata/citus/pull/6933#discussion_r1217896933 --- .../distributed/commands/alter_table.c | 37 +++++++++ .../commands/create_distributed_table.c | 79 ------------------- .../distributed/commands/foreign_constraint.c | 42 ++++++++++ 3 files changed, 79 insertions(+), 79 deletions(-) diff --git a/src/backend/distributed/commands/alter_table.c b/src/backend/distributed/commands/alter_table.c index 86dbe48e9..d574e997a 100644 --- a/src/backend/distributed/commands/alter_table.c +++ b/src/backend/distributed/commands/alter_table.c @@ -361,6 +361,43 @@ worker_change_sequence_dependency(PG_FUNCTION_ARGS) } +/* + * DropFKeysAndUndistributeTable drops all foreign keys that relation with + * relationId is involved then undistributes it. + * Note that as UndistributeTable changes relationId of relation, this + * function also returns new relationId of relation. + * Also note that callers are responsible for storing & recreating foreign + * keys to be dropped if needed. + */ +Oid +DropFKeysAndUndistributeTable(Oid relationId) +{ + DropFKeysRelationInvolvedWithTableType(relationId, INCLUDE_ALL_TABLE_TYPES); + + /* store them before calling UndistributeTable as it changes relationId */ + char *relationName = get_rel_name(relationId); + Oid schemaId = get_rel_namespace(relationId); + + /* suppress notices messages not to be too verbose */ + TableConversionParameters params = { + .relationId = relationId, + .cascadeViaForeignKeys = false, + .suppressNoticeMessages = true + }; + UndistributeTable(¶ms); + + Oid newRelationId = get_relname_relid(relationName, schemaId); + + /* + * We don't expect this to happen but to be on the safe side let's error + * out here. + */ + EnsureRelationExists(newRelationId); + + return newRelationId; +} + + /* * UndistributeTables undistributes given relations. It first collects all foreign keys * to recreate them after the undistribution. Then, drops the foreign keys and diff --git a/src/backend/distributed/commands/create_distributed_table.c b/src/backend/distributed/commands/create_distributed_table.c index a1ae2d9ea..9849da9f4 100644 --- a/src/backend/distributed/commands/create_distributed_table.c +++ b/src/backend/distributed/commands/create_distributed_table.c @@ -1571,85 +1571,6 @@ EnsureDistributedSequencesHaveOneType(Oid relationId, List *seqInfoList) } -/* - * GetFKeyCreationCommandsRelationInvolvedWithTableType returns a list of DDL - * commands to recreate the foreign keys that relation with relationId is involved - * with given table type. - */ -List * -GetFKeyCreationCommandsRelationInvolvedWithTableType(Oid relationId, int tableTypeFlag) -{ - int referencingFKeysFlag = INCLUDE_REFERENCING_CONSTRAINTS | - tableTypeFlag; - List *referencingFKeyCreationCommands = - GetForeignConstraintCommandsInternal(relationId, referencingFKeysFlag); - - /* already captured self referencing foreign keys, so use EXCLUDE_SELF_REFERENCES */ - int referencedFKeysFlag = INCLUDE_REFERENCED_CONSTRAINTS | - EXCLUDE_SELF_REFERENCES | - tableTypeFlag; - List *referencedFKeyCreationCommands = - GetForeignConstraintCommandsInternal(relationId, referencedFKeysFlag); - return list_concat(referencingFKeyCreationCommands, referencedFKeyCreationCommands); -} - - -/* - * DropFKeysAndUndistributeTable drops all foreign keys that relation with - * relationId is involved then undistributes it. - * Note that as UndistributeTable changes relationId of relation, this - * function also returns new relationId of relation. - * Also note that callers are responsible for storing & recreating foreign - * keys to be dropped if needed. - */ -Oid -DropFKeysAndUndistributeTable(Oid relationId) -{ - DropFKeysRelationInvolvedWithTableType(relationId, INCLUDE_ALL_TABLE_TYPES); - - /* store them before calling UndistributeTable as it changes relationId */ - char *relationName = get_rel_name(relationId); - Oid schemaId = get_rel_namespace(relationId); - - /* suppress notices messages not to be too verbose */ - TableConversionParameters params = { - .relationId = relationId, - .cascadeViaForeignKeys = false, - .suppressNoticeMessages = true - }; - UndistributeTable(¶ms); - - Oid newRelationId = get_relname_relid(relationName, schemaId); - - /* - * We don't expect this to happen but to be on the safe side let's error - * out here. - */ - EnsureRelationExists(newRelationId); - - return newRelationId; -} - - -/* - * DropFKeysRelationInvolvedWithTableType drops foreign keys that relation - * with relationId is involved with given table type. - */ -void -DropFKeysRelationInvolvedWithTableType(Oid relationId, int tableTypeFlag) -{ - int referencingFKeysFlag = INCLUDE_REFERENCING_CONSTRAINTS | - tableTypeFlag; - DropRelationForeignKeys(relationId, referencingFKeysFlag); - - /* already captured self referencing foreign keys, so use EXCLUDE_SELF_REFERENCES */ - int referencedFKeysFlag = INCLUDE_REFERENCED_CONSTRAINTS | - EXCLUDE_SELF_REFERENCES | - tableTypeFlag; - DropRelationForeignKeys(relationId, referencedFKeysFlag); -} - - /* * DecideDistTableReplicationModel function decides which replication model should be * used for a distributed table depending on given distribution configuration. diff --git a/src/backend/distributed/commands/foreign_constraint.c b/src/backend/distributed/commands/foreign_constraint.c index 09133f5a3..b48b6c54a 100644 --- a/src/backend/distributed/commands/foreign_constraint.c +++ b/src/backend/distributed/commands/foreign_constraint.c @@ -896,6 +896,48 @@ GetForeignConstraintCommandsInternal(Oid relationId, int flags) } +/* + * GetFKeyCreationCommandsRelationInvolvedWithTableType returns a list of DDL + * commands to recreate the foreign keys that relation with relationId is involved + * with given table type. + */ +List * +GetFKeyCreationCommandsRelationInvolvedWithTableType(Oid relationId, int tableTypeFlag) +{ + int referencingFKeysFlag = INCLUDE_REFERENCING_CONSTRAINTS | + tableTypeFlag; + List *referencingFKeyCreationCommands = + GetForeignConstraintCommandsInternal(relationId, referencingFKeysFlag); + + /* already captured self referencing foreign keys, so use EXCLUDE_SELF_REFERENCES */ + int referencedFKeysFlag = INCLUDE_REFERENCED_CONSTRAINTS | + EXCLUDE_SELF_REFERENCES | + tableTypeFlag; + List *referencedFKeyCreationCommands = + GetForeignConstraintCommandsInternal(relationId, referencedFKeysFlag); + return list_concat(referencingFKeyCreationCommands, referencedFKeyCreationCommands); +} + + +/* + * DropFKeysRelationInvolvedWithTableType drops foreign keys that relation + * with relationId is involved with given table type. + */ +void +DropFKeysRelationInvolvedWithTableType(Oid relationId, int tableTypeFlag) +{ + int referencingFKeysFlag = INCLUDE_REFERENCING_CONSTRAINTS | + tableTypeFlag; + DropRelationForeignKeys(relationId, referencingFKeysFlag); + + /* already captured self referencing foreign keys, so use EXCLUDE_SELF_REFERENCES */ + int referencedFKeysFlag = INCLUDE_REFERENCED_CONSTRAINTS | + EXCLUDE_SELF_REFERENCES | + tableTypeFlag; + DropRelationForeignKeys(relationId, referencedFKeysFlag); +} + + /* * HasForeignKeyWithLocalTable returns true if relation has foreign key * relationship with a local table.