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 4f7873ac2..c117d537b 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,10 +658,10 @@ GetShellTableDDLEventsForCitusLocalTable(Oid relationId) */ IncludeSequenceDefaults includeSequenceDefaults = NEXTVAL_SEQUENCE_DEFAULTS; - bool associateSequenceDependency = false; + bool creatingShellTableOnRemoteNode = false; List *tableDDLCommands = GetFullTableCreationCommands(relationId, includeSequenceDefaults, - associateSequenceDependency); + creatingShellTableOnRemoteNode); List *shellTableDDLEvents = NIL; TableDDLCommand *tableDDLCommand = NULL; diff --git a/src/backend/distributed/commands/dependencies.c b/src/backend/distributed/commands/dependencies.c index 1993040df..9d9839b5a 100644 --- a/src/backend/distributed/commands/dependencies.c +++ b/src/backend/distributed/commands/dependencies.c @@ -243,15 +243,16 @@ GetDependencyCreateDDLCommands(const ObjectAddress *dependency) if (IsCitusTable(relationId)) { - bool associateSequenceDependency = true; + bool creatingShellTableOnRemoteNode = true; List *tableDDLCommands = GetFullTableCreationCommands(relationId, WORKER_NEXTVAL_SEQUENCE_DEFAULTS, - associateSequenceDependency); + creatingShellTableOnRemoteNode); TableDDLCommand *tableDDLCommand = NULL; foreach_ptr(tableDDLCommand, tableDDLCommands) { Assert(CitusIsA(tableDDLCommand, TableDDLCommand)); - commandList = lappend(commandList, GetTableDDLCommand(tableDDLCommand)); + commandList = lappend(commandList, GetTableDDLCommand( + tableDDLCommand)); } } diff --git a/src/backend/distributed/metadata/metadata_sync.c b/src/backend/distributed/metadata/metadata_sync.c index f35746043..d47a0c717 100644 --- a/src/backend/distributed/metadata/metadata_sync.c +++ b/src/backend/distributed/metadata/metadata_sync.c @@ -1574,7 +1574,8 @@ SequenceDependencyCommandList(Oid relationId) CreateSequenceDependencyCommand(relationId, sequenceId, columnName); sequenceCommandList = lappend(sequenceCommandList, - makeTableDDLCommandString(sequenceDependencyCommand)); + makeTableDDLCommandString( + sequenceDependencyCommand)); } return sequenceCommandList; @@ -1805,7 +1806,7 @@ GenerateSetRoleQuery(Oid roleOid) * TruncateTriggerCreateCommand creates a SQL query calling worker_create_truncate_trigger * function, which creates the truncate trigger on the worker. */ -char * +TableDDLCommand * TruncateTriggerCreateCommand(Oid relationId) { StringInfo triggerCreateCommand = makeStringInfo(); @@ -1815,7 +1816,10 @@ TruncateTriggerCreateCommand(Oid relationId) "SELECT worker_create_truncate_trigger(%s)", quote_literal_cstr(tableName)); - return triggerCreateCommand->data; + TableDDLCommand *triggerDDLCommand = makeTableDDLCommandString( + triggerCreateCommand->data); + + return triggerDDLCommand; } @@ -1937,10 +1941,10 @@ CreateShellTableOnWorkers(Oid relationId) IncludeSequenceDefaults includeSequenceDefaults = WORKER_NEXTVAL_SEQUENCE_DEFAULTS; - bool associateSequenceDependency = true; + bool creatingShellTableOnRemoteNode = true; List *tableDDLCommands = GetFullTableCreationCommands(relationId, includeSequenceDefaults, - associateSequenceDependency); + creatingShellTableOnRemoteNode); TableDDLCommand *tableDDLCommand = NULL; foreach_ptr(tableDDLCommand, tableDDLCommands) { @@ -1949,12 +1953,6 @@ CreateShellTableOnWorkers(Oid relationId) } } - if (!IsForeignTable(relationId)) - { - char *truncateTriggerCreateCommand = TruncateTriggerCreateCommand(relationId); - commandList = lappend(commandList, truncateTriggerCreateCommand); - } - /* prevent recursive propagation */ SendCommandToWorkersWithMetadata(DISABLE_DDL_PROPAGATION); diff --git a/src/backend/distributed/metadata/node_metadata.c b/src/backend/distributed/metadata/node_metadata.c index 54e594d8b..f9ab89522 100644 --- a/src/backend/distributed/metadata/node_metadata.c +++ b/src/backend/distributed/metadata/node_metadata.c @@ -668,15 +668,6 @@ PgDistTableMetadataSyncCommandList(void) metadataSnapshotCommandList = lappend(metadataSnapshotCommandList, metadataCommand); - /* add the truncate trigger command after the table became distributed */ - if (!IsForeignTable(clusteredTableId)) - { - char *truncateTriggerCreateCommand = - TruncateTriggerCreateCommand(clusteredTableId); - metadataSnapshotCommandList = lappend(metadataSnapshotCommandList, - truncateTriggerCreateCommand); - } - /* add the pg_dist_shard{,placement} entries */ List *shardIntervalList = LoadShardIntervalList(clusteredTableId); List *shardCreateCommandList = ShardListInsertCommand(shardIntervalList); diff --git a/src/backend/distributed/operations/node_protocol.c b/src/backend/distributed/operations/node_protocol.c index 2c04bd6f4..0cb0a5ace 100644 --- a/src/backend/distributed/operations/node_protocol.c +++ b/src/backend/distributed/operations/node_protocol.c @@ -141,10 +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; + bool creatingShellTableOnRemoteNode = false; List *tableDDLEventList = GetFullTableCreationCommands(relationId, includeSequenceDefaults, - associateSequenceDependency); + creatingShellTableOnRemoteNode); tableDDLEventCell = list_head(tableDDLEventList); ListCellAndListWrapper *wrapper = palloc0(sizeof(ListCellAndListWrapper)); wrapper->list = tableDDLEventList; @@ -462,7 +462,7 @@ ResolveRelationId(text *relationName, bool missingOk) List * GetFullTableCreationCommands(Oid relationId, IncludeSequenceDefaults includeSequenceDefaults, - bool associateSequenceDependency) + bool creatingShellTableOnRemoteNode) { List *tableDDLEventList = NIL; @@ -474,14 +474,23 @@ GetFullTableCreationCommands(Oid relationId, List *postLoadCreationCommandList = GetPostLoadTableCreationCommands(relationId, true, true); - if (associateSequenceDependency) + if (creatingShellTableOnRemoteNode) { /* * While creating shell tables, we need to associate dependencies between - * sequences and the relation. + * sequences and the relation. We also need to add truncate trigger for it + * if it is not the foreign table. */ List *sequenceDependencyCommandList = SequenceDependencyCommandList(relationId); tableDDLEventList = list_concat(tableDDLEventList, sequenceDependencyCommandList); + + if (!IsForeignTable(relationId)) + { + TableDDLCommand *truncateTriggerCommand = TruncateTriggerCreateCommand( + relationId); + tableDDLEventList = lappend(tableDDLEventList, + truncateTriggerCommand); + } } tableDDLEventList = list_concat(tableDDLEventList, postLoadCreationCommandList); diff --git a/src/backend/distributed/operations/stage_protocol.c b/src/backend/distributed/operations/stage_protocol.c index c133527d6..c473e7974 100644 --- a/src/backend/distributed/operations/stage_protocol.c +++ b/src/backend/distributed/operations/stage_protocol.c @@ -325,10 +325,10 @@ CreateAppendDistributedShardPlacements(Oid relationId, int64 shardId, List *foreignConstraintCommandList = GetReferencingForeignConstaintCommands(relationId); IncludeSequenceDefaults includeSequenceDefaults = NO_SEQUENCE_DEFAULTS; - bool associateSequenceDependency = false; + bool creatingShellTableOnRemoteNode = false; List *ddlCommandList = GetFullTableCreationCommands(relationId, includeSequenceDefaults, - associateSequenceDependency); + creatingShellTableOnRemoteNode); uint32 connectionFlag = FOR_DDL; char *relationOwner = TableOwner(relationId); @@ -440,10 +440,10 @@ CreateShardsOnWorkers(Oid distributedRelationId, List *shardPlacements, bool useExclusiveConnection, bool colocatedShard) { IncludeSequenceDefaults includeSequenceDefaults = NO_SEQUENCE_DEFAULTS; - bool associateSequenceDependency = false; + bool creatingShellTableOnRemoteNode = false; List *ddlCommandList = GetFullTableCreationCommands(distributedRelationId, includeSequenceDefaults, - associateSequenceDependency); + creatingShellTableOnRemoteNode); List *foreignConstraintCommandList = GetReferencingForeignConstaintCommands(distributedRelationId); diff --git a/src/include/distributed/coordinator_protocol.h b/src/include/distributed/coordinator_protocol.h index a78daed1e..f0bda8515 100644 --- a/src/include/distributed/coordinator_protocol.h +++ b/src/include/distributed/coordinator_protocol.h @@ -225,7 +225,7 @@ extern uint64 GetNextPlacementId(void); extern Oid ResolveRelationId(text *relationName, bool missingOk); extern List * GetFullTableCreationCommands(Oid relationId, IncludeSequenceDefaults includeSequenceDefaults, - bool associateSequenceDependency); + bool creatingShellTableOnRemoteNode); extern List * GetPostLoadTableCreationCommands(Oid relationId, bool includeIndexes, bool includeReplicaIdentity); extern List * GetPreLoadTableCreationCommands(Oid relationId, IncludeSequenceDefaults diff --git a/src/include/distributed/metadata_sync.h b/src/include/distributed/metadata_sync.h index a4fd719dd..31e631790 100644 --- a/src/include/distributed/metadata_sync.h +++ b/src/include/distributed/metadata_sync.h @@ -13,6 +13,7 @@ #define METADATA_SYNC_H +#include "distributed/coordinator_protocol.h" #include "distributed/metadata_cache.h" #include "nodes/pg_list.h" @@ -55,7 +56,7 @@ extern char * CreateSchemaDDLCommand(Oid schemaId); extern List * GrantOnSchemaDDLCommands(Oid schemaId); extern char * PlacementUpsertCommand(uint64 shardId, uint64 placementId, int shardState, uint64 shardLength, int32 groupId); -extern char * TruncateTriggerCreateCommand(Oid relationId); +extern TableDDLCommand * TruncateTriggerCreateCommand(Oid relationId); extern void CreateInterTableRelationshipOfRelationOnWorkers(Oid relationId); extern List * InterTableRelationshipOfRelationCommandList(Oid relationId); extern void CreateShellTableOnWorkers(Oid relationId);