Propagate for citus local tables

velioglu/prop_dep_view_review
Ahmet Gedemenli 2022-05-23 17:44:27 +03:00
parent f69bf9ff40
commit 5888cb8d25
3 changed files with 45 additions and 4 deletions

View File

@ -194,7 +194,6 @@ static void EnsureTableNotPartition(Oid relationId);
static TableConversionState * CreateTableConversion(TableConversionParameters *params); static TableConversionState * CreateTableConversion(TableConversionParameters *params);
static void CreateDistributedTableLike(TableConversionState *con); static void CreateDistributedTableLike(TableConversionState *con);
static void CreateCitusTableLike(TableConversionState *con); static void CreateCitusTableLike(TableConversionState *con);
static List * GetViewCreationCommandsOfTable(Oid relationId);
static void ReplaceTable(Oid sourceId, Oid targetId, List *justBeforeDropCommands, static void ReplaceTable(Oid sourceId, Oid targetId, List *justBeforeDropCommands,
bool suppressNoticeMessages); bool suppressNoticeMessages);
static bool HasAnyGeneratedStoredColumns(Oid relationId); static bool HasAnyGeneratedStoredColumns(Oid relationId);
@ -575,7 +574,7 @@ ConvertTable(TableConversionState *con)
List *attachPartitionCommands = NIL; List *attachPartitionCommands = NIL;
postLoadCommands = list_concat(postLoadCommands, postLoadCommands = list_concat(postLoadCommands,
GetViewCreationCommandsOfTable(con->relationId)); GetViewCreationCommandsOfTable(con->relationId, true));
List *foreignKeyCommands = NIL; List *foreignKeyCommands = NIL;
if (con->conversionType == ALTER_DISTRIBUTED_TABLE) if (con->conversionType == ALTER_DISTRIBUTED_TABLE)
@ -1256,7 +1255,7 @@ CreateCitusTableLike(TableConversionState *con)
* that recursively depend on the table too. * that recursively depend on the table too.
*/ */
List * List *
GetViewCreationCommandsOfTable(Oid relationId) GetViewCreationCommandsOfTable(Oid relationId, bool asTableDDLCommand)
{ {
List *views = GetDependingViews(relationId); List *views = GetDependingViews(relationId);
List *commands = NIL; List *commands = NIL;
@ -1281,7 +1280,14 @@ GetViewCreationCommandsOfTable(Oid relationId)
char *alterViewCommmand = AlterViewOwnerCommand(viewOid); char *alterViewCommmand = AlterViewOwnerCommand(viewOid);
appendStringInfoString(query, alterViewCommmand); 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; return commands;

View File

@ -81,6 +81,7 @@ static char * GetRenameShardTriggerCommand(Oid shardRelationId, char *triggerNam
uint64 shardId); uint64 shardId);
static void DropRelationTruncateTriggers(Oid relationId); static void DropRelationTruncateTriggers(Oid relationId);
static char * GetDropTriggerCommand(Oid relationId, char *triggerName); static char * GetDropTriggerCommand(Oid relationId, char *triggerName);
static void DropViewsOnTable(Oid relationId);
static List * GetRenameStatsCommandList(List *statsOidList, uint64 shardId); static List * GetRenameStatsCommandList(List *statsOidList, uint64 shardId);
static void AppendExplicitIndexIdsToList(Form_pg_index indexForm, static void AppendExplicitIndexIdsToList(Form_pg_index indexForm,
List **explicitIndexIdList, List **explicitIndexIdList,
@ -328,6 +329,7 @@ CreateCitusLocalTable(Oid relationId, bool cascadeViaForeignKeys, bool autoConve
EnsureReferenceTablesExistOnAllNodes(); EnsureReferenceTablesExistOnAllNodes();
List *shellTableDDLEvents = GetShellTableDDLEventsForCitusLocalTable(relationId); List *shellTableDDLEvents = GetShellTableDDLEventsForCitusLocalTable(relationId);
List *tableViewCreationCommands = GetViewCreationCommandsOfTable(relationId, false);
char *relationName = get_rel_name(relationId); char *relationName = get_rel_name(relationId);
Oid relationSchemaId = get_rel_namespace(relationId); Oid relationSchemaId = get_rel_namespace(relationId);
@ -342,6 +344,11 @@ CreateCitusLocalTable(Oid relationId, bool cascadeViaForeignKeys, bool autoConve
*/ */
ExecuteAndLogUtilityCommandList(shellTableDDLEvents); ExecuteAndLogUtilityCommandList(shellTableDDLEvents);
/*
* Execute the view creation commands with the shell table.
*/
ExecuteAndLogUtilityCommandList(tableViewCreationCommands);
/* /*
* Set shellRelationId as the relation with relationId now points * Set shellRelationId as the relation with relationId now points
* to the shard relation. * to the shard relation.
@ -699,6 +706,9 @@ ConvertLocalTableToShard(Oid relationId)
*/ */
DropRelationTruncateTriggers(relationId); DropRelationTruncateTriggers(relationId);
/* drop views that depend on the shard table */
DropViewsOnTable(relationId);
/* /*
* We create INSERT|DELETE|UPDATE triggers on shard relation too. * We create INSERT|DELETE|UPDATE triggers on shard relation too.
* This is because citus prevents postgres executor to fire those * 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" * GetExplicitIndexOidList returns a list of index oids defined "explicitly"
* on the relation with relationId by the "CREATE INDEX" commands. That means, * on the relation with relationId by the "CREATE INDEX" commands. That means,

View File

@ -534,6 +534,7 @@ extern ObjectAddress AlterViewStmtObjectAddress(Node *node, bool missing_ok);
extern List * PreprocessDropViewStmt(Node *node, const char *queryString, extern List * PreprocessDropViewStmt(Node *node, const char *queryString,
ProcessUtilityContext processUtilityContext); ProcessUtilityContext processUtilityContext);
extern char * CreateViewDDLCommand(Oid viewOid); extern char * CreateViewDDLCommand(Oid viewOid);
extern List * GetViewCreationCommandsOfTable(Oid relationId, bool asTableDDLCommand);
extern char * AlterViewOwnerCommand(Oid viewOid); extern char * AlterViewOwnerCommand(Oid viewOid);
extern char * DeparseViewStmt(Node *node); extern char * DeparseViewStmt(Node *node);
extern char * DeparseDropViewStmt(Node *node); extern char * DeparseDropViewStmt(Node *node);