refactor: Replace pg_namespace joins with ::regnamespace in schema cache
Less joins are much easier to read and understand.
This commit is contained in:
committed by
Wolfgang Walther
parent
5ea83de7ec
commit
1747a4fcc4
@@ -623,14 +623,13 @@ tablesSqlQuery =
|
|||||||
WITH
|
WITH
|
||||||
columns AS (
|
columns AS (
|
||||||
SELECT
|
SELECT
|
||||||
nc.nspname::name AS table_schema,
|
c.oid AS relid,
|
||||||
c.relname::name AS table_name,
|
|
||||||
a.attname::name AS column_name,
|
a.attname::name AS column_name,
|
||||||
d.description AS description,
|
d.description AS description,
|
||||||
-- typbasetype and typdefaultbin handles `CREATE DOMAIN .. DEFAULT val`, attidentity/attgenerated handles generated columns, pg_get_expr gets the default of a column
|
-- typbasetype and typdefaultbin handles `CREATE DOMAIN .. DEFAULT val`, attidentity/attgenerated handles generated columns, pg_get_expr gets the default of a column
|
||||||
CASE
|
CASE
|
||||||
WHEN t.typbasetype != 0 THEN pg_get_expr(t.typdefaultbin, 0)
|
WHEN t.typbasetype != 0 THEN pg_get_expr(t.typdefaultbin, 0)
|
||||||
WHEN a.attidentity = 'd' THEN format('nextval(%s)', quote_literal(seqsch.nspname || '.' || seqclass.relname))
|
WHEN a.attidentity = 'd' THEN format('nextval(%L)', seq.objid::regclass)
|
||||||
WHEN a.attgenerated = 's' THEN null
|
WHEN a.attgenerated = 's' THEN null
|
||||||
ELSE pg_get_expr(ad.adbin, ad.adrelid)::text
|
ELSE pg_get_expr(ad.adbin, ad.adrelid)::text
|
||||||
END AS column_default,
|
END AS column_default,
|
||||||
@@ -638,12 +637,12 @@ tablesSqlQuery =
|
|||||||
CASE
|
CASE
|
||||||
WHEN t.typtype = 'd' THEN
|
WHEN t.typtype = 'd' THEN
|
||||||
CASE
|
CASE
|
||||||
WHEN nbt.oid = 'pg_catalog'::regnamespace THEN format_type(t.typbasetype, NULL::integer)
|
WHEN bt.typnamespace = 'pg_catalog'::regnamespace THEN format_type(t.typbasetype, NULL::integer)
|
||||||
ELSE format_type(a.atttypid, a.atttypmod)
|
ELSE format_type(a.atttypid, a.atttypmod)
|
||||||
END
|
END
|
||||||
ELSE
|
ELSE
|
||||||
CASE
|
CASE
|
||||||
WHEN nt.oid = 'pg_catalog'::regnamespace THEN format_type(a.atttypid, NULL::integer)
|
WHEN t.typnamespace = 'pg_catalog'::regnamespace THEN format_type(a.atttypid, NULL::integer)
|
||||||
ELSE format_type(a.atttypid, a.atttypmod)
|
ELSE format_type(a.atttypid, a.atttypmod)
|
||||||
END
|
END
|
||||||
END::text AS data_type,
|
END::text AS data_type,
|
||||||
@@ -661,18 +660,14 @@ tablesSqlQuery =
|
|||||||
ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
|
ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
|
||||||
JOIN (pg_class c JOIN pg_namespace nc ON c.relnamespace = nc.oid)
|
JOIN (pg_class c JOIN pg_namespace nc ON c.relnamespace = nc.oid)
|
||||||
ON a.attrelid = c.oid
|
ON a.attrelid = c.oid
|
||||||
JOIN (pg_type t JOIN pg_namespace nt ON t.typnamespace = nt.oid)
|
JOIN pg_type t
|
||||||
ON a.atttypid = t.oid
|
ON a.atttypid = t.oid
|
||||||
LEFT JOIN (pg_type bt JOIN pg_namespace nbt ON bt.typnamespace = nbt.oid)
|
LEFT JOIN pg_type bt
|
||||||
ON t.typtype = 'd' AND t.typbasetype = bt.oid
|
ON t.typtype = 'd' AND t.typbasetype = bt.oid
|
||||||
LEFT JOIN pg_depend dep
|
LEFT JOIN pg_depend seq
|
||||||
ON dep.refobjid = a.attrelid and dep.refobjsubid = a.attnum and dep.deptype = 'i'
|
ON seq.refobjid = a.attrelid and seq.refobjsubid = a.attnum and seq.deptype = 'i'
|
||||||
LEFT JOIN pg_class seqclass
|
|
||||||
ON seqclass.oid = dep.objid
|
|
||||||
LEFT JOIN pg_namespace seqsch
|
|
||||||
ON seqsch.oid = seqclass.relnamespace
|
|
||||||
WHERE
|
WHERE
|
||||||
NOT pg_is_other_temp_schema(nc.oid)
|
NOT pg_is_other_temp_schema(c.relnamespace)
|
||||||
AND a.attnum > 0
|
AND a.attnum > 0
|
||||||
AND NOT a.attisdropped
|
AND NOT a.attisdropped
|
||||||
AND c.relkind in ('r', 'v', 'f', 'm', 'p')
|
AND c.relkind in ('r', 'v', 'f', 'm', 'p')
|
||||||
@@ -680,8 +675,7 @@ tablesSqlQuery =
|
|||||||
),
|
),
|
||||||
columns_agg AS (
|
columns_agg AS (
|
||||||
SELECT
|
SELECT
|
||||||
info.table_schema AS table_schema,
|
info.relid,
|
||||||
info.table_name AS table_name,
|
|
||||||
array_agg(row(
|
array_agg(row(
|
||||||
info.column_name,
|
info.column_name,
|
||||||
info.description,
|
info.description,
|
||||||
@@ -702,16 +696,13 @@ tablesSqlQuery =
|
|||||||
JOIN pg_namespace n ON n.oid = t.typnamespace
|
JOIN pg_namespace n ON n.oid = t.typnamespace
|
||||||
GROUP BY s,n
|
GROUP BY s,n
|
||||||
) AS enum_info ON info.udt_name = enum_info.n
|
) AS enum_info ON info.udt_name = enum_info.n
|
||||||
GROUP BY info.table_schema, info.table_name
|
GROUP BY info.relid
|
||||||
),
|
),
|
||||||
tbl_pk_cols AS (
|
tbl_pk_cols AS (
|
||||||
SELECT
|
SELECT
|
||||||
nr.nspname::name AS table_schema,
|
r.oid AS relid,
|
||||||
r.relname::name AS table_name,
|
|
||||||
array_agg(a.attname ORDER BY a.attname) AS pk_cols
|
array_agg(a.attname ORDER BY a.attname) AS pk_cols
|
||||||
FROM pg_namespace nr
|
FROM pg_class r
|
||||||
JOIN pg_class r
|
|
||||||
ON nr.oid = r.relnamespace
|
|
||||||
JOIN pg_constraint c
|
JOIN pg_constraint c
|
||||||
ON r.oid = c.conrelid
|
ON r.oid = c.conrelid
|
||||||
JOIN pg_attribute a
|
JOIN pg_attribute a
|
||||||
@@ -722,7 +713,7 @@ tablesSqlQuery =
|
|||||||
AND r.relnamespace NOT IN ('pg_catalog'::regnamespace, 'information_schema'::regnamespace)
|
AND r.relnamespace NOT IN ('pg_catalog'::regnamespace, 'information_schema'::regnamespace)
|
||||||
AND NOT pg_is_other_temp_schema(r.relnamespace)
|
AND NOT pg_is_other_temp_schema(r.relnamespace)
|
||||||
AND NOT a.attisdropped
|
AND NOT a.attisdropped
|
||||||
GROUP BY table_schema, table_name
|
GROUP BY r.oid
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
n.nspname AS table_schema,
|
n.nspname AS table_schema,
|
||||||
@@ -760,8 +751,8 @@ tablesSqlQuery =
|
|||||||
FROM pg_class c
|
FROM pg_class c
|
||||||
JOIN pg_namespace n ON n.oid = c.relnamespace
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||||
LEFT JOIN pg_description d on d.objoid = c.oid and d.objsubid = 0
|
LEFT JOIN pg_description d on d.objoid = c.oid and d.objsubid = 0
|
||||||
LEFT JOIN tbl_pk_cols tpks ON n.nspname = tpks.table_schema AND c.relname = tpks.table_name
|
LEFT JOIN tbl_pk_cols tpks ON c.oid = tpks.relid
|
||||||
LEFT JOIN columns_agg cols_agg ON n.nspname = cols_agg.table_schema AND c.relname = cols_agg.table_name
|
LEFT JOIN columns_agg cols_agg ON c.oid = cols_agg.relid
|
||||||
WHERE c.relkind IN ('v','r','m','f','p')
|
WHERE c.relkind IN ('v','r','m','f','p')
|
||||||
AND c.relnamespace NOT IN ('pg_catalog'::regnamespace, 'information_schema'::regnamespace)
|
AND c.relnamespace NOT IN ('pg_catalog'::regnamespace, 'information_schema'::regnamespace)
|
||||||
AND not c.relispartition
|
AND not c.relispartition
|
||||||
|
|||||||
Reference in New Issue
Block a user