From 534ee5b6ca6cb2cbbb8c9f319c62e74fbd9abd34 Mon Sep 17 00:00:00 2001 From: Burak Velioglu Date: Fri, 28 Jan 2022 15:08:17 +0300 Subject: [PATCH] Use function to ensure seq dep --- .../citus_add_local_table_to_metadata.c | 13 +++------ .../commands/create_distributed_table.c | 27 ++++++++++++++----- src/backend/distributed/commands/table.c | 7 +---- src/include/distributed/metadata_utility.h | 4 +-- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/backend/distributed/commands/citus_add_local_table_to_metadata.c b/src/backend/distributed/commands/citus_add_local_table_to_metadata.c index c117d537b..f8c2a3042 100644 --- a/src/backend/distributed/commands/citus_add_local_table_to_metadata.c +++ b/src/backend/distributed/commands/citus_add_local_table_to_metadata.c @@ -91,7 +91,7 @@ static void TransferSequenceOwnership(Oid ownedSequenceId, Oid targetRelationId, char *columnName); static void InsertMetadataForCitusLocalTable(Oid citusLocalTableId, uint64 shardId, bool autoConverted); -static void FinalizeCitusLocalTableCreation(Oid relationId, List *dependentSequenceList); +static void FinalizeCitusLocalTableCreation(Oid relationId); PG_FUNCTION_INFO_V1(citus_add_local_table_to_metadata); @@ -311,12 +311,7 @@ CreateCitusLocalTable(Oid relationId, bool cascadeViaForeignKeys, bool autoConve * Ensure that the sequences used in column defaults of the table * have proper types */ - List *attnumList = NIL; - List *dependentSequenceList = NIL; - GetDependentSequencesWithRelation(relationId, &attnumList, - &dependentSequenceList, 0); - EnsureDistributedSequencesHaveOneType(relationId, dependentSequenceList, - attnumList); + EnsureRelationHasCompatibleSequenceTypes(relationId); /* * Ensure dependencies exist as we will create shell table on the other nodes @@ -366,7 +361,7 @@ CreateCitusLocalTable(Oid relationId, bool cascadeViaForeignKeys, bool autoConve InsertMetadataForCitusLocalTable(shellRelationId, shardId, autoConverted); - FinalizeCitusLocalTableCreation(shellRelationId, dependentSequenceList); + FinalizeCitusLocalTableCreation(shellRelationId); } @@ -1230,7 +1225,7 @@ InsertMetadataForCitusLocalTable(Oid citusLocalTableId, uint64 shardId, * sequences dependent with the table. */ static void -FinalizeCitusLocalTableCreation(Oid relationId, List *dependentSequenceList) +FinalizeCitusLocalTableCreation(Oid relationId) { /* * If it is a foreign table, then skip creating citus truncate trigger diff --git a/src/backend/distributed/commands/create_distributed_table.c b/src/backend/distributed/commands/create_distributed_table.c index 607e56d63..43bd1ebd6 100644 --- a/src/backend/distributed/commands/create_distributed_table.c +++ b/src/backend/distributed/commands/create_distributed_table.c @@ -113,6 +113,9 @@ static void EnsureLocalTableEmptyIfNecessary(Oid relationId, char distributionMe static bool ShouldLocalTableBeEmpty(Oid relationId, char distributionMethod, bool viaDeprecatedAPI); static void EnsureCitusTableCanBeCreated(Oid relationOid); +static void EnsureDistributedSequencesHaveOneType(Oid relationId, + List *dependentSequenceList, + List *attnumList); static List * GetFKeyCreationCommandsRelationInvolvedWithTableType(Oid relationId, int tableTypeFlag); static Oid DropFKeysAndUndistributeTable(Oid relationId); @@ -434,11 +437,7 @@ CreateDistributedTable(Oid relationId, Var *distributionColumn, char distributio * Ensure that the sequences used in column defaults of the table * have proper types */ - List *attnumList = NIL; - List *dependentSequenceList = NIL; - GetDependentSequencesWithRelation(relationId, &attnumList, &dependentSequenceList, 0); - EnsureDistributedSequencesHaveOneType(relationId, dependentSequenceList, - attnumList); + EnsureRelationHasCompatibleSequenceTypes(relationId); /* * distributed tables might have dependencies on different objects, since we create @@ -666,12 +665,28 @@ AlterSequenceType(Oid seqOid, Oid typeOid) } +/* + * EnsureRelationHasCompatibleSequenceTypes ensures that sequences used for columns + * of the table have compatible types both with the column type on that table and + * all other distributed tables' columns they have used for + */ +void +EnsureRelationHasCompatibleSequenceTypes(Oid relationId) +{ + List *attnumList = NIL; + List *dependentSequenceList = NIL; + + GetDependentSequencesWithRelation(relationId, &attnumList, &dependentSequenceList, 0); + EnsureDistributedSequencesHaveOneType(relationId, dependentSequenceList, attnumList); +} + + /* * EnsureDistributedSequencesHaveOneType first ensures that the type of the column * in which the sequence is used as default is supported for each sequence in input * dependentSequenceList, and then alters the sequence type if not the same with the column type. */ -void +static void EnsureDistributedSequencesHaveOneType(Oid relationId, List *dependentSequenceList, List *attnumList) { diff --git a/src/backend/distributed/commands/table.c b/src/backend/distributed/commands/table.c index cccda2eb0..9236f0e1a 100644 --- a/src/backend/distributed/commands/table.c +++ b/src/backend/distributed/commands/table.c @@ -1959,12 +1959,7 @@ PostprocessAlterTableStmt(AlterTableStmt *alterTableStatement) * Before ensuring each dependency exist, update dependent sequences * types if necessary. */ - List *attnumList = NIL; - List *dependentSequenceList = NIL; - GetDependentSequencesWithRelation(relationId, &attnumList, &dependentSequenceList, - 0); - EnsureDistributedSequencesHaveOneType(relationId, dependentSequenceList, - attnumList); + EnsureRelationHasCompatibleSequenceTypes(relationId); /* changing a relation could introduce new dependencies */ ObjectAddress tableAddress = { 0 }; diff --git a/src/include/distributed/metadata_utility.h b/src/include/distributed/metadata_utility.h index 68c7ed26e..d1db0f2fe 100644 --- a/src/include/distributed/metadata_utility.h +++ b/src/include/distributed/metadata_utility.h @@ -288,7 +288,5 @@ extern bool GetNodeDiskSpaceStatsForConnection(MultiConnection *connection, extern void ExecuteQueryViaSPI(char *query, int SPIOK); extern void EnsureSequenceTypeSupported(Oid seqOid, Oid seqTypId, Oid ownerRelationId); extern void AlterSequenceType(Oid seqOid, Oid typeOid); -extern void EnsureDistributedSequencesHaveOneType(Oid relationId, - List *dependentSequenceList, - List *attnumList); +extern void EnsureRelationHasCompatibleSequenceTypes(Oid relationId); #endif /* METADATA_UTILITY_H */