|
1 |
| -SELECT |
2 |
| - c.oid :: int8 AS id, |
3 |
| - c.conname AS constraint_name, |
4 |
| - nsa.nspname AS source_schema, |
5 |
| - csa.relname AS source_table_name, |
6 |
| - sa.attname AS source_column_name, |
7 |
| - nta.nspname AS target_table_schema, |
8 |
| - cta.relname AS target_table_name, |
9 |
| - ta.attname AS target_column_name |
10 |
| -FROM |
11 |
| - pg_constraint c |
12 |
| - JOIN ( |
13 |
| - pg_attribute sa |
14 |
| - JOIN pg_class csa ON sa.attrelid = csa.oid |
15 |
| - JOIN pg_namespace nsa ON csa.relnamespace = nsa.oid |
16 |
| - ) ON sa.attrelid = c.conrelid |
17 |
| - AND sa.attnum = ANY (c.conkey) |
18 |
| - JOIN ( |
19 |
| - pg_attribute ta |
20 |
| - JOIN pg_class cta ON ta.attrelid = cta.oid |
21 |
| - JOIN pg_namespace nta ON cta.relnamespace = nta.oid |
22 |
| - ) ON ta.attrelid = c.confrelid |
23 |
| - AND ta.attnum = ANY (c.confkey) |
24 |
| -WHERE |
25 |
| - c.contype = 'f' |
| 1 | +-- Adapted from |
| 2 | +-- https://github.com/PostgREST/postgrest/blob/f9f0f79fa914ac00c11fbf7f4c558e14821e67e2/src/PostgREST/SchemaCache.hs#L722 |
| 3 | +WITH |
| 4 | + pks_uniques_cols AS ( |
| 5 | + SELECT |
| 6 | + connamespace, |
| 7 | + conrelid, |
| 8 | + jsonb_agg(column_info.cols) as cols |
| 9 | + FROM pg_constraint |
| 10 | + JOIN lateral ( |
| 11 | + SELECT array_agg(cols.attname order by cols.attnum) as cols |
| 12 | + FROM ( select unnest(conkey) as col) _ |
| 13 | + JOIN pg_attribute cols on cols.attrelid = conrelid and cols.attnum = col |
| 14 | + ) column_info ON TRUE |
| 15 | + WHERE |
| 16 | + contype IN ('p', 'u') and |
| 17 | + connamespace::regnamespace::text <> 'pg_catalog' |
| 18 | + GROUP BY connamespace, conrelid |
| 19 | + ) |
| 20 | + SELECT |
| 21 | + traint.oid AS id, |
| 22 | + traint.conname AS foreign_key_name, |
| 23 | + ns1.nspname AS schema, |
| 24 | + tab.relname AS relation, |
| 25 | + column_info.cols AS columns, |
| 26 | + ns2.nspname AS referenced_schema, |
| 27 | + other.relname AS referenced_relation, |
| 28 | + column_info.refs AS referenced_columns |
| 29 | + FROM pg_constraint traint |
| 30 | + JOIN LATERAL ( |
| 31 | + SELECT |
| 32 | + array_agg(row(cols.attname, refs.attname) order by ord) AS cols_and_fcols, |
| 33 | + jsonb_agg(cols.attname order by ord) AS cols, |
| 34 | + jsonb_agg(refs.attname order by ord) AS refs |
| 35 | + FROM unnest(traint.conkey, traint.confkey) WITH ORDINALITY AS _(col, ref, ord) |
| 36 | + JOIN pg_attribute cols ON cols.attrelid = traint.conrelid AND cols.attnum = col |
| 37 | + JOIN pg_attribute refs ON refs.attrelid = traint.confrelid AND refs.attnum = ref |
| 38 | + ) AS column_info ON TRUE |
| 39 | + JOIN pg_namespace ns1 ON ns1.oid = traint.connamespace |
| 40 | + JOIN pg_class tab ON tab.oid = traint.conrelid |
| 41 | + JOIN pg_class other ON other.oid = traint.confrelid |
| 42 | + JOIN pg_namespace ns2 ON ns2.oid = other.relnamespace |
| 43 | + LEFT JOIN pks_uniques_cols pks_uqs ON pks_uqs.connamespace = traint.connamespace AND pks_uqs.conrelid = traint.conrelid |
| 44 | + WHERE traint.contype = 'f' |
0 commit comments