From bb985ef1fe246e36b614cae87bb6b8a8a33a0b7b Mon Sep 17 00:00:00 2001 From: Sena Gungor Date: Mon, 21 Apr 2025 13:47:40 +0000 Subject: [PATCH] Merged PR 1609646: [OSS] Enable user operations from any node ### Does this PR have any customer impact? Yes ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) Feature ### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes) No ### Are you introducing any new config? If yes, do you have tests with and without them being set? No ### ChangeLog (Refer [Template](../oss/CHANGELOG.md)) ### Description Direct user related queries to coordinator ---- #### AI description (iteration 1) #### PR Classification New feature #### PR Summary This pull request enables user operations (create, drop, update) to be executed from any node in the cluster. - `users.c`: Added logic to forward user operations to the metadata coordinator if the current node is not the coordinator. - `users.c`: Implemented error handling and response formatting for distributed user operations. - `users.c`: Included necessary header `api_hooks.h` for the new functionality. --- pg_documentdb/src/commands/users.c | 87 ++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/pg_documentdb/src/commands/users.c b/pg_documentdb/src/commands/users.c index 8c374060..3cd93be2 100644 --- a/pg_documentdb/src/commands/users.c +++ b/pg_documentdb/src/commands/users.c @@ -72,6 +72,35 @@ documentdb_extension_create_user(PG_FUNCTION_ARGS) "'createUser', 'pwd' and 'roles' fields must be specified."))); } + if (!IsMetadataCoordinator()) + { + StringInfo createUserQuery = makeStringInfo(); + appendStringInfo(createUserQuery, + "SELECT %s.create_user(%s::%s.bson)", + ApiSchemaNameV2, + quote_literal_cstr(PgbsonToHexadecimalString(PG_GETARG_PGBSON( + 0))), + CoreSchemaNameV2); + DistributedRunCommandResult result = RunCommandOnMetadataCoordinator( + createUserQuery->data); + + if (!result.success) + { + ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR), + errmsg( + "Internal error creating user in metadata coordinator %s", + text_to_cstring(result.response)), + errdetail_log( + "Internal error creating user in metadata coordinator via distributed call %s", + text_to_cstring(result.response)))); + } + + pgbson_writer finalWriter; + PgbsonWriterInit(&finalWriter); + PgbsonWriterAppendInt32(&finalWriter, "ok", 2, 1); + PG_RETURN_POINTER(PgbsonWriterGetPgbson(&finalWriter)); + } + /*Verify that we have not yet hit the limit of users allowed */ const char *cmdStr = FormatSqlQuery( "SELECT COUNT(*) FROM pg_roles parent JOIN pg_auth_members am ON parent.oid = am.roleid JOIN pg_roles child " \ @@ -432,6 +461,35 @@ documentdb_extension_drop_user(PG_FUNCTION_ARGS) errmsg("'dropUser' is a required field."))); } + if (!IsMetadataCoordinator()) + { + StringInfo dropUserQuery = makeStringInfo(); + appendStringInfo(dropUserQuery, + "SELECT %s.drop_user(%s::%s.bson)", + ApiSchemaNameV2, + quote_literal_cstr(PgbsonToHexadecimalString(PG_GETARG_PGBSON( + 0))), + CoreSchemaNameV2); + DistributedRunCommandResult result = RunCommandOnMetadataCoordinator( + dropUserQuery->data); + + if (!result.success) + { + ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR), + errmsg( + "Internal error dropping user in metadata coordinator %s", + text_to_cstring(result.response)), + errdetail_log( + "Internal error dropping user in metadata coordinator via distributed call %s", + text_to_cstring(result.response)))); + } + + pgbson_writer finalWriter; + PgbsonWriterInit(&finalWriter); + PgbsonWriterAppendInt32(&finalWriter, "ok", 2, 1); + PG_RETURN_POINTER(PgbsonWriterGetPgbson(&finalWriter)); + } + pgbson *dropUserSpec = PG_GETARG_PGBSON(0); char *dropUser = ParseDropUserSpec(dropUserSpec); @@ -555,6 +613,35 @@ documentdb_extension_update_user(PG_FUNCTION_ARGS) errmsg("'updateUser' and 'pwd' are required fields."))); } + if (!IsMetadataCoordinator()) + { + StringInfo updateUserQuery = makeStringInfo(); + appendStringInfo(updateUserQuery, + "SELECT %s.update_user(%s::%s.bson)", + ApiSchemaNameV2, + quote_literal_cstr(PgbsonToHexadecimalString(PG_GETARG_PGBSON( + 0))), + CoreSchemaNameV2); + DistributedRunCommandResult result = RunCommandOnMetadataCoordinator( + updateUserQuery->data); + + if (!result.success) + { + ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR), + errmsg( + "Internal error updating user in metadata coordinator %s", + text_to_cstring(result.response)), + errdetail_log( + "Internal error updating user in metadata coordinator via distributed call %s", + text_to_cstring(result.response)))); + } + + pgbson_writer finalWriter; + PgbsonWriterInit(&finalWriter); + PgbsonWriterAppendInt32(&finalWriter, "ok", 2, 1); + PG_RETURN_POINTER(PgbsonWriterGetPgbson(&finalWriter)); + } + pgbson *updateUserSpec = PG_GETARG_PGBSON(0); UpdateUserSpec *spec = ParseUpdateUserSpec(updateUserSpec);