From 760914890d4817a18370d24abeaa31da3818a0fc Mon Sep 17 00:00:00 2001 From: Bricklen Anderson Date: Fri, 10 Oct 2025 14:12:27 -0700 Subject: [PATCH 1/3] feat(db-ops): Allow schemas to have ownership set at creation [ENG_OPER-10194] --- sql/functions/microsharding_ensure_exist.sql | 27 +++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/sql/functions/microsharding_ensure_exist.sql b/sql/functions/microsharding_ensure_exist.sql index 8b3bfba..e8fdf99 100644 --- a/sql/functions/microsharding_ensure_exist.sql +++ b/sql/functions/microsharding_ensure_exist.sql @@ -1,6 +1,9 @@ +DROP FUNCTION IF EXISTS microsharding_ensure_exist(integer, integer); + CREATE OR REPLACE FUNCTION microsharding_ensure_exist( shard_from integer, - shard_to integer = NULL + shard_to integer = NULL, + schema_owner varchar(63) DEFAULT NULL ) RETURNS SETOF regnamespace LANGUAGE plpgsql SET search_path FROM CURRENT @@ -13,6 +16,16 @@ BEGIN IF shard_from < 0 OR shard_to > 9999 THEN RAISE EXCEPTION 'Invalid shard_from or shard_to'; END IF; + + IF schema_owner IS NOT NULL + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = schema_owner + UNION + SELECT 1 FROM pg_catalog.pg_user WHERE usename = schema_owner + ) THEN + RAISE EXCEPTION 'Invalid schema_owner: %', schema_owner; + END IF; + FOR shard IN WITH shards AS ( SELECT microsharding_schema_name_(n) AS shard @@ -22,11 +35,17 @@ BEGIN WHERE NOT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = shards.shard) ORDER BY shards.shard LOOP - EXECUTE format('CREATE SCHEMA %I', shard); + + IF schema_owner IS NOT NULL THEN + EXECUTE format('CREATE SCHEMA IF NOT EXISTS %I AUTHORIZATION %I ', shard, schema_owner); + ELSE + EXECUTE format('CREATE SCHEMA IF NOT EXISTS %I', shard); + END IF; + RETURN NEXT shard::regnamespace; END LOOP; END; $$; -COMMENT ON FUNCTION microsharding_ensure_exist(integer, integer) - IS 'Creates shards (schemas) in the range shard_from..shard_to (inclusive).'; +COMMENT ON FUNCTION microsharding_ensure_exist(integer, integer, varchar) + IS 'Creates shards (schemas) in the range shard_from..shard_to (inclusive).'; \ No newline at end of file From b5014128bfc19d5800a34f7fa8367ab39342deb2 Mon Sep 17 00:00:00 2001 From: Bricklen Anderson Date: Fri, 10 Oct 2025 14:18:37 -0700 Subject: [PATCH 2/3] Made DROP FUNCTION commands idempotent --- sql/pg-microsharding-down.sql | 39 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/sql/pg-microsharding-down.sql b/sql/pg-microsharding-down.sql index 3d94e1f..7913ffc 100644 --- a/sql/pg-microsharding-down.sql +++ b/sql/pg-microsharding-down.sql @@ -1,19 +1,20 @@ -DROP FUNCTION microsharding_active_shards_(); -DROP FUNCTION microsharding_advisory_lock_(); -DROP FUNCTION microsharding_debug_fdw_create(text, text[]); -DROP FUNCTION microsharding_debug_fdw_drop(text); -DROP FUNCTION microsharding_debug_fdw_schemas_(text); -DROP FUNCTION microsharding_debug_views_create(text, text); -DROP FUNCTION microsharding_debug_views_drop(text); -DROP FUNCTION microsharding_do_on_each(text); -DROP FUNCTION microsharding_ensure_absent(integer, integer); -DROP FUNCTION microsharding_ensure_active_shards_(text[]); -DROP FUNCTION microsharding_ensure_active(integer, integer); -DROP FUNCTION microsharding_ensure_exist(integer, integer); -DROP FUNCTION microsharding_ensure_inactive(integer, integer); -DROP FUNCTION microsharding_fmt_(text, integer); -DROP FUNCTION microsharding_list_active_shards(); -DROP FUNCTION microsharding_migration_after(text, text, text); -DROP FUNCTION microsharding_migration_before(text); -DROP FUNCTION microsharding_notice_locks_(text, timestamptz); -DROP FUNCTION microsharding_schema_name_(integer); +DROP FUNCTION IF EXISTS microsharding_active_shards_(); +DROP FUNCTION IF EXISTS microsharding_advisory_lock_(); +DROP FUNCTION IF EXISTS microsharding_debug_fdw_create(text, text[]); +DROP FUNCTION IF EXISTS microsharding_debug_fdw_drop(text); +DROP FUNCTION IF EXISTS microsharding_debug_fdw_schemas_(text); +DROP FUNCTION IF EXISTS microsharding_debug_views_create(text, text); +DROP FUNCTION IF EXISTS microsharding_debug_views_drop(text); +DROP FUNCTION IF EXISTS microsharding_do_on_each(text); +DROP FUNCTION IF EXISTS microsharding_ensure_absent(integer, integer); +DROP FUNCTION IF EXISTS microsharding_ensure_active_shards_(text[]); +DROP FUNCTION IF EXISTS microsharding_ensure_active(integer, integer); +DROP FUNCTION IF EXISTS microsharding_ensure_exist(integer, integer); +DROP FUNCTION IF EXISTS microsharding_ensure_exist(integer, integer, varchar); +DROP FUNCTION IF EXISTS microsharding_ensure_inactive(integer, integer); +DROP FUNCTION IF EXISTS microsharding_fmt_(text, integer); +DROP FUNCTION IF EXISTS microsharding_list_active_shards(); +DROP FUNCTION IF EXISTS microsharding_migration_after(text, text, text); +DROP FUNCTION IF EXISTS microsharding_migration_before(text); +DROP FUNCTION IF EXISTS microsharding_notice_locks_(text, timestamptz); +DROP FUNCTION IF EXISTS microsharding_schema_name_(integer); From 2d187f529dc170eb2255ecac4d6499d916f76244 Mon Sep 17 00:00:00 2001 From: Bricklen Anderson Date: Wed, 15 Oct 2025 16:43:55 -0700 Subject: [PATCH 3/3] Added missing newline --- sql/functions/microsharding_ensure_exist.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/functions/microsharding_ensure_exist.sql b/sql/functions/microsharding_ensure_exist.sql index e8fdf99..3bbb55e 100644 --- a/sql/functions/microsharding_ensure_exist.sql +++ b/sql/functions/microsharding_ensure_exist.sql @@ -48,4 +48,4 @@ END; $$; COMMENT ON FUNCTION microsharding_ensure_exist(integer, integer, varchar) - IS 'Creates shards (schemas) in the range shard_from..shard_to (inclusive).'; \ No newline at end of file + IS 'Creates shards (schemas) in the range shard_from..shard_to (inclusive).';