Skip to content

Commit

Permalink
Performance improvements when query includes sys.indexes.is_primary_k…
Browse files Browse the repository at this point in the history
…ey predicate (babelfish-for-postgresql#2917)

Previously for a query we were observing a performance degradation when it included a join between sys.indexes, sys.index_columns, sys.columns, sys.tables, and sys.schemas views with sys.indexes.is_primary_key = 1 predicate in where clause. We identified that there was the issue with a case statement due to which there was an incorrect row estimation so we have implemented a direct cast by defining new IMPLICIT cast without function from boolean to sys.bit.
Another issue was the use of redundant has_schema_privilege in sys views and functions, as we explicitly grant usage to public beforehand, so we have removed the redundant has_schema_privilege check.

Issues Resolved: BABEL-5215

Signed-off-by: Tanya Gupta <[email protected]>
  • Loading branch information
tanyagupta17 authored Sep 27, 2024
1 parent 0a7bedb commit 2bb7f70
Show file tree
Hide file tree
Showing 14 changed files with 2,562 additions and 111 deletions.
3 changes: 3 additions & 0 deletions contrib/babelfishpg_common/sql/bit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,6 @@ LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE CAST (sys.VARCHAR AS sys.BIT)
WITH FUNCTION sys.varchar2bit(sys.VARCHAR) AS IMPLICIT;

CREATE CAST (bool AS sys.BIT)
WITHOUT FUNCTION AS IMPLICIT;
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ WITH FUNCTION sys.varchar2time(sys.VARCHAR, INT4) AS IMPLICIT;

CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'varchar2time_deprecated_4_4_0');

-- bool bit cast
DO $$
DECLARE
sys_oid Oid;
pg_catalog_oid Oid;
bool_oid Oid;
bit_oid Oid;
BEGIN
sys_oid := (SELECT oid FROM pg_namespace WHERE pg_namespace.nspname ='sys');
pg_catalog_oid := (SELECT oid FROM pg_namespace WHERE pg_namespace.nspname ='pg_catalog');
bool_oid := (SELECT oid FROM pg_type WHERE pg_type.typname ='bool' AND pg_type.typnamespace = pg_catalog_oid);
bit_oid := (SELECT oid FROM pg_type WHERE pg_type.typname ='bit' AND pg_type.typnamespace = sys_oid);
IF (SELECT COUNT(*) FROM pg_cast WHERE pg_cast.castsource = bool_oid AND pg_cast.casttarget = bit_oid) = 0 THEN
CREATE CAST (bool AS sys.BIT)
WITHOUT FUNCTION AS IMPLICIT;
END IF;
END $$;

-- 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);
Expand Down
5 changes: 1 addition & 4 deletions contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,6 @@ CAST(NULL AS varchar(254)) AS remarks
FROM pg_catalog.pg_class AS t1, sys.pg_namespace_ext AS t2, sys.schemas AS t3
WHERE t1.relnamespace = t3.schema_id AND t1.relnamespace = t2.oid AND t1.relkind IN ('r','p','v','m')
AND t1.relispartition = false
AND has_schema_privilege(t1.relnamespace, 'USAGE')
AND has_table_privilege(t1.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER');
GRANT SELECT ON sys.sp_tables_view TO PUBLIC;
Expand Down Expand Up @@ -1499,8 +1498,7 @@ LEFT JOIN sys.types AS t1 ON a.atttypid = t1.user_type_id
LEFT JOIN sys.sp_datatype_info_helper(2::smallint, false) AS t6 ON T.typname = t6.pg_type_name OR T.typname = t6.type_name --need in order to get accurate DATA_TYPE value
, sys.translate_pg_type_to_tsql(t1.user_type_id) AS tsql_type_name
, sys.translate_pg_type_to_tsql(t1.system_type_id) AS tsql_base_type_name
WHERE has_schema_privilege(s1.schema_id, 'USAGE')
AND X.indislive ;
WHERE X.indislive ;
GRANT SELECT ON sys.sp_special_columns_view TO PUBLIC;
Expand Down Expand Up @@ -1858,7 +1856,6 @@ FROM pg_catalog.pg_proc p
INNER JOIN sys.schemas s1 ON p.pronamespace = s1.schema_id
INNER JOIN sys.databases d ON d.database_id = sys.db_id()
WHERE has_schema_privilege(s1.schema_id, 'USAGE')
UNION
Expand Down
153 changes: 53 additions & 100 deletions contrib/babelfishpg_tsql/sql/sys_views.sql

Large diffs are not rendered by default.

Loading

0 comments on commit 2bb7f70

Please sign in to comment.