mx_views_properly
Onder Kalaci 2021-06-29 13:36:01 +02:00
parent c932642e3b
commit a5e09ffa2a
4 changed files with 106 additions and 32 deletions

View File

@ -1120,6 +1120,17 @@ GetViewCreationCommandsOfTable(Oid relationId)
Oid viewOid = InvalidOid; Oid viewOid = InvalidOid;
foreach_oid(viewOid, views) foreach_oid(viewOid, views)
{ {
commands = lappend(commands, makeTableDDLCommandString(GetViewCreationCommand(
viewOid)));
}
return commands;
}
char *
GetViewCreationCommand(Oid viewOid)
{
Datum viewDefinitionDatum = DirectFunctionCall1(pg_get_viewdef, Datum viewDefinitionDatum = DirectFunctionCall1(pg_get_viewdef,
ObjectIdGetDatum(viewOid)); ObjectIdGetDatum(viewOid));
char *viewDefinition = TextDatumGetCString(viewDefinitionDatum); char *viewDefinition = TextDatumGetCString(viewDefinitionDatum);
@ -1132,6 +1143,8 @@ GetViewCreationCommandsOfTable(Oid relationId)
/* here we need to get the access method of the view to recreate it */ /* here we need to get the access method of the view to recreate it */
char *accessMethodName = GetAccessMethodForMatViewIfExists(viewOid); char *accessMethodName = GetAccessMethodForMatViewIfExists(viewOid);
appendStringInfo(query, "DROP VIEW IF EXISTS %s CASCADE;", qualifiedViewName);
appendStringInfoString(query, "CREATE "); appendStringInfoString(query, "CREATE ");
if (isMatView) if (isMatView)
@ -1148,10 +1161,7 @@ GetViewCreationCommandsOfTable(Oid relationId)
appendStringInfo(query, "AS %s", viewDefinition); appendStringInfo(query, "AS %s", viewDefinition);
commands = lappend(commands, makeTableDDLCommandString(query->data)); return query->data;
}
return commands;
} }

View File

@ -69,6 +69,43 @@ EnsureDependenciesExistOnAllNodes(const ObjectAddress *target)
dependenciesWithCommands = lappend(dependenciesWithCommands, dependency); dependenciesWithCommands = lappend(dependenciesWithCommands, dependency);
} }
} }
List *metadataSyncedDependenciesWithCommands = NIL;
if (target->classId == RelationRelationId)
{
/* don't forget partitioned/foreign tables */
if (get_rel_relkind(target->objectId) == RELKIND_RELATION)
{
List *viewIds = GetDependingViews(target->objectId);
Oid viewOid = InvalidOid;
foreach_oid(viewOid, viewIds)
{
ObjectAddress viewAddress = { 0 };
ObjectAddressSet(viewAddress, RelationRelationId, viewOid);
dependencies = GetDependenciesForObject(&viewAddress);
foreach_ptr(dependency, dependencies)
{
List *dependencyCommands = GetDependencyCreateDDLCommands(dependency);
metadataSyncedDependenciesWithCommands = list_concat(
metadataSyncedDependenciesWithCommands, dependencyCommands);
char *viewDefinition = GetViewCreationCommand(viewOid);
metadataSyncedDependenciesWithCommands = lappend(
metadataSyncedDependenciesWithCommands, viewDefinition);
/* create a new list with dependencies that actually created commands */
if (list_length(dependencyCommands) > 0)
{
metadataSyncedDependenciesWithCommands = lappend(
metadataSyncedDependenciesWithCommands, dependency);
}
}
}
}
}
if (list_length(ddlCommands) <= 0) if (list_length(ddlCommands) <= 0)
{ {
/* no ddl commands to be executed */ /* no ddl commands to be executed */
@ -77,6 +114,9 @@ EnsureDependenciesExistOnAllNodes(const ObjectAddress *target)
/* since we are executing ddl commands lets disable propagation, primarily for mx */ /* since we are executing ddl commands lets disable propagation, primarily for mx */
ddlCommands = list_concat(list_make1(DISABLE_DDL_PROPAGATION), ddlCommands); ddlCommands = list_concat(list_make1(DISABLE_DDL_PROPAGATION), ddlCommands);
metadataSyncedDependenciesWithCommands = list_concat(list_make1(
DISABLE_DDL_PROPAGATION),
metadataSyncedDependenciesWithCommands);
/* /*
* Make sure that no new nodes are added after this point until the end of the * Make sure that no new nodes are added after this point until the end of the
@ -112,7 +152,6 @@ EnsureDependenciesExistOnAllNodes(const ObjectAddress *target)
return; return;
} }
WorkerNode *workerNode = NULL; WorkerNode *workerNode = NULL;
foreach_ptr(workerNode, workerNodeList) foreach_ptr(workerNode, workerNodeList)
{ {
@ -123,6 +162,20 @@ EnsureDependenciesExistOnAllNodes(const ObjectAddress *target)
CitusExtensionOwnerName(), CitusExtensionOwnerName(),
ddlCommands); ddlCommands);
} }
foreach_ptr(workerNode, workerNodeList)
{
const char *nodeName = workerNode->workerName;
uint32 nodePort = workerNode->workerPort;
if (workerNode->hasMetadata)
{
SendCommandListToWorkerInSingleTransaction(nodeName, nodePort,
CitusExtensionOwnerName(),
metadataSyncedDependenciesWithCommands);
}
}
} }
@ -182,6 +235,13 @@ GetDependencyCreateDDLCommands(const ObjectAddress *dependency)
{ {
return NIL; return NIL;
} }
else if (get_rel_relkind(dependency->objectId) == RELKIND_VIEW ||
get_rel_relkind(dependency->objectId) == RELKIND_MATVIEW)
{
char *viewDef = GetViewCreationCommand(dependency->objectId);
return list_make1(viewDef);
}
/* if this relation is not supported, break to the error at the end */ /* if this relation is not supported, break to the error at the end */
break; break;

View File

@ -648,7 +648,11 @@ SupportedDependencyByCitus(const ObjectAddress *address)
{ {
return true; return true;
} }
else if (get_rel_relkind(address->objectId) == RELKIND_VIEW ||
get_rel_relkind(address->objectId) == RELKIND_MATVIEW)
{
return true;
}
return false; return false;
} }

View File

@ -248,7 +248,7 @@ extern void CreateDistributedTable(Oid relationId, Var *distributionColumn,
bool viaDeprecatedAPI); bool viaDeprecatedAPI);
extern void CreateTruncateTrigger(Oid relationId); extern void CreateTruncateTrigger(Oid relationId);
extern TableConversionReturn * UndistributeTable(TableConversionParameters *params); extern TableConversionReturn * UndistributeTable(TableConversionParameters *params);
extern char * GetViewCreationCommand(Oid viewOid);
extern void EnsureDependenciesExistOnAllNodes(const ObjectAddress *target); extern void EnsureDependenciesExistOnAllNodes(const ObjectAddress *target);
extern List * GetDistributableDependenciesForObject(const ObjectAddress *target); extern List * GetDistributableDependenciesForObject(const ObjectAddress *target);
extern bool ShouldPropagate(void); extern bool ShouldPropagate(void);