Repository: babelfish-for-postgresql/babelfish_extensions
Pull request #3690: Add version-based support for sys.sql_logins, sys.server_permissions, and sys.fn_varbintohexsubstring
Author: login: shreyeah38
Created: 2025-04-15T09:22:09Z | Merged: 2025-04-23T06:45:33Z

Description
The following PR aims to provide dummy support of the following objects - sys.sql_logins, sys.server_permissions and sys.fn_varbintohexsubstring before 16_10 or 17_6 version and provide their full support after that.

Signed-off-by: Shreya Rai [shreyaxr@amazon.com](mailto:shreyaxr@amazon.com)


Diff:
diff --git a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.1.0--5.2.0.sql b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.1.0--5.2.0.sql
index d12196adbaf..1bd18e60694 100644
--- a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.1.0--5.2.0.sql
+++ b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.1.0--5.2.0.sql
@@ -919,73 +919,29 @@ BEGIN
 END; 
 $$ LANGUAGE plpgsql STABLE;
 
-CREATE OR REPLACE FUNCTION sys.fn_varbintohexsubstring(set_prefix sys.BIT, expression sys.varbinary, start_offset INT, substr_length INT)
-RETURNS sys.nvarchar AS 
-$$ 
+CREATE OR REPLACE FUNCTION sys.fn_varbintohexsubstring(set_prefix INT, expression sys.varbinary(128), start_offset INT, length_to_return INT) 
+RETURNS sys.nvarchar(128) 
+AS $$ 
 DECLARE 
-    pstrout sys.nvarchar;
-    hex_str text;
 BEGIN 
-    IF expression IS NULL THEN 
-        RETURN NULL;
-    END IF;
-
-    IF substr_length IS NULL OR substr_length <= 0 OR substr_length > sys.LEN(expression) THEN 
-        substr_length := sys.LEN(expression);
-    END IF;
-
-    IF start_offset IS NULL OR start_offset < 1 OR start_offset > sys.LEN(expression) THEN 
-        RETURN NULL;
-    END IF;
-
-    IF (sys.LEN(expression) - start_offset + 1) < substr_length THEN 
-        substr_length := sys.LEN(expression) - start_offset + 1;
-    END IF;
+    RETURN NULL; 
+END; 
+$$ LANGUAGE plpgsql STABLE;
 
-    hex_str := sys.LOWER(pg_catalog.ENCODE(sys.SUBSTRING(expression, start_offset, substr_length)::bytea, 'hex'));
-    
-    pstrout := CASE 
-                WHEN set_prefix IS NULL THEN N''
-                WHEN set_prefix = 0 THEN N'' 
-                ELSE N'0x' 
-               END || hex_str;
-    RETURN pstrout;
-END;
-$$ 
-LANGUAGE plpgsql IMMUTABLE;
-
-CREATE OR REPLACE VIEW sys.server_permissions AS 
-WITH super_user AS (SELECT datdba AS super_user FROM pg_database WHERE datname = CURRENT_DATABASE()) 
-SELECT 
-CAST(100 AS sys.tinyint) AS class,
-CAST('SERVER' AS sys.nvarchar(60)) AS class_desc,
-CAST(0 AS int) AS major_id,
-CAST(0 AS int) AS minor_id,
-CAST(Base.oid AS INT) AS grantee_principal_id,
-CAST((SELECT super_user FROM super_user) AS INT) AS grantor_principal_id,
-CAST('COSQ' AS sys.BPCHAR(4)) AS type,
-CAST('CONNECT SQL' AS sys.nvarchar(128)) AS permission_name,
-CAST('G' AS sys.BPCHAR(1)) AS state,
-CAST('GRANT' AS sys.nvarchar(60)) AS state_desc 
-FROM pg_catalog.pg_roles AS Base 
-INNER JOIN sys.babelfish_authid_login_ext AS Ext ON Base.rolname = Ext.rolname 
-WHERE(pg_has_role(sys.suser_id(), 'sysadmin'::TEXT, 'MEMBER')
-  OR pg_has_role(sys.suser_id(), 'securityadmin'::TEXT, 'MEMBER')
-  OR Base.rolname = sys.suser_name() COLLATE sys.database_default 
-  OR Base.rolname = (SELECT pg_get_userbyid(super_user) FROM super_user))
-  AND Ext.type IN ('S', 'U') 
-UNION ALL 
-SELECT 
-CAST(105 AS sys.tinyint) AS class,
-CAST('ENDPOINT' AS sys.nvarchar(60)) AS class_desc,
-CAST(4 AS int) AS major_id,
-CAST(0 AS int) AS minor_id,
-CAST(2 AS INT) AS grantee_principal_id,
-CAST((SELECT super_user FROM super_user) AS INT) AS grantor_principal_id,
-CAST('CO' AS sys.BPCHAR(4)) AS type,
-CAST('CONNECT' AS sys.nvarchar(128)) AS permission_name,
-CAST('G' AS sys.BPCHAR(1)) AS state,
-CAST('GRANT' AS sys.nvarchar(60)) AS state_desc;
+CREATE OR REPLACE VIEW sys.server_permissions 
+AS
+SELECT
+  CAST(0 as sys.tinyint) AS class,
+  CAST(NULL as sys.nvarchar(60)) AS class_desc,
+  CAST(NULL as INT) AS major_id,
+  CAST(NULL as INT) AS minor_id,
+  CAST(NULL as INT) AS grantee_principal_id,
+  CAST(NULL as INT) AS grantor_principal_id,
+  CAST(NULL as sys.BPCHAR(4)) AS type,
+  CAST(NULL as sys.nvarchar(128)) AS permission_name,
+  CAST(NULL as sys.BPCHAR(1)) AS state,
+  CAST(NULL as sys.nvarchar(60)) AS state_desc
+WHERE FALSE;
 GRANT SELECT ON sys.server_permissions TO PUBLIC;
 
 CREATE OR REPLACE VIEW sys.credentials 
@@ -1001,42 +957,31 @@ SELECT
 WHERE FALSE;
 GRANT SELECT ON sys.credentials TO PUBLIC;
 
-CREATE OR REPLACE VIEW sys.sql_logins AS 
-WITH super_user AS (SELECT pg_get_userbyid(datdba) COLLATE sys.database_default AS super_user FROM pg_database WHERE datname = CURRENT_DATABASE())
+ALTER VIEW IF EXISTS sys.sql_logins RENAME TO sql_logins_deprecated_in_5_2_0;
+
+DROP VIEW IF EXISTS sql_logins_deprecated_in_5_2_0;
+
+CREATE OR REPLACE VIEW sys.sql_logins AS
 SELECT
-  CAST(Ext.orig_loginname AS sys.SYSNAME) AS name,
-  CAST(Base.oid AS INT) AS principal_id,
-  CAST(CAST(Base.oid AS INT) AS sys.varbinary(85)) AS sid,
-  CAST('S' AS sys.BPCHAR(1)) AS type,
-  CAST('SQL_LOGIN' AS sys.NVARCHAR(60)) AS type_desc,
-  CAST(Ext.is_disabled AS INT) AS is_disabled,
-  CAST(Ext.create_date AS SYS.DATETIME) AS create_date,
-  CAST(Ext.modify_date AS SYS.DATETIME) AS modify_date,
-  CAST(Ext.default_database_name AS SYS.SYSNAME) AS default_database_name,
-  CAST(Ext.default_language_name AS SYS.SYSNAME) AS default_language_name,
-  CAST(Ext.credential_id AS INT) AS credential_id,
-  CAST(
-    CASE
-      WHEN Ext.orig_loginname = (SELECT super_user FROM super_user) THEN 0
-      ELSE 1
-    END
-  AS sys.BIT) AS is_policy_checked,
-  CAST(0 AS sys.BIT) AS is_expiration_checked,
-  CAST(
-    CASE
-      WHEN (sys.suser_name() = (SELECT super_user FROM super_user) OR pg_has_role(sys.suser_id(), 'sysadmin'::TEXT, 'MEMBER')) THEN Auth.rolpassword
-      ELSE NULL
-    END
-  AS sys.varbinary(256)) AS password_hash 
-FROM pg_catalog.pg_roles AS Base 
-INNER JOIN sys.babelfish_authid_login_ext AS Ext ON Base.rolname = Ext.rolname 
-LEFT JOIN pg_authid Auth ON Auth.rolname = Base.rolname 
-WHERE(pg_has_role(sys.suser_id(), 'sysadmin'::TEXT, 'MEMBER')
-  OR pg_has_role(sys.suser_id(), 'securityadmin'::TEXT, 'MEMBER')
-  OR Ext.orig_loginname = sys.suser_name()
-  OR Ext.orig_loginname = (SELECT super_user FROM super_user))
-  AND Ext.type = 'S';
+    CAST(NULL as sys.sysname) AS name,
+    CAST(NULL as INT) AS principal_id,
+    CAST(NULL as sys.VARBINARY(85)) AS sid,
+    CAST(NULL as sys.BPCHAR(1)) AS type,
+    CAST(NULL as sys.nvarchar(60)) AS type_desc,
+    CAST(NULL as INT) AS is_disabled,
+    CAST(NULL as sys.DATETIME) AS create_date,
+    CAST(NULL as sys.DATETIME) AS modify_date,
+    CAST(NULL as sys.sysname) AS default_database_name,
+    CAST(NULL as sys.sysname) AS default_language_name,
+    CAST(NULL as INT) AS credential_id,
+    CAST(NULL as INT) AS owning_principal_id,
+    CAST(0 as sys.BIT) AS is_fixed_role,
+    CAST(0 as sys.BIT) AS is_policy_checked,
+    CAST(0 as sys.BIT) AS is_expiration_checked,
+    CAST(NULL as sys.varbinary(256)) AS password_hash
+WHERE FALSE;
 GRANT SELECT ON sys.sql_logins TO PUBLIC;
+
 /* Shows the list of objects where the object owner is not same as schema owner */
 /* Covers tables, views, functions, procedures, sequences, types */
 CREATE OR REPLACE FUNCTION sys.get_schema_object_ownership()
diff --git a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.2.0--5.3.0.sql b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.2.0--5.3.0.sql
index f450ef3647b..637715c4a07 100644
--- a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.2.0--5.3.0.sql
+++ b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.2.0--5.3.0.sql
@@ -3,6 +3,36 @@
 -- add 'sys' to search path for the convenience
 SELECT set_config('search_path', 'sys, '||current_setting('search_path'), false);
 
+-- Drops an object if it does not have any dependent objects.
+-- Is a temporary procedure for use by the upgrade script. Will be dropped at the end of the upgrade.
+-- Please have this be one of the first statements executed in this upgrade script. 
+CREATE OR REPLACE PROCEDURE babelfish_drop_deprecated_object(object_type varchar, schema_name varchar, object_name varchar) AS
+$$
+DECLARE
+    error_msg text;
+    query1 text;
+    query2 text;
+BEGIN
+
+    query1 := pg_catalog.format('alter extension babelfishpg_tsql drop %s %s.%s', object_type, schema_name, object_name);
+    query2 := pg_catalog.format('drop %s %s.%s', object_type, schema_name, object_name);
+
+    execute query1;
+    execute query2;
+EXCEPTION
+    when object_not_in_prerequisite_state then --if 'alter extension' statement fails
+        GET STACKED DIAGNOSTICS error_msg = MESSAGE_TEXT;
+        raise warning '%', error_msg;
+    when dependent_objects_still_exist then --if 'drop view' statement fails
+        GET STACKED DIAGNOSTICS error_msg = MESSAGE_TEXT;
+        raise warning '%', error_msg;
+    when undefined_function then --if 'Deprecated function does not exist'
+        GET STACKED DIAGNOSTICS error_msg = MESSAGE_TEXT;
+        raise warning '%', error_msg;
+end
+$$
+LANGUAGE plpgsql;
+
 CREATE OR REPLACE FUNCTION sys.babelfish_update_server_collation_name() RETURNS VOID
 LANGUAGE C
 AS 'babelfishpg_common', 'babelfish_update_server_collation_name';
@@ -33,6 +63,140 @@ $$
     end;
 $$;
 
+DO $$
+DECLARE
+    exception_message text;
+BEGIN
+    ALTER FUNCTION sys.fn_varbintohexsubstring RENAME TO fn_varbintohexsubstring_deprecated_in_5_3_0;
+EXCEPTION WHEN OTHERS THEN
+    GET STACKED DIAGNOSTICS
+    exception_message = MESSAGE_TEXT;
+    RAISE WARNING '%', exception_message;
+END;
+$$;
+
+CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'fn_varbintohexsubstring_deprecated_in_5_3_0');
+
+CREATE OR REPLACE FUNCTION sys.fn_varbintohexsubstring(set_prefix sys.BIT, expression sys.varbinary, start_offset INT, substr_length INT)
+RETURNS sys.nvarchar AS 
+$$ 
+DECLARE 
+    pstrout sys.nvarchar;
+    hex_str text;
+BEGIN 
+    IF expression IS NULL THEN 
+        RETURN NULL;
+    END IF;
+
+    IF substr_length IS NULL OR substr_length <= 0 OR substr_length > sys.LEN(expression) THEN 
+        substr_length := sys.LEN(expression);
+    END IF;
+
+    IF start_offset IS NULL OR start_offset < 1 OR start_offset > sys.LEN(expression) THEN 
+        RETURN NULL;
+    END IF;
+
+    IF (sys.LEN(expression) - start_offset + 1) < substr_length THEN 
+        substr_length := sys.LEN(expression) - start_offset + 1;
+    END IF;
+
+    hex_str := sys.LOWER(pg_catalog.ENCODE(sys.SUBSTRING(expression, start_offset, substr_length)::bytea, 'hex'));
+    
+    pstrout := CASE 
+                WHEN set_prefix IS NULL THEN N''
+                WHEN set_prefix = 0 THEN N'' 
+                ELSE N'0x' 
+               END || hex_str;
+    RETURN pstrout;
+END;
+$$ 
+LANGUAGE plpgsql IMMUTABLE;
+
+CREATE OR REPLACE VIEW sys.server_permissions AS 
+WITH super_user AS (SELECT datdba AS super_user FROM pg_database WHERE datname = CURRENT_DATABASE()) 
+SELECT 
+CAST(100 AS sys.tinyint) AS class,
+CAST('SERVER' AS sys.nvarchar(60)) AS class_desc,
+CAST(0 AS int) AS major_id,
+CAST(0 AS int) AS minor_id,
+CAST(Base.oid AS INT) AS grantee_principal_id,
+CAST((SELECT super_user FROM super_user) AS INT) AS grantor_principal_id,
+CAST('COSQ' AS sys.BPCHAR(4)) AS type,
+CAST('CONNECT SQL' AS sys.nvarchar(128)) AS permission_name,
+CAST('G' AS sys.BPCHAR(1)) AS state,
+CAST('GRANT' AS sys.nvarchar(60)) AS state_desc 
+FROM pg_catalog.pg_roles AS Base 
+INNER JOIN sys.babelfish_authid_login_ext AS Ext ON Base.rolname = Ext.rolname 
+WHERE(pg_has_role(sys.suser_id(), 'sysadmin'::TEXT, 'MEMBER')
+  OR pg_has_role(sys.suser_id(), 'securityadmin'::TEXT, 'MEMBER')
+  OR Base.rolname = sys.suser_name() COLLATE sys.database_default 
+  OR Base.rolname = (SELECT pg_get_userbyid(super_user) FROM super_user))
+  AND Ext.type IN ('S', 'U') 
+UNION ALL 
+SELECT 
+CAST(105 AS sys.tinyint) AS class,
+CAST('ENDPOINT' AS sys.nvarchar(60)) AS class_desc,
+CAST(4 AS int) AS major_id,
+CAST(0 AS int) AS minor_id,
+CAST(2 AS INT) AS grantee_principal_id,
+CAST((SELECT super_user FROM super_user) AS INT) AS grantor_principal_id,
+CAST('CO' AS sys.BPCHAR(4)) AS type,
+CAST('CONNECT' AS sys.nvarchar(128)) AS permission_name,
+CAST('G' AS sys.BPCHAR(1)) AS state,
+CAST('GRANT' AS sys.nvarchar(60)) AS state_desc;
+GRANT SELECT ON sys.server_permissions TO PUBLIC;
+
+DO $$
+DECLARE
+    exception_message text;
+BEGIN
+    ALTER VIEW sys.sql_logins RENAME TO sql_logins_deprecated_in_5_3_0;
+EXCEPTION WHEN OTHERS THEN
+    GET STACKED DIAGNOSTICS
+    exception_message = MESSAGE_TEXT;
+    RAISE WARNING '%', exception_message;
+END;
+$$;
+
+CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'sql_logins_deprecated_in_5_3_0');
+
+CREATE OR REPLACE VIEW sys.sql_logins AS 
+WITH super_user AS (SELECT pg_get_userbyid(datdba) COLLATE sys.database_default AS super_user FROM pg_database WHERE datname = CURRENT_DATABASE())
+SELECT
+  CAST(Ext.orig_loginname AS sys.SYSNAME) AS name,
+  CAST(Base.oid AS INT) AS principal_id,
+  CAST(CAST(Base.oid AS INT) AS sys.varbinary(85)) AS sid,
+  CAST('S' AS sys.BPCHAR(1)) AS type,
+  CAST('SQL_LOGIN' AS sys.NVARCHAR(60)) AS type_desc,
+  CAST(Ext.is_disabled AS INT) AS is_disabled,
+  CAST(Ext.create_date AS SYS.DATETIME) AS create_date,
+  CAST(Ext.modify_date AS SYS.DATETIME) AS modify_date,
+  CAST(Ext.default_database_name AS SYS.SYSNAME) AS default_database_name,
+  CAST(Ext.default_language_name AS SYS.SYSNAME) AS default_language_name,
+  CAST(Ext.credential_id AS INT) AS credential_id,
+  CAST(
+    CASE
+      WHEN Ext.orig_loginname = (SELECT super_user FROM super_user) THEN 0
+      ELSE 1
+    END
+  AS sys.BIT) AS is_policy_checked,
+  CAST(0 AS sys.BIT) AS is_expiration_checked,
+  CAST(
+    CASE
+      WHEN (sys.suser_name() = (SELECT super_user FROM super_user) OR pg_has_role(sys.suser_id(), 'sysadmin'::TEXT, 'MEMBER')) THEN Auth.rolpassword
+      ELSE NULL
+    END
+  AS sys.varbinary(256)) AS password_hash 
+FROM pg_catalog.pg_roles AS Base 
+INNER JOIN sys.babelfish_authid_login_ext AS Ext ON Base.rolname = Ext.rolname 
+LEFT JOIN pg_authid Auth ON Auth.rolname = Base.rolname 
+WHERE(pg_has_role(sys.suser_id(), 'sysadmin'::TEXT, 'MEMBER')
+  OR pg_has_role(sys.suser_id(), 'securityadmin'::TEXT, 'MEMBER')
+  OR Ext.orig_loginname = sys.suser_name()
+  OR Ext.orig_loginname = (SELECT super_user FROM super_user))
+  AND Ext.type = 'S';
+GRANT SELECT ON sys.sql_logins TO PUBLIC;
+
 -- Please add your SQLs here
 /*
  * Note: These SQL statements may get executed multiple times specially when some features get backpatched.
@@ -40,6 +204,10 @@ $$;
  * final behaviour.
  */
 
+-- Drops the temporary procedure used by the upgrade script.
+-- Please have this be one of the last statements executed in this upgrade script.
+DROP PROCEDURE sys.babelfish_drop_deprecated_object(varchar, varchar, varchar);
+
 -- After upgrade, always run analyze for all babelfish catalogs.
 CALL sys.analyze_babelfish_catalogs();
 -- Reset search_path to not affect any subsequent scripts
diff --git a/test/python/expected/sql_validation_framework/expected_drop.out b/test/python/expected/sql_validation_framework/expected_drop.out
index 7e059f8d0f5..d2653537b07 100644
--- a/test/python/expected/sql_validation_framework/expected_drop.out
+++ b/test/python/expected/sql_validation_framework/expected_drop.out
@@ -91,6 +91,7 @@ Unexpected drop found for procedure sys.babelfish_drop_deprecated_object in file
 Unexpected drop found for procedure sys.babelfish_drop_deprecated_object in file babelfishpg_tsql--4.4.0--5.0.0.sql
 Unexpected drop found for procedure sys.babelfish_drop_deprecated_object in file babelfishpg_tsql--5.0.0--5.1.0.sql
 Unexpected drop found for procedure sys.babelfish_drop_deprecated_object in file babelfishpg_tsql--5.1.0--5.2.0.sql
+Unexpected drop found for procedure sys.babelfish_drop_deprecated_object in file babelfishpg_tsql--5.2.0--5.3.0.sql
 Unexpected drop found for procedure sys.babelfish_drop_deprecated_table in file babelfishpg_tsql--2.1.0--2.2.0.sql
 Unexpected drop found for procedure sys.babelfish_drop_deprecated_view in file babelfishpg_tsql--1.0.0--1.1.0.sql
 Unexpected drop found for procedure sys.babelfish_drop_deprecated_view in file babelfishpg_tsql--2.1.0--2.2.0.sql
@@ -111,3 +112,4 @@ Unexpected drop found for procedure sys.create_xp_qv_in_master_dbo in file babel
 Unexpected drop found for procedure sys.sp_babelfish_grant_usage_to_all in file babelfishpg_tsql--1.1.0--1.2.0.sql
 Unexpected drop found for table "#temp_schedules_to_delete" in file babelfishpg_tsql--4.2.0--4.3.0.sql
 Unexpected drop found for table "#temp_schedules_to_delete" in file babelfishpg_tsql--4.4.0--5.0.0.sql
+Unexpected drop found for view sql_logins_deprecated_in_5_2_0 in file babelfishpg_tsql--5.1.0--5.2.0.sql


Reviews:
user: login: anju15bharti
state: CHANGES_REQUESTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-15T09:26:20Z
user: login: shreyeah38
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-15T09:29:20Z
user: login: shalinilohia50
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-22T16:25:17Z
user: login: shreyeah38
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-22T16:28:17Z
user: login: shalinilohia50
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-04-23T04:37:22Z
user: login: anju15bharti
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-04-23T05:13:00Z
user: login: jsudrik
body: Approved.
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-04-23T06:44:52Z

Review comments:
diff_hunk: @@ -1001,41 +957,25 @@ SELECT
 WHERE FALSE;
 GRANT SELECT ON sys.credentials TO PUBLIC;
 
-CREATE OR REPLACE VIEW sys.sql_logins AS 
-WITH super_user AS (SELECT pg_get_userbyid(datdba) COLLATE sys.database_default AS super_user FROM pg_database WHERE datname = CURRENT_DATABASE())
+CREATE VIEW sys.sql_logins AS
 SELECT
-  CAST(Ext.orig_loginname AS sys.SYSNAME) AS name,
-  CAST(Base.oid AS INT) AS principal_id,
-  CAST(CAST(Base.oid AS INT) AS sys.varbinary(85)) AS sid,
-  CAST('S' AS sys.BPCHAR(1)) AS type,
-  CAST('SQL_LOGIN' AS sys.NVARCHAR(60)) AS type_desc,
-  CAST(Ext.is_disabled AS INT) AS is_disabled,
-  CAST(Ext.create_date AS SYS.DATETIME) AS create_date,
-  CAST(Ext.modify_date AS SYS.DATETIME) AS modify_date,
-  CAST(Ext.default_database_name AS SYS.SYSNAME) AS default_database_name,
-  CAST(Ext.default_language_name AS SYS.SYSNAME) AS default_language_name,
-  CAST(Ext.credential_id AS INT) AS credential_id,
-  CAST(
-    CASE
-      WHEN Ext.orig_loginname = (SELECT super_user FROM super_user) THEN 0
-      ELSE 1
-    END
-  AS sys.BIT) AS is_policy_checked,
-  CAST(0 AS sys.BIT) AS is_expiration_checked,
-  CAST(
-    CASE
-      WHEN (sys.suser_name() = (SELECT super_user FROM super_user) OR pg_has_role(sys.suser_id(), 'sysadmin'::TEXT, 'MEMBER')) THEN Auth.rolpassword
-      ELSE NULL
-    END
-  AS sys.varbinary(256)) AS password_hash 
-FROM pg_catalog.pg_roles AS Base 
-INNER JOIN sys.babelfish_authid_login_ext AS Ext ON Base.rolname = Ext.rolname 
-LEFT JOIN pg_authid Auth ON Auth.rolname = Base.rolname 
-WHERE(pg_has_role(sys.suser_id(), 'sysadmin'::TEXT, 'MEMBER')
-  OR pg_has_role(sys.suser_id(), 'securityadmin'::TEXT, 'MEMBER')
-  OR Ext.orig_loginname = sys.suser_name()
-  OR Ext.orig_loginname = (SELECT super_user FROM super_user))
-  AND Ext.type = 'S';
+    CAST(NULL as sys.sysname) AS name,
+    CAST(NULL as INT) AS principal_id,
+    CAST(NULL as sys.VARBINARY(85)) AS sid,
+    CAST(NULL as sys.BPCHAR(1)) AS type,
+    CAST(NULL as sys.nvarchar(60)) AS type_desc,
+    CAST(NULL as INT) AS is_disabled,
+    CAST(NULL as sys.DATETIME) AS create_date,
+    CAST(NULL as sys.DATETIME) AS modify_date,
+    CAST(NULL as sys.sysname) AS default_database_name,
+    CAST(NULL as sys.sysname) AS default_language_name,
+    CAST(NULL as INT) AS credential_id,
+    CAST(NULL as INT) AS owning_principal_id,
+    CAST(0 as sys.BIT) AS is_fixed_role,
path: contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.1.0--5.2.0.sql
user: login: anju15bharti
body: Check the definition of `sql_logins`, these columns should not be there.
created_at: 2025-04-15T09:25:59Z
author_association: CONTRIBUTOR
diff_hunk: @@ -1001,41 +957,25 @@ SELECT
 WHERE FALSE;
 GRANT SELECT ON sys.credentials TO PUBLIC;
 
-CREATE OR REPLACE VIEW sys.sql_logins AS 
-WITH super_user AS (SELECT pg_get_userbyid(datdba) COLLATE sys.database_default AS super_user FROM pg_database WHERE datname = CURRENT_DATABASE())
+CREATE VIEW sys.sql_logins AS
 SELECT
-  CAST(Ext.orig_loginname AS sys.SYSNAME) AS name,
-  CAST(Base.oid AS INT) AS principal_id,
-  CAST(CAST(Base.oid AS INT) AS sys.varbinary(85)) AS sid,
-  CAST('S' AS sys.BPCHAR(1)) AS type,
-  CAST('SQL_LOGIN' AS sys.NVARCHAR(60)) AS type_desc,
-  CAST(Ext.is_disabled AS INT) AS is_disabled,
-  CAST(Ext.create_date AS SYS.DATETIME) AS create_date,
-  CAST(Ext.modify_date AS SYS.DATETIME) AS modify_date,
-  CAST(Ext.default_database_name AS SYS.SYSNAME) AS default_database_name,
-  CAST(Ext.default_language_name AS SYS.SYSNAME) AS default_language_name,
-  CAST(Ext.credential_id AS INT) AS credential_id,
-  CAST(
-    CASE
-      WHEN Ext.orig_loginname = (SELECT super_user FROM super_user) THEN 0
-      ELSE 1
-    END
-  AS sys.BIT) AS is_policy_checked,
-  CAST(0 AS sys.BIT) AS is_expiration_checked,
-  CAST(
-    CASE
-      WHEN (sys.suser_name() = (SELECT super_user FROM super_user) OR pg_has_role(sys.suser_id(), 'sysadmin'::TEXT, 'MEMBER')) THEN Auth.rolpassword
-      ELSE NULL
-    END
-  AS sys.varbinary(256)) AS password_hash 
-FROM pg_catalog.pg_roles AS Base 
-INNER JOIN sys.babelfish_authid_login_ext AS Ext ON Base.rolname = Ext.rolname 
-LEFT JOIN pg_authid Auth ON Auth.rolname = Base.rolname 
-WHERE(pg_has_role(sys.suser_id(), 'sysadmin'::TEXT, 'MEMBER')
-  OR pg_has_role(sys.suser_id(), 'securityadmin'::TEXT, 'MEMBER')
-  OR Ext.orig_loginname = sys.suser_name()
-  OR Ext.orig_loginname = (SELECT super_user FROM super_user))
-  AND Ext.type = 'S';
+    CAST(NULL as sys.sysname) AS name,
+    CAST(NULL as INT) AS principal_id,
+    CAST(NULL as sys.VARBINARY(85)) AS sid,
+    CAST(NULL as sys.BPCHAR(1)) AS type,
+    CAST(NULL as sys.nvarchar(60)) AS type_desc,
+    CAST(NULL as INT) AS is_disabled,
+    CAST(NULL as sys.DATETIME) AS create_date,
+    CAST(NULL as sys.DATETIME) AS modify_date,
+    CAST(NULL as sys.sysname) AS default_database_name,
+    CAST(NULL as sys.sysname) AS default_language_name,
+    CAST(NULL as INT) AS credential_id,
+    CAST(NULL as INT) AS owning_principal_id,
+    CAST(0 as sys.BIT) AS is_fixed_role,
path: contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.1.0--5.2.0.sql
user: login: shreyeah38
body: Yes, fixed that
created_at: 2025-04-15T09:29:20Z
author_association: CONTRIBUTOR
diff_hunk: @@ -1001,42 +957,29 @@ SELECT
 WHERE FALSE;
 GRANT SELECT ON sys.credentials TO PUBLIC;
 
-CREATE OR REPLACE VIEW sys.sql_logins AS 
-WITH super_user AS (SELECT pg_get_userbyid(datdba) COLLATE sys.database_default AS super_user FROM pg_database WHERE datname = CURRENT_DATABASE())
+ALTER VIEW IF EXISTS sys.sql_logins RENAME TO sql_logins_deprecated_in_5_2_0;
path: contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.1.0--5.2.0.sql
user: login: shalinilohia50
body: Drop this deprecated view if it exists.
created_at: 2025-04-22T16:25:16Z
author_association: CONTRIBUTOR
diff_hunk: @@ -1001,42 +957,29 @@ SELECT
 WHERE FALSE;
 GRANT SELECT ON sys.credentials TO PUBLIC;
 
-CREATE OR REPLACE VIEW sys.sql_logins AS 
-WITH super_user AS (SELECT pg_get_userbyid(datdba) COLLATE sys.database_default AS super_user FROM pg_database WHERE datname = CURRENT_DATABASE())
+ALTER VIEW IF EXISTS sys.sql_logins RENAME TO sql_logins_deprecated_in_5_2_0;
path: contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--5.1.0--5.2.0.sql
user: login: shreyeah38
body: Yes, will fix this 
created_at: 2025-04-22T16:28:17Z
author_association: CONTRIBUTOR

PR comments:
user: login: coveralls
created_at: 2025-04-15T09:57:45Z
body: ## Pull Request Test Coverage Report for [Build 14610460960](https://coveralls.io/builds/73342249)

### Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

- For more information on this, see <a target="_blank" href="https://docs.coveralls.io/build-types#tracking-coverage-changes-with-pull_request-builds">Tracking coverage changes with pull request builds</a>.
- To avoid this issue with future PRs, see these <a target="_blank" href="https://docs.coveralls.io/build-types#recommended-ci-configurations">Recommended CI Configurations</a>.
- For a quick fix, <a target="_blank" href="https://github.blog/changelog/2022-02-03-more-ways-to-keep-your-pull-request-branch-up-to-date/#update-your-pull-request-branch-by-rebasing">rebase this PR at GitHub</a>. Your next report should be accurate.

### Details

* **0** of **0**   changed or added relevant lines in **0** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage increased (+**0.007%**) to **75.14%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/73342249/badge)](https://coveralls.io/builds/73342249) |
| :-- | --: |
| Change from base [Build 14570802832](https://coveralls.io/builds/73309156): |  0.007% |
| Covered Lines: | 47917 |
| Relevant Lines: | 63770 |

---
##### 💛  - [Coveralls](https://coveralls.io)

author_association: COLLABORATOR
user: login: shalinilohia50
created_at: 2025-04-22T13:47:31Z
body: **Issue:** Creation of sys.sql_logins view fails in the upgrade script 5.1->5.2
**Root cause:** 16_10(4_X) has the correct definition of view. In 5.1-> 5.2, you are creating view which has extra columns without deprecating the older version.  
**Potential solution**: Deprecate the view in 5.1->5.2 (if exists) before creating it.

`ALTER VIEW IF EXISTS sys.sql_logins RENAME TO sql_logins_deprecated_in_5_2_0;`

author_association: CONTRIBUTOR