-
Notifications
You must be signed in to change notification settings - Fork 3
/
meta.sql
69 lines (59 loc) · 2.27 KB
/
meta.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
BEGIN;
SET LOCAL client_min_messages TO error;
DROP SCHEMA IF EXISTS meta CASCADE;
CREATE SCHEMA meta;
SET LOCAL search_path TO meta; -- All functions will be created in this schema
CREATE VIEW pk AS
SELECT attrelid::regclass AS tab,
array_agg(attname)::name[] AS cols
FROM pg_attribute
JOIN pg_index ON (attrelid = indrelid AND attnum = ANY (indkey))
WHERE indisprimary
GROUP BY attrelid;
CREATE VIEW cols AS
SELECT attrelid::regclass AS tab,
attname::name AS col,
atttypid::regtype AS typ,
attnum AS num
FROM pg_attribute
WHERE attnum > 0
ORDER BY attrelid, attnum;
CREATE VIEW fk AS
SELECT conrelid::regclass AS tab,
names.cols,
confrelid::regclass AS other,
names.refs
FROM pg_constraint,
LATERAL (SELECT array_agg(cols.attname) AS cols,
array_agg(cols.attnum) AS nums,
array_agg(refs.attname) AS refs
FROM unnest(conkey, confkey) AS _(col, ref),
LATERAL (SELECT * FROM pg_attribute
WHERE attrelid = conrelid AND attnum = col)
AS cols,
LATERAL (SELECT * FROM pg_attribute
WHERE attrelid = confrelid AND attnum = ref)
AS refs)
AS names
WHERE confrelid != 0
ORDER BY (conrelid, names.nums); -- Returned in column index order
CREATE FUNCTION schemaname(tab regclass) RETURNS name AS $$
SELECT nspname
FROM pg_class JOIN pg_namespace ON (pg_namespace.oid = relnamespace)
WHERE pg_class.oid = tab
$$ LANGUAGE sql STABLE STRICT;
CREATE FUNCTION tablename(tab regclass) RETURNS name AS $$
SELECT relname FROM pg_class WHERE oid = tab
$$ LANGUAGE sql STABLE STRICT;
CREATE FUNCTION pk(t regclass) RETURNS name[] AS $$
SELECT cols FROM meta.pk WHERE meta.pk.tab = t;
$$ LANGUAGE sql STABLE STRICT;
CREATE FUNCTION cols(t regclass)
RETURNS TABLE (num smallint, col name, typ regtype) AS $$
SELECT num, col, typ FROM meta.cols WHERE meta.cols.tab = t;
$$ LANGUAGE sql STABLE STRICT;
CREATE FUNCTION fk(t regclass)
RETURNS TABLE (cols name[], other regclass, refs name[]) AS $$
SELECT cols, other, refs FROM meta.fk WHERE meta.fk.tab = t;
$$ LANGUAGE sql STABLE STRICT;
END;