-
Notifications
You must be signed in to change notification settings - Fork 31
/
pg_tle--1.3.4--1.4.0.sql
182 lines (163 loc) · 6.04 KB
/
pg_tle--1.3.4--1.4.0.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pg_tle" to load this file. \quit
DROP FUNCTION pgtle.create_base_type CASCADE;
DROP FUNCTION pgtle.create_base_type_if_not_exists CASCADE;
CREATE FUNCTION pgtle.create_base_type
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4,
alignment text default 'int4',
storage text default 'plain'
)
RETURNS void
SET search_path TO 'pgtle'
STRICT
AS 'MODULE_PATHNAME', 'pg_tle_create_base_type_with_storage'
LANGUAGE C;
CREATE FUNCTION pgtle.create_base_type_if_not_exists
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4,
alignment text default 'int4',
storage text default 'plain'
)
RETURNS boolean
SET search_path TO 'pgtle'
AS $_pgtleie_$
BEGIN
PERFORM pgtle.create_base_type(typenamespace, typename, infunc, outfunc, internallength, alignment, storage);
RETURN TRUE;
EXCEPTION
-- only catch the duplicate_object exception, let all other exceptions pass through.
WHEN duplicate_object THEN
RETURN FALSE;
END;
$_pgtleie_$
LANGUAGE plpgsql STRICT;
-- Helper function to register features in the feature_info table
CREATE OR REPLACE FUNCTION pgtle.register_feature(proc regproc, feature pgtle.pg_tle_features)
RETURNS VOID
LANGUAGE plpgsql
AS $$
DECLARE
pg_proc_relid oid;
proc_oid oid;
schema_name text;
nspoid oid;
proname text;
proc_schema_name text;
ident text;
passcheck_enabled text;
clientauth_enabled text;
current_db text;
passcheck_db text;
clientauth_db text;
BEGIN
SELECT setting FROM pg_catalog.pg_settings WHERE name = 'pgtle.enable_password_check' INTO passcheck_enabled;
SELECT setting FROM pg_catalog.pg_settings WHERE name = 'pgtle.enable_clientauth' INTO clientauth_enabled;
SELECT pg_catalog.CURRENT_DATABASE() INTO current_db;
SELECT setting FROM pg_catalog.pg_settings WHERE name = 'pgtle.passcheck_db_name' INTO passcheck_db;
SELECT setting FROM pg_catalog.pg_settings WHERE name = 'pgtle.clientauth_db_name' INTO clientauth_db;
IF feature = 'passcheck' THEN
IF passcheck_enabled = 'off' THEN
RAISE NOTICE 'pgtle.enable_password_check is set to off. To enable passcheck, set pgtle.enable_password_check = on';
ELSE
-- passcheck_db_name is an optional param, we only emit a warning if it's non-empty and is not the current database
IF passcheck_db != '' AND current_db != passcheck_db THEN
RAISE NOTICE '%', pg_catalog.FORMAT('pgtle.passcheck_db_name is currently %I. To trigger this passcheck function, register the function in that database.', passcheck_db)
USING HINT = pg_catalog.FORMAT('Alternatively, to use the current database for passcheck, set pgtle.passcheck_db_name = %I and reload the PostgreSQL configuration.', current_db);
END IF;
END IF;
END IF;
IF feature = 'clientauth' THEN
IF clientauth_enabled = 'off' THEN
RAISE NOTICE 'pgtle.enable_clientauth is set to off. To enable clientauth, set pgtle.enable_clientauth = on';
ELSE
IF current_db != clientauth_db THEN
RAISE NOTICE '%', pg_catalog.FORMAT('pgtle.clientauth_db_name is currently %I. To trigger this clientauth function, register the function in that database.', clientauth_db)
USING HINT = pg_catalog.FORMAT('Alternatively, to use the current database for clientauth, set pgtle.clientauth_db_name = %I and reload the PostgreSQL configuration.', current_db);
END IF;
END IF;
END IF;
SELECT oid into nspoid FROM pg_catalog.pg_namespace
where nspname = 'pg_catalog';
SELECT oid into pg_proc_relid from pg_catalog.pg_class
where relname = 'pg_proc' and relnamespace = nspoid;
SELECT pg_namespace.nspname, pg_proc.oid, pg_proc.proname into proc_schema_name, proc_oid, proname FROM
pg_catalog.pg_namespace, pg_catalog.pg_proc
where pg_proc.oid = proc AND pg_proc.pronamespace = pg_namespace.oid;
SELECT identity into ident FROM pg_catalog.pg_identify_object(pg_proc_relid, proc_oid, 0);
INSERT INTO pgtle.feature_info VALUES (feature, proc_schema_name, proname, ident);
END;
$$;
REVOKE EXECUTE ON FUNCTION pgtle.create_base_type
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4,
alignment text,
storage text
) FROM PUBLIC;
REVOKE EXECUTE ON FUNCTION pgtle.create_base_type_if_not_exists
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4,
alignment text,
storage text
) FROM PUBLIC;
REVOKE EXECUTE ON FUNCTION pgtle.register_feature
(
proc regproc,
feature pgtle.pg_tle_features
) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION pgtle.create_base_type
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4,
alignment text,
storage text
) TO pgtle_admin;
GRANT EXECUTE ON FUNCTION pgtle.create_base_type_if_not_exists
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4,
alignment text,
storage text
) TO pgtle_admin;
GRANT EXECUTE ON FUNCTION pgtle.register_feature
(
proc regproc,
feature pgtle.pg_tle_features
) TO pgtle_admin;