diff --git a/src/backend/distributed/commands/create_citus_local_table.c b/src/backend/distributed/commands/create_citus_local_table.c index a3da24ba0..691f88890 100644 --- a/src/backend/distributed/commands/create_citus_local_table.c +++ b/src/backend/distributed/commands/create_citus_local_table.c @@ -287,8 +287,8 @@ GetShellTableDDLEventsForCitusLocalTable(Oid relationId) */ bool includeSequenceDefaults = true; - List *shellTableDDLEvents = GetTableDDLEvents(relationId, - includeSequenceDefaults); + List *shellTableDDLEvents = GetFullTableCreationCommands(relationId, + includeSequenceDefaults); shellTableDDLEvents = list_concat(shellTableDDLEvents, foreignConstraintCommands); return shellTableDDLEvents; diff --git a/src/backend/distributed/commands/create_distributed_table.c b/src/backend/distributed/commands/create_distributed_table.c index 1740817b4..b7b190d18 100644 --- a/src/backend/distributed/commands/create_distributed_table.c +++ b/src/backend/distributed/commands/create_distributed_table.c @@ -1576,7 +1576,7 @@ RelationUsesHeapAccessMethodOrNone(Relation relation) * * The undistributed table will have the same name, columns and rows. It will also have * partitions, views, sequences of the old table. Finally it will have everything created - * by GetTableConstructionCommands function, which include indexes. These will be + * by GetPostLoadTableCreationCommands function, which include indexes. These will be * re-created during undistribution, so their oids are not preserved either (except for * sequences). However, their names are preserved. * @@ -1618,11 +1618,11 @@ UndistributeTable(Oid relationId) } - List *tableBuildingCommands = GetTableBuildingCommands(relationId, true); - List *tableConstructionCommands = GetTableConstructionCommands(relationId); + List *preLoadCommands = GetPreLoadTableCreationCommands(relationId, true); + List *postLoadCommands = GetPostLoadTableCreationCommands(relationId); - tableConstructionCommands = list_concat(tableConstructionCommands, - GetViewCreationCommandsOfTable(relationId)); + postLoadCommands = list_concat(postLoadCommands, + GetViewCreationCommandsOfTable(relationId)); int spiResult = SPI_connect(); if (spiResult != SPI_OK_CONNECT) @@ -1655,8 +1655,8 @@ UndistributeTable(Oid relationId) { ereport(ERROR, (errmsg("could not run SPI query"))); } - tableBuildingCommands = lappend(tableBuildingCommands, - attachPartitionCommand); + preLoadCommands = lappend(preLoadCommands, + attachPartitionCommand); UndistributeTable(partitionRelationId); } } @@ -1670,7 +1670,7 @@ UndistributeTable(Oid relationId) ereport(NOTICE, (errmsg("Creating a new local table for %s", quote_qualified_identifier(schemaName, relationName)))); - foreach_ptr(tableCreationCommand, tableBuildingCommands) + foreach_ptr(tableCreationCommand, preLoadCommands) { Node *parseTree = ParseTreeNode(tableCreationCommand); @@ -1682,7 +1682,7 @@ UndistributeTable(Oid relationId) ReplaceTable(relationId, get_relname_relid(tempName, schemaId)); char *tableConstructionCommand = NULL; - foreach_ptr(tableConstructionCommand, tableConstructionCommands) + foreach_ptr(tableConstructionCommand, postLoadCommands) { spiResult = SPI_execute(tableConstructionCommand, false, 0); if (spiResult != SPI_OK_UTILITY) diff --git a/src/backend/distributed/metadata/metadata_sync.c b/src/backend/distributed/metadata/metadata_sync.c index a7c85a66e..968cb1299 100644 --- a/src/backend/distributed/metadata/metadata_sync.c +++ b/src/backend/distributed/metadata/metadata_sync.c @@ -398,7 +398,8 @@ MetadataCreateCommands(void) } List *workerSequenceDDLCommands = SequenceDDLCommandsForTable(relationId); - List *ddlCommandList = GetTableDDLEvents(relationId, includeSequenceDefaults); + List *ddlCommandList = GetFullTableCreationCommands(relationId, + includeSequenceDefaults); char *tableOwnerResetCommand = TableOwnerResetCommand(relationId); List *sequenceDependencyCommandList = SequenceDependencyCommandList(relationId); @@ -510,13 +511,10 @@ GetDistributedTableDDLEvents(Oid relationId) commandList = list_concat(commandList, sequenceDDLCommands); /* commands to create the table */ - List *tableDDLCommands = GetTableDDLEvents(relationId, includeSequenceDefaults); + List *tableDDLCommands = GetFullTableCreationCommands(relationId, + includeSequenceDefaults); commandList = list_concat(commandList, tableDDLCommands); - /* command to reset the table owner */ - char *tableOwnerResetCommand = TableOwnerResetCommand(relationId); - commandList = lappend(commandList, tableOwnerResetCommand); - /* command to associate sequences with table */ List *sequenceDependencyCommandList = SequenceDependencyCommandList(relationId); commandList = list_concat(commandList, sequenceDependencyCommandList); diff --git a/src/backend/distributed/operations/node_protocol.c b/src/backend/distributed/operations/node_protocol.c index 07ae278e9..ef8cf1b50 100644 --- a/src/backend/distributed/operations/node_protocol.c +++ b/src/backend/distributed/operations/node_protocol.c @@ -221,7 +221,8 @@ master_get_table_ddl_events(PG_FUNCTION_ARGS) functionContext->multi_call_memory_ctx); /* allocate DDL statements, and then save position in DDL statements */ - List *tableDDLEventList = GetTableDDLEvents(relationId, includeSequenceDefaults); + List *tableDDLEventList = GetFullTableCreationCommands(relationId, + includeSequenceDefaults); tableDDLEventCell = list_head(tableDDLEventList); ListCellAndListWrapper *wrapper = palloc0(sizeof(ListCellAndListWrapper)); wrapper->list = tableDDLEventList; @@ -522,7 +523,7 @@ ResolveRelationId(text *relationName, bool missingOk) /* - * GetTableDDLEvents takes in a relationId, includeSequenceDefaults flag, + * GetFullTableCreationCommands takes in a relationId, includeSequenceDefaults flag, * and returns the list of DDL commands needed to reconstruct the relation. * When the flag includeSequenceDefaults is set, the function also creates * DEFAULT clauses for columns getting their default values from a sequence. @@ -531,28 +532,30 @@ ResolveRelationId(text *relationName, bool missingOk) * constraint and trigger definitions. */ List * -GetTableDDLEvents(Oid relationId, bool includeSequenceDefaults) +GetFullTableCreationCommands(Oid relationId, bool includeSequenceDefaults) { List *tableDDLEventList = NIL; - List *tableCreationCommandList = GetTableCreationCommands(relationId, - includeSequenceDefaults); - tableDDLEventList = list_concat(tableDDLEventList, tableCreationCommandList); + List *preLoadCreationCommandList = + GetPreLoadTableCreationCommands(relationId, includeSequenceDefaults); - List *otherCommands = GetTableConstructionCommands(relationId); - tableDDLEventList = list_concat(tableDDLEventList, otherCommands); + tableDDLEventList = list_concat(tableDDLEventList, preLoadCreationCommandList); + + List *postLoadCreationCommandList = + GetPostLoadTableCreationCommands(relationId); + + tableDDLEventList = list_concat(tableDDLEventList, postLoadCreationCommandList); return tableDDLEventList; } /* - * GetTableConstructionCommands takes in a relationId and returns the list - * of DDL commands needed to reconstruct the relation except the ones that actually - * create the table. + * GetPostLoadTableCreationCommands takes in a relationId and returns the list + * of DDL commands that should be applied after loading the data. */ List * -GetTableConstructionCommands(Oid relationId) +GetPostLoadTableCreationCommands(Oid relationId) { List *tableDDLEventList = NIL; @@ -562,9 +565,6 @@ GetTableConstructionCommands(Oid relationId) List *replicaIdentityEvents = GetTableReplicaIdentityCommand(relationId); tableDDLEventList = list_concat(tableDDLEventList, replicaIdentityEvents); - List *policyCommands = CreatePolicyCommands(relationId); - tableDDLEventList = list_concat(tableDDLEventList, policyCommands); - List *triggerCommands = GetExplicitTriggerCommandList(relationId); tableDDLEventList = list_concat(tableDDLEventList, triggerCommands); @@ -604,12 +604,12 @@ GetTableReplicaIdentityCommand(Oid relationId) /* - * GetTableCreationCommands takes in a relationId, and returns the list of DDL - * commands needed to reconstruct the relation, excluding indexes and - * constraints. + * GetPreLoadTableCreationCommands takes in a relationId, and returns the list of DDL + * commands needed to reconstruct the relation, excluding indexes and constraints, + * to facilitate faster data load. */ List * -GetTableCreationCommands(Oid relationId, bool includeSequenceDefaults) +GetPreLoadTableCreationCommands(Oid relationId, bool includeSequenceDefaults) { List *tableDDLEventList = NIL; @@ -629,30 +629,6 @@ GetTableCreationCommands(Oid relationId, bool includeSequenceDefaults) tableDDLEventList = lappend(tableDDLEventList, serverDef); } - List *tableBuildingCommands = GetTableBuildingCommands(relationId, - includeSequenceDefaults); - tableDDLEventList = list_concat(tableDDLEventList, - tableBuildingCommands); - - /* revert back to original search_path */ - PopOverrideSearchPath(); - - return tableDDLEventList; -} - - -/* - * GetTableBuildingCommands takes in a relationId, and returns the list of DDL - * commands needed to rebuild the relation. This does not include the schema - * and the server commands. - */ -List * -GetTableBuildingCommands(Oid relationId, bool includeSequenceDefaults) -{ - List *tableDDLEventList = NIL; - - PushOverrideEmptySearchPath(CurrentMemoryContext); - /* fetch table schema and column option definitions */ char *tableSchemaDef = pg_get_tableschemadef_string(relationId, includeSequenceDefaults); @@ -670,6 +646,9 @@ GetTableBuildingCommands(Oid relationId, bool includeSequenceDefaults) tableDDLEventList = lappend(tableDDLEventList, tableOwnerDef); } + List *policyCommands = CreatePolicyCommands(relationId); + tableDDLEventList = list_concat(tableDDLEventList, policyCommands); + /* revert back to original search_path */ PopOverrideSearchPath(); diff --git a/src/backend/distributed/operations/repair_shards.c b/src/backend/distributed/operations/repair_shards.c index 82fae08d6..284d5657a 100644 --- a/src/backend/distributed/operations/repair_shards.c +++ b/src/backend/distributed/operations/repair_shards.c @@ -775,7 +775,7 @@ CopyShardCommandList(ShardInterval *shardInterval, const char *sourceNodeName, copyShardDataCommand->data); } - List *indexCommandList = GetTableIndexAndConstraintCommands(relationId); + List *indexCommandList = GetPostLoadTableCreationCommands(relationId); indexCommandList = WorkerApplyShardDDLCommandList(indexCommandList, shardId); copyShardToNodeCommandsList = list_concat(copyShardToNodeCommandsList, @@ -980,8 +980,8 @@ RecreateTableDDLCommandList(Oid relationId) } List *dropCommandList = list_make1(dropCommand->data); - List *createCommandList = GetTableCreationCommands(relationId, - includeSequenceDefaults); + List *createCommandList = GetPreLoadTableCreationCommands(relationId, + includeSequenceDefaults); List *recreateCommandList = list_concat(dropCommandList, createCommandList); return recreateCommandList; diff --git a/src/backend/distributed/operations/stage_protocol.c b/src/backend/distributed/operations/stage_protocol.c index cfbefe406..0793170dc 100644 --- a/src/backend/distributed/operations/stage_protocol.c +++ b/src/backend/distributed/operations/stage_protocol.c @@ -396,7 +396,8 @@ CreateAppendDistributedShardPlacements(Oid relationId, int64 shardId, List *foreignConstraintCommandList = GetReferencingForeignConstaintCommands(relationId); bool includeSequenceDefaults = false; - List *ddlCommandList = GetTableDDLEvents(relationId, includeSequenceDefaults); + List *ddlCommandList = GetFullTableCreationCommands(relationId, + includeSequenceDefaults); uint32 connectionFlag = FOR_DDL; char *relationOwner = TableOwner(relationId); @@ -500,8 +501,8 @@ CreateShardsOnWorkers(Oid distributedRelationId, List *shardPlacements, bool useExclusiveConnection, bool colocatedShard) { bool includeSequenceDefaults = false; - List *ddlCommandList = GetTableDDLEvents(distributedRelationId, - includeSequenceDefaults); + List *ddlCommandList = GetFullTableCreationCommands(distributedRelationId, + includeSequenceDefaults); List *foreignConstraintCommandList = GetReferencingForeignConstaintCommands(distributedRelationId); diff --git a/src/include/distributed/coordinator_protocol.h b/src/include/distributed/coordinator_protocol.h index 8092ea64a..a0efc6cea 100644 --- a/src/include/distributed/coordinator_protocol.h +++ b/src/include/distributed/coordinator_protocol.h @@ -100,10 +100,10 @@ extern bool CStoreTable(Oid relationId); extern uint64 GetNextShardId(void); extern uint64 GetNextPlacementId(void); extern Oid ResolveRelationId(text *relationName, bool missingOk); -extern List * GetTableDDLEvents(Oid relationId, bool forShardCreation); -extern List * GetTableConstructionCommands(Oid relationId); -extern List * GetTableCreationCommands(Oid relationId, bool forShardCreation); -extern List * GetTableBuildingCommands(Oid relationId, bool includeSequenceDefaults); +extern List * GetFullTableCreationCommands(Oid relationId, bool includeSequenceDefaults); +extern List * GetPostLoadTableCreationCommands(Oid relationId); +extern List * GetPreLoadTableCreationCommands(Oid relationId, + bool includeSequenceDefaults); extern List * GetTableIndexAndConstraintCommands(Oid relationId); extern bool IndexImpliedByAConstraint(Form_pg_index indexForm); extern char ShardStorageType(Oid relationId);