From cf4e93a332a2bf53133d48e34b26b30c94245a72 Mon Sep 17 00:00:00 2001 From: aykutbozkurt Date: Fri, 17 Mar 2023 22:43:22 +0300 Subject: [PATCH] =?UTF-8?q?PR=20#6728=20=C2=A0/=20commit=20-=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Drop table, if exists, during table dependency creation. --- .../distributed/commands/dependencies.c | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/backend/distributed/commands/dependencies.c b/src/backend/distributed/commands/dependencies.c index cda2237bc..baa5082d7 100644 --- a/src/backend/distributed/commands/dependencies.c +++ b/src/backend/distributed/commands/dependencies.c @@ -36,6 +36,7 @@ static int ObjectAddressComparator(const void *a, const void *b); static void EnsureDependenciesExistOnAllNodes(const ObjectAddress *target); static List * GetDependencyCreateDDLCommands(const ObjectAddress *dependency); static bool ShouldPropagateObject(const ObjectAddress *address); +static char * DropTableIfExistsCommand(Oid relationId); /* * 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 * commands to execute on a worker to create the object. @@ -376,6 +392,10 @@ GetDependencyCreateDDLCommands(const ObjectAddress *dependency) commandList = lappend(commandList, GetTableDDLCommand( tableDDLCommand)); } + + /* we need to drop table, if exists, first to make table creation idempotent */ + commandList = lcons(DropTableIfExistsCommand(relationId), + commandList); } return commandList;