diff --git a/src/backend/distributed/database/database_sharding.c b/src/backend/distributed/database/database_sharding.c index b6e844890..51cd4a8f6 100644 --- a/src/backend/distributed/database/database_sharding.c +++ b/src/backend/distributed/database/database_sharding.c @@ -24,6 +24,7 @@ #include "distributed/deparse_shard_query.h" #include "distributed/listutils.h" #include "distributed/metadata_sync.h" +#include "distributed/pooler/pgbouncer_manager.h" #include "distributed/remote_commands.h" #include "distributed/shared_library_init.h" #include "distributed/worker_transaction.h" @@ -80,7 +81,7 @@ PreProcessUtilityInDatabaseShard(Node *parseTree, const char *queryString, { if (IsA(parseTree, CreatedbStmt)) { - char *command = DeparseCreatedbStmt(parseTree); + char *command = DeparseCreateDatabaseStmt(parseTree); ExecuteCommandInControlDatabase(command); /* command is fully delegated to control database */ @@ -88,7 +89,7 @@ PreProcessUtilityInDatabaseShard(Node *parseTree, const char *queryString, } else if (IsA(parseTree, DropdbStmt)) { - char *command = DeparseDropdbStmt(parseTree); + char *command = DeparseDropDatabaseStmt(parseTree); ExecuteCommandInControlDatabase(command); /* command is fully delegated to control database */ diff --git a/src/backend/distributed/deparser/deparse_database_stmts.c b/src/backend/distributed/deparser/deparse_database_stmts.c index 1247aba43..864a9cdf7 100644 --- a/src/backend/distributed/deparser/deparse_database_stmts.c +++ b/src/backend/distributed/deparser/deparse_database_stmts.c @@ -196,18 +196,6 @@ DeparseAlterDatabaseSetStmt(Node *node) return str.data; } -char * -DeparseCreateDatabaseSetStmt(Node *node) -{ - CreatedbStmt *stmt = castNode(CreatedbStmt, node); - StringInfoData str = {0}; - initStringInfo(&str); - - AppendCreatedbStmt(&str, stmt); - - return str.data; -} - static void AppendCreatedbStmt(StringInfo buf, CreatedbStmt *stmt) { @@ -338,18 +326,17 @@ AppendCreatedbStmt(StringInfo buf, CreatedbStmt *stmt) } char * -DeparseDropDatabaseStmt(Node *node) +DeparseCreateDatabaseStmt(Node *node) { - DropdbStmt *stmt = castNode(DropdbStmt, node); - StringInfoData str = { 0 }; + CreatedbStmt *stmt = castNode(CreatedbStmt, node); + StringInfoData str = {0}; initStringInfo(&str); - AppendDropDatabaseStmt(&str, stmt); + AppendCreatedbStmt(&str, stmt); return str.data; } - static void AppendDropDatabaseStmt(StringInfo buf, DropdbStmt *stmt) { @@ -373,3 +360,18 @@ AppendDropDatabaseStmt(StringInfo buf, DropdbStmt *stmt) } } } + +char * +DeparseDropDatabaseStmt(Node *node) +{ + DropdbStmt *stmt = castNode(DropdbStmt, node); + StringInfoData str = { 0 }; + initStringInfo(&str); + + AppendDropDatabaseStmt(&str, stmt); + + return str.data; +} + + + diff --git a/src/backend/distributed/metadata/distobject.c b/src/backend/distributed/metadata/distobject.c index c6a8b0a22..a025ba73f 100644 --- a/src/backend/distributed/metadata/distobject.c +++ b/src/backend/distributed/metadata/distobject.c @@ -53,7 +53,7 @@ static char * CreatePgDistObjectEntryCommand(const ObjectAddress *objectAddress); static int ExecuteCommandAsSuperuser(char *query, int paramCount, Oid *paramTypes, Datum *paramValues); -static bool IsObjectDistributed(const ObjectAddress *address); +bool IsObjectDistributed(const ObjectAddress *address); PG_FUNCTION_INFO_V1(citus_unmark_object_distributed); PG_FUNCTION_INFO_V1(master_unmark_object_distributed); @@ -392,7 +392,7 @@ UnmarkObjectDistributed(const ObjectAddress *address) * IsObjectDistributed returns if the object addressed is already distributed in the * cluster. This performs a local indexed lookup in pg_dist_object. */ -static bool +bool IsObjectDistributed(const ObjectAddress *address) { ScanKeyData key[3]; diff --git a/src/backend/distributed/metadata/metadata_cache.c b/src/backend/distributed/metadata/metadata_cache.c index 55d0f11c5..108750ed6 100644 --- a/src/backend/distributed/metadata/metadata_cache.c +++ b/src/backend/distributed/metadata/metadata_cache.c @@ -182,6 +182,8 @@ typedef struct MetadataCacheData Oid citusTaskStatusUnscheduledId; Oid citusTaskStatusCancelledId; Oid citusTaskStatusCancellingId; + Oid databaseShardRelationId; + Oid databaseShardPKeyIndexId; Oid distRebalanceStrategyRelationId; Oid distNodeRelationId; Oid distNodeNodeIdIndexId; @@ -2769,12 +2771,34 @@ DistRebalanceStrategyRelationId(void) return MetadataCache.distRebalanceStrategyRelationId; } +/* return oid of citus_catalog.database_sharding relation */ +Oid +DatabaseShardRelationId(void) +{ + CachedRelationNamespaceLookup("database_shard", CitusCatalogNamespaceId(), + &MetadataCache.databaseShardRelationId); + + return MetadataCache.databaseShardRelationId; +} + + +/* return oid of citus_catalog.database_sharding primary key */ +Oid +DatabaseShardPrimaryKeyIndexId(void) +{ + CachedRelationNamespaceLookup("database_shard_pkey", CitusCatalogNamespaceId(), + &MetadataCache.databaseShardPKeyIndexId); + + return MetadataCache.databaseShardPKeyIndexId; +} + + /* return the oid of citus namespace */ Oid CitusCatalogNamespaceId(void) { - CachedNamespaceLookup("citus", &MetadataCache.citusCatalogNamespaceId); + CachedNamespaceLookup("citus_catalog", &MetadataCache.citusCatalogNamespaceId); return MetadataCache.citusCatalogNamespaceId; } @@ -2805,12 +2829,14 @@ DistObjectRelationId(void) true); if (!OidIsValid(MetadataCache.distObjectRelationId)) { + Oid citusNamespaceId = get_namespace_oid("citus", false); + /* * We can only ever reach here while we are creating/altering our extension before * the table is moved to pg_catalog. */ CachedRelationNamespaceLookupExtended("pg_dist_object", - CitusCatalogNamespaceId(), + citusNamespaceId, &MetadataCache.distObjectRelationId, false); } diff --git a/src/backend/distributed/shared_library_init.c b/src/backend/distributed/shared_library_init.c index e5d593295..3cdd3dbaa 100644 --- a/src/backend/distributed/shared_library_init.c +++ b/src/backend/distributed/shared_library_init.c @@ -172,6 +172,8 @@ static GucStringAssignHook OldApplicationNameAssignHook = NULL; */ static bool FinishedStartupCitusBackend = false; +char *CitusMainDatabase = "postgres"; + static object_access_hook_type PrevObjectAccessHook = NULL; #if PG_VERSION_NUM >= PG_VERSION_15 diff --git a/src/include/distributed/commands/utility_hook.h b/src/include/distributed/commands/utility_hook.h index f02f83fe3..9ae57b49a 100644 --- a/src/include/distributed/commands/utility_hook.h +++ b/src/include/distributed/commands/utility_hook.h @@ -40,6 +40,7 @@ typedef enum extern PropSetCmdBehavior PropagateSetCommands; extern bool EnableDDLPropagation; extern int CreateObjectPropagationMode; +extern bool EnableCreateDatabasePropagation; extern bool EnableCreateTypePropagation; extern bool EnableCreateRolePropagation; extern bool EnableAlterRolePropagation; diff --git a/src/include/distributed/metadata_cache.h b/src/include/distributed/metadata_cache.h index 34b95b859..a5e9081a4 100644 --- a/src/include/distributed/metadata_cache.h +++ b/src/include/distributed/metadata_cache.h @@ -247,6 +247,7 @@ extern Oid DistLocalGroupIdRelationId(void); extern Oid DistObjectRelationId(void); extern Oid DistEnabledCustomAggregatesId(void); extern Oid DistTenantSchemaRelationId(void); +extern Oid DatabaseShardRelationId(void); /* index oids */ extern Oid DistNodeNodeIdIndexId(void); @@ -271,6 +272,7 @@ extern Oid DistObjectPrimaryKeyIndexId(void); extern Oid DistCleanupPrimaryKeyIndexId(void); extern Oid DistTenantSchemaPrimaryKeyIndexId(void); extern Oid DistTenantSchemaUniqueColocationIdIndexId(void); +extern Oid DatabaseShardPrimaryKeyIndexId(void); /* sequence oids */ extern Oid DistBackgroundJobJobIdSequenceId(void); diff --git a/src/include/distributed/shared_library_init.h b/src/include/distributed/shared_library_init.h index 3764b52fd..82910d453 100644 --- a/src/include/distributed/shared_library_init.h +++ b/src/include/distributed/shared_library_init.h @@ -17,6 +17,8 @@ #define MAX_SHARD_COUNT 64000 #define MAX_SHARD_REPLICATION_FACTOR 100 +extern char *CitusMainDatabase; + extern PGDLLEXPORT ColumnarSupportsIndexAM_type extern_ColumnarSupportsIndexAM; extern PGDLLEXPORT CompressionTypeStr_type extern_CompressionTypeStr; extern PGDLLEXPORT IsColumnarTableAmTable_type extern_IsColumnarTableAmTable;