forked from awslabs/amazon-redshift-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v_generate_udf_ddl.sql
105 lines (105 loc) · 3.76 KB
/
v_generate_udf_ddl.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
--DROP VIEW admin.v_generate_udf_ddl;
/**********************************************************************************************
Purpose: View to get the DDL for a UDF.
History:
2016-04-20 chriz-bigdata Created
2018-01-15 pvbouwel Add QUOTE_IDENT for identifiers (function name)
2018-01-24 joeharris76 Support for SQL functions
2019-04-03 adedotua Added schemaname, ending semi-colon and 'OR REPLACE'
2020-04-15 joeharris76 Exclude stored procedures - use `SHOW PROCEDURE sp_name;` instead
2020-08-17 adedotua Updated filter to ignore stored procedures
**********************************************************************************************/
CREATE OR REPLACE VIEW admin.v_generate_udf_ddl
AS
WITH arguments AS (SELECT oid, i, arg_name[i] as argument_name, arg_types[i-1] argument_type
FROM (
SELECT generate_series(1, arg_count) AS i, arg_name, arg_types,oid
FROM (SELECT oid, proargnames arg_name, proargtypes arg_types, pronargs arg_count from pg_proc where proowner != 1) t
) t)
SELECT
schemaname,
udfname,
seq,
trim(ddl) ddl FROM (
SELECT
n.nspname AS schemaname,
p.proname AS udfname,
p.oid AS udfoid,
1000 as seq, ('CREATE OR REPLACE FUNCTION ' || QUOTE_IDENT(n.nspname) ||'.'|| QUOTE_IDENT(p.proname) || ' \(')::varchar(max) as ddl
FROM pg_proc p
LEFT JOIN pg_namespace n on n.oid = p.pronamespace
JOIN pg_language l on p.prolang = l.oid
WHERE p.proowner != 1 AND l.lanname <> 'plpgsql'
UNION ALL
SELECT
n.nspname AS schemaname,
p.proname AS udfname,
p.oid AS udfoid,
2000+nvl(i,0) as seq, case when i = 1 then NVL(argument_name,'') || ' ' || format_type(argument_type,null) else ',' || NVL(argument_name,'') || ' ' || format_type(argument_type,null) end as ddl
FROM pg_proc p
LEFT JOIN pg_namespace n on n.oid = p.pronamespace
LEFT JOIN arguments a on a.oid = p.oid
JOIN pg_language l on p.prolang = l.oid
WHERE p.proowner != 1 AND l.lanname <> 'plpgsql'
UNION ALL
SELECT
n.nspname AS schemaname,
p.proname AS udfname,
p.oid AS udfoid,
3000 as seq, '\)' as ddl
FROM pg_proc p
LEFT JOIN pg_namespace n on n.oid = p.pronamespace
JOIN pg_language l on p.prolang = l.oid
WHERE p.proowner != 1 AND l.lanname <> 'plpgsql'
UNION ALL
SELECT
n.nspname AS schemaname,
p.proname AS udfname,
p.oid AS udfoid,
4000 as seq, ' RETURNS ' || pg_catalog.format_type(p.prorettype, NULL) as ddl
FROM pg_proc p
LEFT JOIN pg_namespace n on n.oid = p.pronamespace
JOIN pg_language l on p.prolang = l.oid
WHERE p.proowner != 1 AND l.lanname <> 'plpgsql'
UNION ALL
SELECT
n.nspname AS schemaname,
p.proname AS udfname,
p.oid AS udfoid,
5000 AS seq, CASE WHEN p.provolatile = 'v' THEN 'VOLATILE' WHEN p.provolatile = 's' THEN 'STABLE' WHEN p.provolatile = 'i' THEN 'IMMUTABLE' ELSE '' END as ddl
FROM pg_proc p
LEFT JOIN pg_namespace n on n.oid = p.pronamespace
JOIN pg_language l on p.prolang = l.oid
WHERE p.proowner != 1 AND l.lanname <> 'plpgsql'
UNION ALL
SELECT
n.nspname AS schemaname,
p.proname AS udfname,
p.oid AS udfoid,
6000 AS seq, 'AS $$' as ddl
FROM pg_proc p
LEFT JOIN pg_namespace n on n.oid = p.pronamespace
JOIN pg_language l on p.prolang = l.oid
WHERE p.proowner != 1 AND l.lanname <> 'plpgsql'
UNION ALL
SELECT
n.nspname AS schemaname,
p.proname AS udfname,
p.oid AS udfoid,
7000 AS seq, p.prosrc as DDL
FROM pg_proc p
LEFT JOIN pg_namespace n on n.oid = p.pronamespace
JOIN pg_language l on p.prolang = l.oid
WHERE p.proowner != 1 AND l.lanname <> 'plpgsql'
UNION ALL
SELECT
n.nspname AS schemaname,
p.proname AS udfname,
p.oid AS udfoid,
8000 as seq, '$$ LANGUAGE ' + lang.lanname + ';' as ddl
FROM pg_proc p
LEFT JOIN pg_namespace n on n.oid = p.pronamespace
LEFT JOIN (select oid, lanname FROM pg_language) lang on p.prolang = lang.oid
WHERE p.proowner != 1 AND lang.lanname <> 'plpgsql'
)
ORDER BY udfoid,seq;