From f87a2d02b0ceccba0bf0ad57bc366eb320590d39 Mon Sep 17 00:00:00 2001 From: Onur Tirtir Date: Fri, 14 Apr 2023 16:13:39 +0300 Subject: [PATCH] Move the common logic related to creating a Citus table down to CreateCitusTable (#6836) .. rather than having it in user facing functions. That way, we can use the same logic for creating Citus tables from other places too. This would be useful for creating tenant tables via a simple function call in the utility hook, for schema-based sharding purposes. --- .../commands/create_distributed_table.c | 65 ++++--------------- 1 file changed, 14 insertions(+), 51 deletions(-) diff --git a/src/backend/distributed/commands/create_distributed_table.c b/src/backend/distributed/commands/create_distributed_table.c index a4fb89b87..7e907c8a8 100644 --- a/src/backend/distributed/commands/create_distributed_table.c +++ b/src/backend/distributed/commands/create_distributed_table.c @@ -247,25 +247,6 @@ create_distributed_table(PG_FUNCTION_ARGS) shardCountIsStrict = true; } - EnsureCitusTableCanBeCreated(relationId); - - /* enable create_distributed_table on an empty node */ - InsertCoordinatorIfClusterEmpty(); - - /* - * Lock target relation with an exclusive lock - there's no way to make - * sense of this table until we've committed, and we don't want multiple - * backends manipulating this relation. - */ - Relation relation = try_relation_open(relationId, ExclusiveLock); - if (relation == NULL) - { - ereport(ERROR, (errmsg("could not create distributed table: " - "relation does not exist"))); - } - - relation_close(relation, NoLock); - char *distributionColumnName = text_to_cstring(distributionColumnText); Assert(distributionColumnName != NULL); @@ -887,38 +868,6 @@ create_reference_table(PG_FUNCTION_ARGS) CheckCitusVersion(ERROR); Oid relationId = PG_GETARG_OID(0); - EnsureCitusTableCanBeCreated(relationId); - - /* enable create_reference_table on an empty node */ - InsertCoordinatorIfClusterEmpty(); - - /* - * Lock target relation with an exclusive lock - there's no way to make - * sense of this table until we've committed, and we don't want multiple - * backends manipulating this relation. - */ - Relation relation = try_relation_open(relationId, ExclusiveLock); - if (relation == NULL) - { - ereport(ERROR, (errmsg("could not create reference table: " - "relation does not exist"))); - } - - relation_close(relation, NoLock); - - List *workerNodeList = ActivePrimaryNodeList(ShareLock); - int workerCount = list_length(workerNodeList); - - /* if there are no workers, error out */ - if (workerCount == 0) - { - char *relationName = get_rel_name(relationId); - - ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), - errmsg("cannot create reference table \"%s\"", relationName), - errdetail("There are no active worker nodes."))); - } - CreateReferenceTable(relationId); PG_RETURN_VOID(); } @@ -1058,6 +1007,20 @@ CreateCitusTable(Oid relationId, CitusTableType tableType, "not be otherwise"))); } + EnsureCitusTableCanBeCreated(relationId); + + /* allow creating a Citus table on an empty cluster */ + InsertCoordinatorIfClusterEmpty(); + + Relation relation = try_relation_open(relationId, ExclusiveLock); + if (relation == NULL) + { + ereport(ERROR, (errmsg("could not create Citus table: " + "relation does not exist"))); + } + + relation_close(relation, NoLock); + /* * EnsureTableNotDistributed errors out when relation is a citus table but * we don't want to ask user to first undistribute their citus local tables