forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_function_metadata.sql
146 lines (142 loc) · 5.29 KB
/
pg_function_metadata.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
SELECT
"function_info".function_schema,
"function_info".function_name,
coalesce("function_info".info, '[]'::json) AS info
FROM (
SELECT
function_name,
function_schema,
-- This field corresponds to the 'RawFunctionInfo' Haskell type
json_agg(
json_build_object(
'oid', "pg_function".function_oid,
'description', "pg_function".description,
'has_variadic', "pg_function".has_variadic,
'function_type', "pg_function".function_type,
'return_type_schema', "pg_function".return_type_schema,
'return_type_name', "pg_function".return_type_name,
'return_type_type', "pg_function".return_type_type,
'returns_set', "pg_function".returns_set,
'input_arg_types', "pg_function".input_arg_types,
'input_arg_names', "pg_function".input_arg_names,
'default_args', "pg_function".default_args,
'returns_table', "pg_function".returns_table
)
) AS info
FROM (
-- Necessary metadata from Postgres
SELECT
"function".function_name,
"function".function_schema,
pd.description,
CASE
WHEN ("function".provariadic = (0) :: oid) THEN false
ELSE true
END AS has_variadic,
CASE
WHEN (
("function".provolatile) :: text = ('i' :: character(1)) :: text
) THEN 'IMMUTABLE' :: text
WHEN (
("function".provolatile) :: text = ('s' :: character(1)) :: text
) THEN 'STABLE' :: text
WHEN (
("function".provolatile) :: text = ('v' :: character(1)) :: text
) THEN 'VOLATILE' :: text
ELSE NULL :: text
END AS function_type,
pg_get_functiondef("function".function_oid) AS function_definition,
rtn.nspname::text as return_type_schema,
rt.typname::text as return_type_name,
rt.typtype::text as return_type_type,
"function".proretset AS returns_set,
( SELECT
COALESCE(json_agg(
json_build_object('schema', q."schema",
'name', q."name",
'type', q."type"
)
), '[]')
FROM
(
SELECT
pt.typname AS "name",
pns.nspname AS "schema",
pt.typtype AS "type",
pat.ordinality
FROM
unnest(
COALESCE("function".proallargtypes, ("function".proargtypes) :: oid [])
) WITH ORDINALITY pat(oid, ordinality)
LEFT JOIN pg_type pt ON ((pt.oid = pat.oid))
LEFT JOIN pg_namespace pns ON (pt.typnamespace = pns.oid)
ORDER BY pat.ordinality ASC
) q
) AS input_arg_types,
to_json(COALESCE("function".proargnames, ARRAY [] :: text [])) AS input_arg_names,
"function".pronargdefaults AS default_args,
"function".function_oid::integer AS function_oid,
(exists(
SELECT
1
FROM
information_schema.tables
WHERE
table_schema = rtn.nspname::text
AND table_name = rt.typname::text
) OR
exists(
SELECT
1
FROM
pg_matviews
WHERE
schemaname = rtn.nspname::text
AND matviewname = rt.typname::text
)
) AS returns_table
FROM
jsonb_to_recordset($1::jsonb) AS tracked("schema" text, "name" text)
JOIN
( SELECT p.oid AS function_oid,
p.*,
p.proname::text AS function_name,
pn.nspname::text AS function_schema
FROM pg_proc p
JOIN pg_namespace pn ON (pn.oid = p.pronamespace)
) "function" ON "function".function_name = tracked.name
AND "function".function_schema = tracked.schema
JOIN pg_type rt ON (rt.oid = "function".prorettype)
JOIN pg_namespace rtn ON (rtn.oid = rt.typnamespace)
LEFT JOIN pg_description pd ON "function".function_oid = pd.objoid
WHERE
-- Do not fetch some default functions in public schema
"function".function_name NOT LIKE 'pgp_%'
AND "function".function_name NOT IN
( 'armor'
, 'crypt'
, 'dearmor'
, 'decrypt'
, 'decrypt_iv'
, 'digest'
, 'encrypt'
, 'encrypt_iv'
, 'gen_random_bytes'
, 'gen_random_uuid'
, 'gen_salt'
, 'hmac'
)
AND "function".function_schema NOT LIKE 'pg\_%'
AND "function".function_schema NOT IN ('information_schema', 'hdb_catalog', '_timescaledb_internal')
AND (NOT EXISTS (
SELECT
1
FROM
pg_aggregate
WHERE
((pg_aggregate.aggfnoid) :: oid = "function".function_oid)
)
)
) AS "pg_function"
GROUP BY "pg_function".function_schema, "pg_function".function_name
) "function_info"