1
|
--
|
2
|
-- PostgreSQL database dump
|
3
|
--
|
4
|
|
5
|
SET statement_timeout = 0;
|
6
|
SET lock_timeout = 0;
|
7
|
SET client_encoding = 'UTF8';
|
8
|
SET standard_conforming_strings = on;
|
9
|
SET check_function_bodies = false;
|
10
|
SET client_min_messages = warning;
|
11
|
|
12
|
--
|
13
|
-- Name: util; Type: SCHEMA; Schema: -; Owner: -
|
14
|
--
|
15
|
|
16
|
CREATE SCHEMA util;
|
17
|
|
18
|
|
19
|
--
|
20
|
-- Name: SCHEMA util; Type: COMMENT; Schema: -; Owner: -
|
21
|
--
|
22
|
|
23
|
COMMENT ON SCHEMA util IS '
|
24
|
IMPORTANT: Functions must always return NULL in place of '''' (the empty string). This ensures that empty strings do not find their way into VegBIEN.
|
25
|
|
26
|
NOTE: SQL-language functions should never be declared STRICT, because this prevents them from being inlined. inlining can create a significant speed improvement (7x+), by avoiding function calls and enabling additional constant folding. avoiding use of STRICT also makes functions *much* easier to troubleshoot, because they won''t mysteriously do nothing if called with only NULL arguments, even when you have added debug-print statements.
|
27
|
';
|
28
|
|
29
|
|
30
|
SET search_path = util, pg_catalog;
|
31
|
|
32
|
--
|
33
|
-- Name: col_cast; Type: TYPE; Schema: util; Owner: -
|
34
|
--
|
35
|
|
36
|
CREATE TYPE col_cast AS (
|
37
|
col_name text,
|
38
|
type regtype
|
39
|
);
|
40
|
|
41
|
|
42
|
--
|
43
|
-- Name: col_ref; Type: TYPE; Schema: util; Owner: -
|
44
|
--
|
45
|
|
46
|
CREATE TYPE col_ref AS (
|
47
|
table_ regclass,
|
48
|
name text
|
49
|
);
|
50
|
|
51
|
|
52
|
--
|
53
|
-- Name: compass_dir; Type: TYPE; Schema: util; Owner: -
|
54
|
--
|
55
|
|
56
|
CREATE TYPE compass_dir AS ENUM (
|
57
|
'N',
|
58
|
'E',
|
59
|
'S',
|
60
|
'W'
|
61
|
);
|
62
|
|
63
|
|
64
|
--
|
65
|
-- Name: datatype; Type: TYPE; Schema: util; Owner: -
|
66
|
--
|
67
|
|
68
|
CREATE TYPE datatype AS ENUM (
|
69
|
'str',
|
70
|
'float'
|
71
|
);
|
72
|
|
73
|
|
74
|
--
|
75
|
-- Name: db_item; Type: TYPE; Schema: util; Owner: -
|
76
|
--
|
77
|
|
78
|
CREATE TYPE db_item AS (
|
79
|
path text,
|
80
|
def text
|
81
|
);
|
82
|
|
83
|
|
84
|
--
|
85
|
-- Name: restore_views_info; Type: TYPE; Schema: util; Owner: -
|
86
|
--
|
87
|
|
88
|
CREATE TYPE restore_views_info AS (
|
89
|
views db_item[]
|
90
|
);
|
91
|
|
92
|
|
93
|
--
|
94
|
-- Name: %==(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
95
|
--
|
96
|
|
97
|
CREATE FUNCTION "%=="(left_ anyelement, right_ anyelement) RETURNS boolean
|
98
|
LANGUAGE sql STABLE
|
99
|
AS $_$
|
100
|
SELECT keys($1) = keys($2)
|
101
|
$_$;
|
102
|
|
103
|
|
104
|
--
|
105
|
-- Name: FUNCTION "%=="(left_ anyelement, right_ anyelement); Type: COMMENT; Schema: util; Owner: -
|
106
|
--
|
107
|
|
108
|
COMMENT ON FUNCTION "%=="(left_ anyelement, right_ anyelement) IS '
|
109
|
needs to be declared STABLE instead of IMMUTABLE because it depends on the search_path (as described at http://vegpath.org/links/#PostgreSQL:%20Documentation:%209.3:%20Function%20Volatility%20Categories%20**)
|
110
|
';
|
111
|
|
112
|
|
113
|
--
|
114
|
-- Name: _alt(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
115
|
--
|
116
|
|
117
|
CREATE FUNCTION _alt("0" anyelement DEFAULT NULL::unknown, "1" anyelement DEFAULT NULL::unknown, "2" anyelement DEFAULT NULL::unknown, "3" anyelement DEFAULT NULL::unknown, "4" anyelement DEFAULT NULL::unknown, "5" anyelement DEFAULT NULL::unknown, "6" anyelement DEFAULT NULL::unknown, "7" anyelement DEFAULT NULL::unknown, "8" anyelement DEFAULT NULL::unknown, "9" anyelement DEFAULT NULL::unknown, "10" anyelement DEFAULT NULL::unknown, "11" anyelement DEFAULT NULL::unknown, "12" anyelement DEFAULT NULL::unknown) RETURNS anyelement
|
118
|
LANGUAGE sql IMMUTABLE
|
119
|
AS $_$
|
120
|
SELECT coalesce($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
|
121
|
$_$;
|
122
|
|
123
|
|
124
|
--
|
125
|
-- Name: _and(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: util; Owner: -
|
126
|
--
|
127
|
|
128
|
CREATE FUNCTION _and("0" boolean DEFAULT NULL::boolean, "1" boolean DEFAULT NULL::boolean, "2" boolean DEFAULT NULL::boolean, "3" boolean DEFAULT NULL::boolean, "4" boolean DEFAULT NULL::boolean) RETURNS boolean
|
129
|
LANGUAGE sql IMMUTABLE
|
130
|
AS $_$
|
131
|
SELECT bool_and(value)
|
132
|
FROM
|
133
|
(VALUES
|
134
|
($1)
|
135
|
, ($2)
|
136
|
, ($3)
|
137
|
, ($4)
|
138
|
, ($5)
|
139
|
)
|
140
|
AS v (value)
|
141
|
$_$;
|
142
|
|
143
|
|
144
|
--
|
145
|
-- Name: FUNCTION _and("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: util; Owner: -
|
146
|
--
|
147
|
|
148
|
COMMENT ON FUNCTION _and("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean) IS '
|
149
|
_and() ignores NULL values, while AND combines them with the other values to potentially convert true to NULL. AND should be used with required fields, and _and() with optional fields.
|
150
|
';
|
151
|
|
152
|
|
153
|
--
|
154
|
-- Name: _avg(double precision, double precision, double precision, double precision, double precision); Type: FUNCTION; Schema: util; Owner: -
|
155
|
--
|
156
|
|
157
|
CREATE FUNCTION _avg("0" double precision DEFAULT NULL::double precision, "1" double precision DEFAULT NULL::double precision, "2" double precision DEFAULT NULL::double precision, "3" double precision DEFAULT NULL::double precision, "4" double precision DEFAULT NULL::double precision) RETURNS double precision
|
158
|
LANGUAGE sql IMMUTABLE
|
159
|
AS $_$
|
160
|
SELECT avg(value)
|
161
|
FROM
|
162
|
(VALUES
|
163
|
($1)
|
164
|
, ($2)
|
165
|
, ($3)
|
166
|
, ($4)
|
167
|
, ($5)
|
168
|
)
|
169
|
AS v (value)
|
170
|
$_$;
|
171
|
|
172
|
|
173
|
--
|
174
|
-- Name: _dms_to_dd(text); Type: FUNCTION; Schema: util; Owner: -
|
175
|
--
|
176
|
|
177
|
CREATE FUNCTION _dms_to_dd(value text DEFAULT NULL::text) RETURNS double precision
|
178
|
LANGUAGE sql IMMUTABLE
|
179
|
AS $_$
|
180
|
SELECT (g[1]||'1')::integer*util._dms_to_dd(deg := g[2]::double precision, min := g[3]::double precision, sec := g[4]::double precision, dir := g[5]::util.compass_dir)
|
181
|
FROM
|
182
|
(
|
183
|
SELECT regexp_matches($1, '^ *(-?)(\d{1,3}(?:\.\d*)?)(?:(?:deg|[°º])(?: *([\d.]+)(?:min|[''’]))?(?: *([\d.]+)(?:sec|["”]))?)? *([NESW])? *$')
|
184
|
UNION ALL
|
185
|
SELECT ARRAY[g[1], g[2], g[3]||'.'||g[4], NULL, g[5]]
|
186
|
FROM regexp_matches($1, '^ *(-?)(\d{2,3})(\d{2})(\d{3}) *([NESW])? *$') matches (g) -- [D]DDMMmmm, where MMmmm = MM.mmm
|
187
|
)
|
188
|
matches (g)
|
189
|
$_$;
|
190
|
|
191
|
|
192
|
--
|
193
|
-- Name: _dms_to_dd(double precision, double precision, double precision, compass_dir); Type: FUNCTION; Schema: util; Owner: -
|
194
|
--
|
195
|
|
196
|
CREATE FUNCTION _dms_to_dd(deg double precision DEFAULT NULL::double precision, min double precision DEFAULT NULL::double precision, sec double precision DEFAULT NULL::double precision, dir compass_dir DEFAULT NULL::compass_dir) RETURNS double precision
|
197
|
LANGUAGE sql IMMUTABLE
|
198
|
AS $_$
|
199
|
SELECT sum(value)*COALESCE(util._map('N=>1,E=>1,S=>-1,W=>-1', $4::text)::integer, 1)
|
200
|
FROM
|
201
|
(VALUES
|
202
|
($1)
|
203
|
, ($2/60)
|
204
|
, ($3/60/60)
|
205
|
)
|
206
|
AS v (value)
|
207
|
$_$;
|
208
|
|
209
|
|
210
|
--
|
211
|
-- Name: _dms_to_dd(text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
|
212
|
--
|
213
|
|
214
|
CREATE FUNCTION _dms_to_dd(deg text DEFAULT NULL::text, min text DEFAULT NULL::text, sec text DEFAULT NULL::text, dir text DEFAULT NULL::text) RETURNS double precision
|
215
|
LANGUAGE sql IMMUTABLE
|
216
|
AS $_$
|
217
|
SELECT util._dms_to_dd($1::double precision, $2::double precision, $3::double precision, $4::util.compass_dir)
|
218
|
$_$;
|
219
|
|
220
|
|
221
|
--
|
222
|
-- Name: _eq(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
223
|
--
|
224
|
|
225
|
CREATE FUNCTION _eq("left" anyelement DEFAULT NULL::unknown, "right" anyelement DEFAULT NULL::unknown) RETURNS boolean
|
226
|
LANGUAGE sql IMMUTABLE
|
227
|
AS $_$
|
228
|
SELECT $1 = $2
|
229
|
$_$;
|
230
|
|
231
|
|
232
|
--
|
233
|
-- Name: _fix_date(date, date); Type: FUNCTION; Schema: util; Owner: -
|
234
|
--
|
235
|
|
236
|
CREATE FUNCTION _fix_date(value date DEFAULT NULL::date, threshold date DEFAULT NULL::date) RETURNS date
|
237
|
LANGUAGE sql IMMUTABLE
|
238
|
AS $_$
|
239
|
-- Fix dates after threshold date
|
240
|
-- This fixes e.g. 2-digit years before 1970 misinterpreted as 20xx
|
241
|
SELECT (CASE WHEN $1 > COALESCE($2, now()) THEN ($1 - '100 years'::interval)::date ELSE $1 END)
|
242
|
$_$;
|
243
|
|
244
|
|
245
|
--
|
246
|
-- Name: _if(boolean, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
247
|
--
|
248
|
|
249
|
CREATE FUNCTION _if(cond boolean DEFAULT NULL::boolean, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
|
250
|
LANGUAGE sql IMMUTABLE
|
251
|
AS $_$
|
252
|
SELECT (CASE WHEN $1 THEN $2 ELSE $3 END)
|
253
|
$_$;
|
254
|
|
255
|
|
256
|
--
|
257
|
-- Name: _if(text, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
258
|
--
|
259
|
|
260
|
CREATE FUNCTION _if(cond text DEFAULT NULL::text, "then" anyelement DEFAULT NULL::unknown, "else" anyelement DEFAULT NULL::unknown) RETURNS anyelement
|
261
|
LANGUAGE sql IMMUTABLE
|
262
|
AS $_$
|
263
|
SELECT util._if($1 != '', $2, $3)
|
264
|
$_$;
|
265
|
|
266
|
|
267
|
--
|
268
|
-- Name: _join(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
|
269
|
--
|
270
|
|
271
|
CREATE FUNCTION _join("0" text DEFAULT NULL::text, "1" text DEFAULT NULL::text, "2" text DEFAULT NULL::text, "3" text DEFAULT NULL::text, "4" text DEFAULT NULL::text, "5" text DEFAULT NULL::text, "6" text DEFAULT NULL::text, "7" text DEFAULT NULL::text, "8" text DEFAULT NULL::text, "9" text DEFAULT NULL::text) RETURNS text
|
272
|
LANGUAGE sql IMMUTABLE
|
273
|
AS $_$
|
274
|
SELECT NULLIF(concat_ws('; ', $1, $2, $3, $4, $5, $6, $7, $8, $9, $10), '')
|
275
|
$_$;
|
276
|
|
277
|
|
278
|
--
|
279
|
-- Name: _join_words(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
|
280
|
--
|
281
|
|
282
|
CREATE FUNCTION _join_words("0" text DEFAULT NULL::text, "1" text DEFAULT NULL::text, "2" text DEFAULT NULL::text, "3" text DEFAULT NULL::text, "4" text DEFAULT NULL::text, "5" text DEFAULT NULL::text, "6" text DEFAULT NULL::text, "7" text DEFAULT NULL::text, "8" text DEFAULT NULL::text, "9" text DEFAULT NULL::text) RETURNS text
|
283
|
LANGUAGE sql IMMUTABLE
|
284
|
AS $_$
|
285
|
SELECT NULLIF(concat_ws(' ', $1, $2, $3, $4, $5, $6, $7, $8, $9, $10), '')
|
286
|
$_$;
|
287
|
|
288
|
|
289
|
--
|
290
|
-- Name: _km_to_m(double precision); Type: FUNCTION; Schema: util; Owner: -
|
291
|
--
|
292
|
|
293
|
CREATE FUNCTION _km_to_m(value double precision) RETURNS double precision
|
294
|
LANGUAGE sql IMMUTABLE
|
295
|
AS $_$
|
296
|
SELECT $1*1000.
|
297
|
$_$;
|
298
|
|
299
|
|
300
|
--
|
301
|
-- Name: _label(text, text); Type: FUNCTION; Schema: util; Owner: -
|
302
|
--
|
303
|
|
304
|
CREATE FUNCTION _label(label text, value text) RETURNS text
|
305
|
LANGUAGE sql IMMUTABLE
|
306
|
AS $_$
|
307
|
SELECT coalesce($1 || ': ', '') || $2
|
308
|
$_$;
|
309
|
|
310
|
|
311
|
--
|
312
|
-- Name: _lowercase(text); Type: FUNCTION; Schema: util; Owner: -
|
313
|
--
|
314
|
|
315
|
CREATE FUNCTION _lowercase(value text) RETURNS text
|
316
|
LANGUAGE sql IMMUTABLE
|
317
|
AS $_$
|
318
|
SELECT lower($1)
|
319
|
$_$;
|
320
|
|
321
|
|
322
|
--
|
323
|
-- Name: _map(hstore, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
324
|
--
|
325
|
|
326
|
CREATE FUNCTION _map(map hstore, value anyelement) RETURNS anyelement
|
327
|
LANGUAGE plpgsql IMMUTABLE STRICT
|
328
|
AS $$
|
329
|
DECLARE
|
330
|
result value%TYPE := util._map(map, value::text)::unknown;
|
331
|
BEGIN
|
332
|
RETURN result;
|
333
|
END;
|
334
|
$$;
|
335
|
|
336
|
|
337
|
--
|
338
|
-- Name: _map(hstore, text); Type: FUNCTION; Schema: util; Owner: -
|
339
|
--
|
340
|
|
341
|
CREATE FUNCTION _map(map hstore, value text) RETURNS text
|
342
|
LANGUAGE plpgsql IMMUTABLE STRICT
|
343
|
AS $$
|
344
|
DECLARE
|
345
|
match text := map -> value;
|
346
|
BEGIN
|
347
|
IF match IS NULL AND NOT map ? value THEN -- no match rather than NULL match
|
348
|
match := map -> '*'; -- use default entry
|
349
|
IF match IS NULL AND NOT map ? '*' THEN match := '!'; -- no default
|
350
|
END IF;
|
351
|
END IF;
|
352
|
|
353
|
-- Interpret result
|
354
|
IF match = '!' THEN RAISE 'Value not in map: %', value USING ERRCODE = 'data_exception';
|
355
|
ELSIF match = '*' THEN RETURN value;
|
356
|
ELSE RETURN match;
|
357
|
END IF;
|
358
|
END;
|
359
|
$$;
|
360
|
|
361
|
|
362
|
--
|
363
|
-- Name: _max(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
364
|
--
|
365
|
|
366
|
CREATE FUNCTION _max("0" anyelement DEFAULT NULL::unknown, "1" anyelement DEFAULT NULL::unknown, "2" anyelement DEFAULT NULL::unknown, "3" anyelement DEFAULT NULL::unknown, "4" anyelement DEFAULT NULL::unknown, "5" anyelement DEFAULT NULL::unknown, "6" anyelement DEFAULT NULL::unknown, "7" anyelement DEFAULT NULL::unknown, "8" anyelement DEFAULT NULL::unknown, "9" anyelement DEFAULT NULL::unknown) RETURNS anyelement
|
367
|
LANGUAGE sql IMMUTABLE
|
368
|
AS $_$
|
369
|
SELECT GREATEST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
370
|
$_$;
|
371
|
|
372
|
|
373
|
--
|
374
|
-- Name: _merge(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
375
|
--
|
376
|
|
377
|
CREATE FUNCTION _merge("0" anyelement DEFAULT NULL::unknown, "1" anyelement DEFAULT NULL::unknown, "2" anyelement DEFAULT NULL::unknown, "3" anyelement DEFAULT NULL::unknown, "4" anyelement DEFAULT NULL::unknown, "5" anyelement DEFAULT NULL::unknown, "6" anyelement DEFAULT NULL::unknown, "7" anyelement DEFAULT NULL::unknown, "8" anyelement DEFAULT NULL::unknown, "9" anyelement DEFAULT NULL::unknown) RETURNS anyelement
|
378
|
LANGUAGE sql IMMUTABLE
|
379
|
AS $_$
|
380
|
SELECT util.join_strs(value, '; ')
|
381
|
FROM
|
382
|
(
|
383
|
SELECT *
|
384
|
FROM
|
385
|
(
|
386
|
SELECT
|
387
|
DISTINCT ON (value)
|
388
|
*
|
389
|
FROM
|
390
|
(VALUES
|
391
|
(1, $1)
|
392
|
, (2, $2)
|
393
|
, (3, $3)
|
394
|
, (4, $4)
|
395
|
, (5, $5)
|
396
|
, (6, $6)
|
397
|
, (7, $7)
|
398
|
, (8, $8)
|
399
|
, (9, $9)
|
400
|
, (10, $10)
|
401
|
)
|
402
|
AS v (sort_order, value)
|
403
|
WHERE value IS NOT NULL
|
404
|
)
|
405
|
AS v
|
406
|
ORDER BY sort_order
|
407
|
)
|
408
|
AS v
|
409
|
$_$;
|
410
|
|
411
|
|
412
|
--
|
413
|
-- Name: _merge_prefix(text, text); Type: FUNCTION; Schema: util; Owner: -
|
414
|
--
|
415
|
|
416
|
CREATE FUNCTION _merge_prefix(prefix text DEFAULT NULL::text, value text DEFAULT NULL::text) RETURNS text
|
417
|
LANGUAGE sql IMMUTABLE
|
418
|
AS $_$
|
419
|
SELECT _join_words((CASE WHEN $2 ~ ('^'||$1||E'\\y') THEN NULL ELSE $1 END), $2)
|
420
|
$_$;
|
421
|
|
422
|
|
423
|
--
|
424
|
-- Name: _merge_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
425
|
--
|
426
|
|
427
|
CREATE FUNCTION _merge_words("0" anyelement DEFAULT NULL::unknown, "1" anyelement DEFAULT NULL::unknown, "2" anyelement DEFAULT NULL::unknown, "3" anyelement DEFAULT NULL::unknown, "4" anyelement DEFAULT NULL::unknown, "5" anyelement DEFAULT NULL::unknown, "6" anyelement DEFAULT NULL::unknown, "7" anyelement DEFAULT NULL::unknown, "8" anyelement DEFAULT NULL::unknown, "9" anyelement DEFAULT NULL::unknown) RETURNS anyelement
|
428
|
LANGUAGE sql IMMUTABLE
|
429
|
AS $_$
|
430
|
SELECT util.join_strs(value, ' ')
|
431
|
FROM
|
432
|
(
|
433
|
SELECT *
|
434
|
FROM
|
435
|
(
|
436
|
SELECT
|
437
|
DISTINCT ON (value)
|
438
|
*
|
439
|
FROM
|
440
|
(VALUES
|
441
|
(1, $1)
|
442
|
, (2, $2)
|
443
|
, (3, $3)
|
444
|
, (4, $4)
|
445
|
, (5, $5)
|
446
|
, (6, $6)
|
447
|
, (7, $7)
|
448
|
, (8, $8)
|
449
|
, (9, $9)
|
450
|
, (10, $10)
|
451
|
)
|
452
|
AS v (sort_order, value)
|
453
|
WHERE value IS NOT NULL
|
454
|
)
|
455
|
AS v
|
456
|
ORDER BY sort_order
|
457
|
)
|
458
|
AS v
|
459
|
$_$;
|
460
|
|
461
|
|
462
|
--
|
463
|
-- Name: _min(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
464
|
--
|
465
|
|
466
|
CREATE FUNCTION _min("0" anyelement DEFAULT NULL::unknown, "1" anyelement DEFAULT NULL::unknown, "2" anyelement DEFAULT NULL::unknown, "3" anyelement DEFAULT NULL::unknown, "4" anyelement DEFAULT NULL::unknown, "5" anyelement DEFAULT NULL::unknown, "6" anyelement DEFAULT NULL::unknown, "7" anyelement DEFAULT NULL::unknown, "8" anyelement DEFAULT NULL::unknown, "9" anyelement DEFAULT NULL::unknown) RETURNS anyelement
|
467
|
LANGUAGE sql IMMUTABLE
|
468
|
AS $_$
|
469
|
SELECT LEAST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
470
|
$_$;
|
471
|
|
472
|
|
473
|
--
|
474
|
-- Name: _not(boolean); Type: FUNCTION; Schema: util; Owner: -
|
475
|
--
|
476
|
|
477
|
CREATE FUNCTION _not(value boolean) RETURNS boolean
|
478
|
LANGUAGE sql IMMUTABLE
|
479
|
AS $_$
|
480
|
SELECT NOT $1
|
481
|
$_$;
|
482
|
|
483
|
|
484
|
--
|
485
|
-- Name: _now(); Type: FUNCTION; Schema: util; Owner: -
|
486
|
--
|
487
|
|
488
|
CREATE FUNCTION _now() RETURNS timestamp with time zone
|
489
|
LANGUAGE sql STABLE
|
490
|
AS $$
|
491
|
SELECT now()
|
492
|
$$;
|
493
|
|
494
|
|
495
|
--
|
496
|
-- Name: _nullIf(anyelement, text, text); Type: FUNCTION; Schema: util; Owner: -
|
497
|
--
|
498
|
|
499
|
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type text) RETURNS anyelement
|
500
|
LANGUAGE sql IMMUTABLE
|
501
|
AS $_$
|
502
|
SELECT util."_nullIf"($1, $2, $3::util.datatype)
|
503
|
$_$;
|
504
|
|
505
|
|
506
|
--
|
507
|
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: util; Owner: -
|
508
|
--
|
509
|
|
510
|
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
|
511
|
LANGUAGE plpgsql IMMUTABLE
|
512
|
AS $$
|
513
|
DECLARE
|
514
|
type util.datatype NOT NULL := type; -- add NOT NULL
|
515
|
BEGIN
|
516
|
IF type = 'str' THEN RETURN nullif(value::text, "null");
|
517
|
-- Invalid value is ignored, but invalid null value generates error
|
518
|
ELSIF type = 'float' THEN
|
519
|
DECLARE
|
520
|
-- Outside the try block so that invalid null value generates error
|
521
|
"null" double precision := "null"::double precision;
|
522
|
BEGIN
|
523
|
RETURN nullif(value::double precision, "null");
|
524
|
EXCEPTION
|
525
|
WHEN data_exception THEN RETURN value; -- ignore invalid value
|
526
|
END;
|
527
|
END IF;
|
528
|
END;
|
529
|
$$;
|
530
|
|
531
|
|
532
|
--
|
533
|
-- Name: _or(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: util; Owner: -
|
534
|
--
|
535
|
|
536
|
CREATE FUNCTION _or("0" boolean DEFAULT NULL::boolean, "1" boolean DEFAULT NULL::boolean, "2" boolean DEFAULT NULL::boolean, "3" boolean DEFAULT NULL::boolean, "4" boolean DEFAULT NULL::boolean) RETURNS boolean
|
537
|
LANGUAGE sql IMMUTABLE
|
538
|
AS $_$
|
539
|
SELECT bool_or(value)
|
540
|
FROM
|
541
|
(VALUES
|
542
|
($1)
|
543
|
, ($2)
|
544
|
, ($3)
|
545
|
, ($4)
|
546
|
, ($5)
|
547
|
)
|
548
|
AS v (value)
|
549
|
$_$;
|
550
|
|
551
|
|
552
|
--
|
553
|
-- Name: FUNCTION _or("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: util; Owner: -
|
554
|
--
|
555
|
|
556
|
COMMENT ON FUNCTION _or("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean) IS '
|
557
|
_or() ignores NULL values, while OR combines them with the other values to potentially convert false to NULL. OR should be used with required fields, and _or() with optional fields.
|
558
|
';
|
559
|
|
560
|
|
561
|
--
|
562
|
-- Name: _range(double precision, double precision); Type: FUNCTION; Schema: util; Owner: -
|
563
|
--
|
564
|
|
565
|
CREATE FUNCTION _range("from" double precision DEFAULT NULL::double precision, "to" double precision DEFAULT NULL::double precision) RETURNS double precision
|
566
|
LANGUAGE sql IMMUTABLE
|
567
|
AS $_$
|
568
|
SELECT $2 - $1
|
569
|
$_$;
|
570
|
|
571
|
|
572
|
--
|
573
|
-- Name: _split(text, text); Type: FUNCTION; Schema: util; Owner: -
|
574
|
--
|
575
|
|
576
|
CREATE FUNCTION _split(value text DEFAULT NULL::text, separator text DEFAULT '; '::text) RETURNS SETOF text
|
577
|
LANGUAGE sql IMMUTABLE
|
578
|
AS $_$
|
579
|
SELECT regexp_split_to_table($1, $2)
|
580
|
$_$;
|
581
|
|
582
|
|
583
|
--
|
584
|
-- Name: added_cols(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
|
585
|
--
|
586
|
|
587
|
CREATE FUNCTION added_cols(table_ regclass, names regclass) RETURNS SETOF text
|
588
|
LANGUAGE sql STABLE
|
589
|
AS $_$
|
590
|
SELECT util.derived_cols($1, $2)
|
591
|
UNION
|
592
|
SELECT util.eval2set($$
|
593
|
SELECT col
|
594
|
FROM util.col_names($$||quote_nullable($1)||$$::regclass) f (col)
|
595
|
JOIN $$||$2||$$ ON "to" = col
|
596
|
WHERE "from" LIKE ':%'
|
597
|
$$, NULL::text)
|
598
|
$_$;
|
599
|
|
600
|
|
601
|
--
|
602
|
-- Name: FUNCTION added_cols(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
|
603
|
--
|
604
|
|
605
|
COMMENT ON FUNCTION added_cols(table_ regclass, names regclass) IS '
|
606
|
gets table_''s added columns (all the columns not in the original data)
|
607
|
';
|
608
|
|
609
|
|
610
|
--
|
611
|
-- Name: all_same_final(anyarray); Type: FUNCTION; Schema: util; Owner: -
|
612
|
--
|
613
|
|
614
|
CREATE FUNCTION all_same_final(state anyarray) RETURNS boolean
|
615
|
LANGUAGE sql IMMUTABLE
|
616
|
AS $_$
|
617
|
SELECT $1 IS NULL/*no rows*/ OR util.not_empty($1)/*not no_match_sentinel*/
|
618
|
$_$;
|
619
|
|
620
|
|
621
|
--
|
622
|
-- Name: all_same_transform(anyarray, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
623
|
--
|
624
|
|
625
|
CREATE FUNCTION all_same_transform(state anyarray, value anyelement) RETURNS anyarray
|
626
|
LANGUAGE plpgsql IMMUTABLE
|
627
|
AS $$
|
628
|
DECLARE
|
629
|
value_cmp state%TYPE = ARRAY[value];
|
630
|
state state%TYPE = COALESCE(state, value_cmp);
|
631
|
no_match_sentinel state%TYPE = value_cmp[1:0]/*=ARRAY[]::state%TYPE*/;
|
632
|
BEGIN
|
633
|
RETURN (CASE WHEN value_cmp IS NOT DISTINCT FROM state THEN state ELSE no_match_sentinel END);
|
634
|
END;
|
635
|
$$;
|
636
|
|
637
|
|
638
|
--
|
639
|
-- Name: analyze_(regclass); Type: FUNCTION; Schema: util; Owner: -
|
640
|
--
|
641
|
|
642
|
CREATE FUNCTION analyze_(table_ regclass) RETURNS void
|
643
|
LANGUAGE sql
|
644
|
AS $_$
|
645
|
SELECT util.eval($$ANALYZE VERBOSE $$||$1)
|
646
|
$_$;
|
647
|
|
648
|
|
649
|
--
|
650
|
-- Name: append_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
651
|
--
|
652
|
|
653
|
CREATE FUNCTION append_comment(table_ regclass, comment text) RETURNS void
|
654
|
LANGUAGE sql
|
655
|
AS $_$
|
656
|
SELECT util.set_comment($1, concat(util.comment($1), $2))
|
657
|
$_$;
|
658
|
|
659
|
|
660
|
--
|
661
|
-- Name: FUNCTION append_comment(table_ regclass, comment text); Type: COMMENT; Schema: util; Owner: -
|
662
|
--
|
663
|
|
664
|
COMMENT ON FUNCTION append_comment(table_ regclass, comment text) IS '
|
665
|
comment: must start and end with a newline
|
666
|
';
|
667
|
|
668
|
|
669
|
--
|
670
|
-- Name: array_fill(anyelement, integer); Type: FUNCTION; Schema: util; Owner: -
|
671
|
--
|
672
|
|
673
|
CREATE FUNCTION array_fill(value anyelement, length integer) RETURNS anyarray
|
674
|
LANGUAGE sql IMMUTABLE
|
675
|
AS $_$
|
676
|
SELECT pg_catalog.array_fill($1, ARRAY[$2])
|
677
|
$_$;
|
678
|
|
679
|
|
680
|
--
|
681
|
-- Name: array_length(anyarray); Type: FUNCTION; Schema: util; Owner: -
|
682
|
--
|
683
|
|
684
|
CREATE FUNCTION array_length("array" anyarray) RETURNS integer
|
685
|
LANGUAGE sql IMMUTABLE
|
686
|
AS $_$
|
687
|
SELECT util.array_length($1, 1)
|
688
|
$_$;
|
689
|
|
690
|
|
691
|
--
|
692
|
-- Name: array_length(anyarray, integer); Type: FUNCTION; Schema: util; Owner: -
|
693
|
--
|
694
|
|
695
|
CREATE FUNCTION array_length("array" anyarray, dimension integer) RETURNS integer
|
696
|
LANGUAGE sql IMMUTABLE
|
697
|
AS $_$
|
698
|
SELECT CASE WHEN $1 IS NULL THEN NULL ELSE COALESCE(pg_catalog.array_length($1, $2), 0) END
|
699
|
$_$;
|
700
|
|
701
|
|
702
|
--
|
703
|
-- Name: FUNCTION array_length("array" anyarray, dimension integer); Type: COMMENT; Schema: util; Owner: -
|
704
|
--
|
705
|
|
706
|
COMMENT ON FUNCTION array_length("array" anyarray, dimension integer) IS '
|
707
|
returns 0 instead of NULL for empty arrays
|
708
|
';
|
709
|
|
710
|
|
711
|
--
|
712
|
-- Name: array_reverse(anyarray); Type: FUNCTION; Schema: util; Owner: -
|
713
|
--
|
714
|
|
715
|
CREATE FUNCTION array_reverse("array" anyarray) RETURNS anyarray
|
716
|
LANGUAGE sql IMMUTABLE
|
717
|
AS $_$
|
718
|
SELECT array(SELECT elem FROM util.in_reverse($1) elem)
|
719
|
$_$;
|
720
|
|
721
|
|
722
|
--
|
723
|
-- Name: auto_rm_freq(regclass[], text); Type: FUNCTION; Schema: util; Owner: -
|
724
|
--
|
725
|
|
726
|
CREATE FUNCTION auto_rm_freq(tables regclass[], freq_col text DEFAULT 'copies'::text) RETURNS void
|
727
|
LANGUAGE sql
|
728
|
AS $_$
|
729
|
SELECT CASE WHEN util.freq_always_1($1, $2)
|
730
|
THEN util.rm_freq($1, $2)
|
731
|
ELSE util.try_create($$ALTER TABLE $$||util.parent($1[1])||$$ ADD COLUMN $$||quote_ident($2)||$$ bigint NOT NULL$$)
|
732
|
END
|
733
|
$_$;
|
734
|
|
735
|
|
736
|
--
|
737
|
-- Name: cast(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
738
|
--
|
739
|
|
740
|
CREATE FUNCTION "cast"(value text, ret_type_null anyelement) RETURNS anyelement
|
741
|
LANGUAGE plpgsql IMMUTABLE
|
742
|
AS $$
|
743
|
/* must use LANGUAGE plpgsql because LANGUAGE sql does not assignment-cast the
|
744
|
return value, causing a type mismatch */
|
745
|
BEGIN
|
746
|
-- will then be assignment-cast to return type via INOUT
|
747
|
RETURN value::cstring;
|
748
|
END;
|
749
|
$$;
|
750
|
|
751
|
|
752
|
--
|
753
|
-- Name: FUNCTION "cast"(value text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
|
754
|
--
|
755
|
|
756
|
COMMENT ON FUNCTION "cast"(value text, ret_type_null anyelement) IS '
|
757
|
allows casting to an arbitrary type without eval()
|
758
|
|
759
|
usage:
|
760
|
SELECT util.cast(''value'', NULL::integer);
|
761
|
|
762
|
note that there does *not* need to be a cast from text to the output type,
|
763
|
because an INOUT cast is used instead
|
764
|
(http://www.postgresql.org/docs/9.3/static/sql-createcast.html#AEN69507)
|
765
|
|
766
|
ret_type_null: NULL::ret_type
|
767
|
';
|
768
|
|
769
|
|
770
|
--
|
771
|
-- Name: cluster_index(regclass); Type: FUNCTION; Schema: util; Owner: -
|
772
|
--
|
773
|
|
774
|
CREATE FUNCTION cluster_index(table_ regclass) RETURNS regclass
|
775
|
LANGUAGE sql STABLE
|
776
|
AS $_$
|
777
|
SELECT indexrelid FROM pg_index WHERE indrelid = $1 AND indisclustered
|
778
|
$_$;
|
779
|
|
780
|
|
781
|
--
|
782
|
-- Name: cluster_once(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
|
783
|
--
|
784
|
|
785
|
CREATE FUNCTION cluster_once(table_ regclass, index regclass) RETURNS void
|
786
|
LANGUAGE plpgsql STRICT
|
787
|
AS $_$
|
788
|
BEGIN
|
789
|
-- not yet clustered (ARRAY[] compares NULLs literally)
|
790
|
IF ARRAY[util.cluster_index(table_)] != ARRAY[index] THEN
|
791
|
EXECUTE $$CLUSTER $$||table_||$$ USING $$||index;
|
792
|
END IF;
|
793
|
END;
|
794
|
$_$;
|
795
|
|
796
|
|
797
|
--
|
798
|
-- Name: FUNCTION cluster_once(table_ regclass, index regclass); Type: COMMENT; Schema: util; Owner: -
|
799
|
--
|
800
|
|
801
|
COMMENT ON FUNCTION cluster_once(table_ regclass, index regclass) IS '
|
802
|
idempotent
|
803
|
';
|
804
|
|
805
|
|
806
|
--
|
807
|
-- Name: coalesce(anyarray); Type: FUNCTION; Schema: util; Owner: -
|
808
|
--
|
809
|
|
810
|
CREATE FUNCTION "coalesce"(VARIADIC values_ anyarray) RETURNS anyelement
|
811
|
LANGUAGE sql IMMUTABLE
|
812
|
AS $_$
|
813
|
SELECT value
|
814
|
FROM unnest($1) value
|
815
|
WHERE value IS NOT NULL
|
816
|
LIMIT 1
|
817
|
$_$;
|
818
|
|
819
|
|
820
|
--
|
821
|
-- Name: FUNCTION "coalesce"(VARIADIC values_ anyarray); Type: COMMENT; Schema: util; Owner: -
|
822
|
--
|
823
|
|
824
|
COMMENT ON FUNCTION "coalesce"(VARIADIC values_ anyarray) IS '
|
825
|
uses:
|
826
|
* coalescing array elements or rows together
|
827
|
* forcing evaluation of all values of a COALESCE()
|
828
|
';
|
829
|
|
830
|
|
831
|
--
|
832
|
-- Name: col__min(col_ref); Type: FUNCTION; Schema: util; Owner: -
|
833
|
--
|
834
|
|
835
|
CREATE FUNCTION col__min(col col_ref) RETURNS integer
|
836
|
LANGUAGE sql STABLE
|
837
|
AS $_$
|
838
|
SELECT util.eval2val($$
|
839
|
SELECT $$||quote_ident($1.name)||$$
|
840
|
FROM $$||$1.table_||$$
|
841
|
ORDER BY $$||quote_ident($1.name)||$$ ASC
|
842
|
LIMIT 1
|
843
|
$$, NULL::integer)
|
844
|
$_$;
|
845
|
|
846
|
|
847
|
--
|
848
|
-- Name: col_comment(col_ref); Type: FUNCTION; Schema: util; Owner: -
|
849
|
--
|
850
|
|
851
|
CREATE FUNCTION col_comment(col col_ref) RETURNS text
|
852
|
LANGUAGE plpgsql STABLE STRICT
|
853
|
AS $$
|
854
|
DECLARE
|
855
|
comment text;
|
856
|
BEGIN
|
857
|
SELECT description
|
858
|
FROM pg_attribute
|
859
|
LEFT JOIN pg_description ON objoid = attrelid
|
860
|
AND classoid = 'pg_class'::regclass AND objsubid = attnum
|
861
|
WHERE attrelid = col.table_ AND attname = col.name
|
862
|
INTO STRICT comment
|
863
|
;
|
864
|
RETURN comment;
|
865
|
EXCEPTION
|
866
|
WHEN no_data_found THEN PERFORM util.raise_undefined_column(col);
|
867
|
END;
|
868
|
$$;
|
869
|
|
870
|
|
871
|
--
|
872
|
-- Name: col_default_sql(col_ref); Type: FUNCTION; Schema: util; Owner: -
|
873
|
--
|
874
|
|
875
|
CREATE FUNCTION col_default_sql(col col_ref) RETURNS text
|
876
|
LANGUAGE plpgsql STABLE STRICT
|
877
|
AS $$
|
878
|
DECLARE
|
879
|
default_sql text;
|
880
|
BEGIN
|
881
|
SELECT adsrc
|
882
|
FROM pg_attribute
|
883
|
LEFT JOIN pg_attrdef ON adrelid = attrelid AND adnum = attnum
|
884
|
WHERE attrelid = col.table_ AND attname = col.name
|
885
|
INTO STRICT default_sql
|
886
|
;
|
887
|
RETURN default_sql;
|
888
|
EXCEPTION
|
889
|
WHEN no_data_found THEN PERFORM util.raise_undefined_column(col);
|
890
|
END;
|
891
|
$$;
|
892
|
|
893
|
|
894
|
--
|
895
|
-- Name: col_default_value(col_ref, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
896
|
--
|
897
|
|
898
|
CREATE FUNCTION col_default_value(col col_ref, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
|
899
|
LANGUAGE sql STABLE
|
900
|
AS $_$
|
901
|
SELECT util.eval_expr_passthru(util.col_default_sql($1), $2)
|
902
|
$_$;
|
903
|
|
904
|
|
905
|
--
|
906
|
-- Name: FUNCTION col_default_value(col col_ref, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
|
907
|
--
|
908
|
|
909
|
COMMENT ON FUNCTION col_default_value(col col_ref, ret_type_null anyelement) IS '
|
910
|
ret_type_null: NULL::ret_type
|
911
|
';
|
912
|
|
913
|
|
914
|
--
|
915
|
-- Name: col_exists(col_ref); Type: FUNCTION; Schema: util; Owner: -
|
916
|
--
|
917
|
|
918
|
CREATE FUNCTION col_exists(col col_ref) RETURNS boolean
|
919
|
LANGUAGE plpgsql STRICT
|
920
|
AS $$
|
921
|
BEGIN
|
922
|
PERFORM util.col_type(col);
|
923
|
RETURN true;
|
924
|
EXCEPTION
|
925
|
WHEN undefined_column THEN RETURN false;
|
926
|
END;
|
927
|
$$;
|
928
|
|
929
|
|
930
|
--
|
931
|
-- Name: col_global_names(regtype); Type: FUNCTION; Schema: util; Owner: -
|
932
|
--
|
933
|
|
934
|
CREATE FUNCTION col_global_names(type regtype, OUT name text, OUT global_name text) RETURNS SETOF record
|
935
|
LANGUAGE plpgsql STABLE STRICT
|
936
|
AS $$
|
937
|
DECLARE
|
938
|
prefix text := util.name(type)||'.';
|
939
|
BEGIN
|
940
|
RETURN QUERY
|
941
|
SELECT name_, (CASE WHEN util.contains(search_for:='.', in_str:=name_) THEN '' ELSE prefix END)||name_
|
942
|
FROM util.col_names(type) f (name_);
|
943
|
END;
|
944
|
$$;
|
945
|
|
946
|
|
947
|
--
|
948
|
-- Name: col_names(regclass); Type: FUNCTION; Schema: util; Owner: -
|
949
|
--
|
950
|
|
951
|
CREATE FUNCTION col_names(table_ regclass) RETURNS SETOF text
|
952
|
LANGUAGE sql STABLE
|
953
|
AS $_$
|
954
|
SELECT attname::text
|
955
|
FROM pg_attribute
|
956
|
WHERE attrelid = $1 AND attnum >= 1 AND NOT attisdropped
|
957
|
ORDER BY attnum
|
958
|
$_$;
|
959
|
|
960
|
|
961
|
--
|
962
|
-- Name: col_names(regtype); Type: FUNCTION; Schema: util; Owner: -
|
963
|
--
|
964
|
|
965
|
CREATE FUNCTION col_names(type regtype) RETURNS SETOF text
|
966
|
LANGUAGE plpgsql STABLE STRICT
|
967
|
AS $_$
|
968
|
BEGIN
|
969
|
RETURN QUERY EXECUTE $$SELECT skeys(hstore(NULL::$$||type||$$))$$;
|
970
|
END;
|
971
|
$_$;
|
972
|
|
973
|
|
974
|
--
|
975
|
-- Name: col_type(col_ref); Type: FUNCTION; Schema: util; Owner: -
|
976
|
--
|
977
|
|
978
|
CREATE FUNCTION col_type(col col_ref) RETURNS regtype
|
979
|
LANGUAGE plpgsql STABLE STRICT
|
980
|
AS $$
|
981
|
DECLARE
|
982
|
type regtype;
|
983
|
BEGIN
|
984
|
SELECT atttypid FROM pg_attribute
|
985
|
WHERE attrelid = col.table_ AND attname = col.name
|
986
|
INTO STRICT type
|
987
|
;
|
988
|
RETURN type;
|
989
|
EXCEPTION
|
990
|
WHEN no_data_found THEN
|
991
|
RAISE undefined_column USING MESSAGE =
|
992
|
concat('undefined column: ', col.name);
|
993
|
END;
|
994
|
$$;
|
995
|
|
996
|
|
997
|
--
|
998
|
-- Name: comment(oid); Type: FUNCTION; Schema: util; Owner: -
|
999
|
--
|
1000
|
|
1001
|
CREATE FUNCTION comment(element oid) RETURNS text
|
1002
|
LANGUAGE sql STABLE
|
1003
|
AS $_$
|
1004
|
SELECT description FROM pg_description WHERE objoid = $1
|
1005
|
$_$;
|
1006
|
|
1007
|
|
1008
|
--
|
1009
|
-- Name: concat_esc(text, text); Type: FUNCTION; Schema: util; Owner: -
|
1010
|
--
|
1011
|
|
1012
|
CREATE FUNCTION concat_esc("left" text, "right" text) RETURNS text
|
1013
|
LANGUAGE sql IMMUTABLE
|
1014
|
AS $_$
|
1015
|
SELECT util.esc_name__append($2, $1)
|
1016
|
$_$;
|
1017
|
|
1018
|
|
1019
|
--
|
1020
|
-- Name: contains(text, text); Type: FUNCTION; Schema: util; Owner: -
|
1021
|
--
|
1022
|
|
1023
|
CREATE FUNCTION contains(search_for text, in_str text) RETURNS boolean
|
1024
|
LANGUAGE sql IMMUTABLE
|
1025
|
AS $_$
|
1026
|
SELECT position($1 in $2) > 0 /*1-based offset*/
|
1027
|
$_$;
|
1028
|
|
1029
|
|
1030
|
--
|
1031
|
-- Name: copy_struct(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
1032
|
--
|
1033
|
|
1034
|
CREATE FUNCTION copy_struct(from_ regclass, to_ text) RETURNS void
|
1035
|
LANGUAGE sql
|
1036
|
AS $_$
|
1037
|
SELECT util.eval($$CREATE TABLE $$||$2||$$ (LIKE $$||$1||$$ INCLUDING ALL)$$)
|
1038
|
$_$;
|
1039
|
|
1040
|
|
1041
|
--
|
1042
|
-- Name: copy_types_and_data(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
1043
|
--
|
1044
|
|
1045
|
CREATE FUNCTION copy_types_and_data(from_ regclass, to_ text) RETURNS void
|
1046
|
LANGUAGE sql
|
1047
|
AS $_$
|
1048
|
SELECT util.materialize_view($2, $1)
|
1049
|
$_$;
|
1050
|
|
1051
|
|
1052
|
--
|
1053
|
-- Name: create_if_not_exists(text, text); Type: FUNCTION; Schema: util; Owner: -
|
1054
|
--
|
1055
|
|
1056
|
CREATE FUNCTION create_if_not_exists(sql text, relation text DEFAULT NULL::text) RETURNS void
|
1057
|
LANGUAGE plpgsql
|
1058
|
AS $$
|
1059
|
BEGIN
|
1060
|
/* always generate standard exception if exists, even if table definition
|
1061
|
would be invalid (which generates a variety of exceptions) */
|
1062
|
IF util.relation_exists(relation) THEN RAISE duplicate_table; END IF;
|
1063
|
PERFORM util.eval(sql);
|
1064
|
EXCEPTION
|
1065
|
WHEN duplicate_table
|
1066
|
OR duplicate_object -- eg. constraint
|
1067
|
OR duplicate_column
|
1068
|
OR duplicate_function
|
1069
|
THEN NULL;
|
1070
|
WHEN invalid_table_definition THEN
|
1071
|
IF SQLERRM LIKE 'multiple primary keys for table % are not allowed' THEN NULL;
|
1072
|
ELSE RAISE;
|
1073
|
END IF;
|
1074
|
END;
|
1075
|
$$;
|
1076
|
|
1077
|
|
1078
|
--
|
1079
|
-- Name: FUNCTION create_if_not_exists(sql text, relation text); Type: COMMENT; Schema: util; Owner: -
|
1080
|
--
|
1081
|
|
1082
|
COMMENT ON FUNCTION create_if_not_exists(sql text, relation text) IS '
|
1083
|
idempotent
|
1084
|
';
|
1085
|
|
1086
|
|
1087
|
--
|
1088
|
-- Name: curr_func(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
1089
|
--
|
1090
|
|
1091
|
CREATE FUNCTION curr_func(func text, schema_anchor anyelement) RETURNS text
|
1092
|
LANGUAGE sql STABLE
|
1093
|
AS $$
|
1094
|
SELECT util.schema_esc(schema_anchor)||'.'||quote_ident(func)
|
1095
|
$$;
|
1096
|
|
1097
|
|
1098
|
--
|
1099
|
-- Name: debug_print_func_call(text); Type: FUNCTION; Schema: util; Owner: -
|
1100
|
--
|
1101
|
|
1102
|
CREATE FUNCTION debug_print_func_call(func_call text) RETURNS void
|
1103
|
LANGUAGE sql IMMUTABLE
|
1104
|
AS $_$
|
1105
|
SELECT util.raise('NOTICE', $$SELECT $$||$1)
|
1106
|
$_$;
|
1107
|
|
1108
|
|
1109
|
--
|
1110
|
-- Name: debug_print_return_value(anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1111
|
--
|
1112
|
|
1113
|
CREATE FUNCTION debug_print_return_value(value anyelement, encode boolean DEFAULT false) RETURNS anyelement
|
1114
|
LANGUAGE sql IMMUTABLE
|
1115
|
AS $_$
|
1116
|
SELECT util.debug_print_value('returns: ', $1, $2);
|
1117
|
SELECT $1;
|
1118
|
$_$;
|
1119
|
|
1120
|
|
1121
|
--
|
1122
|
-- Name: debug_print_sql(text); Type: FUNCTION; Schema: util; Owner: -
|
1123
|
--
|
1124
|
|
1125
|
CREATE FUNCTION debug_print_sql(sql text) RETURNS void
|
1126
|
LANGUAGE sql IMMUTABLE
|
1127
|
AS $_$
|
1128
|
/* newline before so the query starts at the beginning of the line.
|
1129
|
newline after to visually separate queries from one another. */
|
1130
|
SELECT util.raise('NOTICE', $$
|
1131
|
$$||util.runnable_sql($1)||$$
|
1132
|
$$||COALESCE(util.explain2notice_msg_if_can($1), ''))
|
1133
|
$_$;
|
1134
|
|
1135
|
|
1136
|
--
|
1137
|
-- Name: debug_print_value(text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1138
|
--
|
1139
|
|
1140
|
CREATE FUNCTION debug_print_value(label text, value anyelement, encode boolean DEFAULT false) RETURNS void
|
1141
|
LANGUAGE sql IMMUTABLE
|
1142
|
AS $_$
|
1143
|
SELECT util.raise('NOTICE', concat($1,
|
1144
|
(CASE WHEN $3 THEN util.quote_typed($2) ELSE $2::text END))||$$
|
1145
|
$$)
|
1146
|
$_$;
|
1147
|
|
1148
|
|
1149
|
--
|
1150
|
-- Name: debug_print_var(text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1151
|
--
|
1152
|
|
1153
|
CREATE FUNCTION debug_print_var(var text, value anyelement, encode boolean DEFAULT false) RETURNS void
|
1154
|
LANGUAGE sql IMMUTABLE
|
1155
|
AS $_$
|
1156
|
/* can't use EXECUTE in the caller because "No substitution of PL/pgSQL
|
1157
|
variables is done on the computed command string"
|
1158
|
(http://www.postgresql.org/docs/9.3/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN) */
|
1159
|
SELECT util.debug_print_value($1||' = ', $2, $3)
|
1160
|
$_$;
|
1161
|
|
1162
|
|
1163
|
--
|
1164
|
-- Name: derived_cols(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
|
1165
|
--
|
1166
|
|
1167
|
CREATE FUNCTION derived_cols(table_ regclass, names regclass) RETURNS SETOF text
|
1168
|
LANGUAGE sql STABLE
|
1169
|
AS $_$
|
1170
|
SELECT util.eval2set($$
|
1171
|
SELECT col
|
1172
|
FROM util.col_names($$||quote_nullable($1)||$$::regclass) f (col)
|
1173
|
LEFT JOIN $$||$2||$$ ON "to" = col
|
1174
|
WHERE "from" IS NULL
|
1175
|
$$, NULL::text)
|
1176
|
$_$;
|
1177
|
|
1178
|
|
1179
|
--
|
1180
|
-- Name: FUNCTION derived_cols(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
|
1181
|
--
|
1182
|
|
1183
|
COMMENT ON FUNCTION derived_cols(table_ regclass, names regclass) IS '
|
1184
|
gets table_''s derived columns (all the columns not in the names table)
|
1185
|
';
|
1186
|
|
1187
|
|
1188
|
--
|
1189
|
-- Name: diff(regclass, regclass, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
1190
|
--
|
1191
|
|
1192
|
CREATE FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) RETURNS SETOF record
|
1193
|
LANGUAGE sql
|
1194
|
AS $_$
|
1195
|
-- create a diff when the # of copies of a row differs between the tables
|
1196
|
SELECT util.to_freq($1);
|
1197
|
SELECT util.to_freq($2);
|
1198
|
SELECT util.auto_rm_freq(ARRAY[$1, $2]);
|
1199
|
|
1200
|
SELECT * FROM util.diff($1, $2, $3, has_freq := true)
|
1201
|
$_$;
|
1202
|
|
1203
|
|
1204
|
--
|
1205
|
-- Name: FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement); Type: COMMENT; Schema: util; Owner: -
|
1206
|
--
|
1207
|
|
1208
|
COMMENT ON FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) IS '
|
1209
|
usage:
|
1210
|
SELECT * FROM util.diff(''"left_table"''::regclass, ''"right_table"''::regclass, NULL::shared_base_type)
|
1211
|
|
1212
|
col_type_null (*required*): NULL::shared_base_type
|
1213
|
';
|
1214
|
|
1215
|
|
1216
|
--
|
1217
|
-- Name: diff(text, text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1218
|
--
|
1219
|
|
1220
|
CREATE FUNCTION diff(left__ text, right__ text, col_type_null anyelement, single_row boolean DEFAULT false, OUT left_ anyelement, OUT right_ anyelement) RETURNS SETOF record
|
1221
|
LANGUAGE plpgsql
|
1222
|
SET search_path TO pg_temp
|
1223
|
AS $_$
|
1224
|
/* must use LANGUAGE plpgsql because LANGUAGE sql does not support runtime
|
1225
|
changes of search_path (schema elements are bound at inline time rather than
|
1226
|
runtime) */
|
1227
|
/* function option search_path is needed to limit the effects of
|
1228
|
`SET LOCAL search_path` to the current function */
|
1229
|
BEGIN
|
1230
|
PERFORM util.use_schema($3); -- includes util.%== as default/fallback
|
1231
|
|
1232
|
PERFORM util.mk_keys_func(pg_typeof($3));
|
1233
|
RETURN QUERY
|
1234
|
SELECT * FROM util.eval2col_pair(util.mk_diff_query($1, $2,
|
1235
|
$$/* need to explicitly cast each side to the return type because this does not
|
1236
|
happen automatically even when an implicit cast is available */
|
1237
|
left_::$$||util.typeof($3)||$$
|
1238
|
, right_::$$||util.typeof($3)
|
1239
|
/* when using the util.%==(anyelement, anyelement) operator, you must cast to
|
1240
|
the *same* base type, *even though* this is optional when using a custom %== */
|
1241
|
, util._if($4, $$true/*= CROSS JOIN*/$$,
|
1242
|
$$ left_::$$||util.typeof($3)||$$
|
1243
|
%== right_::$$||util.typeof($3)||$$
|
1244
|
-- refer to EXPLAIN output for expansion of %==$$
|
1245
|
)
|
1246
|
, $$ left_::$$||util.typeof($3)||$$
|
1247
|
IS DISTINCT FROM right_::$$||util.typeof($3)
|
1248
|
), $3)
|
1249
|
;
|
1250
|
END;
|
1251
|
$_$;
|
1252
|
|
1253
|
|
1254
|
--
|
1255
|
-- Name: FUNCTION diff(left__ text, right__ text, col_type_null anyelement, single_row boolean, OUT left_ anyelement, OUT right_ anyelement); Type: COMMENT; Schema: util; Owner: -
|
1256
|
--
|
1257
|
|
1258
|
COMMENT ON FUNCTION diff(left__ text, right__ text, col_type_null anyelement, single_row boolean, OUT left_ anyelement, OUT right_ anyelement) IS '
|
1259
|
col_type_null (*required*): NULL::col_type
|
1260
|
single_row: whether the tables consist of a single row, which should be
|
1261
|
displayed side-by-side
|
1262
|
|
1263
|
to match up rows using a subset of the columns, create a custom keys() function
|
1264
|
which returns this subset as a record:
|
1265
|
-- note that OUT parameters for the returned fields are *not* needed
|
1266
|
CREATE OR REPLACE FUNCTION [schema].keys(value [schema].[base_type])
|
1267
|
RETURNS record AS
|
1268
|
$BODY$
|
1269
|
SELECT ($1.key_field_0, $1.key_field_1)
|
1270
|
$BODY$
|
1271
|
LANGUAGE sql IMMUTABLE
|
1272
|
COST 100;
|
1273
|
|
1274
|
|
1275
|
to run EXPLAIN on the FULL JOIN query:
|
1276
|
# run this function
|
1277
|
# look for a NOTICE containing the expanded query that it ran
|
1278
|
# run EXPLAIN on this expanded query
|
1279
|
';
|
1280
|
|
1281
|
|
1282
|
--
|
1283
|
-- Name: diff(regclass, regclass, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1284
|
--
|
1285
|
|
1286
|
CREATE FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, has_freq boolean, OUT left_ anyelement, OUT right_ anyelement) RETURNS SETOF record
|
1287
|
LANGUAGE sql
|
1288
|
AS $_$
|
1289
|
SELECT * FROM util.diff($1::text, $2::text, $3,
|
1290
|
single_row := util.has_single_row($1) AND util.has_single_row($2))
|
1291
|
$_$;
|
1292
|
|
1293
|
|
1294
|
--
|
1295
|
-- Name: FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, has_freq boolean, OUT left_ anyelement, OUT right_ anyelement); Type: COMMENT; Schema: util; Owner: -
|
1296
|
--
|
1297
|
|
1298
|
COMMENT ON FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, has_freq boolean, OUT left_ anyelement, OUT right_ anyelement) IS '
|
1299
|
helper function used by diff(regclass, regclass)
|
1300
|
|
1301
|
usage:
|
1302
|
SELECT * FROM util.diff(''"left_freq_table"''::regclass, ''"right_freq_table"''::regclass, NULL::shared_base_type, has_freq := true)
|
1303
|
|
1304
|
col_type_null (*required*): NULL::shared_base_type
|
1305
|
';
|
1306
|
|
1307
|
|
1308
|
--
|
1309
|
-- Name: do_optionally_ignore(text, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1310
|
--
|
1311
|
|
1312
|
CREATE FUNCTION do_optionally_ignore(sql text, ignore boolean) RETURNS void
|
1313
|
LANGUAGE sql
|
1314
|
AS $_$
|
1315
|
SELECT CASE WHEN $2 THEN util.try_create($1) ELSE util.create_if_not_exists($1) END
|
1316
|
$_$;
|
1317
|
|
1318
|
|
1319
|
--
|
1320
|
-- Name: FUNCTION do_optionally_ignore(sql text, ignore boolean); Type: COMMENT; Schema: util; Owner: -
|
1321
|
--
|
1322
|
|
1323
|
COMMENT ON FUNCTION do_optionally_ignore(sql text, ignore boolean) IS '
|
1324
|
idempotent
|
1325
|
';
|
1326
|
|
1327
|
|
1328
|
--
|
1329
|
-- Name: drop_column(col_ref, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1330
|
--
|
1331
|
|
1332
|
CREATE FUNCTION drop_column(col col_ref, force boolean DEFAULT false) RETURNS void
|
1333
|
LANGUAGE sql
|
1334
|
AS $_$
|
1335
|
SELECT util.eval($$ALTER TABLE $$||$1.table_||$$ DROP COLUMN IF EXISTS $$||
|
1336
|
quote_ident($1.name)||util._if($2, $$ CASCADE$$, ''::text))
|
1337
|
$_$;
|
1338
|
|
1339
|
|
1340
|
--
|
1341
|
-- Name: FUNCTION drop_column(col col_ref, force boolean); Type: COMMENT; Schema: util; Owner: -
|
1342
|
--
|
1343
|
|
1344
|
COMMENT ON FUNCTION drop_column(col col_ref, force boolean) IS '
|
1345
|
idempotent
|
1346
|
';
|
1347
|
|
1348
|
|
1349
|
--
|
1350
|
-- Name: drop_column(regclass[], text, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1351
|
--
|
1352
|
|
1353
|
CREATE FUNCTION drop_column(tables regclass[], col text, force boolean DEFAULT false) RETURNS void
|
1354
|
LANGUAGE sql
|
1355
|
AS $_$
|
1356
|
SELECT util.drop_column((table_, $2), $3) FROM unnest($1) table_;
|
1357
|
SELECT NULL::void; -- don't fold away functions called in previous query
|
1358
|
$_$;
|
1359
|
|
1360
|
|
1361
|
--
|
1362
|
-- Name: FUNCTION drop_column(tables regclass[], col text, force boolean); Type: COMMENT; Schema: util; Owner: -
|
1363
|
--
|
1364
|
|
1365
|
COMMENT ON FUNCTION drop_column(tables regclass[], col text, force boolean) IS '
|
1366
|
idempotent
|
1367
|
';
|
1368
|
|
1369
|
|
1370
|
--
|
1371
|
-- Name: drop_relation(anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1372
|
--
|
1373
|
|
1374
|
CREATE FUNCTION drop_relation(relation anyelement, force boolean DEFAULT false) RETURNS void
|
1375
|
LANGUAGE sql
|
1376
|
AS $_$
|
1377
|
/* use util.qual_name() instead of ::text so that the schema qualifier is always
|
1378
|
included in the debug SQL */
|
1379
|
SELECT util.drop_relation(util.relation_type($1), util.qual_name($1), $2)
|
1380
|
$_$;
|
1381
|
|
1382
|
|
1383
|
--
|
1384
|
-- Name: drop_relation(text, text, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1385
|
--
|
1386
|
|
1387
|
CREATE FUNCTION drop_relation(type text, relation_esc text, force boolean DEFAULT false) RETURNS void
|
1388
|
LANGUAGE sql
|
1389
|
AS $_$
|
1390
|
SELECT util.eval($$DROP $$||$1||$$ IF EXISTS $$||$2
|
1391
|
||util._if($3, $$ CASCADE$$, ''::text))
|
1392
|
$_$;
|
1393
|
|
1394
|
|
1395
|
--
|
1396
|
-- Name: FUNCTION drop_relation(type text, relation_esc text, force boolean); Type: COMMENT; Schema: util; Owner: -
|
1397
|
--
|
1398
|
|
1399
|
COMMENT ON FUNCTION drop_relation(type text, relation_esc text, force boolean) IS '
|
1400
|
idempotent
|
1401
|
';
|
1402
|
|
1403
|
|
1404
|
--
|
1405
|
-- Name: drop_relations_like(text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1406
|
--
|
1407
|
|
1408
|
CREATE FUNCTION drop_relations_like(name_regexp text, schema_anchor anyelement, force boolean DEFAULT false) RETURNS void
|
1409
|
LANGUAGE sql
|
1410
|
AS $_$
|
1411
|
SELECT util.drop_relations_like($1, util.schema_regexp($2), $3)
|
1412
|
$_$;
|
1413
|
|
1414
|
|
1415
|
--
|
1416
|
-- Name: drop_relations_like(text, text, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1417
|
--
|
1418
|
|
1419
|
CREATE FUNCTION drop_relations_like(name_regexp text, schema_regexp text DEFAULT ''::text, force boolean DEFAULT false) RETURNS void
|
1420
|
LANGUAGE sql
|
1421
|
AS $_$
|
1422
|
SELECT util.debug_print_func_call(util.quote_func_call(
|
1423
|
'util.drop_relations_like', util.quote_typed($1), util.quote_typed($2),
|
1424
|
util.quote_typed($3)))
|
1425
|
;
|
1426
|
SELECT util.drop_relation(relation, $3)
|
1427
|
FROM util.show_relations_like($1, $2) relation
|
1428
|
;
|
1429
|
SELECT NULL::void; -- don't fold away functions called in previous query
|
1430
|
$_$;
|
1431
|
|
1432
|
|
1433
|
--
|
1434
|
-- Name: drop_table(text, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1435
|
--
|
1436
|
|
1437
|
CREATE FUNCTION drop_table(table_ text, force boolean DEFAULT false) RETURNS void
|
1438
|
LANGUAGE sql
|
1439
|
AS $_$
|
1440
|
SELECT util.drop_relation('TABLE', $1, $2)
|
1441
|
$_$;
|
1442
|
|
1443
|
|
1444
|
--
|
1445
|
-- Name: FUNCTION drop_table(table_ text, force boolean); Type: COMMENT; Schema: util; Owner: -
|
1446
|
--
|
1447
|
|
1448
|
COMMENT ON FUNCTION drop_table(table_ text, force boolean) IS '
|
1449
|
idempotent
|
1450
|
';
|
1451
|
|
1452
|
|
1453
|
--
|
1454
|
-- Name: drop_view(text, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1455
|
--
|
1456
|
|
1457
|
CREATE FUNCTION drop_view(view_ text, force boolean DEFAULT false) RETURNS void
|
1458
|
LANGUAGE sql
|
1459
|
AS $_$
|
1460
|
SELECT util.drop_relation('VIEW', $1, $2)
|
1461
|
$_$;
|
1462
|
|
1463
|
|
1464
|
--
|
1465
|
-- Name: FUNCTION drop_view(view_ text, force boolean); Type: COMMENT; Schema: util; Owner: -
|
1466
|
--
|
1467
|
|
1468
|
COMMENT ON FUNCTION drop_view(view_ text, force boolean) IS '
|
1469
|
idempotent
|
1470
|
';
|
1471
|
|
1472
|
|
1473
|
--
|
1474
|
-- Name: empty_array(anyelement); Type: FUNCTION; Schema: util; Owner: -
|
1475
|
--
|
1476
|
|
1477
|
CREATE FUNCTION empty_array(elem_type_null anyelement DEFAULT NULL::text) RETURNS anyarray
|
1478
|
LANGUAGE sql IMMUTABLE
|
1479
|
AS $_$
|
1480
|
SELECT util.array_fill($1, 0)
|
1481
|
$_$;
|
1482
|
|
1483
|
|
1484
|
--
|
1485
|
-- Name: FUNCTION empty_array(elem_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
|
1486
|
--
|
1487
|
|
1488
|
COMMENT ON FUNCTION empty_array(elem_type_null anyelement) IS '
|
1489
|
constructs proper empty 1-dimensional array whose dimensions are not NULL ( ''{}''::text[] does not do this)
|
1490
|
';
|
1491
|
|
1492
|
|
1493
|
--
|
1494
|
-- Name: ensure_prefix(text, text); Type: FUNCTION; Schema: util; Owner: -
|
1495
|
--
|
1496
|
|
1497
|
CREATE FUNCTION ensure_prefix(prefix text, str text) RETURNS text
|
1498
|
LANGUAGE sql IMMUTABLE
|
1499
|
AS $_$
|
1500
|
SELECT (CASE WHEN util.has_prefix($1, $2) THEN $2 ELSE $1||$2 END)
|
1501
|
$_$;
|
1502
|
|
1503
|
|
1504
|
--
|
1505
|
-- Name: esc_name__append(text, text); Type: FUNCTION; Schema: util; Owner: -
|
1506
|
--
|
1507
|
|
1508
|
CREATE FUNCTION esc_name__append(suffix text, esc_name text) RETURNS text
|
1509
|
LANGUAGE sql IMMUTABLE
|
1510
|
AS $_$
|
1511
|
SELECT regexp_replace($2, '("?)$', $1||'\1')
|
1512
|
$_$;
|
1513
|
|
1514
|
|
1515
|
--
|
1516
|
-- Name: eval(text[]); Type: FUNCTION; Schema: util; Owner: -
|
1517
|
--
|
1518
|
|
1519
|
CREATE FUNCTION eval(queries text[]) RETURNS void
|
1520
|
LANGUAGE sql
|
1521
|
AS $_$
|
1522
|
SELECT util.eval(query) FROM unnest($1) query;
|
1523
|
SELECT NULL::void; -- don't fold away functions called in previous query
|
1524
|
$_$;
|
1525
|
|
1526
|
|
1527
|
--
|
1528
|
-- Name: eval(text, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1529
|
--
|
1530
|
|
1531
|
CREATE FUNCTION eval(sql text, verbose_ boolean DEFAULT true) RETURNS void
|
1532
|
LANGUAGE plpgsql
|
1533
|
AS $$
|
1534
|
BEGIN
|
1535
|
IF verbose_ THEN PERFORM util.debug_print_sql(sql); END IF;
|
1536
|
EXECUTE sql;
|
1537
|
END;
|
1538
|
$$;
|
1539
|
|
1540
|
|
1541
|
--
|
1542
|
-- Name: eval2col_pair(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
1543
|
--
|
1544
|
|
1545
|
CREATE FUNCTION eval2col_pair(sql text, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) RETURNS SETOF record
|
1546
|
LANGUAGE plpgsql
|
1547
|
AS $$
|
1548
|
BEGIN
|
1549
|
PERFORM util.debug_print_sql(sql);
|
1550
|
RETURN QUERY EXECUTE sql;
|
1551
|
END;
|
1552
|
$$;
|
1553
|
|
1554
|
|
1555
|
--
|
1556
|
-- Name: FUNCTION eval2col_pair(sql text, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement); Type: COMMENT; Schema: util; Owner: -
|
1557
|
--
|
1558
|
|
1559
|
COMMENT ON FUNCTION eval2col_pair(sql text, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) IS '
|
1560
|
col_type_null (*required*): NULL::col_type
|
1561
|
';
|
1562
|
|
1563
|
|
1564
|
--
|
1565
|
-- Name: eval2records(text); Type: FUNCTION; Schema: util; Owner: -
|
1566
|
--
|
1567
|
|
1568
|
CREATE FUNCTION eval2records(sql text) RETURNS SETOF record
|
1569
|
LANGUAGE plpgsql
|
1570
|
AS $$
|
1571
|
BEGIN
|
1572
|
PERFORM util.debug_print_sql(sql);
|
1573
|
RETURN QUERY EXECUTE sql;
|
1574
|
END;
|
1575
|
$$;
|
1576
|
|
1577
|
|
1578
|
--
|
1579
|
-- Name: eval2set(text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
|
1580
|
--
|
1581
|
|
1582
|
CREATE FUNCTION eval2set(sql text, ret_type_null anyelement DEFAULT NULL::text, verbose_ boolean DEFAULT true) RETURNS SETOF anyelement
|
1583
|
LANGUAGE plpgsql
|
1584
|
AS $$
|
1585
|
BEGIN
|
1586
|
IF verbose_ THEN PERFORM util.debug_print_sql(sql); END IF;
|
1587
|
RETURN QUERY EXECUTE sql;
|
1588
|
END;
|
1589
|
$$;
|
1590
|
|
1591
|
|
1592
|
--
|
1593
|
-- Name: eval2val(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
1594
|
--
|
1595
|
|
1596
|
CREATE FUNCTION eval2val(sql text, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
|
1597
|
LANGUAGE plpgsql STABLE
|
1598
|
AS $$
|
1599
|
DECLARE
|
1600
|
ret_val ret_type_null%TYPE;
|
1601
|
BEGIN
|
1602
|
PERFORM util.debug_print_sql(sql);
|
1603
|
EXECUTE sql INTO STRICT ret_val;
|
1604
|
RETURN ret_val;
|
1605
|
END;
|
1606
|
$$;
|
1607
|
|
1608
|
|
1609
|
--
|
1610
|
-- Name: FUNCTION eval2val(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
|
1611
|
--
|
1612
|
|
1613
|
COMMENT ON FUNCTION eval2val(sql text, ret_type_null anyelement) IS '
|
1614
|
ret_type_null: NULL::ret_type
|
1615
|
';
|
1616
|
|
1617
|
|
1618
|
--
|
1619
|
-- Name: eval_expr(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
1620
|
--
|
1621
|
|
1622
|
CREATE FUNCTION eval_expr(sql text, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
|
1623
|
LANGUAGE sql
|
1624
|
AS $_$
|
1625
|
SELECT util.eval2val($$SELECT $$||$1, $2)
|
1626
|
$_$;
|
1627
|
|
1628
|
|
1629
|
--
|
1630
|
-- Name: FUNCTION eval_expr(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
|
1631
|
--
|
1632
|
|
1633
|
COMMENT ON FUNCTION eval_expr(sql text, ret_type_null anyelement) IS '
|
1634
|
ret_type_null: NULL::ret_type
|
1635
|
';
|
1636
|
|
1637
|
|
1638
|
--
|
1639
|
-- Name: eval_expr_passthru(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
1640
|
--
|
1641
|
|
1642
|
CREATE FUNCTION eval_expr_passthru(sql text, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
|
1643
|
LANGUAGE sql
|
1644
|
AS $_$
|
1645
|
SELECT CASE WHEN $1 IS NULL THEN NULL ELSE util.eval_expr($1, $2) END
|
1646
|
$_$;
|
1647
|
|
1648
|
|
1649
|
--
|
1650
|
-- Name: FUNCTION eval_expr_passthru(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
|
1651
|
--
|
1652
|
|
1653
|
COMMENT ON FUNCTION eval_expr_passthru(sql text, ret_type_null anyelement) IS '
|
1654
|
sql: can be NULL, which will be passed through
|
1655
|
ret_type_null: NULL::ret_type
|
1656
|
';
|
1657
|
|
1658
|
|
1659
|
--
|
1660
|
-- Name: existing_cols(regclass, text[]); Type: FUNCTION; Schema: util; Owner: -
|
1661
|
--
|
1662
|
|
1663
|
CREATE FUNCTION existing_cols(table_ regclass, VARIADIC col_names text[]) RETURNS SETOF text
|
1664
|
LANGUAGE sql STABLE
|
1665
|
AS $_$
|
1666
|
SELECT col_name
|
1667
|
FROM unnest($2) s (col_name)
|
1668
|
WHERE util.col_exists(($1, col_name))
|
1669
|
$_$;
|
1670
|
|
1671
|
|
1672
|
--
|
1673
|
-- Name: explain(text); Type: FUNCTION; Schema: util; Owner: -
|
1674
|
--
|
1675
|
|
1676
|
CREATE FUNCTION explain(sql text) RETURNS SETOF text
|
1677
|
LANGUAGE sql
|
1678
|
SET client_min_messages TO 'error'
|
1679
|
AS $_$
|
1680
|
/* `client_min_messages = ERROR`: EXPLAIN apparently runs IMMUTABLE functions in
|
1681
|
the query, so this prevents displaying any log messages printed by them */
|
1682
|
SELECT util.eval2set($$EXPLAIN $$||$1, verbose_ := false)
|
1683
|
$_$;
|
1684
|
|
1685
|
|
1686
|
--
|
1687
|
-- Name: explain2notice(text); Type: FUNCTION; Schema: util; Owner: -
|
1688
|
--
|
1689
|
|
1690
|
CREATE FUNCTION explain2notice(sql text) RETURNS void
|
1691
|
LANGUAGE sql
|
1692
|
AS $_$
|
1693
|
SELECT util.raise('NOTICE', util.explain2notice_msg($1))
|
1694
|
$_$;
|
1695
|
|
1696
|
|
1697
|
--
|
1698
|
-- Name: explain2notice_msg(text); Type: FUNCTION; Schema: util; Owner: -
|
1699
|
--
|
1700
|
|
1701
|
CREATE FUNCTION explain2notice_msg(sql text) RETURNS text
|
1702
|
LANGUAGE sql
|
1703
|
AS $_$
|
1704
|
-- newline before and after to visually separate it from other debug info
|
1705
|
SELECT COALESCE($$
|
1706
|
EXPLAIN:
|
1707
|
$$||util.fold_explain_msg(util.explain2str($1))||$$
|
1708
|
$$, '')
|
1709
|
$_$;
|
1710
|
|
1711
|
|
1712
|
--
|
1713
|
-- Name: explain2notice_msg_if_can(text); Type: FUNCTION; Schema: util; Owner: -
|
1714
|
--
|
1715
|
|
1716
|
CREATE FUNCTION explain2notice_msg_if_can(sql text) RETURNS text
|
1717
|
LANGUAGE plpgsql
|
1718
|
AS $$
|
1719
|
BEGIN
|
1720
|
RETURN util.explain2notice_msg(sql);
|
1721
|
EXCEPTION
|
1722
|
WHEN syntax_error THEN RETURN NULL; -- non-explainable query
|
1723
|
/* don't use util.is_explainable() because the list provided by Postgres
|
1724
|
(http://www.postgresql.org/docs/9.3/static/sql-explain.html#AEN77691)
|
1725
|
excludes some query types that are in fact EXPLAIN-able */
|
1726
|
END;
|
1727
|
$$;
|
1728
|
|
1729
|
|
1730
|
--
|
1731
|
-- Name: explain2str(text); Type: FUNCTION; Schema: util; Owner: -
|
1732
|
--
|
1733
|
|
1734
|
CREATE FUNCTION explain2str(sql text) RETURNS text
|
1735
|
LANGUAGE sql
|
1736
|
AS $_$
|
1737
|
SELECT util.join_strs(explain, $$
|
1738
|
$$) FROM util.explain($1)
|
1739
|
$_$;
|
1740
|
|
1741
|
|
1742
|
SET default_tablespace = '';
|
1743
|
|
1744
|
SET default_with_oids = false;
|
1745
|
|
1746
|
--
|
1747
|
-- Name: explain; Type: TABLE; Schema: util; Owner: -; Tablespace:
|
1748
|
--
|
1749
|
|
1750
|
CREATE TABLE explain (
|
1751
|
line text NOT NULL
|
1752
|
);
|
1753
|
|
1754
|
|
1755
|
--
|
1756
|
-- Name: explain2table(text, regclass); Type: FUNCTION; Schema: util; Owner: -
|
1757
|
--
|
1758
|
|
1759
|
CREATE FUNCTION explain2table(sql text, table_ regclass DEFAULT 'explain'::regclass) RETURNS void
|
1760
|
LANGUAGE sql
|
1761
|
AS $_$
|
1762
|
SELECT util.eval($$INSERT INTO $$||$2||$$ SELECT util.explain(
|
1763
|
$$||quote_nullable($1)||$$
|
1764
|
)$$)
|
1765
|
$_$;
|
1766
|
|
1767
|
|
1768
|
--
|
1769
|
-- Name: FUNCTION explain2table(sql text, table_ regclass); Type: COMMENT; Schema: util; Owner: -
|
1770
|
--
|
1771
|
|
1772
|
COMMENT ON FUNCTION explain2table(sql text, table_ regclass) IS '
|
1773
|
usage:
|
1774
|
PERFORM util.explain2table($$
|
1775
|
query
|
1776
|
$$);
|
1777
|
';
|
1778
|
|
1779
|
|
1780
|
--
|
1781
|
-- Name: first_word(text); Type: FUNCTION; Schema: util; Owner: -
|
1782
|
--
|
1783
|
|
1784
|
CREATE FUNCTION first_word(str text) RETURNS text
|
1785
|
LANGUAGE sql IMMUTABLE
|
1786
|
AS $_$
|
1787
|
SELECT match[1] FROM regexp_matches(util.ltrim_nl($1), '^(\S*)') match
|
1788
|
$_$;
|
1789
|
|
1790
|
|
1791
|
--
|
1792
|
-- Name: fix_array(anyarray); Type: FUNCTION; Schema: util; Owner: -
|
1793
|
--
|
1794
|
|
1795
|
CREATE FUNCTION fix_array("array" anyarray) RETURNS anyarray
|
1796
|
LANGUAGE sql IMMUTABLE
|
1797
|
AS $_$
|
1798
|
SELECT CASE WHEN $1 IS NULL THEN NULL ELSE (
|
1799
|
CASE WHEN pg_catalog.array_ndims($1) IS NULL THEN util.empty_array($1[1]) ELSE $1 END
|
1800
|
) END
|
1801
|
$_$;
|
1802
|
|
1803
|
|
1804
|
--
|
1805
|
-- Name: FUNCTION fix_array("array" anyarray); Type: COMMENT; Schema: util; Owner: -
|
1806
|
--
|
1807
|
|
1808
|
COMMENT ON FUNCTION fix_array("array" anyarray) IS '
|
1809
|
ensures that an array will always have proper non-NULL dimensions
|
1810
|
';
|
1811
|
|
1812
|
|
1813
|
--
|
1814
|
-- Name: fold_explain_msg(text); Type: FUNCTION; Schema: util; Owner: -
|
1815
|
--
|
1816
|
|
1817
|
CREATE FUNCTION fold_explain_msg(explain text) RETURNS text
|
1818
|
LANGUAGE sql IMMUTABLE
|
1819
|
AS $_$
|
1820
|
SELECT (CASE WHEN util.first_word($1) = 'Result' THEN NULL ELSE $1 END)
|
1821
|
$_$;
|
1822
|
|
1823
|
|
1824
|
--
|
1825
|
-- Name: force_update_view(text, text); Type: FUNCTION; Schema: util; Owner: -
|
1826
|
--
|
1827
|
|
1828
|
CREATE FUNCTION force_update_view(view_ text, query text) RETURNS void
|
1829
|
LANGUAGE plpgsql STRICT
|
1830
|
AS $_$
|
1831
|
DECLARE
|
1832
|
mk_view text = $$CREATE OR REPLACE VIEW $$||view_||$$ AS
|
1833
|
$$||query;
|
1834
|
BEGIN
|
1835
|
EXECUTE mk_view;
|
1836
|
EXCEPTION
|
1837
|
WHEN invalid_table_definition THEN
|
1838
|
IF SQLERRM = 'cannot drop columns from view'
|
1839
|
OR SQLERRM LIKE 'cannot change name of view column "%" to "%"'
|
1840
|
THEN
|
1841
|
EXECUTE $$DROP VIEW $$||view_||$$ CASCADE$$;
|
1842
|
EXECUTE mk_view;
|
1843
|
ELSE RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
|
1844
|
END IF;
|
1845
|
END;
|
1846
|
$_$;
|
1847
|
|
1848
|
|
1849
|
--
|
1850
|
-- Name: FUNCTION force_update_view(view_ text, query text); Type: COMMENT; Schema: util; Owner: -
|
1851
|
--
|
1852
|
|
1853
|
COMMENT ON FUNCTION force_update_view(view_ text, query text) IS '
|
1854
|
idempotent
|
1855
|
';
|
1856
|
|
1857
|
|
1858
|
--
|
1859
|
-- Name: freq_always_1(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
1860
|
--
|
1861
|
|
1862
|
CREATE FUNCTION freq_always_1(table_ regclass, freq_col text DEFAULT 'copies'::text) RETURNS boolean
|
1863
|
LANGUAGE sql STABLE
|
1864
|
AS $_$
|
1865
|
SELECT util.eval2val(
|
1866
|
$$SELECT NOT EXISTS( -- there is no row that is != 1
|
1867
|
SELECT NULL
|
1868
|
FROM $$||$1||$$
|
1869
|
WHERE $$||quote_ident(freq_col)||$$ IS DISTINCT FROM 1
|
1870
|
LIMIT 1
|
1871
|
)
|
1872
|
$$, NULL::boolean)
|
1873
|
$_$;
|
1874
|
|
1875
|
|
1876
|
--
|
1877
|
-- Name: freq_always_1(regclass[], text); Type: FUNCTION; Schema: util; Owner: -
|
1878
|
--
|
1879
|
|
1880
|
CREATE FUNCTION freq_always_1(tables regclass[], freq_col text DEFAULT 'copies'::text) RETURNS boolean
|
1881
|
LANGUAGE sql STABLE
|
1882
|
AS $_$
|
1883
|
SELECT bool_and(util.freq_always_1(table_, $2)) FROM unnest($1) table_
|
1884
|
$_$;
|
1885
|
|
1886
|
|
1887
|
--
|
1888
|
-- Name: grants_users(); Type: FUNCTION; Schema: util; Owner: -
|
1889
|
--
|
1890
|
|
1891
|
CREATE FUNCTION grants_users() RETURNS SETOF text
|
1892
|
LANGUAGE sql IMMUTABLE
|
1893
|
AS $$
|
1894
|
VALUES ('bien_read'), ('public_')
|
1895
|
$$;
|
1896
|
|
1897
|
|
1898
|
--
|
1899
|
-- Name: has_prefix(text, text); Type: FUNCTION; Schema: util; Owner: -
|
1900
|
--
|
1901
|
|
1902
|
CREATE FUNCTION has_prefix(prefix text, str text) RETURNS boolean
|
1903
|
LANGUAGE sql IMMUTABLE
|
1904
|
AS $_$
|
1905
|
SELECT substring($2 for length($1)) = $1
|
1906
|
$_$;
|
1907
|
|
1908
|
|
1909
|
--
|
1910
|
-- Name: has_single_row(regclass); Type: FUNCTION; Schema: util; Owner: -
|
1911
|
--
|
1912
|
|
1913
|
CREATE FUNCTION has_single_row(table_ regclass) RETURNS boolean
|
1914
|
LANGUAGE sql STABLE
|
1915
|
AS $_$
|
1916
|
SELECT util.eval2val($$SELECT COUNT(*) = 1 FROM $$||$1, NULL::boolean)
|
1917
|
$_$;
|
1918
|
|
1919
|
|
1920
|
--
|
1921
|
-- Name: hstore(text[], text); Type: FUNCTION; Schema: util; Owner: -
|
1922
|
--
|
1923
|
|
1924
|
CREATE FUNCTION hstore(keys text[], value text) RETURNS hstore
|
1925
|
LANGUAGE sql IMMUTABLE
|
1926
|
AS $_$
|
1927
|
SELECT hstore(util.fix_array($1), util.array_fill($2, util.array_length($1)))
|
1928
|
$_$;
|
1929
|
|
1930
|
|
1931
|
--
|
1932
|
-- Name: FUNCTION hstore(keys text[], value text); Type: COMMENT; Schema: util; Owner: -
|
1933
|
--
|
1934
|
|
1935
|
COMMENT ON FUNCTION hstore(keys text[], value text) IS '
|
1936
|
avoids repeating the same value for each key
|
1937
|
';
|
1938
|
|
1939
|
|
1940
|
--
|
1941
|
-- Name: ifnull(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
1942
|
--
|
1943
|
|
1944
|
CREATE FUNCTION ifnull(value anyelement, null_ anyelement) RETURNS anyelement
|
1945
|
LANGUAGE sql IMMUTABLE
|
1946
|
AS $_$
|
1947
|
SELECT COALESCE($1, $2)
|
1948
|
$_$;
|
1949
|
|
1950
|
|
1951
|
--
|
1952
|
-- Name: FUNCTION ifnull(value anyelement, null_ anyelement); Type: COMMENT; Schema: util; Owner: -
|
1953
|
--
|
1954
|
|
1955
|
COMMENT ON FUNCTION ifnull(value anyelement, null_ anyelement) IS '
|
1956
|
equivalent to MySQL''s IFNULL() (Postgres auto-lowercases the name)
|
1957
|
';
|
1958
|
|
1959
|
|
1960
|
--
|
1961
|
-- Name: in_reverse(anyarray); Type: FUNCTION; Schema: util; Owner: -
|
1962
|
--
|
1963
|
|
1964
|
CREATE FUNCTION in_reverse("array" anyarray) RETURNS SETOF anyelement
|
1965
|
LANGUAGE sql IMMUTABLE
|
1966
|
AS $_$
|
1967
|
SELECT elem FROM unnest($1) elem ORDER BY row_number() OVER () DESC
|
1968
|
$_$;
|
1969
|
|
1970
|
|
1971
|
--
|
1972
|
-- Name: inherit(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
|
1973
|
--
|
1974
|
|
1975
|
CREATE FUNCTION inherit(derived regclass, base regclass) RETURNS void
|
1976
|
LANGUAGE sql
|
1977
|
AS $_$
|
1978
|
SELECT util.eval($$ALTER TABLE $$||$1||$$ INHERIT $$||$2)
|
1979
|
$_$;
|
1980
|
|
1981
|
|
1982
|
--
|
1983
|
-- Name: is_castable(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
1984
|
--
|
1985
|
|
1986
|
CREATE FUNCTION is_castable(value text, ret_type_null anyelement) RETURNS boolean
|
1987
|
LANGUAGE plpgsql IMMUTABLE
|
1988
|
AS $$
|
1989
|
BEGIN
|
1990
|
PERFORM util.cast(value, ret_type_null);
|
1991
|
-- must happen *after* cast check, because NULL is not valid for some types
|
1992
|
IF value IS NULL THEN RETURN NULL; END IF; -- pass NULL through
|
1993
|
RETURN true;
|
1994
|
EXCEPTION
|
1995
|
WHEN data_exception
|
1996
|
OR syntax_error_or_access_rule_violation -- eg. ::regclass
|
1997
|
THEN
|
1998
|
RETURN false;
|
1999
|
END;
|
2000
|
$$;
|
2001
|
|
2002
|
|
2003
|
--
|
2004
|
-- Name: FUNCTION is_castable(value text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
|
2005
|
--
|
2006
|
|
2007
|
COMMENT ON FUNCTION is_castable(value text, ret_type_null anyelement) IS '
|
2008
|
passes NULL through. however, if NULL is not valid for the type, false will be
|
2009
|
returned instead.
|
2010
|
|
2011
|
ret_type_null: NULL::ret_type
|
2012
|
';
|
2013
|
|
2014
|
|
2015
|
--
|
2016
|
-- Name: is_constant(col_ref); Type: FUNCTION; Schema: util; Owner: -
|
2017
|
--
|
2018
|
|
2019
|
CREATE FUNCTION is_constant(col col_ref) RETURNS boolean
|
2020
|
LANGUAGE sql STABLE
|
2021
|
AS $_$
|
2022
|
SELECT COALESCE(util.col_comment($1) LIKE '
|
2023
|
constant
|
2024
|
%', false)
|
2025
|
$_$;
|
2026
|
|
2027
|
|
2028
|
--
|
2029
|
-- Name: is_empty(anyarray); Type: FUNCTION; Schema: util; Owner: -
|
2030
|
--
|
2031
|
|
2032
|
CREATE FUNCTION is_empty("array" anyarray) RETURNS boolean
|
2033
|
LANGUAGE sql IMMUTABLE
|
2034
|
AS $_$
|
2035
|
SELECT util.array_length($1) = 0
|
2036
|
$_$;
|
2037
|
|
2038
|
|
2039
|
--
|
2040
|
-- Name: is_explain(text); Type: FUNCTION; Schema: util; Owner: -
|
2041
|
--
|
2042
|
|
2043
|
CREATE FUNCTION is_explain(sql text) RETURNS boolean
|
2044
|
LANGUAGE sql IMMUTABLE
|
2045
|
AS $_$
|
2046
|
SELECT upper(util.first_word($1)) = 'EXPLAIN'
|
2047
|
$_$;
|
2048
|
|
2049
|
|
2050
|
--
|
2051
|
-- Name: is_explainable(text); Type: FUNCTION; Schema: util; Owner: -
|
2052
|
--
|
2053
|
|
2054
|
CREATE FUNCTION is_explainable(sql text) RETURNS boolean
|
2055
|
LANGUAGE sql IMMUTABLE
|
2056
|
AS $_$
|
2057
|
SELECT upper(util.first_word($1)) = ANY(
|
2058
|
'{SELECT,INSERT,UPDATE,DELETE,VALUES,EXECUTE,DECLARE}'::text[]
|
2059
|
/*from http://www.postgresql.org/docs/9.3/static/sql-explain.html#AEN77691*/
|
2060
|
)
|
2061
|
$_$;
|
2062
|
|
2063
|
|
2064
|
--
|
2065
|
-- Name: is_more_complete_than(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
2066
|
--
|
2067
|
|
2068
|
CREATE FUNCTION is_more_complete_than("left" anyelement, "right" anyelement) RETURNS boolean
|
2069
|
LANGUAGE sql IMMUTABLE
|
2070
|
AS $_$
|
2071
|
SELECT $1 IS NOT DISTINCT FROM $2 OR ($1 IS NOT NULL AND $2 IS NULL)
|
2072
|
$_$;
|
2073
|
|
2074
|
|
2075
|
--
|
2076
|
-- Name: is_populated_more_often_than(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
2077
|
--
|
2078
|
|
2079
|
CREATE FUNCTION is_populated_more_often_than("left" anyelement, "right" anyelement) RETURNS boolean
|
2080
|
LANGUAGE sql IMMUTABLE
|
2081
|
AS $_$
|
2082
|
SELECT $1 IS NOT NULL >= $2 IS NOT NULL -- true > false
|
2083
|
$_$;
|
2084
|
|
2085
|
|
2086
|
--
|
2087
|
-- Name: is_set_stmt(text); Type: FUNCTION; Schema: util; Owner: -
|
2088
|
--
|
2089
|
|
2090
|
CREATE FUNCTION is_set_stmt(sql text) RETURNS boolean
|
2091
|
LANGUAGE sql IMMUTABLE
|
2092
|
AS $_$
|
2093
|
SELECT upper(util.first_word($1)) = 'SET'
|
2094
|
$_$;
|
2095
|
|
2096
|
|
2097
|
--
|
2098
|
-- Name: is_table(regclass); Type: FUNCTION; Schema: util; Owner: -
|
2099
|
--
|
2100
|
|
2101
|
CREATE FUNCTION is_table(relation regclass) RETURNS boolean
|
2102
|
LANGUAGE sql STABLE
|
2103
|
AS $_$
|
2104
|
SELECT relkind = 'r' FROM pg_class WHERE oid = $1
|
2105
|
$_$;
|
2106
|
|
2107
|
|
2108
|
--
|
2109
|
-- Name: is_view(regclass); Type: FUNCTION; Schema: util; Owner: -
|
2110
|
--
|
2111
|
|
2112
|
CREATE FUNCTION is_view(relation regclass) RETURNS boolean
|
2113
|
LANGUAGE sql STABLE
|
2114
|
AS $_$
|
2115
|
SELECT relkind = 'v' FROM pg_class WHERE oid = $1
|
2116
|
$_$;
|
2117
|
|
2118
|
|
2119
|
--
|
2120
|
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: util; Owner: -
|
2121
|
--
|
2122
|
|
2123
|
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
|
2124
|
LANGUAGE sql IMMUTABLE STRICT
|
2125
|
AS $_$
|
2126
|
SELECT $1 || $3 || $2
|
2127
|
$_$;
|
2128
|
|
2129
|
|
2130
|
--
|
2131
|
-- Name: FUNCTION join_strs_transform(state text, value text, delim text); Type: COMMENT; Schema: util; Owner: -
|
2132
|
--
|
2133
|
|
2134
|
COMMENT ON FUNCTION join_strs_transform(state text, value text, delim text) IS '
|
2135
|
must be declared STRICT to use the special handling of STRICT aggregating functions
|
2136
|
';
|
2137
|
|
2138
|
|
2139
|
--
|
2140
|
-- Name: keys(anyelement); Type: FUNCTION; Schema: util; Owner: -
|
2141
|
--
|
2142
|
|
2143
|
CREATE FUNCTION keys(value anyelement) RETURNS anyelement
|
2144
|
LANGUAGE sql IMMUTABLE
|
2145
|
AS $_$
|
2146
|
SELECT $1 -- compare on the entire value
|
2147
|
$_$;
|
2148
|
|
2149
|
|
2150
|
--
|
2151
|
-- Name: limit2row_num(integer, integer, integer); Type: FUNCTION; Schema: util; Owner: -
|
2152
|
--
|
2153
|
|
2154
|
CREATE FUNCTION limit2row_num(limit_ integer, offset_ integer DEFAULT NULL::integer, min_row_num integer DEFAULT 1) RETURNS integer
|
2155
|
LANGUAGE sql IMMUTABLE
|
2156
|
AS $_$
|
2157
|
SELECT COALESCE(util.offset2row_num($2, $3) + $1 - 1, 2147483647)
|
2158
|
$_$;
|
2159
|
|
2160
|
|
2161
|
--
|
2162
|
-- Name: loop_ignore_errors(text, text, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
2163
|
--
|
2164
|
|
2165
|
CREATE FUNCTION loop_ignore_errors(iter_sql text, loop_body_sql text, loop_type_null anyelement DEFAULT NULL::text) RETURNS void
|
2166
|
LANGUAGE plpgsql
|
2167
|
AS $$
|
2168
|
DECLARE
|
2169
|
errors_ct integer = 0;
|
2170
|
loop_var loop_type_null%TYPE;
|
2171
|
BEGIN
|
2172
|
FOR loop_var IN SELECT * FROM util.eval2set(iter_sql, loop_type_null)
|
2173
|
LOOP
|
2174
|
BEGIN
|
2175
|
EXECUTE loop_body_sql USING loop_var;
|
2176
|
EXCEPTION
|
2177
|
WHEN OTHERS THEN
|
2178
|
errors_ct = errors_ct+1;
|
2179
|
PERFORM util.raise_error_warning(SQLERRM);
|
2180
|
END;
|
2181
|
END LOOP;
|
2182
|
IF errors_ct > 0 THEN
|
2183
|
-- can't raise exception because this would roll back the transaction
|
2184
|
PERFORM util.raise_error_warning('there were '||errors_ct
|
2185
|
||' errors: see the WARNINGs for details');
|
2186
|
END IF;
|
2187
|
END;
|
2188
|
$$;
|
2189
|
|
2190
|
|
2191
|
--
|
2192
|
-- Name: ltrim_nl(text); Type: FUNCTION; Schema: util; Owner: -
|
2193
|
--
|
2194
|
|
2195
|
CREATE FUNCTION ltrim_nl(str text) RETURNS text
|
2196
|
LANGUAGE sql IMMUTABLE
|
2197
|
AS $_$
|
2198
|
SELECT ltrim($1, $$
|
2199
|
$$)
|
2200
|
$_$;
|
2201
|
|
2202
|
|
2203
|
--
|
2204
|
-- Name: map_filter_insert(); Type: FUNCTION; Schema: util; Owner: -
|
2205
|
--
|
2206
|
|
2207
|
CREATE FUNCTION map_filter_insert() RETURNS trigger
|
2208
|
LANGUAGE plpgsql
|
2209
|
AS $$
|
2210
|
BEGIN
|
2211
|
IF new."from" LIKE ':%' THEN RETURN NULL; END IF; -- exclude metadata values
|
2212
|
RETURN new;
|
2213
|
END;
|
2214
|
$$;
|
2215
|
|
2216
|
|
2217
|
--
|
2218
|
-- Name: map_get(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
2219
|
--
|
2220
|
|
2221
|
CREATE FUNCTION map_get(map regclass, key text) RETURNS text
|
2222
|
LANGUAGE plpgsql STABLE STRICT
|
2223
|
AS $_$
|
2224
|
DECLARE
|
2225
|
value text;
|
2226
|
BEGIN
|
2227
|
EXECUTE $$SELECT "to" FROM $$||map||$$ WHERE "from" = $1$$
|
2228
|
INTO value USING key;
|
2229
|
RETURN value;
|
2230
|
END;
|
2231
|
$_$;
|
2232
|
|
2233
|
|
2234
|
--
|
2235
|
-- Name: map_nulls(text[], anyelement); Type: FUNCTION; Schema: util; Owner: -
|
2236
|
--
|
2237
|
|
2238
|
CREATE FUNCTION map_nulls(nulls text[], value anyelement) RETURNS anyelement
|
2239
|
LANGUAGE sql IMMUTABLE
|
2240
|
AS $_$
|
2241
|
SELECT util._map(util.nulls_map($1), $2)
|
2242
|
$_$;
|
2243
|
|
2244
|
|
2245
|
--
|
2246
|
-- Name: FUNCTION map_nulls(nulls text[], value anyelement); Type: COMMENT; Schema: util; Owner: -
|
2247
|
--
|
2248
|
|
2249
|
COMMENT ON FUNCTION map_nulls(nulls text[], value anyelement) IS '
|
2250
|
due to dynamic inlining[1], this is just as fast as util._map() which it wraps[2].
|
2251
|
|
2252
|
[1] inlining of function calls, which is different from constant folding
|
2253
|
[2] _map()''s profiling query
|
2254
|
SELECT util._map(''"1"=>NULL, "2"=>NULL, "3"=>NULL, *=>*'', v) FROM unnest(array_fill(1, array[100000])) f (v)
|
2255
|
and map_nulls()''s profiling query
|
2256
|
SELECT util.map_nulls(array[1, 2, 3]::text[], v) FROM unnest(array_fill(1, array[100000])) f (v)
|
2257
|
both take ~920 ms.
|
2258
|
also, /inputs/REMIB/Specimen/postprocess.sql > country takes the same amount of time (56000 ms) to build with map_nulls() as with a literal hstore.
|
2259
|
';
|
2260
|
|
2261
|
|
2262
|
--
|
2263
|
-- Name: map_values(regclass); Type: FUNCTION; Schema: util; Owner: -
|
2264
|
--
|
2265
|
|
2266
|
CREATE FUNCTION map_values(map regclass) RETURNS SETOF text
|
2267
|
LANGUAGE plpgsql STABLE STRICT
|
2268
|
AS $_$
|
2269
|
BEGIN
|
2270
|
RETURN QUERY EXECUTE $$SELECT "to" FROM $$||map;
|
2271
|
END;
|
2272
|
$_$;
|
2273
|
|
2274
|
|
2275
|
--
|
2276
|
-- Name: materialize_query(text, text); Type: FUNCTION; Schema: util; Owner: -
|
2277
|
--
|
2278
|
|
2279
|
CREATE FUNCTION materialize_query(table_esc text, sql text) RETURNS void
|
2280
|
LANGUAGE sql
|
2281
|
AS $_$
|
2282
|
SELECT util.create_if_not_exists($$CREATE TABLE $$||$1||$$ AS
|
2283
|
$$||util.ltrim_nl($2));
|
2284
|
-- make sure the created table has the correct estimated row count
|
2285
|
SELECT util.analyze_($1);
|
2286
|
|
2287
|
SELECT util.append_comment($1, '
|
2288
|
contents generated from:
|
2289
|
'||util.ltrim_nl(util.runnable_sql($2))||';
|
2290
|
');
|
2291
|
$_$;
|
2292
|
|
2293
|
|
2294
|
--
|
2295
|
-- Name: FUNCTION materialize_query(table_esc text, sql text); Type: COMMENT; Schema: util; Owner: -
|
2296
|
--
|
2297
|
|
2298
|
COMMENT ON FUNCTION materialize_query(table_esc text, sql text) IS '
|
2299
|
idempotent
|
2300
|
';
|
2301
|
|
2302
|
|
2303
|
--
|
2304
|
-- Name: materialize_view(text, regclass); Type: FUNCTION; Schema: util; Owner: -
|
2305
|
--
|
2306
|
|
2307
|
CREATE FUNCTION materialize_view(table_esc text, view_ regclass) RETURNS void
|
2308
|
LANGUAGE sql
|
2309
|
AS $_$
|
2310
|
SELECT util.materialize_query($1, $$SELECT * FROM $$||$2)
|
2311
|
$_$;
|
2312
|
|
2313
|
|
2314
|
--
|
2315
|
-- Name: FUNCTION materialize_view(table_esc text, view_ regclass); Type: COMMENT; Schema: util; Owner: -
|
2316
|
--
|
2317
|
|
2318
|
COMMENT ON FUNCTION materialize_view(table_esc text, view_ regclass) IS '
|
2319
|
idempotent
|
2320
|
';
|
2321
|
|
2322
|
|
2323
|
--
|
2324
|
-- Name: mk_const_col(col_ref, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
2325
|
--
|
2326
|
|
2327
|
CREATE FUNCTION mk_const_col(col col_ref, value anyelement) RETURNS void
|
2328
|
LANGUAGE sql
|
2329
|
AS $_$
|
2330
|
SELECT util.create_if_not_exists($$
|
2331
|
ALTER TABLE $$||$1.table_||$$ ADD COLUMN $$
|
2332
|
||quote_ident($1.name)||$$ $$||pg_typeof($2)||util.type_qual($2)||$$ DEFAULT $$
|
2333
|
||quote_literal($2)||$$;
|
2334
|
COMMENT ON COLUMN $$||$1.table_||$$.$$||quote_ident($1.name)||$$ IS '
|
2335
|
constant
|
2336
|
';
|
2337
|
$$)
|
2338
|
$_$;
|
2339
|
|
2340
|
|
2341
|
--
|
2342
|
-- Name: FUNCTION mk_const_col(col col_ref, value anyelement); Type: COMMENT; Schema: util; Owner: -
|
2343
|
--
|
2344
|
|
2345
|
COMMENT ON FUNCTION mk_const_col(col col_ref, value anyelement) IS '
|
2346
|
idempotent
|
2347
|
';
|
2348
|
|
2349
|
|
2350
|
--
|
2351
|
-- Name: mk_derived_col(col_ref, text, boolean); Type: FUNCTION; Schema: util; Owner: -
|
2352
|
--
|
2353
|
|
2354
|
CREATE FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean DEFAULT false) RETURNS void
|
2355
|
LANGUAGE plpgsql STRICT
|
2356
|
AS $_$
|
2357
|
DECLARE
|
2358
|
type regtype = util.typeof(expr, col.table_::text::regtype);
|
2359
|
col_name_sql text = quote_ident(col.name);
|
2360
|
BEGIN
|
2361
|
PERFORM util.create_if_not_exists((CASE WHEN overwrite THEN '' ELSE $$
|
2362
|
ALTER TABLE $$||col.table_||$$ ADD COLUMN $$||col_name_sql||$$ $$||type||$$;$$ END)||$$
|
2363
|
ALTER TABLE $$||col.table_||$$ ALTER COLUMN $$||col_name_sql||$$ TYPE $$||type||$$ USING
|
2364
|
$$||expr||$$;
|
2365
|
$$);
|
2366
|
END;
|
2367
|
$_$;
|
2368
|
|
2369
|
|
2370
|
--
|
2371
|
-- Name: FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean); Type: COMMENT; Schema: util; Owner: -
|
2372
|
--
|
2373
|
|
2374
|
COMMENT ON FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean) IS '
|
2375
|
idempotent
|
2376
|
';
|
2377
|
|
2378
|
|
2379
|
--
|
2380
|
-- Name: mk_diff_query(text, text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
|
2381
|
--
|
2382
|
|
2383
|
CREATE FUNCTION mk_diff_query(left_ text, right_ text, cols text DEFAULT 'left_, right_'::text, join_cond text DEFAULT 'left_ %== right_ -- refer to EXPLAIN output for expansion of %=='::text, filter text DEFAULT 'left_ IS DISTINCT FROM right_'::text) RETURNS text
|
2384
|
LANGUAGE sql IMMUTABLE
|
2385
|
AS $_$
|
2386
|
SELECT
|
2387
|
$$SELECT
|
2388
|
$$||$3||$$
|
2389
|
FROM $$||$1||$$ left_
|
2390
|
FULL JOIN $$||$2||$$ right_
|
2391
|
ON $$||$4||$$
|
2392
|
WHERE $$||$5||$$
|
2393
|
ORDER BY left_, right_
|
2394
|
$$
|
2395
|
$_$;
|
2396
|
|
2397
|
|
2398
|
--
|
2399
|
-- Name: mk_drop_from_create(text); Type: FUNCTION; Schema: util; Owner: -
|
2400
|
--
|
2401
|
|
2402
|
CREATE FUNCTION mk_drop_from_create(create_cmd text) RETURNS text
|
2403
|
LANGUAGE sql IMMUTABLE
|
2404
|
AS $_$
|
2405
|
SELECT $$DROP $$||(util.regexp_match($1,
|
2406
|
'(?m)^CREATE(?: OR REPLACE)? ((?:)??[[:upper:]]+ .*?)(?: AS ?)?$'))[1]||$$;$$
|
2407
|
/* (?:)?? makes the surrounding () group non-greedy, so that (?: AS ?)?
|
2408
|
works properly (due to nonstandard Postgres regexp behavior:
|
2409
|
http://www.postgresql.org/docs/9.3/static/functions-matching.html#POSIX-MATCHING-RULES) */
|
2410
|
$_$;
|
2411
|
|
2412
|
|
2413
|
--
|
2414
|
-- Name: mk_keys_func(regtype); Type: FUNCTION; Schema: util; Owner: -
|
2415
|
--
|
2416
|
|
2417
|
CREATE FUNCTION mk_keys_func(type regtype) RETURNS void
|
2418
|
LANGUAGE sql
|
2419
|
AS $_$
|
2420
|
-- keys()
|
2421
|
SELECT util.mk_keys_func($1, ARRAY(
|
2422
|
SELECT col FROM util.typed_cols($1) col
|
2423
|
WHERE (col).type != ANY('{bigint}'::regtype[]) -- not a count col
|
2424
|
));
|
2425
|
|
2426
|
-- values_()
|
2427
|
SELECT util.mk_keys_func($1, COALESCE(
|
2428
|
NULLIF(ARRAY(
|
2429
|
SELECT col FROM util.typed_cols($1) col
|
2430
|
WHERE (col).type = ANY('{bigint}'::regtype[]) -- is a count col
|
2431
|
), ARRAY[]::util.col_cast[])
|
2432
|
, ARRAY(SELECT util.typed_cols($1))) -- no count cols, so use all cols
|
2433
|
, 'values_');
|
2434
|
$_$;
|
2435
|
|
2436
|
|
2437
|
--
|
2438
|
-- Name: mk_keys_func(regtype, col_cast[], text); Type: FUNCTION; Schema: util; Owner: -
|
2439
|
--
|
2440
|
|
2441
|
CREATE FUNCTION mk_keys_func(type regtype, cols col_cast[], name text DEFAULT 'keys'::text) RETURNS void
|
2442
|
LANGUAGE sql
|
2443
|
AS $_$
|
2444
|
SELECT util.create_if_not_exists($$
|
2445
|
CREATE TYPE $$||util.prefixed_name($3||'_', $1)||$$ AS
|
2446
|
($$||util.mk_typed_cols_list($2)||$$);
|
2447
|
COMMENT ON TYPE $$||util.prefixed_name($3||'_', $1)||$$ IS '
|
2448
|
autogenerated
|
2449
|
';
|
2450
|
$$);
|
2451
|
|
2452
|
SELECT util.mk_keys_func($1, util.prefixed_name($3||'_', $1)::regtype, $3);
|
2453
|
$_$;
|
2454
|
|
2455
|
|
2456
|
--
|
2457
|
-- Name: mk_keys_func(regtype, regtype, text); Type: FUNCTION; Schema: util; Owner: -
|
2458
|
--
|
2459
|
|
2460
|
CREATE FUNCTION mk_keys_func(type regtype, return_type regtype, name text DEFAULT 'keys'::text) RETURNS void
|
2461
|
LANGUAGE sql
|
2462
|
AS $_$
|
2463
|
SELECT util.create_if_not_exists($$
|
2464
|
CREATE FUNCTION $$||util.qual_name(util.schema($1), $3)||$$(value $$
|
2465
|
||util.qual_name($1)||$$)
|
2466
|
RETURNS $$||util.qual_name($2)||$$ AS
|
2467
|
$BODY1$
|
2468
|
SELECT ROW($$||
|
2469
|
(SELECT COALESCE(string_agg($$$1.$$||quote_ident((col).col_name), ', '), '')
|
2470
|
FROM util.typed_cols($2) col) ||$$)::$$||util.qual_name($2)||$$
|
2471
|
$BODY1$
|
2472
|
LANGUAGE sql IMMUTABLE
|
2473
|
COST 100;
|
2474
|
$$);
|
2475
|
$_$;
|
2476
|
|
2477
|
|
2478
|
--
|
2479
|
-- Name: mk_map_table(text); Type: FUNCTION; Schema: util; Owner: -
|
2480
|
--
|
2481
|
|
2482
|
CREATE FUNCTION mk_map_table(table_ text) RETURNS void
|
2483
|
LANGUAGE sql
|
2484
|
AS $_$
|
2485
|
SELECT util.create_if_not_exists($$
|
2486
|
CREATE TABLE $$||$1||$$
|
2487
|
(
|
2488
|
LIKE util.map INCLUDING ALL
|
2489
|
);
|
2490
|
|
2491
|
CREATE TRIGGER map_filter_insert
|
2492
|
BEFORE INSERT
|
2493
|
ON $$||$1||$$
|
2494
|
FOR EACH ROW
|
2495
|
EXECUTE PROCEDURE util.map_filter_insert();
|
2496
|
$$)
|
2497
|
$_$;
|
2498
|
|
2499
|
|
2500
|
--
|
2501
|
-- Name: mk_not_null(text); Type: FUNCTION; Schema: util; Owner: -
|
2502
|
--
|
2503
|
|
2504
|
CREATE FUNCTION mk_not_null(text) RETURNS text
|
2505
|
LANGUAGE sql IMMUTABLE
|
2506
|
AS $_$
|
2507
|
SELECT COALESCE($1, '<NULL>')
|
2508
|
$_$;
|
2509
|
|
2510
|
|
2511
|
--
|
2512
|
-- Name: mk_out_params(col_cast[]); Type: FUNCTION; Schema: util; Owner: -
|
2513
|
--
|
2514
|
|
2515
|
CREATE FUNCTION mk_out_params(cols col_cast[]) RETURNS text
|
2516
|
LANGUAGE sql IMMUTABLE
|
2517
|
AS $_$
|
2518
|
SELECT COALESCE(string_agg($$, OUT $$||(unnest).col_name||$$ $$||
|
2519
|
util.qual_name((unnest).type), ''), '')
|
2520
|
FROM unnest($1)
|
2521
|
$_$;
|
2522
|
|
2523
|
|
2524
|
--
|
2525
|
-- Name: mk_search_path(text[]); Type: FUNCTION; Schema: util; Owner: -
|
2526
|
--
|
2527
|
|
2528
|
CREATE FUNCTION mk_search_path(VARIADIC schemas text[]) RETURNS text
|
2529
|
LANGUAGE sql IMMUTABLE
|
2530
|
AS $_$
|
2531
|
SELECT string_agg(quote_ident(unnest), ', ') FROM unnest($1||'util'::text)
|
2532
|
$_$;
|
2533
|
|
2534
|
|
2535
|
--
|
2536
|
-- Name: FUNCTION mk_search_path(VARIADIC schemas text[]); Type: COMMENT; Schema: util; Owner: -
|
2537
|
--
|
2538
|
|
2539
|
COMMENT ON FUNCTION mk_search_path(VARIADIC schemas text[]) IS '
|
2540
|
auto-appends util to the search_path to enable use of util operators
|
2541
|
';
|
2542
|
|
2543
|
|
2544
|
--
|
2545
|
-- Name: mk_set_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
2546
|
--
|
2547
|
|
2548
|
CREATE FUNCTION mk_set_comment(table_ regclass, comment text) RETURNS text
|
2549
|
LANGUAGE sql STABLE
|
2550
|
AS $_$
|
2551
|
SELECT COALESCE($$COMMENT ON $$||util.relation_type($1)||$$ $$||$1||$$ IS $$
|
2552
|
||quote_literal($2)/*pass NULL through*/||$$;$$, ''/*no comment*/)
|
2553
|
$_$;
|
2554
|
|
2555
|
|
2556
|
--
|
2557
|
-- Name: mk_set_relation_metadata(regclass); Type: FUNCTION; Schema: util; Owner: -
|
2558
|
--
|
2559
|
|
2560
|
CREATE FUNCTION mk_set_relation_metadata(relation regclass) RETURNS text
|
2561
|
LANGUAGE sql STABLE
|
2562
|
AS $_$
|
2563
|
SELECT util.show_grants_for($1)
|
2564
|
||util.show_set_comment($1)||$$
|
2565
|
$$
|
2566
|
$_$;
|
2567
|
|
2568
|
|
2569
|
--
|
2570
|
-- Name: mk_set_search_path(boolean); Type: FUNCTION; Schema: util; Owner: -
|
2571
|
--
|
2572
|
|
2573
|
CREATE FUNCTION mk_set_search_path(for_printing boolean DEFAULT false) RETURNS text
|
2574
|
LANGUAGE sql IMMUTABLE
|
2575
|
AS $_$
|
2576
|
SELECT util.mk_set_search_path(current_setting('search_path'), $1)
|
2577
|
$_$;
|
2578
|
|
2579
|
|
2580
|
--
|
2581
|
-- Name: mk_set_search_path(text, boolean); Type: FUNCTION; Schema: util; Owner: -
|
2582
|
--
|
2583
|
|
2584
|
CREATE FUNCTION mk_set_search_path(search_path text, for_printing boolean DEFAULT false) RETURNS text
|
2585
|
LANGUAGE sql IMMUTABLE
|
2586
|
AS $_$
|
2587
|
/* debug_print_return_value() needed because this function is used with EXECUTE
|
2588
|
rather than util.eval() (in order to affect the calling function), so the
|
2589
|
search_path would not otherwise be printed */
|
2590
|
SELECT $$SET$$||util._if($2, $$ /*LOCAL*/$$::text, $$ LOCAL$$)
|
2591
|
||$$ search_path TO $$||$1
|
2592
|
$_$;
|
2593
|
|
2594
|
|
2595
|
--
|
2596
|
-- Name: mk_source_col(regclass); Type: FUNCTION; Schema: util; Owner: -
|
2597
|
--
|
2598
|
|
2599
|
CREATE FUNCTION mk_source_col(table_ regclass) RETURNS void
|
2600
|
LANGUAGE sql
|
2601
|
AS $_$
|
2602
|
SELECT util.mk_const_col(($1, 'source'), util.schema($1))
|
2603
|
$_$;
|
2604
|
|
2605
|
|
2606
|
--
|
2607
|
-- Name: FUNCTION mk_source_col(table_ regclass); Type: COMMENT; Schema: util; Owner: -
|
2608
|
--
|
2609
|
|
2610
|
COMMENT ON FUNCTION mk_source_col(table_ regclass) IS '
|
2611
|
idempotent
|
2612
|
';
|
2613
|
|
2614
|
|
2615
|
--
|
2616
|
-- Name: mk_subset_by_row_num_func(regclass); Type: FUNCTION; Schema: util; Owner: -
|
2617
|
--
|
2618
|
|
2619
|
CREATE FUNCTION mk_subset_by_row_num_func(view_ regclass) RETURNS void
|
2620
|
LANGUAGE plpgsql STRICT
|
2621
|
AS $_$
|
2622
|
DECLARE
|
2623
|
view_qual_name text = util.qual_name(view_);
|
2624
|
BEGIN
|
2625
|
EXECUTE $$
|
2626
|
CREATE OR REPLACE FUNCTION $$||view_||$$(limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
|
2627
|
RETURNS SETOF $$||view_||$$ AS
|
2628
|
$BODY1$
|
2629
|
SELECT * FROM $$||view_qual_name||$$
|
2630
|
ORDER BY sort_col
|
2631
|
LIMIT $1 OFFSET $2
|
2632
|
$BODY1$
|
2633
|
LANGUAGE sql STABLE
|
2634
|
COST 100
|
2635
|
ROWS 1000
|
2636
|
$$;
|
2637
|
|
2638
|
PERFORM util.mk_subset_by_row_num_no_sort_func(view_);
|
2639
|
END;
|
2640
|
$_$;
|
2641
|
|
2642
|
|
2643
|
--
|
2644
|
-- Name: mk_subset_by_row_num_func(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
2645
|
--
|
2646
|
|
2647
|
CREATE FUNCTION mk_subset_by_row_num_func(view_ regclass, row_num_col text) RETURNS void
|
2648
|
LANGUAGE plpgsql STRICT
|
2649
|
AS $_$
|
2650
|
DECLARE
|
2651
|
view_qual_name text = util.qual_name(view_);
|
2652
|
row_num__min__fn text = util.esc_name__append('__row_num__min', view_qual_name);
|
2653
|
BEGIN
|
2654
|
EXECUTE $$
|
2655
|
CREATE OR REPLACE FUNCTION $$||row_num__min__fn||$$()
|
2656
|
RETURNS integer AS
|
2657
|
$BODY1$
|
2658
|
SELECT $$||quote_ident(row_num_col)||$$
|
2659
|
FROM $$||view_qual_name||$$
|
2660
|
ORDER BY $$||quote_ident(row_num_col)||$$ ASC
|
2661
|
LIMIT 1
|
2662
|
$BODY1$
|
2663
|
LANGUAGE sql STABLE
|
2664
|
COST 100;
|
2665
|
$$;
|
2666
|
|
2667
|
EXECUTE $$
|
2668
|
CREATE OR REPLACE FUNCTION $$||view_||$$(limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
|
2669
|
RETURNS SETOF $$||view_||$$ AS
|
2670
|
$BODY1$
|
2671
|
SELECT * FROM $$||view_qual_name||$$
|
2672
|
WHERE $$||quote_ident(row_num_col)||$$ BETWEEN
|
2673
|
util.offset2row_num( $2, $$||row_num__min__fn||$$())
|
2674
|
AND util.limit2row_num ($1, $2, $$||row_num__min__fn||$$())
|
2675
|
ORDER BY $$||quote_ident(row_num_col)||$$
|
2676
|
$BODY1$
|
2677
|
LANGUAGE sql STABLE
|
2678
|
COST 100
|
2679
|
ROWS 1000
|
2680
|
$$;
|
2681
|
|
2682
|
PERFORM util.mk_subset_by_row_num_no_sort_func(view_);
|
2683
|
END;
|
2684
|
$_$;
|
2685
|
|
2686
|
|
2687
|
--
|
2688
|
-- Name: mk_subset_by_row_num_no_sort_func(regclass); Type: FUNCTION; Schema: util; Owner: -
|
2689
|
--
|
2690
|
|
2691
|
CREATE FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass) RETURNS void
|
2692
|
LANGUAGE plpgsql STRICT
|
2693
|
AS $_$
|
2694
|
DECLARE
|
2695
|
view_qual_name text = util.qual_name(view_);
|
2696
|
BEGIN
|
2697
|
EXECUTE $$
|
2698
|
CREATE OR REPLACE FUNCTION $$||view_||$$(no_sort boolean, limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
|
2699
|
RETURNS SETOF $$||view_||$$
|
2700
|
SET enable_sort TO 'off'
|
2701
|
AS
|
2702
|
$BODY1$
|
2703
|
SELECT * FROM $$||view_qual_name||$$($2, $3)
|
2704
|
$BODY1$
|
2705
|
LANGUAGE sql STABLE
|
2706
|
COST 100
|
2707
|
ROWS 1000
|
2708
|
;
|
2709
|
COMMENT ON FUNCTION $$||view_||$$(no_sort boolean, limit_ integer, offset_ integer) IS '
|
2710
|
Use this for limit values greater than ~100,000 to avoid unwanted slow sorts.
|
2711
|
If you want to run EXPLAIN and get expanded output, use the regular subset
|
2712
|
function instead. (When a config param is set on a function, EXPLAIN produces
|
2713
|
just a function scan.)
|
2714
|
';
|
2715
|
$$;
|
2716
|
END;
|
2717
|
$_$;
|
2718
|
|
2719
|
|
2720
|
--
|
2721
|
-- Name: FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass); Type: COMMENT; Schema: util; Owner: -
|
2722
|
--
|
2723
|
|
2724
|
COMMENT ON FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass) IS '
|
2725
|
creates subset function which turns off enable_sort
|
2726
|
';
|
2727
|
|
2728
|
|
2729
|
--
|
2730
|
-- Name: mk_typed_cols_list(col_cast[]); Type: FUNCTION; Schema: util; Owner: -
|
2731
|
--
|
2732
|
|
2733
|
CREATE FUNCTION mk_typed_cols_list(cols col_cast[]) RETURNS text
|
2734
|
LANGUAGE sql IMMUTABLE
|
2735
|
AS $_$
|
2736
|
SELECT COALESCE(string_agg(quote_ident((unnest).col_name)||$$ $$||
|
2737
|
util.qual_name((unnest).type), ', '), '')
|
2738
|
FROM unnest($1)
|
2739
|
$_$;
|
2740
|
|
2741
|
|
2742
|
--
|
2743
|
-- Name: name(regclass); Type: FUNCTION; Schema: util; Owner: -
|
2744
|
--
|
2745
|
|
2746
|
CREATE FUNCTION name(table_ regclass) RETURNS text
|
2747
|
LANGUAGE sql STABLE
|
2748
|
AS $_$
|
2749
|
SELECT relname::text FROM pg_class WHERE oid = $1
|
2750
|
$_$;
|
2751
|
|
2752
|
|
2753
|
--
|
2754
|
-- Name: name(regtype); Type: FUNCTION; Schema: util; Owner: -
|
2755
|
--
|
2756
|
|
2757
|
CREATE FUNCTION name(type regtype) RETURNS text
|
2758
|
LANGUAGE sql STABLE
|
2759
|
AS $_$
|
2760
|
SELECT typname::text FROM pg_type WHERE oid = $1
|
2761
|
$_$;
|
2762
|
|
2763
|
|
2764
|
--
|
2765
|
-- Name: name_was_truncated(text, integer); Type: FUNCTION; Schema: util; Owner: -
|
2766
|
--
|
2767
|
|
2768
|
CREATE FUNCTION name_was_truncated(name_ text, max_prefix_len integer DEFAULT 0) RETURNS boolean
|
2769
|
LANGUAGE sql IMMUTABLE
|
2770
|
AS $_$
|
2771
|
SELECT octet_length($1) >= util.namedatalen() - $2
|
2772
|
$_$;
|
2773
|
|
2774
|
|
2775
|
--
|
2776
|
-- Name: namedatalen(); Type: FUNCTION; Schema: util; Owner: -
|
2777
|
--
|
2778
|
|
2779
|
CREATE FUNCTION namedatalen() RETURNS integer
|
2780
|
LANGUAGE sql IMMUTABLE
|
2781
|
AS $$
|
2782
|
SELECT octet_length(repeat('_', 1024/*>63*/)::name::text)
|
2783
|
$$;
|
2784
|
|
2785
|
|
2786
|
--
|
2787
|
-- Name: not_empty(anyarray); Type: FUNCTION; Schema: util; Owner: -
|
2788
|
--
|
2789
|
|
2790
|
CREATE FUNCTION not_empty(value anyarray) RETURNS boolean
|
2791
|
LANGUAGE sql IMMUTABLE
|
2792
|
AS $_$
|
2793
|
SELECT $1 IS NOT NULL AND util.array_length($1) > 0
|
2794
|
$_$;
|
2795
|
|
2796
|
|
2797
|
--
|
2798
|
-- Name: not_null(anyelement); Type: FUNCTION; Schema: util; Owner: -
|
2799
|
--
|
2800
|
|
2801
|
CREATE FUNCTION not_null(value anyelement) RETURNS boolean
|
2802
|
LANGUAGE sql IMMUTABLE
|
2803
|
AS $_$
|
2804
|
SELECT $1 IS NOT NULL
|
2805
|
$_$;
|
2806
|
|
2807
|
|
2808
|
--
|
2809
|
-- Name: nulls_map(text[]); Type: FUNCTION; Schema: util; Owner: -
|
2810
|
--
|
2811
|
|
2812
|
CREATE FUNCTION nulls_map(nulls text[]) RETURNS hstore
|
2813
|
LANGUAGE sql IMMUTABLE
|
2814
|
AS $_$
|
2815
|
SELECT util.hstore($1, NULL) || '*=>*'
|
2816
|
$_$;
|
2817
|
|
2818
|
|
2819
|
--
|
2820
|
-- Name: FUNCTION nulls_map(nulls text[]); Type: COMMENT; Schema: util; Owner: -
|
2821
|
--
|
2822
|
|
2823
|
COMMENT ON FUNCTION nulls_map(nulls text[]) IS '
|
2824
|
for use with _map()
|
2825
|
';
|
2826
|
|
2827
|
|
2828
|
--
|
2829
|
-- Name: offset2row_num(integer, integer); Type: FUNCTION; Schema: util; Owner: -
|
2830
|
--
|
2831
|
|
2832
|
CREATE FUNCTION offset2row_num(offset_ integer, min_row_num integer DEFAULT 1) RETURNS integer
|
2833
|
LANGUAGE sql IMMUTABLE
|
2834
|
AS $_$
|
2835
|
SELECT $2 + COALESCE($1, 0)
|
2836
|
$_$;
|
2837
|
|
2838
|
|
2839
|
--
|
2840
|
-- Name: parent(regclass); Type: FUNCTION; Schema: util; Owner: -
|
2841
|
--
|
2842
|
|
2843
|
CREATE FUNCTION parent(table_ regclass) RETURNS regclass
|
2844
|
LANGUAGE sql STABLE
|
2845
|
AS $_$
|
2846
|
SELECT inhparent FROM pg_inherits WHERE inhrelid = $1
|
2847
|
$_$;
|
2848
|
|
2849
|
|
2850
|
--
|
2851
|
-- Name: populate_table(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
2852
|
--
|
2853
|
|
2854
|
CREATE FUNCTION populate_table(table_ regclass, sql text) RETURNS void
|
2855
|
LANGUAGE sql
|
2856
|
AS $_$
|
2857
|
SELECT util.eval($$INSERT INTO $$||$1||$$
|
2858
|
$$||util.ltrim_nl($2));
|
2859
|
-- make sure the created table has the correct estimated row count
|
2860
|
SELECT util.analyze_($1);
|
2861
|
$_$;
|
2862
|
|
2863
|
|
2864
|
--
|
2865
|
-- Name: prefixed_name(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
2866
|
--
|
2867
|
|
2868
|
CREATE FUNCTION prefixed_name(prefix text, type anyelement) RETURNS text
|
2869
|
LANGUAGE sql IMMUTABLE
|
2870
|
AS $_$
|
2871
|
SELECT util.qual_name(util.schema($2), $1||util.name($2))
|
2872
|
$_$;
|
2873
|
|
2874
|
|
2875
|
--
|
2876
|
-- Name: prepend_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
2877
|
--
|
2878
|
|
2879
|
CREATE FUNCTION prepend_comment(table_ regclass, comment text) RETURNS void
|
2880
|
LANGUAGE sql
|
2881
|
AS $_$
|
2882
|
SELECT util.set_comment($1, concat($2, util.comment($1)))
|
2883
|
$_$;
|
2884
|
|
2885
|
|
2886
|
--
|
2887
|
-- Name: FUNCTION prepend_comment(table_ regclass, comment text); Type: COMMENT; Schema: util; Owner: -
|
2888
|
--
|
2889
|
|
2890
|
COMMENT ON FUNCTION prepend_comment(table_ regclass, comment text) IS '
|
2891
|
comment: must start and end with a newline
|
2892
|
';
|
2893
|
|
2894
|
|
2895
|
--
|
2896
|
-- Name: qual_name(text[]); Type: FUNCTION; Schema: util; Owner: -
|
2897
|
--
|
2898
|
|
2899
|
CREATE FUNCTION qual_name(VARIADIC elems text[]) RETURNS text
|
2900
|
LANGUAGE sql IMMUTABLE
|
2901
|
AS $_$
|
2902
|
SELECT string_agg(quote_ident(unnest), '.') FROM unnest($1)
|
2903
|
$_$;
|
2904
|
|
2905
|
|
2906
|
--
|
2907
|
-- Name: qual_name(regclass); Type: FUNCTION; Schema: util; Owner: -
|
2908
|
--
|
2909
|
|
2910
|
CREATE FUNCTION qual_name(table_ regclass) RETURNS text
|
2911
|
LANGUAGE sql STABLE
|
2912
|
SET search_path TO pg_temp
|
2913
|
AS $_$
|
2914
|
SELECT $1::text
|
2915
|
$_$;
|
2916
|
|
2917
|
|
2918
|
--
|
2919
|
-- Name: qual_name(regtype); Type: FUNCTION; Schema: util; Owner: -
|
2920
|
--
|
2921
|
|
2922
|
CREATE FUNCTION qual_name(type regtype) RETURNS text
|
2923
|
LANGUAGE sql STABLE
|
2924
|
SET search_path TO pg_temp
|
2925
|
AS $_$
|
2926
|
SELECT $1::text
|
2927
|
$_$;
|
2928
|
|
2929
|
|
2930
|
--
|
2931
|
-- Name: FUNCTION qual_name(type regtype); Type: COMMENT; Schema: util; Owner: -
|
2932
|
--
|
2933
|
|
2934
|
COMMENT ON FUNCTION qual_name(type regtype) IS '
|
2935
|
a type''s schema-qualified name
|
2936
|
';
|
2937
|
|
2938
|
|
2939
|
--
|
2940
|
-- Name: qual_name(unknown); Type: FUNCTION; Schema: util; Owner: -
|
2941
|
--
|
2942
|
|
2943
|
CREATE FUNCTION qual_name(type unknown) RETURNS text
|
2944
|
LANGUAGE sql STABLE
|
2945
|
AS $_$
|
2946
|
SELECT util.qual_name($1::text::regtype)
|
2947
|
$_$;
|
2948
|
|
2949
|
|
2950
|
--
|
2951
|
-- Name: quote_func_call(regprocedure, text[]); Type: FUNCTION; Schema: util; Owner: -
|
2952
|
--
|
2953
|
|
2954
|
CREATE FUNCTION quote_func_call(func regprocedure, VARIADIC args_esc text[]) RETURNS text
|
2955
|
LANGUAGE sql IMMUTABLE
|
2956
|
AS $_$
|
2957
|
SELECT util.quote_func_call($1::regproc::text, VARIADIC $2)
|
2958
|
$_$;
|
2959
|
|
2960
|
|
2961
|
--
|
2962
|
-- Name: quote_func_call(text, text[]); Type: FUNCTION; Schema: util; Owner: -
|
2963
|
--
|
2964
|
|
2965
|
CREATE FUNCTION quote_func_call(func_esc text, VARIADIC args_esc text[]) RETURNS text
|
2966
|
LANGUAGE sql IMMUTABLE
|
2967
|
AS $_$
|
2968
|
SELECT $1||'('||concat_ws(', ', VARIADIC $2)||')'
|
2969
|
$_$;
|
2970
|
|
2971
|
|
2972
|
--
|
2973
|
-- Name: quote_typed(anyelement); Type: FUNCTION; Schema: util; Owner: -
|
2974
|
--
|
2975
|
|
2976
|
CREATE FUNCTION quote_typed(value anyelement) RETURNS text
|
2977
|
LANGUAGE sql IMMUTABLE
|
2978
|
AS $_$
|
2979
|
SELECT quote_nullable($1)||$$::$$||util.qual_name(pg_typeof($1))
|
2980
|
$_$;
|
2981
|
|
2982
|
|
2983
|
--
|
2984
|
-- Name: raise(text, text); Type: FUNCTION; Schema: util; Owner: -
|
2985
|
--
|
2986
|
|
2987
|
CREATE FUNCTION raise(type text, msg text) RETURNS void
|
2988
|
LANGUAGE sql IMMUTABLE
|
2989
|
AS $_X$
|
2990
|
SELECT util.eval($$
|
2991
|
CREATE OR REPLACE FUNCTION pg_temp.__raise()
|
2992
|
RETURNS void AS
|
2993
|
-- $__BODY1$ in case msg contains $BODY1$ (in SQL)
|
2994
|
$__BODY1$
|
2995
|
BEGIN
|
2996
|
RAISE $$||$1||$$ USING MESSAGE = $$||quote_nullable($2)||$$;
|
2997
|
END;
|
2998
|
$__BODY1$
|
2999
|
LANGUAGE plpgsql IMMUTABLE
|
3000
|
COST 100;
|
3001
|
$$, verbose_ := false);
|
3002
|
|
3003
|
SELECT util.eval($$SELECT pg_temp.__raise()$$, verbose_ := false);
|
3004
|
$_X$;
|
3005
|
|
3006
|
|
3007
|
--
|
3008
|
-- Name: FUNCTION raise(type text, msg text); Type: COMMENT; Schema: util; Owner: -
|
3009
|
--
|
3010
|
|
3011
|
COMMENT ON FUNCTION raise(type text, msg text) IS '
|
3012
|
type: a log level from
|
3013
|
http://www.postgresql.org/docs/9.3/static/plpgsql-errors-and-messages.html
|
3014
|
or a condition name from
|
3015
|
http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html
|
3016
|
';
|
3017
|
|
3018
|
|
3019
|
--
|
3020
|
-- Name: raise_error_warning(text); Type: FUNCTION; Schema: util; Owner: -
|
3021
|
--
|
3022
|
|
3023
|
CREATE FUNCTION raise_error_warning(msg text) RETURNS void
|
3024
|
LANGUAGE sql IMMUTABLE
|
3025
|
AS $_$
|
3026
|
SELECT util.raise('WARNING', 'ERROR: '||$1)
|
3027
|
$_$;
|
3028
|
|
3029
|
|
3030
|
--
|
3031
|
-- Name: raise_undefined_column(col_ref); Type: FUNCTION; Schema: util; Owner: -
|
3032
|
--
|
3033
|
|
3034
|
CREATE FUNCTION raise_undefined_column(col col_ref) RETURNS text
|
3035
|
LANGUAGE plpgsql IMMUTABLE STRICT
|
3036
|
AS $$
|
3037
|
BEGIN
|
3038
|
RAISE undefined_column USING MESSAGE = concat('undefined column: ', col.name);
|
3039
|
END;
|
3040
|
$$;
|
3041
|
|
3042
|
|
3043
|
--
|
3044
|
-- Name: recreate(text, text[]); Type: FUNCTION; Schema: util; Owner: -
|
3045
|
--
|
3046
|
|
3047
|
CREATE FUNCTION recreate(cmd text, users text[] DEFAULT NULL::text[]) RETURNS void
|
3048
|
LANGUAGE plpgsql
|
3049
|
AS $_$
|
3050
|
DECLARE
|
3051
|
PG_EXCEPTION_DETAIL text;
|
3052
|
restore_views_info util.restore_views_info;
|
3053
|
BEGIN
|
3054
|
restore_views_info = util.save_drop_views(users);
|
3055
|
|
3056
|
-- trigger the dependent_objects_still_exist exception
|
3057
|
PERFORM util.eval(util.mk_drop_from_create(cmd));
|
3058
|
-- *not* CASCADE; it must trigger an exception
|
3059
|
|
3060
|
PERFORM util.eval(cmd);
|
3061
|
|
3062
|
PERFORM util.restore_views(restore_views_info);
|
3063
|
EXCEPTION
|
3064
|
WHEN dependent_objects_still_exist THEN
|
3065
|
IF users IS NOT NULL THEN RAISE; END IF; -- save_drop_views() didn't fix it
|
3066
|
GET STACKED DIAGNOSTICS PG_EXCEPTION_DETAIL = PG_EXCEPTION_DETAIL;
|
3067
|
users = array(SELECT * FROM util.regexp_matches_group(
|
3068
|
PG_EXCEPTION_DETAIL, '(?m)^view (.*) depends on [[:lower:]]+ .*$'));
|
3069
|
-- will be in forward dependency order
|
3070
|
PERFORM util.debug_print_var('PG_EXCEPTION_DETAIL', PG_EXCEPTION_DETAIL);
|
3071
|
PERFORM util.debug_print_var('users', users);
|
3072
|
IF util.is_empty(users) THEN RAISE; END IF;
|
3073
|
PERFORM util.recreate(cmd, users);
|
3074
|
END;
|
3075
|
$_$;
|
3076
|
|
3077
|
|
3078
|
--
|
3079
|
-- Name: FUNCTION recreate(cmd text, users text[]); Type: COMMENT; Schema: util; Owner: -
|
3080
|
--
|
3081
|
|
3082
|
COMMENT ON FUNCTION recreate(cmd text, users text[]) IS '
|
3083
|
the appropriate drop statement will be added automatically.
|
3084
|
|
3085
|
usage:
|
3086
|
SELECT util.recreate($$
|
3087
|
CREATE VIEW schemas.main_view AS _;
|
3088
|
|
3089
|
-- manually restore views that need to be updated for the changes
|
3090
|
CREATE VIEW schemas.dependent_view AS _;
|
3091
|
$$);
|
3092
|
|
3093
|
idempotent
|
3094
|
|
3095
|
users: not necessary to provide this because it will be autopopulated
|
3096
|
';
|
3097
|
|
3098
|
|
3099
|
--
|
3100
|
-- Name: regexp_match(text, text); Type: FUNCTION; Schema: util; Owner: -
|
3101
|
--
|
3102
|
|
3103
|
CREATE FUNCTION regexp_match(str text, re text) RETURNS text[]
|
3104
|
LANGUAGE sql IMMUTABLE
|
3105
|
AS $_$
|
3106
|
SELECT match FROM regexp_matches($1, $2) match LIMIT 1/*only 1st match*/
|
3107
|
$_$;
|
3108
|
|
3109
|
|
3110
|
--
|
3111
|
-- Name: regexp_matches_group(text, text, integer); Type: FUNCTION; Schema: util; Owner: -
|
3112
|
--
|
3113
|
|
3114
|
CREATE FUNCTION regexp_matches_group(str text, re text, group_ integer DEFAULT 1) RETURNS SETOF text
|
3115
|
LANGUAGE sql IMMUTABLE
|
3116
|
AS $_$
|
3117
|
SELECT regexp_matches[$3] FROM regexp_matches($1, $2, 'g')
|
3118
|
$_$;
|
3119
|
|
3120
|
|
3121
|
--
|
3122
|
-- Name: regexp_quote(text); Type: FUNCTION; Schema: util; Owner: -
|
3123
|
--
|
3124
|
|
3125
|
CREATE FUNCTION regexp_quote(str text) RETURNS text
|
3126
|
LANGUAGE sql IMMUTABLE
|
3127
|
AS $_$
|
3128
|
SELECT regexp_replace($1, '\W', /*\char*/'\\\&', 'g')
|
3129
|
$_$;
|
3130
|
|
3131
|
|
3132
|
--
|
3133
|
-- Name: regprocedure(text); Type: FUNCTION; Schema: util; Owner: -
|
3134
|
--
|
3135
|
|
3136
|
CREATE FUNCTION regprocedure(func text) RETURNS regprocedure
|
3137
|
LANGUAGE sql IMMUTABLE
|
3138
|
AS $_$
|
3139
|
SELECT (CASE WHEN right($1, 1) = ')'
|
3140
|
THEN $1::regprocedure ELSE $1::regproc::regprocedure END)
|
3141
|
$_$;
|
3142
|
|
3143
|
|
3144
|
--
|
3145
|
-- Name: relation_exists(text); Type: FUNCTION; Schema: util; Owner: -
|
3146
|
--
|
3147
|
|
3148
|
CREATE FUNCTION relation_exists(relation text) RETURNS boolean
|
3149
|
LANGUAGE sql STABLE
|
3150
|
AS $_$
|
3151
|
SELECT $1 IS NOT NULL AND util.is_castable($1, NULL::regclass)
|
3152
|
$_$;
|
3153
|
|
3154
|
|
3155
|
--
|
3156
|
-- Name: relation_type(regclass); Type: FUNCTION; Schema: util; Owner: -
|
3157
|
--
|
3158
|
|
3159
|
CREATE FUNCTION relation_type(relation regclass) RETURNS text
|
3160
|
LANGUAGE sql STABLE
|
3161
|
AS $_$
|
3162
|
SELECT util.relation_type(util.relation_type_char($1))
|
3163
|
$_$;
|
3164
|
|
3165
|
|
3166
|
--
|
3167
|
-- Name: relation_type("char"); Type: FUNCTION; Schema: util; Owner: -
|
3168
|
--
|
3169
|
|
3170
|
CREATE FUNCTION relation_type(relation_type_char "char") RETURNS text
|
3171
|
LANGUAGE sql IMMUTABLE
|
3172
|
AS $_$
|
3173
|
SELECT 'c=>TYPE, r=>TABLE, v=>VIEW'::hstore -> $1
|
3174
|
$_$;
|
3175
|
|
3176
|
|
3177
|
--
|
3178
|
-- Name: relation_type(regtype); Type: FUNCTION; Schema: util; Owner: -
|
3179
|
--
|
3180
|
|
3181
|
CREATE FUNCTION relation_type(type regtype) RETURNS text
|
3182
|
LANGUAGE sql IMMUTABLE
|
3183
|
AS $$
|
3184
|
SELECT 'TYPE'::text
|
3185
|
$$;
|
3186
|
|
3187
|
|
3188
|
--
|
3189
|
-- Name: relation_type_char(regclass); Type: FUNCTION; Schema: util; Owner: -
|
3190
|
--
|
3191
|
|
3192
|
CREATE FUNCTION relation_type_char(relation regclass) RETURNS "char"
|
3193
|
LANGUAGE sql STABLE
|
3194
|
AS $_$
|
3195
|
SELECT relkind FROM pg_class WHERE oid = $1
|
3196
|
$_$;
|
3197
|
|
3198
|
|
3199
|
--
|
3200
|
-- Name: remake_diff_table(text, regclass, regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
3201
|
--
|
3202
|
|
3203
|
CREATE FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text) RETURNS void
|
3204
|
LANGUAGE sql
|
3205
|
AS $_$
|
3206
|
/* can't have in_table/out_table inherit from *each other*, because inheritance
|
3207
|
also causes the rows of the parent table to be included in the child table.
|
3208
|
instead, they need to inherit from a common, empty table. */
|
3209
|
SELECT util.create_if_not_exists($$SELECT $$||util.quote_func_call(
|
3210
|
'util.copy_struct', util.quote_typed($2), util.quote_typed($4)));
|
3211
|
SELECT util.rm_freq(ARRAY[$4]); -- left/right_table don't have freq yet
|
3212
|
SELECT util.inherit($2, $4);
|
3213
|
SELECT util.inherit($3, $4);
|
3214
|
|
3215
|
SELECT util.rematerialize_query($1, $$
|
3216
|
SELECT * FROM util.diff(
|
3217
|
$$||util.quote_typed($2)||$$
|
3218
|
, $$||util.quote_typed($3)||$$
|
3219
|
, NULL::$$||$4||$$)
|
3220
|
$$);
|
3221
|
|
3222
|
/* the table unfortunately cannot be *materialized* in human-readable form,
|
3223
|
because this would create column name collisions between the two sides */
|
3224
|
SELECT util.prepend_comment($1, '
|
3225
|
to view this table in human-readable form (with each side''s tuple column
|
3226
|
expanded to its component fields):
|
3227
|
SELECT (left_).*, ('||util.schema($4::regclass)||'.values_(right_)).* FROM '||$1||';
|
3228
|
|
3229
|
to display NULL values that are extra or missing:
|
3230
|
SELECT * FROM '||$1||';
|
3231
|
');
|
3232
|
$_$;
|
3233
|
|
3234
|
|
3235
|
--
|
3236
|
-- Name: FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text); Type: COMMENT; Schema: util; Owner: -
|
3237
|
--
|
3238
|
|
3239
|
COMMENT ON FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text) IS '
|
3240
|
type_table (*required*): table to create as the shared base type
|
3241
|
';
|
3242
|
|
3243
|
|
3244
|
--
|
3245
|
-- Name: rematerialize_query(text, text); Type: FUNCTION; Schema: util; Owner: -
|
3246
|
--
|
3247
|
|
3248
|
CREATE FUNCTION rematerialize_query(table_esc text, sql text) RETURNS void
|
3249
|
LANGUAGE sql
|
3250
|
AS $_$
|
3251
|
SELECT util.drop_table($1);
|
3252
|
SELECT util.materialize_query($1, $2);
|
3253
|
$_$;
|
3254
|
|
3255
|
|
3256
|
--
|
3257
|
-- Name: FUNCTION rematerialize_query(table_esc text, sql text); Type: COMMENT; Schema: util; Owner: -
|
3258
|
--
|
3259
|
|
3260
|
COMMENT ON FUNCTION rematerialize_query(table_esc text, sql text) IS '
|
3261
|
idempotent, but repeats action each time
|
3262
|
';
|
3263
|
|
3264
|
|
3265
|
--
|
3266
|
-- Name: rematerialize_view(text, regclass); Type: FUNCTION; Schema: util; Owner: -
|
3267
|
--
|
3268
|
|
3269
|
CREATE FUNCTION rematerialize_view(table_esc text, view_ regclass) RETURNS void
|
3270
|
LANGUAGE sql
|
3271
|
AS $_$
|
3272
|
SELECT util.drop_table($1);
|
3273
|
SELECT util.materialize_view($1, $2);
|
3274
|
$_$;
|
3275
|
|
3276
|
|
3277
|
--
|
3278
|
-- Name: FUNCTION rematerialize_view(table_esc text, view_ regclass); Type: COMMENT; Schema: util; Owner: -
|
3279
|
--
|
3280
|
|
3281
|
COMMENT ON FUNCTION rematerialize_view(table_esc text, view_ regclass) IS '
|
3282
|
idempotent, but repeats action each time
|
3283
|
';
|
3284
|
|
3285
|
|
3286
|
--
|
3287
|
-- Name: rename_cols(regclass, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
3288
|
--
|
3289
|
|
3290
|
CREATE FUNCTION rename_cols(table_ regclass, renames anyelement) RETURNS void
|
3291
|
LANGUAGE sql
|
3292
|
AS $_$
|
3293
|
SELECT util.try_create($$ALTER TABLE $$||$1||$$ RENAME $$
|
3294
|
||quote_ident(name)||$$ TO $$||quote_ident($2 -> name))
|
3295
|
FROM util.col_names($1::text::regtype) f (name);
|
3296
|
SELECT NULL::void; -- don't fold away functions called in previous query
|
3297
|
$_$;
|
3298
|
|
3299
|
|
3300
|
--
|
3301
|
-- Name: FUNCTION rename_cols(table_ regclass, renames anyelement); Type: COMMENT; Schema: util; Owner: -
|
3302
|
--
|
3303
|
|
3304
|
COMMENT ON FUNCTION rename_cols(table_ regclass, renames anyelement) IS '
|
3305
|
idempotent
|
3306
|
';
|
3307
|
|
3308
|
|
3309
|
--
|
3310
|
-- Name: rename_relation(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
3311
|
--
|
3312
|
|
3313
|
CREATE FUNCTION rename_relation(from_ regclass, to_ text) RETURNS void
|
3314
|
LANGUAGE sql
|
3315
|
AS $_$
|
3316
|
/* use util.qual_name() instead of ::text so that the schema qualifier is always
|
3317
|
included in the debug SQL */
|
3318
|
SELECT util.rename_relation(util.qual_name($1), $2)
|
3319
|
$_$;
|
3320
|
|
3321
|
|
3322
|
--
|
3323
|
-- Name: rename_relation(text, text); Type: FUNCTION; Schema: util; Owner: -
|
3324
|
--
|
3325
|
|
3326
|
CREATE FUNCTION rename_relation(from_esc text, to_name text) RETURNS void
|
3327
|
LANGUAGE sql
|
3328
|
AS $_$
|
3329
|
/* 'ALTER TABLE can be used with views too'
|
3330
|
(http://www.postgresql.org/docs/9.3/static/sql-alterview.html) */
|
3331
|
SELECT util.eval($$ALTER TABLE IF EXISTS $$||$1||$$ RENAME TO $$
|
3332
|
||quote_ident($2))
|
3333
|
$_$;
|
3334
|
|
3335
|
|
3336
|
--
|
3337
|
-- Name: FUNCTION rename_relation(from_esc text, to_name text); Type: COMMENT; Schema: util; Owner: -
|
3338
|
--
|
3339
|
|
3340
|
COMMENT ON FUNCTION rename_relation(from_esc text, to_name text) IS '
|
3341
|
idempotent
|
3342
|
';
|
3343
|
|
3344
|
|
3345
|
--
|
3346
|
-- Name: replace_suffix(text, text, text, integer); Type: FUNCTION; Schema: util; Owner: -
|
3347
|
--
|
3348
|
|
3349
|
CREATE FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer DEFAULT 0) RETURNS text
|
3350
|
LANGUAGE sql IMMUTABLE
|
3351
|
AS $_$
|
3352
|
SELECT regexp_replace($1, util.truncated_prefixed_name_regexp($2, $4), '\1'||$3)
|
3353
|
$_$;
|
3354
|
|
3355
|
|
3356
|
--
|
3357
|
-- Name: FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer); Type: COMMENT; Schema: util; Owner: -
|
3358
|
--
|
3359
|
|
3360
|
COMMENT ON FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer) IS '
|
3361
|
max_prefix_len: when str may have been truncated (eg. as a table name) due to the prepending of a prefix, support prefixes up to this length
|
3362
|
';
|
3363
|
|
3364
|
|
3365
|
--
|
3366
|
-- Name: reset_col_names(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
|
3367
|
--
|
3368
|
|
3369
|
CREATE FUNCTION reset_col_names(table_ regclass, names regclass) RETURNS void
|
3370
|
LANGUAGE sql
|
3371
|
AS $_$
|
3372
|
SELECT util.eval($$DELETE FROM $$||$2||$$ WHERE "from" LIKE ':%'$$);
|
3373
|
SELECT util.mk_derived_col(($2, 'to'), $$"from"$$, overwrite := true);
|
3374
|
SELECT util.set_col_names($1, $2);
|
3375
|
$_$;
|
3376
|
|
3377
|
|
3378
|
--
|
3379
|
-- Name: FUNCTION reset_col_names(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
|
3380
|
--
|
3381
|
|
3382
|
COMMENT ON FUNCTION reset_col_names(table_ regclass, names regclass) IS '
|
3383
|
idempotent.
|
3384
|
alters the names table, so it will need to be repopulated after running this function.
|
3385
|
';
|
3386
|
|
3387
|
|
3388
|
--
|
3389
|
-- Name: reset_map_table(text); Type: FUNCTION; Schema: util; Owner: -
|
3390
|
--
|
3391
|
|
3392
|
CREATE FUNCTION reset_map_table(table_ text) RETURNS void
|
3393
|
LANGUAGE sql
|
3394
|
AS $_$
|
3395
|
SELECT util.drop_table($1);
|
3396
|
SELECT util.mk_map_table($1);
|
3397
|
$_$;
|
3398
|
|
3399
|
|
3400
|
--
|
3401
|
-- Name: restore_views(restore_views_info); Type: FUNCTION; Schema: util; Owner: -
|
3402
|
--
|
3403
|
|
3404
|
CREATE FUNCTION restore_views(restore_views_info) RETURNS void
|
3405
|
LANGUAGE sql
|
3406
|
AS $_$
|
3407
|
SELECT util.debug_print_var('views', $1);
|
3408
|
SELECT util.create_if_not_exists((view_).def, (view_).path)
|
3409
|
/* need to specify view name for manual existence check, in case view def
|
3410
|
becomes invalid, which would produce nonstandard (uncatchable) exception */
|
3411
|
FROM unnest($1.views) view_; -- in forward dependency order
|
3412
|
/* create_if_not_exists() rather than eval(), because cmd might manually
|
3413
|
re-create a deleted dependent view, causing it to already exist */
|
3414
|
SELECT NULL::void; -- don't fold away functions called in previous query
|
3415
|
$_$;
|
3416
|
|
3417
|
|
3418
|
--
|
3419
|
-- Name: rm_freq(regclass[], text); Type: FUNCTION; Schema: util; Owner: -
|
3420
|
--
|
3421
|
|
3422
|
CREATE FUNCTION rm_freq(tables regclass[], freq_col text DEFAULT 'copies'::text) RETURNS void
|
3423
|
LANGUAGE sql
|
3424
|
AS $_$
|
3425
|
SELECT util.drop_column($1, $2, force := true)
|
3426
|
$_$;
|
3427
|
|
3428
|
|
3429
|
--
|
3430
|
-- Name: rtrim_n(text, integer); Type: FUNCTION; Schema: util; Owner: -
|
3431
|
--
|
3432
|
|
3433
|
CREATE FUNCTION rtrim_n(str text, count integer) RETURNS text
|
3434
|
LANGUAGE sql IMMUTABLE
|
3435
|
AS $_$
|
3436
|
SELECT (CASE WHEN $2 <= 0 THEN $1 ELSE left($1, -$2) END)
|
3437
|
$_$;
|
3438
|
|
3439
|
|
3440
|
--
|
3441
|
-- Name: runnable_sql(text); Type: FUNCTION; Schema: util; Owner: -
|
3442
|
--
|
3443
|
|
3444
|
CREATE FUNCTION runnable_sql(sql text) RETURNS text
|
3445
|
LANGUAGE sql IMMUTABLE
|
3446
|
AS $_$
|
3447
|
SELECT (CASE WHEN util.is_set_stmt($1) THEN ''
|
3448
|
ELSE util.mk_set_search_path(for_printing := true)||$$;
|
3449
|
$$ END)||$1
|
3450
|
$_$;
|
3451
|
|
3452
|
|
3453
|
--
|
3454
|
-- Name: save_drop_view(text); Type: FUNCTION; Schema: util; Owner: -
|
3455
|
--
|
3456
|
|
3457
|
CREATE FUNCTION save_drop_view(view_ text) RETURNS text
|
3458
|
LANGUAGE plpgsql STRICT
|
3459
|
AS $$
|
3460
|
DECLARE
|
3461
|
result text = NULL;
|
3462
|
BEGIN
|
3463
|
BEGIN
|
3464
|
result = util.show_create_view(view_, replace := false);
|
3465
|
/* replace: no `OR REPLACE` because that causes nonuniform errors
|
3466
|
(eg. invalid_table_definition), instead of the standard
|
3467
|
duplicate_table exception caught by util.create_if_not_exists() */
|
3468
|
PERFORM util.drop_view(view_);
|
3469
|
EXCEPTION
|
3470
|
WHEN undefined_table THEN NULL;
|
3471
|
END;
|
3472
|
RETURN result;
|
3473
|
END;
|
3474
|
$$;
|
3475
|
|
3476
|
|
3477
|
--
|
3478
|
-- Name: save_drop_views(text[]); Type: FUNCTION; Schema: util; Owner: -
|
3479
|
--
|
3480
|
|
3481
|
CREATE FUNCTION save_drop_views(views text[]) RETURNS restore_views_info
|
3482
|
LANGUAGE sql
|
3483
|
AS $_$
|
3484
|
SELECT ROW(/*return in forward dependency order*/util.array_reverse(array(
|
3485
|
SELECT (view_, util.save_drop_view(view_))::util.db_item
|
3486
|
FROM unnest(/*drop in reverse dependency order*/util.array_reverse($1)) view_
|
3487
|
)))::util.restore_views_info
|
3488
|
$_$;
|
3489
|
|
3490
|
|
3491
|
--
|
3492
|
-- Name: schema(oid); Type: FUNCTION; Schema: util; Owner: -
|
3493
|
--
|
3494
|
|
3495
|
CREATE FUNCTION schema(pg_namespace_oid oid) RETURNS text
|
3496
|
LANGUAGE sql STABLE
|
3497
|
AS $_$
|
3498
|
SELECT nspname::text FROM pg_namespace WHERE pg_namespace.oid = $1
|
3499
|
$_$;
|
3500
|
|
3501
|
|
3502
|
--
|
3503
|
-- Name: schema(regclass); Type: FUNCTION; Schema: util; Owner: -
|
3504
|
--
|
3505
|
|
3506
|
CREATE FUNCTION schema(table_ regclass) RETURNS text
|
3507
|
LANGUAGE sql STABLE
|
3508
|
AS $_$
|
3509
|
SELECT util.schema(relnamespace) FROM pg_class WHERE oid = $1
|
3510
|
$_$;
|
3511
|
|
3512
|
|
3513
|
--
|
3514
|
-- Name: schema(regtype); Type: FUNCTION; Schema: util; Owner: -
|
3515
|
--
|
3516
|
|
3517
|
CREATE FUNCTION schema(type regtype) RETURNS text
|
3518
|
LANGUAGE sql STABLE
|
3519
|
AS $_$
|
3520
|
SELECT util.schema(typnamespace) FROM pg_type WHERE oid = $1
|
3521
|
$_$;
|
3522
|
|
3523
|
|
3524
|
--
|
3525
|
-- Name: schema(anyelement); Type: FUNCTION; Schema: util; Owner: -
|
3526
|
--
|
3527
|
|
3528
|
CREATE FUNCTION schema(type_null anyelement) RETURNS text
|
3529
|
LANGUAGE sql STABLE
|
3530
|
AS $_$
|
3531
|
SELECT util.schema(pg_typeof($1))
|
3532
|
$_$;
|
3533
|
|
3534
|
|
3535
|
--
|
3536
|
-- Name: schema_bundle_get_schemas(text); Type: FUNCTION; Schema: util; Owner: -
|
3537
|
--
|
3538
|
|
3539
|
CREATE FUNCTION schema_bundle_get_schemas(schema_bundle text) RETURNS SETOF text
|
3540
|
LANGUAGE sql STABLE
|
3541
|
AS $_$
|
3542
|
SELECT nspname::text FROM pg_namespace WHERE nspname ~ ('^'||$1||'(?=\y|_)')
|
3543
|
$_$;
|
3544
|
|
3545
|
|
3546
|
--
|
3547
|
-- Name: FUNCTION schema_bundle_get_schemas(schema_bundle text); Type: COMMENT; Schema: util; Owner: -
|
3548
|
--
|
3549
|
|
3550
|
COMMENT ON FUNCTION schema_bundle_get_schemas(schema_bundle text) IS '
|
3551
|
a schema bundle is a group of schemas with a common prefix
|
3552
|
';
|
3553
|
|
3554
|
|
3555
|
--
|
3556
|
-- Name: schema_bundle_rename(text, text); Type: FUNCTION; Schema: util; Owner: -
|
3557
|
--
|
3558
|
|
3559
|
CREATE FUNCTION schema_bundle_rename(old text, new text) RETURNS void
|
3560
|
LANGUAGE sql
|
3561
|
AS $_$
|
3562
|
SELECT util.schema_rename(old_schema,
|
3563
|
overlay(old_schema placing new from 1 for length(old))) -- replace prefix
|
3564
|
FROM util.schema_bundle_get_schemas($1) f (old_schema);
|
3565
|
SELECT NULL::void; -- don't fold away functions called in previous query
|
3566
|
$_$;
|
3567
|
|
3568
|
|
3569
|
--
|
3570
|
-- Name: schema_bundle_replace(text, text); Type: FUNCTION; Schema: util; Owner: -
|
3571
|
--
|
3572
|
|
3573
|
CREATE FUNCTION schema_bundle_replace(replace text, with_ text) RETURNS void
|
3574
|
LANGUAGE plpgsql
|
3575
|
AS $$
|
3576
|
BEGIN
|
3577
|
-- don't schema_bundle_rm() the schema_bundle to keep!
|
3578
|
IF replace = with_ THEN RETURN; END IF;
|
3579
|
|
3580
|
PERFORM util.schema_bundle_rm(replace);
|
3581
|
PERFORM util.schema_bundle_rename(with_, replace);
|
3582
|
END;
|
3583
|
$$;
|
3584
|
|
3585
|
|
3586
|
--
|
3587
|
-- Name: schema_bundle_rm(text); Type: FUNCTION; Schema: util; Owner: -
|
3588
|
--
|
3589
|
|
3590
|
CREATE FUNCTION schema_bundle_rm(schema_bundle text) RETURNS void
|
3591
|
LANGUAGE sql
|
3592
|
AS $_$
|
3593
|
SELECT util.schema_rm(schema)
|
3594
|
FROM util.schema_bundle_get_schemas($1) f (schema);
|
3595
|
SELECT NULL::void; -- don't fold away functions called in previous query
|
3596
|
$_$;
|
3597
|
|
3598
|
|
3599
|
--
|
3600
|
-- Name: schema_esc(anyelement); Type: FUNCTION; Schema: util; Owner: -
|
3601
|
--
|
3602
|
|
3603
|
CREATE FUNCTION schema_esc(type_null anyelement) RETURNS text
|
3604
|
LANGUAGE sql STABLE
|
3605
|
AS $_$
|
3606
|
SELECT quote_ident(util.schema($1))
|
3607
|
$_$;
|
3608
|
|
3609
|
|
3610
|
--
|
3611
|
-- Name: schema_matches(text, text); Type: FUNCTION; Schema: util; Owner: -
|
3612
|
--
|
3613
|
|
3614
|
CREATE FUNCTION schema_matches(schema text, schema_regexp text) RETURNS boolean
|
3615
|
LANGUAGE sql IMMUTABLE
|
3616
|
AS $_$
|
3617
|
SELECT $1 ~ $2 AND /*in userspace*/$1 !~ '^(?:information_schema|pg_.*)$'
|
3618
|
$_$;
|
3619
|
|
3620
|
|
3621
|
--
|
3622
|
-- Name: schema_oid(text); Type: FUNCTION; Schema: util; Owner: -
|
3623
|
--
|
3624
|
|
3625
|
CREATE FUNCTION schema_oid(schema text) RETURNS oid
|
3626
|
LANGUAGE sql STABLE
|
3627
|
AS $_$
|
3628
|
SELECT oid FROM pg_namespace WHERE nspname = $1
|
3629
|
$_$;
|
3630
|
|
3631
|
|
3632
|
--
|
3633
|
-- Name: schema_regexp(regclass); Type: FUNCTION; Schema: util; Owner: -
|
3634
|
--
|
3635
|
|
3636
|
CREATE FUNCTION schema_regexp(relation regclass) RETURNS text
|
3637
|
LANGUAGE sql IMMUTABLE
|
3638
|
AS $_$
|
3639
|
SELECT util.schema_regexp(schema_anchor := $1)
|
3640
|
$_$;
|
3641
|
|
3642
|
|
3643
|
--
|
3644
|
-- Name: schema_regexp(anyelement); Type: FUNCTION; Schema: util; Owner: -
|
3645
|
--
|
3646
|
|
3647
|
CREATE FUNCTION schema_regexp(schema_anchor anyelement) RETURNS text
|
3648
|
LANGUAGE sql IMMUTABLE
|
3649
|
AS $_$
|
3650
|
SELECT util.str_equality_regexp(util.schema($1))
|
3651
|
$_$;
|
3652
|
|
3653
|
|
3654
|
--
|
3655
|
-- Name: schema_rename(text, text); Type: FUNCTION; Schema: util; Owner: -
|
3656
|
--
|
3657
|
|
3658
|
CREATE FUNCTION schema_rename(old text, new text) RETURNS void
|
3659
|
LANGUAGE sql
|
3660
|
AS $_$
|
3661
|
SELECT util.eval($$ALTER SCHEMA $$||quote_ident($1)||$$ RENAME TO $$||quote_ident($2));
|
3662
|
$_$;
|
3663
|
|
3664
|
|
3665
|
--
|
3666
|
-- Name: schema_replace(text, text); Type: FUNCTION; Schema: util; Owner: -
|
3667
|
--
|
3668
|
|
3669
|
CREATE FUNCTION schema_replace(replace text, with_ text) RETURNS void
|
3670
|
LANGUAGE plpgsql
|
3671
|
AS $$
|
3672
|
BEGIN
|
3673
|
-- don't schema_rm() the schema to keep!
|
3674
|
IF replace = with_ THEN RETURN; END IF;
|
3675
|
|
3676
|
PERFORM util.schema_rm(replace);
|
3677
|
PERFORM util.schema_rename(with_, replace);
|
3678
|
END;
|
3679
|
$$;
|
3680
|
|
3681
|
|
3682
|
--
|
3683
|
-- Name: schema_rm(text); Type: FUNCTION; Schema: util; Owner: -
|
3684
|
--
|
3685
|
|
3686
|
CREATE FUNCTION schema_rm(schema text) RETURNS void
|
3687
|
LANGUAGE sql
|
3688
|
AS $_$
|
3689
|
SELECT util.eval($$DROP SCHEMA IF EXISTS $$||quote_ident($1)||$$ CASCADE$$);
|
3690
|
$_$;
|
3691
|
|
3692
|
|
3693
|
--
|
3694
|
-- Name: search_path_append(text); Type: FUNCTION; Schema: util; Owner: -
|
3695
|
--
|
3696
|
|
3697
|
CREATE FUNCTION search_path_append(schemas text) RETURNS void
|
3698
|
LANGUAGE sql
|
3699
|
AS $_$
|
3700
|
SELECT util.eval(
|
3701
|
$$SET search_path TO $$||current_setting('search_path')||$$, $$||$1);
|
3702
|
$_$;
|
3703
|
|
3704
|
|
3705
|
--
|
3706
|
-- Name: set_col_names(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
|
3707
|
--
|
3708
|
|
3709
|
CREATE FUNCTION set_col_names(table_ regclass, names regclass) RETURNS void
|
3710
|
LANGUAGE plpgsql STRICT
|
3711
|
AS $_$
|
3712
|
DECLARE
|
3713
|
old text[] = ARRAY(SELECT util.col_names(table_));
|
3714
|
new text[] = ARRAY(SELECT util.map_values(names));
|
3715
|
BEGIN
|
3716
|
old = old[1:array_length(new, 1)]; -- truncate to same length
|
3717
|
PERFORM util.eval($$ALTER TABLE $$||$1||$$ RENAME $$||quote_ident(key)
|
3718
|
||$$ TO $$||quote_ident(value))
|
3719
|
FROM each(hstore(old, new))
|
3720
|
WHERE value != key -- not same name
|
3721
|
;
|
3722
|
END;
|
3723
|
$_$;
|
3724
|
|
3725
|
|
3726
|
--
|
3727
|
-- Name: FUNCTION set_col_names(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
|
3728
|
--
|
3729
|
|
3730
|
COMMENT ON FUNCTION set_col_names(table_ regclass, names regclass) IS '
|
3731
|
idempotent
|
3732
|
';
|
3733
|
|
3734
|
|
3735
|
--
|
3736
|
-- Name: set_col_names_with_metadata(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
|
3737
|
--
|
3738
|
|
3739
|
CREATE FUNCTION set_col_names_with_metadata(table_ regclass, names regclass) RETURNS void
|
3740
|
LANGUAGE plpgsql STRICT
|
3741
|
AS $_$
|
3742
|
DECLARE
|
3743
|
row_ util.map;
|
3744
|
BEGIN
|
3745
|
-- rename any metadata cols rather than re-adding them with new names
|
3746
|
BEGIN
|
3747
|
PERFORM util.set_col_names(table_, names);
|
3748
|
EXCEPTION
|
3749
|
WHEN array_subscript_error THEN -- selective suppress
|
3750
|
IF SQLERRM LIKE 'arrays must have same bounds' THEN NULL;
|
3751
|
-- metadata cols not yet added
|
3752
|
ELSE RAISE;
|
3753
|
END IF;
|
3754
|
END;
|
3755
|
|
3756
|
FOR row_ IN EXECUTE $$SELECT * FROM $$||names||$$ WHERE "from" LIKE ':%'$$
|
3757
|
LOOP
|
3758
|
PERFORM util.mk_const_col((table_, row_."to"),
|
3759
|
substring(row_."from" from 2));
|
3760
|
END LOOP;
|
3761
|
|
3762
|
PERFORM util.set_col_names(table_, names);
|
3763
|
END;
|
3764
|
$_$;
|
3765
|
|
3766
|
|
3767
|
--
|
3768
|
-- Name: FUNCTION set_col_names_with_metadata(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
|
3769
|
--
|
3770
|
|
3771
|
COMMENT ON FUNCTION set_col_names_with_metadata(table_ regclass, names regclass) IS '
|
3772
|
idempotent.
|
3773
|
the metadata mappings must be *last* in the names table.
|
3774
|
';
|
3775
|
|
3776
|
|
3777
|
--
|
3778
|
-- Name: set_col_types(regclass, col_cast[]); Type: FUNCTION; Schema: util; Owner: -
|
3779
|
--
|
3780
|
|
3781
|
CREATE FUNCTION set_col_types(table_ regclass, col_casts col_cast[]) RETURNS void
|
3782
|
LANGUAGE sql
|
3783
|
AS $_$
|
3784
|
SELECT util.eval(COALESCE(
|
3785
|
$$ALTER TABLE $$||$1||$$
|
3786
|
$$||(
|
3787
|
SELECT
|
3788
|
string_agg($$ALTER COLUMN $$||col_name_sql||$$ TYPE $$||target_type
|
3789
|
||$$ USING $$||col_name_sql||$$::$$||target_type, $$
|
3790
|
, $$)
|
3791
|
FROM
|
3792
|
(
|
3793
|
SELECT
|
3794
|
quote_ident(col_name) AS col_name_sql
|
3795
|
, util.col_type(($1, col_name)) AS curr_type
|
3796
|
, type AS target_type
|
3797
|
FROM unnest($2)
|
3798
|
) s
|
3799
|
WHERE curr_type != target_type
|
3800
|
), ''))
|
3801
|
$_$;
|
3802
|
|
3803
|
|
3804
|
--
|
3805
|
-- Name: FUNCTION set_col_types(table_ regclass, col_casts col_cast[]); Type: COMMENT; Schema: util; Owner: -
|
3806
|
--
|
3807
|
|
3808
|
COMMENT ON FUNCTION set_col_types(table_ regclass, col_casts col_cast[]) IS '
|
3809
|
idempotent
|
3810
|
';
|
3811
|
|
3812
|
|
3813
|
--
|
3814
|
-- Name: set_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
3815
|
--
|
3816
|
|
3817
|
CREATE FUNCTION set_comment(table_ regclass, comment text) RETURNS void
|
3818
|
LANGUAGE sql
|
3819
|
AS $_$
|
3820
|
SELECT util.eval(util.mk_set_comment($1, $2))
|
3821
|
$_$;
|
3822
|
|
3823
|
|
3824
|
--
|
3825
|
-- Name: set_search_path(text, boolean); Type: FUNCTION; Schema: util; Owner: -
|
3826
|
--
|
3827
|
|
3828
|
CREATE FUNCTION set_search_path(search_path text, for_session boolean DEFAULT false) RETURNS void
|
3829
|
LANGUAGE sql
|
3830
|
AS $_$
|
3831
|
SELECT util.eval(util.mk_set_search_path($1, $2))
|
3832
|
$_$;
|
3833
|
|
3834
|
|
3835
|
--
|
3836
|
-- Name: show_create_view(regclass, boolean); Type: FUNCTION; Schema: util; Owner: -
|
3837
|
--
|
3838
|
|
3839
|
CREATE FUNCTION show_create_view(view_ regclass, replace boolean DEFAULT true) RETURNS text
|
3840
|
LANGUAGE sql STABLE
|
3841
|
AS $_$
|
3842
|
SELECT $$CREATE$$||(CASE WHEN $2 THEN $$ OR REPLACE$$ ELSE '' END)||$$ VIEW $$
|
3843
|
||$1||$$ AS
|
3844
|
$$||pg_get_viewdef($1)/*no ; because pg_get_viewdef() includes one*/||$$
|
3845
|
$$||util.mk_set_relation_metadata($1)
|
3846
|
$_$;
|
3847
|
|
3848
|
|
3849
|
--
|
3850
|
-- Name: show_grants_for(regclass); Type: FUNCTION; Schema: util; Owner: -
|
3851
|
--
|
3852
|
|
3853
|
CREATE FUNCTION show_grants_for(table_ regclass) RETURNS text
|
3854
|
LANGUAGE sql STABLE
|
3855
|
AS $_$
|
3856
|
SELECT string_agg(cmd, '')
|
3857
|
FROM
|
3858
|
(
|
3859
|
SELECT (CASE WHEN has_table_privilege(user_, $1, 'SELECT') THEN
|
3860
|
$$GRANT SELECT ON TABLE $$||$1||$$ TO $$||quote_ident(user_)||$$;
|
3861
|
$$ ELSE '' END) AS cmd
|
3862
|
FROM util.grants_users() f (user_)
|
3863
|
) s
|
3864
|
$_$;
|
3865
|
|
3866
|
|
3867
|
--
|
3868
|
-- Name: show_relations_like(text, text, character[]); Type: FUNCTION; Schema: util; Owner: -
|
3869
|
--
|
3870
|
|
3871
|
CREATE FUNCTION show_relations_like(name_regexp text, schema_regexp text DEFAULT ''::text, types character[] DEFAULT ARRAY['c'::text, 'r'::text, 'v'::text]) RETURNS SETOF regclass
|
3872
|
LANGUAGE sql STABLE
|
3873
|
AS $_$
|
3874
|
SELECT oid FROM pg_class
|
3875
|
WHERE relkind = ANY($3) AND relname ~ $1
|
3876
|
AND util.schema_matches(util.schema(relnamespace), $2)
|
3877
|
ORDER BY relname
|
3878
|
$_$;
|
3879
|
|
3880
|
|
3881
|
--
|
3882
|
-- Name: show_set_comment(regclass); Type: FUNCTION; Schema: util; Owner: -
|
3883
|
--
|
3884
|
|
3885
|
CREATE FUNCTION show_set_comment(table_ regclass) RETURNS text
|
3886
|
LANGUAGE sql STABLE
|
3887
|
AS $_$
|
3888
|
SELECT util.mk_set_comment($1, util.comment($1))
|
3889
|
$_$;
|
3890
|
|
3891
|
|
3892
|
--
|
3893
|
-- Name: show_types_like(text, text); Type: FUNCTION; Schema: util; Owner: -
|
3894
|
--
|
3895
|
|
3896
|
CREATE FUNCTION show_types_like(name_regexp text DEFAULT ''::text, schema_regexp text DEFAULT ''::text) RETURNS SETOF regtype
|
3897
|
LANGUAGE sql STABLE
|
3898
|
AS $_$
|
3899
|
SELECT oid
|
3900
|
FROM pg_type
|
3901
|
WHERE typname ~ $1 AND util.schema_matches(util.schema(typnamespace), $2)
|
3902
|
ORDER BY typname
|
3903
|
$_$;
|
3904
|
|
3905
|
|
3906
|
--
|
3907
|
-- Name: show_views_like(text, text); Type: FUNCTION; Schema: util; Owner: -
|
3908
|
--
|
3909
|
|
3910
|
CREATE FUNCTION show_views_like(name_regexp text, schema_regexp text DEFAULT ''::text) RETURNS SETOF regclass
|
3911
|
LANGUAGE sql STABLE
|
3912
|
AS $_$
|
3913
|
SELECT * FROM util.show_relations_like($1, $2, ARRAY['v'])
|
3914
|
$_$;
|
3915
|
|
3916
|
|
3917
|
--
|
3918
|
-- Name: str_equality_regexp(text); Type: FUNCTION; Schema: util; Owner: -
|
3919
|
--
|
3920
|
|
3921
|
CREATE FUNCTION str_equality_regexp(literal text) RETURNS text
|
3922
|
LANGUAGE sql IMMUTABLE
|
3923
|
AS $_$
|
3924
|
SELECT '^'||util.regexp_quote($1)||'$'
|
3925
|
$_$;
|
3926
|
|
3927
|
|
3928
|
--
|
3929
|
-- Name: table2hstore(regclass); Type: FUNCTION; Schema: util; Owner: -
|
3930
|
--
|
3931
|
|
3932
|
CREATE FUNCTION table2hstore(table_ regclass) RETURNS hstore
|
3933
|
LANGUAGE plpgsql STABLE STRICT
|
3934
|
AS $_$
|
3935
|
DECLARE
|
3936
|
hstore hstore;
|
3937
|
BEGIN
|
3938
|
EXECUTE $$SELECT hstore(ARRAY(SELECT unnest(ARRAY["from", "to"]) FROM $$||
|
3939
|
table_||$$))$$ INTO STRICT hstore;
|
3940
|
RETURN hstore;
|
3941
|
END;
|
3942
|
$_$;
|
3943
|
|
3944
|
|
3945
|
--
|
3946
|
-- Name: table_flag__get(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
3947
|
--
|
3948
|
|
3949
|
CREATE FUNCTION table_flag__get(table_ regclass, flag text) RETURNS boolean
|
3950
|
LANGUAGE sql STABLE
|
3951
|
AS $_$
|
3952
|
SELECT COUNT(*) > 0 FROM pg_constraint
|
3953
|
WHERE conrelid = $1 AND contype = 'c' AND conname = $2
|
3954
|
$_$;
|
3955
|
|
3956
|
|
3957
|
--
|
3958
|
-- Name: FUNCTION table_flag__get(table_ regclass, flag text); Type: COMMENT; Schema: util; Owner: -
|
3959
|
--
|
3960
|
|
3961
|
COMMENT ON FUNCTION table_flag__get(table_ regclass, flag text) IS '
|
3962
|
gets whether a status flag is set by the presence of a table constraint
|
3963
|
';
|
3964
|
|
3965
|
|
3966
|
--
|
3967
|
-- Name: table_flag__set(regclass, text); Type: FUNCTION; Schema: util; Owner: -
|
3968
|
--
|
3969
|
|
3970
|
CREATE FUNCTION table_flag__set(table_ regclass, flag text) RETURNS void
|
3971
|
LANGUAGE sql
|
3972
|
AS $_$
|
3973
|
SELECT util.create_if_not_exists($$ALTER TABLE $$||$1||$$ ADD CONSTRAINT $$
|
3974
|
||quote_ident($2)||$$ CHECK (true)$$)
|
3975
|
$_$;
|
3976
|
|
3977
|
|
3978
|
--
|
3979
|
-- Name: FUNCTION table_flag__set(table_ regclass, flag text); Type: COMMENT; Schema: util; Owner: -
|
3980
|
--
|
3981
|
|
3982
|
COMMENT ON FUNCTION table_flag__set(table_ regclass, flag text) IS '
|
3983
|
stores a status flag by the presence of a table constraint.
|
3984
|
idempotent.
|
3985
|
';
|
3986
|
|
3987
|
|
3988
|
--
|
3989
|
-- Name: table_nulls_mapped__get(regclass); Type: FUNCTION; Schema: util; Owner: -
|
3990
|
--
|
3991
|
|
3992
|
CREATE FUNCTION table_nulls_mapped__get(table_ regclass) RETURNS boolean
|
3993
|
LANGUAGE sql STABLE
|
3994
|
AS $_$
|
3995
|
SELECT util.table_flag__get($1, 'nulls_mapped')
|
3996
|
$_$;
|
3997
|
|
3998
|
|
3999
|
--
|
4000
|
-- Name: FUNCTION table_nulls_mapped__get(table_ regclass); Type: COMMENT; Schema: util; Owner: -
|
4001
|
--
|
4002
|
|
4003
|
COMMENT ON FUNCTION table_nulls_mapped__get(table_ regclass) IS '
|
4004
|
gets whether a table''s NULL-equivalent strings have been replaced with NULL
|
4005
|
';
|
4006
|
|
4007
|
|
4008
|
--
|
4009
|
-- Name: table_nulls_mapped__set(regclass); Type: FUNCTION; Schema: util; Owner: -
|
4010
|
--
|
4011
|
|
4012
|
CREATE FUNCTION table_nulls_mapped__set(table_ regclass) RETURNS void
|
4013
|
LANGUAGE sql
|
4014
|
AS $_$
|
4015
|
SELECT util.table_flag__set($1, 'nulls_mapped')
|
4016
|
$_$;
|
4017
|
|
4018
|
|
4019
|
--
|
4020
|
-- Name: FUNCTION table_nulls_mapped__set(table_ regclass); Type: COMMENT; Schema: util; Owner: -
|
4021
|
--
|
4022
|
|
4023
|
COMMENT ON FUNCTION table_nulls_mapped__set(table_ regclass) IS '
|
4024
|
sets that a table''s NULL-equivalent strings have been replaced with NULL.
|
4025
|
idempotent.
|
4026
|
';
|
4027
|
|
4028
|
|
4029
|
--
|
4030
|
-- Name: to_freq(regclass); Type: FUNCTION; Schema: util; Owner: -
|
4031
|
--
|
4032
|
|
4033
|
CREATE FUNCTION to_freq(table_ regclass) RETURNS void
|
4034
|
LANGUAGE sql
|
4035
|
AS $_$
|
4036
|
-- save data before truncating main table
|
4037
|
SELECT util.copy_types_and_data($1, 'pg_temp.__copy');
|
4038
|
|
4039
|
-- repopulate main table w/ copies column
|
4040
|
SELECT util.truncate($1);
|
4041
|
SELECT util.eval($$ALTER TABLE $$||$1||$$ ADD COLUMN copies bigint NOT NULL$$);
|
4042
|
SELECT util.populate_table($1, $$
|
4043
|
SELECT (table_).*, copies
|
4044
|
FROM (
|
4045
|
SELECT table_, COUNT(*) AS copies
|
4046
|
FROM pg_temp.__copy table_
|
4047
|
GROUP BY table_
|
4048
|
) s
|
4049
|
$$);
|
4050
|
|
4051
|
-- delete temp table so it doesn't stay around until end of connection
|
4052
|
SELECT util.drop_table('pg_temp.__copy');
|
4053
|
$_$;
|
4054
|
|
4055
|
|
4056
|
--
|
4057
|
-- Name: to_global_col_names(regclass); Type: FUNCTION; Schema: util; Owner: -
|
4058
|
--
|
4059
|
|
4060
|
CREATE FUNCTION to_global_col_names(table_ regclass) RETURNS void
|
4061
|
LANGUAGE plpgsql STRICT
|
4062
|
AS $_$
|
4063
|
DECLARE
|
4064
|
row record;
|
4065
|
BEGIN
|
4066
|
FOR row IN SELECT * FROM util.col_global_names(table_::text::regtype)
|
4067
|
LOOP
|
4068
|
IF row.global_name != row.name THEN
|
4069
|
EXECUTE $$ALTER TABLE $$||table_||$$ RENAME $$
|
4070
|
||quote_ident(row.name)||$$ TO $$||quote_ident(row.global_name);
|
4071
|
END IF;
|
4072
|
END LOOP;
|
4073
|
END;
|
4074
|
$_$;
|
4075
|
|
4076
|
|
4077
|
--
|
4078
|
-- Name: FUNCTION to_global_col_names(table_ regclass); Type: COMMENT; Schema: util; Owner: -
|
4079
|
--
|
4080
|
|
4081
|
COMMENT ON FUNCTION to_global_col_names(table_ regclass) IS '
|
4082
|
idempotent
|
4083
|
';
|
4084
|
|
4085
|
|
4086
|
--
|
4087
|
-- Name: trim(regclass, regclass, boolean); Type: FUNCTION; Schema: util; Owner: -
|
4088
|
--
|
4089
|
|
4090
|
CREATE FUNCTION "trim"(table_ regclass, names regclass, force boolean DEFAULT true) RETURNS void
|
4091
|
LANGUAGE sql
|
4092
|
AS $_$
|
4093
|
SELECT util.drop_column(($1, col), $3) FROM util.added_cols($1, $2) f (col);
|
4094
|
SELECT NULL::void; -- don't fold away functions called in previous query
|
4095
|
$_$;
|
4096
|
|
4097
|
|
4098
|
--
|
4099
|
-- Name: FUNCTION "trim"(table_ regclass, names regclass, force boolean); Type: COMMENT; Schema: util; Owner: -
|
4100
|
--
|
4101
|
|
4102
|
COMMENT ON FUNCTION "trim"(table_ regclass, names regclass, force boolean) IS '
|
4103
|
trims table_ to include only columns in the original data
|
4104
|
|
4105
|
by default, cascadingly drops dependent columns so that they don''t prevent
|
4106
|
trim() from succeeding. note that this requires the dependent columns to then be
|
4107
|
manually re-created.
|
4108
|
|
4109
|
idempotent
|
4110
|
';
|
4111
|
|
4112
|
|
4113
|
--
|
4114
|
-- Name: truncate(regclass); Type: FUNCTION; Schema: util; Owner: -
|
4115
|
--
|
4116
|
|
4117
|
CREATE FUNCTION truncate(table_ regclass) RETURNS void
|
4118
|
LANGUAGE plpgsql STRICT
|
4119
|
AS $_$
|
4120
|
BEGIN
|
4121
|
EXECUTE $$TRUNCATE $$||table_||$$ CASCADE$$;
|
4122
|
END;
|
4123
|
$_$;
|
4124
|
|
4125
|
|
4126
|
--
|
4127
|
-- Name: FUNCTION truncate(table_ regclass); Type: COMMENT; Schema: util; Owner: -
|
4128
|
--
|
4129
|
|
4130
|
COMMENT ON FUNCTION truncate(table_ regclass) IS '
|
4131
|
idempotent
|
4132
|
';
|
4133
|
|
4134
|
|
4135
|
--
|
4136
|
-- Name: truncated_prefixed_name_regexp(text, integer); Type: FUNCTION; Schema: util; Owner: -
|
4137
|
--
|
4138
|
|
4139
|
CREATE FUNCTION truncated_prefixed_name_regexp(name text, max_prefix_len integer) RETURNS text
|
4140
|
LANGUAGE sql IMMUTABLE
|
4141
|
AS $_$
|
4142
|
SELECT '^(.*)'||util._if(util.name_was_truncated($1, $2),
|
4143
|
util.regexp_quote(util.rtrim_n($1, $2))||'.*', util.regexp_quote($1)) ||'$'
|
4144
|
$_$;
|
4145
|
|
4146
|
|
4147
|
--
|
4148
|
-- Name: try_cast(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
|
4149
|
--
|
4150
|
|
4151
|
CREATE FUNCTION try_cast(value text, ret_type_null anyelement) RETURNS anyelement
|
4152
|
LANGUAGE plpgsql IMMUTABLE
|
4153
|
AS $$
|
4154
|
BEGIN
|
4155
|
/* need explicit cast because some types not implicitly-castable, and also
|
4156
|
to make the cast happen inside the try block. (*implicit* casts to the
|
4157
|
return type happen at the end of the function, outside any block.) */
|
4158
|
RETURN util.cast(value, ret_type_null);
|
4159
|
EXCEPTION
|
4160
|
WHEN data_exception
|
4161
|
OR syntax_error_or_access_rule_violation -- eg. ::regclass
|
4162
|
THEN
|
4163
|
PERFORM util.raise('WARNING', SQLERRM);
|
4164
|
RETURN NULL;
|
4165
|
END;
|
4166
|
$$;
|
4167
|
|
4168
|
|
4169
|
--
|
4170
|
-- Name: FUNCTION try_cast(value text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
|
4171
|
--
|
4172
|
|
4173
|
COMMENT ON FUNCTION try_cast(value text, ret_type_null anyelement) IS '
|
4174
|
ret_type_null: NULL::ret_type
|
4175
|
';
|
4176
|
|
4177
|
|
4178
|
--
|
4179
|
-- Name: try_create(text); Type: FUNCTION; Schema: util; Owner: -
|
4180
|
--
|
4181
|
|
4182
|
CREATE FUNCTION try_create(sql text) RETURNS void
|
4183
|
LANGUAGE plpgsql STRICT
|
4184
|
AS $$
|
4185
|
BEGIN
|
4186
|
PERFORM util.eval(sql);
|
4187
|
EXCEPTION
|
4188
|
WHEN not_null_violation
|
4189
|
/* trying to add NOT NULL column to parent table, which cascades to
|
4190
|
child table whose values for the new column will be NULL */
|
4191
|
OR wrong_object_type -- trying to alter a view's columns
|
4192
|
OR undefined_column
|
4193
|
OR duplicate_column
|
4194
|
THEN NULL;
|
4195
|
WHEN datatype_mismatch THEN
|
4196
|
IF SQLERRM LIKE 'child table is missing column %' THEN NULL;
|
4197
|
ELSE RAISE; -- rethrow
|
4198
|
END IF;
|
4199
|
END;
|
4200
|
$$;
|
4201
|
|
4202
|
|
4203
|
--
|
4204
|
-- Name: FUNCTION try_create(sql text); Type: COMMENT; Schema: util; Owner: -
|
4205
|
--
|
4206
|
|
4207
|
COMMENT ON FUNCTION try_create(sql text) IS '
|
4208
|
idempotent
|
4209
|
';
|
4210
|
|
4211
|
|
4212
|
--
|
4213
|
-- Name: try_mk_derived_col(col_ref, text); Type: FUNCTION; Schema: util; Owner: -
|
4214
|
--
|
4215
|
|
4216
|
CREATE FUNCTION try_mk_derived_col(col col_ref, expr text) RETURNS void
|
4217
|
LANGUAGE sql
|
4218
|
AS $_$
|
4219
|
SELECT util.try_create($$SELECT util.mk_derived_col($$||quote_literal($1)||$$, $$||quote_literal($2)||$$)$$)
|
4220
|
$_$;
|
4221
|
|
4222
|
|
4223
|
--
|
4224
|
-- Name: FUNCTION try_mk_derived_col(col col_ref, expr text); Type: COMMENT; Schema: util; Owner: -
|
4225
|
--
|
4226
|
|
4227
|
COMMENT ON FUNCTION try_mk_derived_col(col col_ref, expr text) IS '
|
4228
|
idempotent
|
4229
|
';
|
4230
|
|
4231
|
|
4232
|
--
|
4233
|
-- Name: type_qual(anyelement); Type: FUNCTION; Schema: util; Owner: -
|
4234
|
--
|
4235
|
|
4236
|
CREATE FUNCTION type_qual(value anyelement) RETURNS text
|
4237
|
LANGUAGE sql IMMUTABLE
|
4238
|
AS $_$
|
4239
|
SELECT CASE WHEN $1 IS NULL THEN '' ELSE $$ NOT NULL$$ END
|
4240
|
$_$;
|
4241
|
|
4242
|
|
4243
|
--
|
4244
|
-- Name: FUNCTION type_qual(value anyelement); Type: COMMENT; Schema: util; Owner: -
|
4245
|
--
|
4246
|
|
4247
|
COMMENT ON FUNCTION type_qual(value anyelement) IS '
|
4248
|
a type''s NOT NULL qualifier
|
4249
|
';
|
4250
|
|
4251
|
|
4252
|
--
|
4253
|
-- Name: typed_cols(regtype); Type: FUNCTION; Schema: util; Owner: -
|
4254
|
--
|
4255
|
|
4256
|
CREATE FUNCTION typed_cols(type regtype) RETURNS SETOF col_cast
|
4257
|
LANGUAGE sql STABLE
|
4258
|
AS $_$
|
4259
|
SELECT (attname::text, atttypid)::util.col_cast
|
4260
|
FROM pg_attribute
|
4261
|
WHERE attrelid = $1::text::regclass AND attnum >= 1 AND NOT attisdropped
|
4262
|
ORDER BY attnum
|
4263
|
$_$;
|
4264
|
|
4265
|
|
4266
|
--
|
4267
|
-- Name: typeof(anyelement); Type: FUNCTION; Schema: util; Owner: -
|
4268
|
--
|
4269
|
|
4270
|
CREATE FUNCTION typeof(value anyelement) RETURNS text
|
4271
|
LANGUAGE sql IMMUTABLE
|
4272
|
AS $_$
|
4273
|
SELECT util.qual_name(pg_typeof($1))
|
4274
|
$_$;
|
4275
|
|
4276
|
|
4277
|
--
|
4278
|
-- Name: typeof(text, regtype); Type: FUNCTION; Schema: util; Owner: -
|
4279
|
--
|
4280
|
|
4281
|
CREATE FUNCTION typeof(expr text, table_ regtype DEFAULT NULL::regtype) RETURNS regtype
|
4282
|
LANGUAGE plpgsql STABLE
|
4283
|
AS $_$
|
4284
|
DECLARE
|
4285
|
type regtype;
|
4286
|
BEGIN
|
4287
|
EXECUTE $$SELECT pg_typeof($$||expr||$$)$$||
|
4288
|
COALESCE($$ FROM (SELECT (NULL::$$||table_||$$).*) _s$$, '') INTO STRICT type;
|
4289
|
RETURN type;
|
4290
|
END;
|
4291
|
$_$;
|
4292
|
|
4293
|
|
4294
|
--
|
4295
|
-- Name: use_schema(anyelement); Type: FUNCTION; Schema: util; Owner: -
|
4296
|
--
|
4297
|
|
4298
|
CREATE FUNCTION use_schema(schema_anchor anyelement) RETURNS void
|
4299
|
LANGUAGE sql
|
4300
|
AS $_$
|
4301
|
SELECT util.set_search_path(util.mk_search_path(util.schema($1)))
|
4302
|
$_$;
|
4303
|
|
4304
|
|
4305
|
--
|
4306
|
-- Name: FUNCTION use_schema(schema_anchor anyelement); Type: COMMENT; Schema: util; Owner: -
|
4307
|
--
|
4308
|
|
4309
|
COMMENT ON FUNCTION use_schema(schema_anchor anyelement) IS '
|
4310
|
auto-appends util to the search_path to enable use of util operators
|
4311
|
';
|
4312
|
|
4313
|
|
4314
|
--
|
4315
|
-- Name: all_same(anyelement); Type: AGGREGATE; Schema: util; Owner: -
|
4316
|
--
|
4317
|
|
4318
|
CREATE AGGREGATE all_same(anyelement) (
|
4319
|
SFUNC = all_same_transform,
|
4320
|
STYPE = anyarray,
|
4321
|
FINALFUNC = all_same_final
|
4322
|
);
|
4323
|
|
4324
|
|
4325
|
--
|
4326
|
-- Name: AGGREGATE all_same(anyelement); Type: COMMENT; Schema: util; Owner: -
|
4327
|
--
|
4328
|
|
4329
|
COMMENT ON AGGREGATE all_same(anyelement) IS '
|
4330
|
includes NULLs in comparison
|
4331
|
';
|
4332
|
|
4333
|
|
4334
|
--
|
4335
|
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: util; Owner: -
|
4336
|
--
|
4337
|
|
4338
|
CREATE AGGREGATE join_strs(text, text) (
|
4339
|
SFUNC = join_strs_transform,
|
4340
|
STYPE = text
|
4341
|
);
|
4342
|
|
4343
|
|
4344
|
--
|
4345
|
-- Name: %==; Type: OPERATOR; Schema: util; Owner: -
|
4346
|
--
|
4347
|
|
4348
|
CREATE OPERATOR %== (
|
4349
|
PROCEDURE = "%==",
|
4350
|
LEFTARG = anyelement,
|
4351
|
RIGHTARG = anyelement
|
4352
|
);
|
4353
|
|
4354
|
|
4355
|
--
|
4356
|
-- Name: OPERATOR %== (anyelement, anyelement); Type: COMMENT; Schema: util; Owner: -
|
4357
|
--
|
4358
|
|
4359
|
COMMENT ON OPERATOR %== (anyelement, anyelement) IS '
|
4360
|
returns whether the map-keys of the compared values are the same
|
4361
|
(mnemonic: % is the Perl symbol for a hash map)
|
4362
|
|
4363
|
should be overridden for types that store both keys and values
|
4364
|
|
4365
|
used in a FULL JOIN to select which columns to join on
|
4366
|
';
|
4367
|
|
4368
|
|
4369
|
--
|
4370
|
-- Name: ->; Type: OPERATOR; Schema: util; Owner: -
|
4371
|
--
|
4372
|
|
4373
|
CREATE OPERATOR -> (
|
4374
|
PROCEDURE = map_get,
|
4375
|
LEFTARG = regclass,
|
4376
|
RIGHTARG = text
|
4377
|
);
|
4378
|
|
4379
|
|
4380
|
--
|
4381
|
-- Name: =>; Type: OPERATOR; Schema: util; Owner: -
|
4382
|
--
|
4383
|
|
4384
|
CREATE OPERATOR => (
|
4385
|
PROCEDURE = hstore,
|
4386
|
LEFTARG = text[],
|
4387
|
RIGHTARG = text
|
4388
|
);
|
4389
|
|
4390
|
|
4391
|
--
|
4392
|
-- Name: OPERATOR => (text[], text); Type: COMMENT; Schema: util; Owner: -
|
4393
|
--
|
4394
|
|
4395
|
COMMENT ON OPERATOR => (text[], text) IS '
|
4396
|
usage: array[''key1'', ...]::text[] => ''value''
|
4397
|
';
|
4398
|
|
4399
|
|
4400
|
--
|
4401
|
-- Name: ?*>=; Type: OPERATOR; Schema: util; Owner: -
|
4402
|
--
|
4403
|
|
4404
|
CREATE OPERATOR ?*>= (
|
4405
|
PROCEDURE = is_populated_more_often_than,
|
4406
|
LEFTARG = anyelement,
|
4407
|
RIGHTARG = anyelement
|
4408
|
);
|
4409
|
|
4410
|
|
4411
|
--
|
4412
|
-- Name: ?>=; Type: OPERATOR; Schema: util; Owner: -
|
4413
|
--
|
4414
|
|
4415
|
CREATE OPERATOR ?>= (
|
4416
|
PROCEDURE = is_more_complete_than,
|
4417
|
LEFTARG = anyelement,
|
4418
|
RIGHTARG = anyelement
|
4419
|
);
|
4420
|
|
4421
|
|
4422
|
--
|
4423
|
-- Name: ||%; Type: OPERATOR; Schema: util; Owner: -
|
4424
|
--
|
4425
|
|
4426
|
CREATE OPERATOR ||% (
|
4427
|
PROCEDURE = concat_esc,
|
4428
|
LEFTARG = text,
|
4429
|
RIGHTARG = text
|
4430
|
);
|
4431
|
|
4432
|
|
4433
|
--
|
4434
|
-- Name: OPERATOR ||% (text, text); Type: COMMENT; Schema: util; Owner: -
|
4435
|
--
|
4436
|
|
4437
|
COMMENT ON OPERATOR ||% (text, text) IS '
|
4438
|
% indicates an identifier, as in Perl hashes and one of the x86 assembler syntaxes for registers
|
4439
|
';
|
4440
|
|
4441
|
|
4442
|
--
|
4443
|
-- Name: map; Type: TABLE; Schema: util; Owner: -; Tablespace:
|
4444
|
--
|
4445
|
|
4446
|
CREATE TABLE map (
|
4447
|
"from" text NOT NULL,
|
4448
|
"to" text,
|
4449
|
filter text,
|
4450
|
notes text
|
4451
|
);
|
4452
|
|
4453
|
|
4454
|
--
|
4455
|
-- Data for Name: explain; Type: TABLE DATA; Schema: util; Owner: -
|
4456
|
--
|
4457
|
|
4458
|
|
4459
|
|
4460
|
--
|
4461
|
-- Data for Name: map; Type: TABLE DATA; Schema: util; Owner: -
|
4462
|
--
|
4463
|
|
4464
|
|
4465
|
|
4466
|
--
|
4467
|
-- Name: map__unique__from; Type: CONSTRAINT; Schema: util; Owner: -; Tablespace:
|
4468
|
--
|
4469
|
|
4470
|
ALTER TABLE ONLY map
|
4471
|
ADD CONSTRAINT map__unique__from UNIQUE ("from");
|
4472
|
|
4473
|
|
4474
|
--
|
4475
|
-- Name: map__unique__to; Type: CONSTRAINT; Schema: util; Owner: -; Tablespace:
|
4476
|
--
|
4477
|
|
4478
|
ALTER TABLE ONLY map
|
4479
|
ADD CONSTRAINT map__unique__to UNIQUE ("to");
|
4480
|
|
4481
|
|
4482
|
--
|
4483
|
-- Name: map_filter_insert; Type: TRIGGER; Schema: util; Owner: -
|
4484
|
--
|
4485
|
|
4486
|
CREATE TRIGGER map_filter_insert BEFORE INSERT ON map FOR EACH ROW EXECUTE PROCEDURE map_filter_insert();
|
4487
|
|
4488
|
|
4489
|
--
|
4490
|
-- PostgreSQL database dump complete
|
4491
|
--
|
4492
|
|