-
-
Notifications
You must be signed in to change notification settings - Fork 160
fix: replace CTEs with joins #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,64 @@ SELECT | |
) AS size, | ||
pg_stat_get_live_tuples(c.oid) AS live_rows_estimate, | ||
pg_stat_get_dead_tuples(c.oid) AS dead_rows_estimate, | ||
obj_description(c.oid) AS comment | ||
obj_description(c.oid) AS comment, | ||
coalesce( | ||
jsonb_agg(primary_keys) filter (where primary_keys is not null), | ||
'[]' | ||
) as primary_keys, | ||
coalesce( | ||
jsonb_agg(relationships) filter (where relationships is not null), | ||
'[]' | ||
) as relationships | ||
FROM | ||
pg_namespace nc | ||
JOIN pg_class c ON nc.oid = c.relnamespace | ||
left join ( | ||
select | ||
n.nspname as schema, | ||
c.relname as table_name, | ||
a.attname as name, | ||
c.oid :: int8 as table_id | ||
from | ||
pg_index i, | ||
pg_class c, | ||
pg_attribute a, | ||
pg_namespace n | ||
where | ||
i.indrelid = c.oid | ||
and c.relnamespace = n.oid | ||
and a.attrelid = c.oid | ||
and a.attnum = any (i.indkey) | ||
and i.indisprimary | ||
) as primary_keys | ||
on primary_keys.table_id = c.oid | ||
left join ( | ||
select | ||
c.oid :: int8 as id, | ||
c.conname as constraint_name, | ||
nsa.nspname as source_schema, | ||
csa.relname as source_table_name, | ||
sa.attname as source_column_name, | ||
nta.nspname as target_table_schema, | ||
cta.relname as target_table_name, | ||
ta.attname as target_column_name | ||
from | ||
pg_constraint c | ||
join ( | ||
pg_attribute sa | ||
join pg_class csa on sa.attrelid = csa.oid | ||
join pg_namespace nsa on csa.relnamespace = nsa.oid | ||
) on sa.attrelid = c.conrelid and sa.attnum = any (c.conkey) | ||
join ( | ||
pg_attribute ta | ||
join pg_class cta on ta.attrelid = cta.oid | ||
join pg_namespace nta on cta.relnamespace = nta.oid | ||
) on ta.attrelid = c.confrelid and ta.attnum = any (c.confkey) | ||
where | ||
c.contype = 'f' | ||
Comment on lines
+54
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just |
||
) as relationships | ||
on (relationships.source_schema = nc.nspname and relationships.source_table_name = c.relname) | ||
or (relationships.target_table_schema = nc.nspname and relationships.target_table_name = c.relname) | ||
WHERE | ||
c.relkind IN ('r', 'p') | ||
AND NOT pg_is_other_temp_schema(nc.oid) | ||
|
@@ -31,3 +85,5 @@ WHERE | |
) | ||
OR has_any_column_privilege(c.oid, 'SELECT, INSERT, UPDATE, REFERENCES') | ||
) | ||
group by | ||
c.oid, nc.nspname |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -465,12 +465,12 @@ test('primary keys', async () => { | |
"name": "t", | ||
"primary_keys": [ | ||
{ | ||
"name": "c", | ||
"name": "cc", | ||
"schema": "public", | ||
"table_name": "t", | ||
}, | ||
{ | ||
"name": "cc", | ||
"name": "c", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't guarantee ordering of pkeys, so 🤷 |
||
"schema": "public", | ||
"table_name": "t", | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just
primary_keys.sql
but inlined