diff --git a/src/backend/distributed/commands/alter_table.c b/src/backend/distributed/commands/alter_table.c index ca22f929b..c24e9bb91 100644 --- a/src/backend/distributed/commands/alter_table.c +++ b/src/backend/distributed/commands/alter_table.c @@ -194,7 +194,6 @@ static void EnsureTableNotPartition(Oid relationId); static TableConversionState * CreateTableConversion(TableConversionParameters *params); static void CreateDistributedTableLike(TableConversionState *con); static void CreateCitusTableLike(TableConversionState *con); -static List * GetViewCreationCommandsOfTable(Oid relationId); static void ReplaceTable(Oid sourceId, Oid targetId, List *justBeforeDropCommands, bool suppressNoticeMessages); static bool HasAnyGeneratedStoredColumns(Oid relationId); @@ -575,7 +574,7 @@ ConvertTable(TableConversionState *con) List *attachPartitionCommands = NIL; postLoadCommands = list_concat(postLoadCommands, - GetViewCreationCommandsOfTable(con->relationId)); + GetViewCreationCommandsOfTable(con->relationId, true)); List *foreignKeyCommands = NIL; if (con->conversionType == ALTER_DISTRIBUTED_TABLE) @@ -1256,7 +1255,7 @@ CreateCitusTableLike(TableConversionState *con) * that recursively depend on the table too. */ List * -GetViewCreationCommandsOfTable(Oid relationId) +GetViewCreationCommandsOfTable(Oid relationId, bool asTableDDLCommand) { List *views = GetDependingViews(relationId); List *commands = NIL; @@ -1281,7 +1280,14 @@ GetViewCreationCommandsOfTable(Oid relationId) char *alterViewCommmand = AlterViewOwnerCommand(viewOid); appendStringInfoString(query, alterViewCommmand); - commands = lappend(commands, makeTableDDLCommandString(query->data)); + if (asTableDDLCommand) + { + commands = lappend(commands, makeTableDDLCommandString(query->data)); + } + else + { + commands = lappend(commands, query->data); + } } return commands; 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 f587c81c6..b76d210d3 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 @@ -81,6 +81,7 @@ static char * GetRenameShardTriggerCommand(Oid shardRelationId, char *triggerNam uint64 shardId); static void DropRelationTruncateTriggers(Oid relationId); static char * GetDropTriggerCommand(Oid relationId, char *triggerName); +static void DropViewsOnTable(Oid relationId); static List * GetRenameStatsCommandList(List *statsOidList, uint64 shardId); static void AppendExplicitIndexIdsToList(Form_pg_index indexForm, List **explicitIndexIdList, @@ -328,6 +329,7 @@ CreateCitusLocalTable(Oid relationId, bool cascadeViaForeignKeys, bool autoConve EnsureReferenceTablesExistOnAllNodes(); List *shellTableDDLEvents = GetShellTableDDLEventsForCitusLocalTable(relationId); + List *tableViewCreationCommands = GetViewCreationCommandsOfTable(relationId, false); char *relationName = get_rel_name(relationId); Oid relationSchemaId = get_rel_namespace(relationId); @@ -342,6 +344,11 @@ CreateCitusLocalTable(Oid relationId, bool cascadeViaForeignKeys, bool autoConve */ ExecuteAndLogUtilityCommandList(shellTableDDLEvents); + /* + * Execute the view creation commands with the shell table. + */ + ExecuteAndLogUtilityCommandList(tableViewCreationCommands); + /* * Set shellRelationId as the relation with relationId now points * to the shard relation. @@ -699,6 +706,9 @@ ConvertLocalTableToShard(Oid relationId) */ DropRelationTruncateTriggers(relationId); + /* drop views that depend on the shard table */ + DropViewsOnTable(relationId); + /* * We create INSERT|DELETE|UPDATE triggers on shard relation too. * This is because citus prevents postgres executor to fire those @@ -1019,6 +1029,30 @@ GetDropTriggerCommand(Oid relationId, char *triggerName) } +/* + * DropViewsOnTable drops the views that depend on the given relation. + */ +static void +DropViewsOnTable(Oid relationId) +{ + List *views = GetDependingViews(relationId); + + Oid viewId = InvalidOid; + foreach_oid(viewId, views) + { + char *viewName = get_rel_name(viewId); + char *schemaName = get_namespace_name(get_rel_namespace(viewId)); + char *qualifiedViewName = quote_qualified_identifier(schemaName, viewName); + + StringInfo dropCommand = makeStringInfo(); + appendStringInfo(dropCommand, "DROP VIEW IF EXISTS %s CASCADE", + qualifiedViewName); + + ExecuteAndLogUtilityCommand(dropCommand->data); + } +} + + /* * GetExplicitIndexOidList returns a list of index oids defined "explicitly" * on the relation with relationId by the "CREATE INDEX" commands. That means, diff --git a/src/include/distributed/commands.h b/src/include/distributed/commands.h index 483c279d5..17abc3d77 100644 --- a/src/include/distributed/commands.h +++ b/src/include/distributed/commands.h @@ -534,6 +534,7 @@ extern ObjectAddress AlterViewStmtObjectAddress(Node *node, bool missing_ok); extern List * PreprocessDropViewStmt(Node *node, const char *queryString, ProcessUtilityContext processUtilityContext); extern char * CreateViewDDLCommand(Oid viewOid); +extern List * GetViewCreationCommandsOfTable(Oid relationId, bool asTableDDLCommand); extern char * AlterViewOwnerCommand(Oid viewOid); extern char * DeparseViewStmt(Node *node); extern char * DeparseDropViewStmt(Node *node);