Skip to content

Commit b59c594

Browse files
authored
Materialize table_types_internal as CTE in sys.all_objects to prevent view inlining (#4682)
The sys.all_objects view joins against sys.table_types_internal to exclude table types from the user table branches. Because table_types_internal is a plain view, the planner inlines its definition (a 4-way join across pg_type, pg_namespace, pg_class, pg_depend) and re-executes it per row inside a nested loop, causing significant performance degradation. This change wraps table_types_internal in a materialized CTE inside sys.all_objects so it is computed once and reused. Task: BABEL-6404 Signed-off-by: Rucha Kulkarni <ruchask@amazon.com>
1 parent c50c662 commit b59c594

File tree

8 files changed

+442
-3
lines changed

8 files changed

+442
-3
lines changed

contrib/babelfishpg_tsql/sql/sys_views.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,9 @@ c.contype = 'c' and c.conrelid != 0;
12931293
GRANT SELECT ON sys.check_constraints TO PUBLIC;
12941294

12951295
create or replace view sys.all_objects as
1296+
WITH tt_internal AS MATERIALIZED (
1297+
SELECT typrelid FROM sys.table_types_internal
1298+
)
12961299
select
12971300
name collate sys.database_default
12981301
, cast (object_id as integer)
@@ -1327,7 +1330,7 @@ select
13271330
, 0 as is_published
13281331
, 0 as is_schema_published
13291332
from pg_class t inner join pg_namespace s on s.oid = t.relnamespace
1330-
left join sys.table_types_internal tt on t.oid = tt.typrelid
1333+
left join tt_internal tt on t.oid = tt.typrelid
13311334
left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id())
13321335
left join sys.shipped_objects_not_in_sys nis on nis.name = t.relname and nis.schemaid = s.oid and nis.type = 'U'
13331336
where t.relpersistence in ('p', 'u', 't')
@@ -1352,7 +1355,7 @@ select
13521355
, 0 as is_published
13531356
, 0 as is_schema_published
13541357
from pg_class t inner join pg_namespace s on s.oid = t.relnamespace
1355-
left join sys.table_types_internal tt on t.oid = tt.typrelid
1358+
left join tt_internal tt on t.oid = tt.typrelid
13561359
left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id())
13571360
left join sys.shipped_objects_not_in_sys nis on nis.name = t.relname and nis.schemaid = s.oid and nis.type = 'U'
13581361
where t.relpersistence in ('p', 'u', 't')

0 commit comments

Comments
 (0)