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 cb986f5cb..4f7873ac2 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 @@ -658,8 +658,10 @@ GetShellTableDDLEventsForCitusLocalTable(Oid relationId) */ IncludeSequenceDefaults includeSequenceDefaults = NEXTVAL_SEQUENCE_DEFAULTS; + bool associateSequenceDependency = false; List *tableDDLCommands = GetFullTableCreationCommands(relationId, - includeSequenceDefaults); + includeSequenceDefaults, + associateSequenceDependency); List *shellTableDDLEvents = NIL; TableDDLCommand *tableDDLCommand = NULL; diff --git a/src/backend/distributed/commands/dependencies.c b/src/backend/distributed/commands/dependencies.c index 6f9169c79..1993040df 100644 --- a/src/backend/distributed/commands/dependencies.c +++ b/src/backend/distributed/commands/dependencies.c @@ -31,7 +31,6 @@ typedef bool (*AddressPredicate)(const ObjectAddress *); static int ObjectAddressComparator(const void *a, const void *b); static List * GetDependencyCreateDDLCommands(const ObjectAddress *dependency); -static List * GetCitusTableDDLCommandList(Oid relationId); static List * FilterObjectAddressListByPredicate(List *objectAddressList, AddressPredicate predicate); @@ -244,7 +243,16 @@ GetDependencyCreateDDLCommands(const ObjectAddress *dependency) if (IsCitusTable(relationId)) { - commandList = GetCitusTableDDLCommandList(relationId); + bool associateSequenceDependency = true; + List *tableDDLCommands = GetFullTableCreationCommands(relationId, + WORKER_NEXTVAL_SEQUENCE_DEFAULTS, + associateSequenceDependency); + TableDDLCommand *tableDDLCommand = NULL; + foreach_ptr(tableDDLCommand, tableDDLCommands) + { + Assert(CitusIsA(tableDDLCommand, TableDDLCommand)); + commandList = lappend(commandList, GetTableDDLCommand(tableDDLCommand)); + } } return commandList; @@ -338,34 +346,6 @@ GetDependencyCreateDDLCommands(const ObjectAddress *dependency) } -/* - * GetCitusTableDDLCommandList returns the list of commands to create citus table - * including the commands to associate sequences with table. - */ -static List * -GetCitusTableDDLCommandList(Oid relationId) -{ - List *commandList = NIL; - List *tableDDLCommands = GetFullTableCreationCommands(relationId, - WORKER_NEXTVAL_SEQUENCE_DEFAULTS); - - TableDDLCommand *tableDDLCommand = NULL; - foreach_ptr(tableDDLCommand, tableDDLCommands) - { - Assert(CitusIsA(tableDDLCommand, TableDDLCommand)); - commandList = lappend(commandList, GetTableDDLCommand(tableDDLCommand)); - } - - /* - * Get commands to associate sequences with dependencies - */ - List *sequenceDependencyCommandList = SequenceDependencyCommandList(relationId); - commandList = list_concat(commandList, sequenceDependencyCommandList); - - return commandList; -} - - /* * ReplicateAllObjectsToNodeCommandList returns commands to replicate all * previously marked objects to a worker node. The function also sets diff --git a/src/backend/distributed/metadata/metadata_sync.c b/src/backend/distributed/metadata/metadata_sync.c index e9a1d3969..f35746043 100644 --- a/src/backend/distributed/metadata/metadata_sync.c +++ b/src/backend/distributed/metadata/metadata_sync.c @@ -1574,7 +1574,7 @@ SequenceDependencyCommandList(Oid relationId) CreateSequenceDependencyCommand(relationId, sequenceId, columnName); sequenceCommandList = lappend(sequenceCommandList, - sequenceDependencyCommand); + makeTableDDLCommandString(sequenceDependencyCommand)); } return sequenceCommandList; @@ -1937,18 +1937,16 @@ CreateShellTableOnWorkers(Oid relationId) IncludeSequenceDefaults includeSequenceDefaults = WORKER_NEXTVAL_SEQUENCE_DEFAULTS; + bool associateSequenceDependency = true; List *tableDDLCommands = GetFullTableCreationCommands(relationId, - includeSequenceDefaults); + includeSequenceDefaults, + associateSequenceDependency); TableDDLCommand *tableDDLCommand = NULL; foreach_ptr(tableDDLCommand, tableDDLCommands) { Assert(CitusIsA(tableDDLCommand, TableDDLCommand)); commandList = lappend(commandList, GetTableDDLCommand(tableDDLCommand)); } - - /* command to associate sequences with table */ - List *sequenceDependencyCommandList = SequenceDependencyCommandList(relationId); - commandList = list_concat(commandList, sequenceDependencyCommandList); } if (!IsForeignTable(relationId)) diff --git a/src/backend/distributed/operations/node_protocol.c b/src/backend/distributed/operations/node_protocol.c index da9314143..2c04bd6f4 100644 --- a/src/backend/distributed/operations/node_protocol.c +++ b/src/backend/distributed/operations/node_protocol.c @@ -141,8 +141,10 @@ master_get_table_ddl_events(PG_FUNCTION_ARGS) functionContext->multi_call_memory_ctx); /* allocate DDL statements, and then save position in DDL statements */ + bool associateSequenceDependency = false; List *tableDDLEventList = GetFullTableCreationCommands(relationId, - includeSequenceDefaults); + includeSequenceDefaults, + associateSequenceDependency); tableDDLEventCell = list_head(tableDDLEventList); ListCellAndListWrapper *wrapper = palloc0(sizeof(ListCellAndListWrapper)); wrapper->list = tableDDLEventList; @@ -458,8 +460,9 @@ ResolveRelationId(text *relationName, bool missingOk) * constraint and trigger definitions. */ List * -GetFullTableCreationCommands(Oid relationId, IncludeSequenceDefaults - includeSequenceDefaults) +GetFullTableCreationCommands(Oid relationId, + IncludeSequenceDefaults includeSequenceDefaults, + bool associateSequenceDependency) { List *tableDDLEventList = NIL; @@ -471,6 +474,16 @@ GetFullTableCreationCommands(Oid relationId, IncludeSequenceDefaults List *postLoadCreationCommandList = GetPostLoadTableCreationCommands(relationId, true, true); + if (associateSequenceDependency) + { + /* + * While creating shell tables, we need to associate dependencies between + * sequences and the relation. + */ + List *sequenceDependencyCommandList = SequenceDependencyCommandList(relationId); + tableDDLEventList = list_concat(tableDDLEventList, sequenceDependencyCommandList); + } + tableDDLEventList = list_concat(tableDDLEventList, postLoadCreationCommandList); return tableDDLEventList; diff --git a/src/backend/distributed/operations/stage_protocol.c b/src/backend/distributed/operations/stage_protocol.c index 046fedf19..c133527d6 100644 --- a/src/backend/distributed/operations/stage_protocol.c +++ b/src/backend/distributed/operations/stage_protocol.c @@ -325,8 +325,10 @@ CreateAppendDistributedShardPlacements(Oid relationId, int64 shardId, List *foreignConstraintCommandList = GetReferencingForeignConstaintCommands(relationId); IncludeSequenceDefaults includeSequenceDefaults = NO_SEQUENCE_DEFAULTS; + bool associateSequenceDependency = false; List *ddlCommandList = GetFullTableCreationCommands(relationId, - includeSequenceDefaults); + includeSequenceDefaults, + associateSequenceDependency); uint32 connectionFlag = FOR_DDL; char *relationOwner = TableOwner(relationId); @@ -438,8 +440,10 @@ CreateShardsOnWorkers(Oid distributedRelationId, List *shardPlacements, bool useExclusiveConnection, bool colocatedShard) { IncludeSequenceDefaults includeSequenceDefaults = NO_SEQUENCE_DEFAULTS; + bool associateSequenceDependency = false; List *ddlCommandList = GetFullTableCreationCommands(distributedRelationId, - includeSequenceDefaults); + includeSequenceDefaults, + associateSequenceDependency); List *foreignConstraintCommandList = GetReferencingForeignConstaintCommands(distributedRelationId); diff --git a/src/backend/distributed/test/metadata_sync.c b/src/backend/distributed/test/metadata_sync.c index edac3a5b0..542e37316 100644 --- a/src/backend/distributed/test/metadata_sync.c +++ b/src/backend/distributed/test/metadata_sync.c @@ -48,7 +48,7 @@ activate_node_snapshot(PG_FUNCTION_ARGS) */ WorkerNode *dummyWorkerNode = GetFirstPrimaryWorkerNode(); - List *updateLocalGroupCommand = + List *updateLocalGroupCommand = list_make1(LocalGroupIdUpdateCommand(dummyWorkerNode->groupId)); List *syncObjectDepCommands = SyncObjectDependenciesCommandList(dummyWorkerNode); List *dropSnapshotCommands = NodeMetadataDropCommands(); @@ -59,7 +59,8 @@ activate_node_snapshot(PG_FUNCTION_ARGS) int activateNodeCommandIndex = 0; Oid ddlCommandTypeId = TEXTOID; - activateNodeCommandList = list_concat(activateNodeCommandList, updateLocalGroupCommand); + activateNodeCommandList = list_concat(activateNodeCommandList, + updateLocalGroupCommand); activateNodeCommandList = list_concat(activateNodeCommandList, syncObjectDepCommands); activateNodeCommandList = list_concat(activateNodeCommandList, dropSnapshotCommands); activateNodeCommandList = list_concat(activateNodeCommandList, diff --git a/src/backend/distributed/worker/worker_drop_protocol.c b/src/backend/distributed/worker/worker_drop_protocol.c index 2d99c32bb..81dabab08 100644 --- a/src/backend/distributed/worker/worker_drop_protocol.c +++ b/src/backend/distributed/worker/worker_drop_protocol.c @@ -181,7 +181,7 @@ worker_drop_shell_table(PG_FUNCTION_ARGS) if (IsObjectAddressOwnedByExtension(&distributedTableObject, NULL)) { - PG_RETURN_VOID();; + PG_RETURN_VOID(); } /* Drop dependent sequences from pg_dist_object */ diff --git a/src/include/distributed/coordinator_protocol.h b/src/include/distributed/coordinator_protocol.h index cceb12c40..a78daed1e 100644 --- a/src/include/distributed/coordinator_protocol.h +++ b/src/include/distributed/coordinator_protocol.h @@ -224,7 +224,8 @@ extern uint64 GetNextShardId(void); extern uint64 GetNextPlacementId(void); extern Oid ResolveRelationId(text *relationName, bool missingOk); extern List * GetFullTableCreationCommands(Oid relationId, - IncludeSequenceDefaults includeSequenceDefaults); + IncludeSequenceDefaults includeSequenceDefaults, + bool associateSequenceDependency); extern List * GetPostLoadTableCreationCommands(Oid relationId, bool includeIndexes, bool includeReplicaIdentity); extern List * GetPreLoadTableCreationCommands(Oid relationId, IncludeSequenceDefaults