From 3f629a7c14232cb0886c81d61118c4df04c952dc Mon Sep 17 00:00:00 2001 From: Dipesh Dhameliya Date: Thu, 14 Dec 2023 13:36:06 +0530 Subject: [PATCH] Fix system procedure sp_pkeys which may fail to return any row if parallel mode is enforced (#2147) It is Postgres peculiarity with Hash cond (with operator text = name and text = name) that hash functions being used to hash text and name data type values are totally different, for example, hashtext would consider explicit collation whereas hashname does not care about explicit collation. That is the reason why hash condition would fail every time. (hashtext and hashname may produce different hash value for same input under collation other than "C"). This means that whenever there is such hash condition for Hash join then it would always fail and will not return any row. And optimiser can choose such hash join based on stats. Such issue is observed with helper view sys.sp_pkeys_view which is being used by system procedure sp_pkeys causing procedure to return zero rows when optimiser chooses certain hash join. This commit fixes such issue by casting name data type to appropriate T-SQL data type. Task: BABEL-4603 Signed-off-by: Dipesh Dhameliya dddhamel@amazon.com --- .../babelfishpg_tsql/sql/babelfishpg_tsql.sql | 2 +- .../babelfishpg_tsql--3.4.0--3.5.0.sql | 21 +++++++++++++++++++ test/JDBC/parallel_query_jdbc_schedule | 4 ---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql b/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql index 76ce519b30..c195f79555 100644 --- a/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql +++ b/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql @@ -1123,7 +1123,7 @@ FROM pg_catalog.pg_class t1 JOIN sys.pg_namespace_ext t2 ON t1.relnamespace = t2.oid JOIN pg_catalog.pg_roles t3 ON t1.relowner = t3.oid LEFT OUTER JOIN sys.babelfish_namespace_ext ext on t2.nspname = ext.nspname - JOIN information_schema_tsql.columns t4 ON (t1.relname = t4."TABLE_NAME" COLLATE sys.database_default AND ext.orig_name = t4."TABLE_SCHEMA" ) + JOIN information_schema_tsql.columns t4 ON (cast(t1.relname as sys.nvarchar(128)) = t4."TABLE_NAME" AND ext.orig_name = t4."TABLE_SCHEMA" ) JOIN pg_constraint t5 ON t1.oid = t5.conrelid , generate_series(1,16) seq -- SQL server has max 16 columns per primary key WHERE t5.contype = 'p' diff --git a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.4.0--3.5.0.sql b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.4.0--3.5.0.sql index 7b1076d53a..f264604221 100644 --- a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.4.0--3.5.0.sql +++ b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.4.0--3.5.0.sql @@ -38,6 +38,27 @@ LANGUAGE plpgsql; * final behaviour. */ +CREATE OR REPLACE VIEW sys.sp_pkeys_view AS +SELECT +CAST(t4."TABLE_CATALOG" AS sys.sysname) AS TABLE_QUALIFIER, +CAST(t4."TABLE_SCHEMA" AS sys.sysname) AS TABLE_OWNER, +CAST(t1.relname AS sys.sysname) AS TABLE_NAME, +CAST(t4."COLUMN_NAME" AS sys.sysname) AS COLUMN_NAME, +CAST(seq AS smallint) AS KEY_SEQ, +CAST(t5.conname AS sys.sysname) AS PK_NAME +FROM pg_catalog.pg_class t1 + JOIN sys.pg_namespace_ext t2 ON t1.relnamespace = t2.oid + JOIN pg_catalog.pg_roles t3 ON t1.relowner = t3.oid + LEFT OUTER JOIN sys.babelfish_namespace_ext ext on t2.nspname = ext.nspname + JOIN information_schema_tsql.columns t4 ON (cast(t1.relname as sys.nvarchar(128)) = t4."TABLE_NAME" AND ext.orig_name = t4."TABLE_SCHEMA" ) + JOIN pg_constraint t5 ON t1.oid = t5.conrelid + , generate_series(1,16) seq -- SQL server has max 16 columns per primary key +WHERE t5.contype = 'p' + AND CAST(t4."ORDINAL_POSITION" AS smallint) = ANY (t5.conkey) + AND CAST(t4."ORDINAL_POSITION" AS smallint) = t5.conkey[seq] + AND ext.dbid = sys.db_id(); + + -- 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); diff --git a/test/JDBC/parallel_query_jdbc_schedule b/test/JDBC/parallel_query_jdbc_schedule index e0668f01f4..153138d282 100644 --- a/test/JDBC/parallel_query_jdbc_schedule +++ b/test/JDBC/parallel_query_jdbc_schedule @@ -60,10 +60,6 @@ ignore#!#babel_datetime-vu-verify ignore#!#babel_datetime ignore#!#BABEL-2812-vu-verify -# output difference: JIRA-4542 -ignore#!#sys-sp_pkeys-dep-vu-verify -ignore#!#BABEL-SP_PKEYS - # Test failures caused by babel_extra_join_test_cases_northwind failure ignore#!#babelfish_sysdatabases-vu-prepare ignore#!#babelfish_sysdatabases-vu-verify