PR #6728  / commit - 8

Drop table, if exists, during table dependency creation.
pull/6728/head
aykutbozkurt 2023-03-17 22:43:22 +03:00
parent f8fb20cc95
commit cf4e93a332
1 changed files with 20 additions and 0 deletions

View File

@ -36,6 +36,7 @@ static int ObjectAddressComparator(const void *a, const void *b);
static void EnsureDependenciesExistOnAllNodes(const ObjectAddress *target); static void EnsureDependenciesExistOnAllNodes(const ObjectAddress *target);
static List * GetDependencyCreateDDLCommands(const ObjectAddress *dependency); static List * GetDependencyCreateDDLCommands(const ObjectAddress *dependency);
static bool ShouldPropagateObject(const ObjectAddress *address); static bool ShouldPropagateObject(const ObjectAddress *address);
static char * DropTableIfExistsCommand(Oid relationId);
/* /*
* EnsureDependenciesExistOnAllNodes finds all the dependencies that we support and makes * EnsureDependenciesExistOnAllNodes finds all the dependencies that we support and makes
@ -322,6 +323,21 @@ GetDistributableDependenciesForObject(const ObjectAddress *target)
} }
/*
* DropTableIfExistsCommand returns command to drop given table if exists.
*/
static char *
DropTableIfExistsCommand(Oid relationId)
{
char *qualifiedRelationName = generate_qualified_relation_name(relationId);
StringInfo dropTableCommand = makeStringInfo();
appendStringInfo(dropTableCommand, "DROP TABLE IF EXISTS %s CASCADE",
qualifiedRelationName);
return dropTableCommand->data;
}
/* /*
* GetDependencyCreateDDLCommands returns a list (potentially empty or NIL) of ddl * GetDependencyCreateDDLCommands returns a list (potentially empty or NIL) of ddl
* commands to execute on a worker to create the object. * commands to execute on a worker to create the object.
@ -376,6 +392,10 @@ GetDependencyCreateDDLCommands(const ObjectAddress *dependency)
commandList = lappend(commandList, GetTableDDLCommand( commandList = lappend(commandList, GetTableDDLCommand(
tableDDLCommand)); tableDDLCommand));
} }
/* we need to drop table, if exists, first to make table creation idempotent */
commandList = lcons(DropTableIfExistsCommand(relationId),
commandList);
} }
return commandList; return commandList;