Project

General

Profile

1 2094 aaronmk
--
2
-- PostgreSQL database dump
3
--
4
5
SET statement_timeout = 0;
6 11667 aaronmk
SET lock_timeout = 0;
7 2094 aaronmk
SET client_encoding = 'UTF8';
8 6213 aaronmk
SET standard_conforming_strings = on;
9 2094 aaronmk
SET check_function_bodies = false;
10
SET client_min_messages = warning;
11
12
--
13 8183 aaronmk
-- Name: util; Type: SCHEMA; Schema: -; Owner: -
14 2094 aaronmk
--
15
16 8183 aaronmk
CREATE SCHEMA util;
17 2094 aaronmk
18
19 4982 aaronmk
--
20 8183 aaronmk
-- Name: SCHEMA util; Type: COMMENT; Schema: -; Owner: -
21 4982 aaronmk
--
22
23 12235 aaronmk
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 4982 aaronmk
26 12447 aaronmk
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 12235 aaronmk
';
28 4982 aaronmk
29 10378 aaronmk
30 8183 aaronmk
SET search_path = util, pg_catalog;
31 2107 aaronmk
32 2094 aaronmk
--
33 8183 aaronmk
-- Name: col_cast; Type: TYPE; Schema: util; Owner: -
34 8107 aaronmk
--
35
36
CREATE TYPE col_cast AS (
37
	col_name text,
38
	type regtype
39
);
40
41
42
--
43 8183 aaronmk
-- Name: col_ref; Type: TYPE; Schema: util; Owner: -
44 8106 aaronmk
--
45
46
CREATE TYPE col_ref AS (
47
	table_ regclass,
48
	name text
49
);
50
51
52
--
53 8183 aaronmk
-- Name: compass_dir; Type: TYPE; Schema: util; Owner: -
54 7673 aaronmk
--
55
56
CREATE TYPE compass_dir AS ENUM (
57
    'N',
58
    'E',
59
    'S',
60
    'W'
61
);
62
63
64
--
65 8183 aaronmk
-- Name: datatype; Type: TYPE; Schema: util; Owner: -
66 2610 aaronmk
--
67
68
CREATE TYPE datatype AS ENUM (
69
    'str',
70
    'float'
71
);
72
73
74
--
75 13491 aaronmk
-- 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 13488 aaronmk
-- Name: restore_views_info; Type: TYPE; Schema: util; Owner: -
86
--
87
88
CREATE TYPE restore_views_info AS (
89 13491 aaronmk
	views db_item[]
90 13488 aaronmk
);
91
92
93
--
94 12423 aaronmk
-- Name: %==(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
95
--
96
97
CREATE FUNCTION "%=="(left_ anyelement, right_ anyelement) RETURNS boolean
98 12443 aaronmk
    LANGUAGE sql STABLE
99 12423 aaronmk
    AS $_$
100 12436 aaronmk
SELECT keys($1) = keys($2)
101 12423 aaronmk
$_$;
102
103
104
--
105 12443 aaronmk
-- 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 8183 aaronmk
-- Name: _alt(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
115 2596 aaronmk
--
116
117 4501 aaronmk
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 3422 aaronmk
    LANGUAGE sql IMMUTABLE
119
    AS $_$
120 4501 aaronmk
SELECT coalesce($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
121 3422 aaronmk
$_$;
122
123
124
--
125 8183 aaronmk
-- Name: _and(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: util; Owner: -
126 5937 aaronmk
--
127
128 5956 aaronmk
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 5937 aaronmk
    LANGUAGE sql IMMUTABLE
130
    AS $_$
131
SELECT bool_and(value)
132
FROM
133
(VALUES
134
      ($1)
135
    , ($2)
136 5956 aaronmk
    , ($3)
137
    , ($4)
138
    , ($5)
139 5937 aaronmk
)
140
AS v (value)
141
$_$;
142
143
144
--
145 8183 aaronmk
-- Name: FUNCTION _and("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: util; Owner: -
146 5937 aaronmk
--
147
148 12235 aaronmk
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 5937 aaronmk
152
153
--
154 8183 aaronmk
-- Name: _avg(double precision, double precision, double precision, double precision, double precision); Type: FUNCTION; Schema: util; Owner: -
155 7704 aaronmk
--
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 8183 aaronmk
-- Name: _dms_to_dd(text); Type: FUNCTION; Schema: util; Owner: -
175 7679 aaronmk
--
176
177
CREATE FUNCTION _dms_to_dd(value text DEFAULT NULL::text) RETURNS double precision
178 12441 aaronmk
    LANGUAGE sql IMMUTABLE
179 7679 aaronmk
    AS $_$
180 8183 aaronmk
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 7698 aaronmk
FROM
182
(
183
    SELECT regexp_matches($1, '^ *(-?)(\d{1,3}(?:\.\d*)?)(?:(?:deg|[°º])(?: *([\d.]+)(?:min|[''’]))?(?: *([\d.]+)(?:sec|["”]))?)? *([NESW])? *$')
184
    UNION ALL
185 7702 aaronmk
    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 7698 aaronmk
)
188
matches (g)
189 7679 aaronmk
$_$;
190
191
192
--
193 8183 aaronmk
-- Name: _dms_to_dd(double precision, double precision, double precision, compass_dir); Type: FUNCTION; Schema: util; Owner: -
194 7672 aaronmk
--
195
196 7674 aaronmk
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 7672 aaronmk
    LANGUAGE sql IMMUTABLE
198
    AS $_$
199 8183 aaronmk
SELECT sum(value)*COALESCE(util._map('N=>1,E=>1,S=>-1,W=>-1', $4::text)::integer, 1)
200 7672 aaronmk
FROM
201
(VALUES
202 7677 aaronmk
      ($1)
203 7672 aaronmk
    , ($2/60)
204
    , ($3/60/60)
205
)
206
AS v (value)
207
$_$;
208
209
210
--
211 8183 aaronmk
-- Name: _dms_to_dd(text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
212 7723 aaronmk
--
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 8183 aaronmk
SELECT util._dms_to_dd($1::double precision, $2::double precision, $3::double precision, $4::util.compass_dir)
218 7723 aaronmk
$_$;
219
220
221
--
222 8183 aaronmk
-- Name: _eq(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
223 4142 aaronmk
--
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 8183 aaronmk
-- Name: _fix_date(date, date); Type: FUNCTION; Schema: util; Owner: -
234 7396 aaronmk
--
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 8183 aaronmk
-- Name: _if(boolean, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
247 4147 aaronmk
--
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 8183 aaronmk
-- Name: _if(text, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
258 4147 aaronmk
--
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 8183 aaronmk
SELECT util._if($1 != '', $2, $3)
264 4147 aaronmk
$_$;
265
266
267
--
268 10699 aaronmk
-- Name: _join(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
269 4325 aaronmk
--
270
271 10699 aaronmk
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 4325 aaronmk
    LANGUAGE sql IMMUTABLE
273
    AS $_$
274 7848 aaronmk
SELECT NULLIF(concat_ws('; ', $1, $2, $3, $4, $5, $6, $7, $8, $9, $10), '')
275 4325 aaronmk
$_$;
276
277
278
--
279 10699 aaronmk
-- Name: _join_words(text, text, text, text, text, text, text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
280 5009 aaronmk
--
281
282 10699 aaronmk
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 5009 aaronmk
    LANGUAGE sql IMMUTABLE
284
    AS $_$
285 7848 aaronmk
SELECT NULLIF(concat_ws(' ', $1, $2, $3, $4, $5, $6, $7, $8, $9, $10), '')
286 5009 aaronmk
$_$;
287
288
289
--
290 13355 aaronmk
-- 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 10699 aaronmk
-- Name: _label(text, text); Type: FUNCTION; Schema: util; Owner: -
302 3422 aaronmk
--
303
304 10699 aaronmk
CREATE FUNCTION _label(label text, value text) RETURNS text
305 4682 aaronmk
    LANGUAGE sql IMMUTABLE
306
    AS $_$
307
SELECT coalesce($1 || ': ', '') || $2
308
$_$;
309 2596 aaronmk
310
311
--
312 8825 aaronmk
-- Name: _lowercase(text); Type: FUNCTION; Schema: util; Owner: -
313
--
314
315
CREATE FUNCTION _lowercase(value text) RETURNS text
316 10388 aaronmk
    LANGUAGE sql IMMUTABLE
317 8825 aaronmk
    AS $_$
318
SELECT lower($1)
319
$_$;
320
321
322
--
323 11667 aaronmk
-- 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 8183 aaronmk
-- Name: _map(hstore, text); Type: FUNCTION; Schema: util; Owner: -
339 6222 aaronmk
--
340
341
CREATE FUNCTION _map(map hstore, value text) RETURNS text
342 7820 aaronmk
    LANGUAGE plpgsql IMMUTABLE STRICT
343 6222 aaronmk
    AS $$
344
DECLARE
345 6271 aaronmk
    match text := map -> value;
346 6222 aaronmk
BEGIN
347 6271 aaronmk
    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 6243 aaronmk
    IF match = '!' THEN RAISE 'Value not in map: %', value USING ERRCODE = 'data_exception';
355
    ELSIF match = '*' THEN RETURN value;
356
    ELSE RETURN match;
357 6222 aaronmk
    END IF;
358
END;
359
$$;
360
361
362
--
363 8183 aaronmk
-- Name: _max(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
364 5408 aaronmk
--
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 7289 aaronmk
SELECT GREATEST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
370 5408 aaronmk
$_$;
371
372
373
--
374 8183 aaronmk
-- Name: _merge(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
375 2940 aaronmk
--
376
377 4150 aaronmk
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 2940 aaronmk
    LANGUAGE sql IMMUTABLE
379
    AS $_$
380 8183 aaronmk
SELECT util.join_strs(value, '; ')
381 2940 aaronmk
FROM
382
(
383
    SELECT *
384
    FROM
385
    (
386
        SELECT
387
        DISTINCT ON (value)
388
        *
389
        FROM
390
        (VALUES
391 4012 aaronmk
              (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 2940 aaronmk
        )
402
        AS v (sort_order, value)
403 4011 aaronmk
        WHERE value IS NOT NULL
404 2940 aaronmk
    )
405
    AS v
406
    ORDER BY sort_order
407
)
408
AS v
409
$_$;
410
411
412
--
413 8183 aaronmk
-- Name: _merge_prefix(text, text); Type: FUNCTION; Schema: util; Owner: -
414 7140 aaronmk
--
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 8183 aaronmk
-- Name: _merge_words(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
425 6354 aaronmk
--
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 8183 aaronmk
SELECT util.join_strs(value, ' ')
431 6354 aaronmk
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 8183 aaronmk
-- Name: _min(anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
464 5408 aaronmk
--
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 7289 aaronmk
SELECT LEAST($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
470 5408 aaronmk
$_$;
471
472
473
--
474 8183 aaronmk
-- Name: _not(boolean); Type: FUNCTION; Schema: util; Owner: -
475 6316 aaronmk
--
476
477
CREATE FUNCTION _not(value boolean) RETURNS boolean
478 10388 aaronmk
    LANGUAGE sql IMMUTABLE
479 6316 aaronmk
    AS $_$
480
SELECT NOT $1
481
$_$;
482
483
484
--
485 8183 aaronmk
-- Name: _now(); Type: FUNCTION; Schema: util; Owner: -
486 7104 aaronmk
--
487
488
CREATE FUNCTION _now() RETURNS timestamp with time zone
489
    LANGUAGE sql STABLE
490
    AS $$
491
SELECT now()
492
$$;
493
494
495
--
496 11667 aaronmk
-- 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 8183 aaronmk
-- Name: _nullIf(anyelement, text, datatype); Type: FUNCTION; Schema: util; Owner: -
508 2949 aaronmk
--
509
510 4475 aaronmk
CREATE FUNCTION "_nullIf"(value anyelement, "null" text, type datatype DEFAULT 'str'::datatype) RETURNS anyelement
511 2949 aaronmk
    LANGUAGE plpgsql IMMUTABLE
512
    AS $$
513
DECLARE
514 8183 aaronmk
    type util.datatype NOT NULL := type; -- add NOT NULL
515 2949 aaronmk
BEGIN
516 4475 aaronmk
    IF type = 'str' THEN RETURN nullif(value::text, "null");
517 2722 aaronmk
    -- Invalid value is ignored, but invalid null value generates error
518 2949 aaronmk
    ELSIF type = 'float' THEN
519 2722 aaronmk
        DECLARE
520
            -- Outside the try block so that invalid null value generates error
521 2949 aaronmk
            "null" double precision := "null"::double precision;
522 2722 aaronmk
        BEGIN
523 2949 aaronmk
            RETURN nullif(value::double precision, "null");
524 2722 aaronmk
        EXCEPTION
525 2949 aaronmk
            WHEN data_exception THEN RETURN value; -- ignore invalid value
526 2722 aaronmk
        END;
527 2610 aaronmk
    END IF;
528
END;
529
$$;
530
531
532
--
533 8183 aaronmk
-- Name: _or(boolean, boolean, boolean, boolean, boolean); Type: FUNCTION; Schema: util; Owner: -
534 6355 aaronmk
--
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 8183 aaronmk
-- Name: FUNCTION _or("0" boolean, "1" boolean, "2" boolean, "3" boolean, "4" boolean); Type: COMMENT; Schema: util; Owner: -
554 6437 aaronmk
--
555
556 12235 aaronmk
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 6437 aaronmk
560
561
--
562 8183 aaronmk
-- Name: _range(double precision, double precision); Type: FUNCTION; Schema: util; Owner: -
563 7706 aaronmk
--
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 8183 aaronmk
-- Name: _split(text, text); Type: FUNCTION; Schema: util; Owner: -
574 6793 aaronmk
--
575
576
CREATE FUNCTION _split(value text DEFAULT NULL::text, separator text DEFAULT '; '::text) RETURNS SETOF text
577 10388 aaronmk
    LANGUAGE sql IMMUTABLE
578 6793 aaronmk
    AS $_$
579
SELECT regexp_split_to_table($1, $2)
580
$_$;
581
582
583
--
584 10594 aaronmk
-- 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 12446 aaronmk
    LANGUAGE sql STABLE
589 10594 aaronmk
    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 12235 aaronmk
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 10594 aaronmk
609
610
--
611 9959 aaronmk
-- 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 12320 aaronmk
-- 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 12369 aaronmk
-- Name: append_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
651
--
652
653
CREATE FUNCTION append_comment(table_ regclass, comment text) RETURNS void
654 12446 aaronmk
    LANGUAGE sql
655 12369 aaronmk
    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 10305 aaronmk
-- 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 10303 aaronmk
-- Name: array_length(anyarray); Type: FUNCTION; Schema: util; Owner: -
682
--
683
684
CREATE FUNCTION array_length("array" anyarray) RETURNS integer
685 10354 aaronmk
    LANGUAGE sql IMMUTABLE
686 10303 aaronmk
    AS $_$
687 10321 aaronmk
SELECT util.array_length($1, 1)
688 10303 aaronmk
$_$;
689
690
691
--
692 10304 aaronmk
-- Name: array_length(anyarray, integer); Type: FUNCTION; Schema: util; Owner: -
693
--
694
695
CREATE FUNCTION array_length("array" anyarray, dimension integer) RETURNS integer
696 10354 aaronmk
    LANGUAGE sql IMMUTABLE
697 10304 aaronmk
    AS $_$
698 10354 aaronmk
SELECT CASE WHEN $1 IS NULL THEN NULL ELSE COALESCE(pg_catalog.array_length($1, $2), 0) END
699 10304 aaronmk
$_$;
700
701
702
--
703
-- Name: FUNCTION array_length("array" anyarray, dimension integer); Type: COMMENT; Schema: util; Owner: -
704
--
705
706 12235 aaronmk
COMMENT ON FUNCTION array_length("array" anyarray, dimension integer) IS '
707
returns 0 instead of NULL for empty arrays
708
';
709 10304 aaronmk
710
711
--
712 13453 aaronmk
-- 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 13489 aaronmk
SELECT array(SELECT elem FROM util.in_reverse($1) elem)
719 13453 aaronmk
$_$;
720
721
722
--
723 12662 aaronmk
-- Name: auto_rm_freq(regclass[], text); Type: FUNCTION; Schema: util; Owner: -
724 12655 aaronmk
--
725
726 12662 aaronmk
CREATE FUNCTION auto_rm_freq(tables regclass[], freq_col text DEFAULT 'copies'::text) RETURNS void
727 12655 aaronmk
    LANGUAGE sql
728
    AS $_$
729 12662 aaronmk
SELECT CASE WHEN util.freq_always_1($1, $2)
730 13097 aaronmk
THEN util.rm_freq($1, $2)
731 12685 aaronmk
ELSE util.try_create($$ALTER TABLE $$||util.parent($1[1])||$$ ADD COLUMN $$||quote_ident($2)||$$ bigint NOT NULL$$)
732 12662 aaronmk
END
733 12655 aaronmk
$_$;
734
735
736
--
737 13134 aaronmk
-- 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 8183 aaronmk
-- Name: cluster_index(regclass); Type: FUNCTION; Schema: util; Owner: -
772 8104 aaronmk
--
773
774
CREATE FUNCTION cluster_index(table_ regclass) RETURNS regclass
775 12446 aaronmk
    LANGUAGE sql STABLE
776 8104 aaronmk
    AS $_$
777
SELECT indexrelid FROM pg_index WHERE indrelid = $1 AND indisclustered
778
$_$;
779
780
781
--
782 8183 aaronmk
-- Name: cluster_once(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
783 8105 aaronmk
--
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 8183 aaronmk
    IF ARRAY[util.cluster_index(table_)] != ARRAY[index] THEN
791 8105 aaronmk
        EXECUTE $$CLUSTER $$||table_||$$ USING $$||index;
792
    END IF;
793
END;
794
$_$;
795
796
797
--
798 8183 aaronmk
-- Name: FUNCTION cluster_once(table_ regclass, index regclass); Type: COMMENT; Schema: util; Owner: -
799 8105 aaronmk
--
800
801 12235 aaronmk
COMMENT ON FUNCTION cluster_once(table_ regclass, index regclass) IS '
802
idempotent
803
';
804 8105 aaronmk
805
806
--
807 12683 aaronmk
-- 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 10986 aaronmk
-- 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 10136 aaronmk
-- 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 10130 aaronmk
-- 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 10134 aaronmk
-- 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 12235 aaronmk
COMMENT ON FUNCTION col_default_value(col col_ref, ret_type_null anyelement) IS '
910
ret_type_null: NULL::ret_type
911
';
912 10134 aaronmk
913
914
--
915 8183 aaronmk
-- Name: col_exists(col_ref); Type: FUNCTION; Schema: util; Owner: -
916 8180 aaronmk
--
917
918
CREATE FUNCTION col_exists(col col_ref) RETURNS boolean
919
    LANGUAGE plpgsql STRICT
920
    AS $$
921
BEGIN
922 8183 aaronmk
    PERFORM util.col_type(col);
923 8180 aaronmk
    RETURN true;
924
EXCEPTION
925
    WHEN undefined_column THEN RETURN false;
926
END;
927
$$;
928
929
930
--
931 8183 aaronmk
-- Name: col_global_names(regtype); Type: FUNCTION; Schema: util; Owner: -
932 8084 aaronmk
--
933
934
CREATE FUNCTION col_global_names(type regtype, OUT name text, OUT global_name text) RETURNS SETOF record
935 8097 aaronmk
    LANGUAGE plpgsql STABLE STRICT
936 8084 aaronmk
    AS $$
937
DECLARE
938 8183 aaronmk
    prefix text := util.name(type)||'.';
939 8084 aaronmk
BEGIN
940
    RETURN QUERY
941 8183 aaronmk
        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 8084 aaronmk
END;
944
$$;
945
946
947
--
948 8183 aaronmk
-- Name: col_names(regclass); Type: FUNCTION; Schema: util; Owner: -
949 8151 aaronmk
--
950
951
CREATE FUNCTION col_names(table_ regclass) RETURNS SETOF text
952 12446 aaronmk
    LANGUAGE sql STABLE
953 8151 aaronmk
    AS $_$
954
SELECT attname::text
955
FROM pg_attribute
956 10158 aaronmk
WHERE attrelid = $1 AND attnum >= 1 AND NOT attisdropped
957 8151 aaronmk
ORDER BY attnum
958
$_$;
959
960
961
--
962 11667 aaronmk
-- 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 8183 aaronmk
-- Name: col_type(col_ref); Type: FUNCTION; Schema: util; Owner: -
976 8106 aaronmk
--
977
978
CREATE FUNCTION col_type(col col_ref) RETURNS regtype
979 8169 aaronmk
    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 8171 aaronmk
    WHEN no_data_found THEN
991 8181 aaronmk
        RAISE undefined_column USING MESSAGE =
992
            concat('undefined column: ', col.name);
993 8169 aaronmk
END;
994
$$;
995 8106 aaronmk
996
997
--
998 12368 aaronmk
-- Name: comment(oid); Type: FUNCTION; Schema: util; Owner: -
999
--
1000
1001
CREATE FUNCTION comment(element oid) RETURNS text
1002 12446 aaronmk
    LANGUAGE sql STABLE
1003 12368 aaronmk
    AS $_$
1004
SELECT description FROM pg_description WHERE objoid = $1
1005
$_$;
1006
1007
1008
--
1009 11005 aaronmk
-- 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 8183 aaronmk
-- Name: contains(text, text); Type: FUNCTION; Schema: util; Owner: -
1021 8095 aaronmk
--
1022
1023
CREATE FUNCTION contains(search_for text, in_str text) RETURNS boolean
1024 10388 aaronmk
    LANGUAGE sql IMMUTABLE
1025 8095 aaronmk
    AS $_$
1026
SELECT position($1 in $2) > 0 /*1-based offset*/
1027
$_$;
1028
1029
1030
--
1031 12288 aaronmk
-- 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 12649 aaronmk
-- 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 13495 aaronmk
-- Name: create_if_not_exists(text, text); Type: FUNCTION; Schema: util; Owner: -
1054 8094 aaronmk
--
1055
1056 13495 aaronmk
CREATE FUNCTION create_if_not_exists(sql text, relation text DEFAULT NULL::text) RETURNS void
1057
    LANGUAGE plpgsql
1058 8094 aaronmk
    AS $$
1059
BEGIN
1060 13495 aaronmk
	/* 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 12595 aaronmk
	PERFORM util.eval(sql);
1064 8094 aaronmk
EXCEPTION
1065 12676 aaronmk
WHEN   duplicate_table
1066
	OR duplicate_object -- eg. constraint
1067
	OR duplicate_column
1068
	OR duplicate_function
1069
THEN NULL;
1070 12595 aaronmk
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 8094 aaronmk
END;
1075
$$;
1076
1077
1078
--
1079 13495 aaronmk
-- Name: FUNCTION create_if_not_exists(sql text, relation text); Type: COMMENT; Schema: util; Owner: -
1080 8094 aaronmk
--
1081
1082 13495 aaronmk
COMMENT ON FUNCTION create_if_not_exists(sql text, relation text) IS '
1083 12235 aaronmk
idempotent
1084
';
1085 8094 aaronmk
1086
1087
--
1088 12378 aaronmk
-- 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 12590 aaronmk
    LANGUAGE sql STABLE
1093 12378 aaronmk
    AS $$
1094
SELECT util.schema_esc(schema_anchor)||'.'||quote_ident(func)
1095
$$;
1096
1097
1098
--
1099 12668 aaronmk
-- 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 12433 aaronmk
-- Name: debug_print_return_value(anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1111 12430 aaronmk
--
1112
1113 12433 aaronmk
CREATE FUNCTION debug_print_return_value(value anyelement, encode boolean DEFAULT false) RETURNS anyelement
1114 12430 aaronmk
    LANGUAGE sql IMMUTABLE
1115
    AS $_$
1116 13447 aaronmk
SELECT util.debug_print_value('returns: ', $1, $2);
1117 12430 aaronmk
SELECT $1;
1118
$_$;
1119
1120
1121
--
1122 12250 aaronmk
-- 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 12534 aaronmk
SELECT util.raise('NOTICE', $$
1131 12474 aaronmk
$$||util.runnable_sql($1)||$$
1132 12464 aaronmk
$$||COALESCE(util.explain2notice_msg_if_can($1), ''))
1133 12250 aaronmk
$_$;
1134
1135
1136
--
1137 13446 aaronmk
-- 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 13449 aaronmk
(CASE WHEN $3 THEN util.quote_typed($2) ELSE $2::text END))||$$
1145
$$)
1146 13446 aaronmk
$_$;
1147
1148
1149
--
1150 13448 aaronmk
-- 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 10364 aaronmk
-- 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 12446 aaronmk
    LANGUAGE sql STABLE
1169 10364 aaronmk
    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 12235 aaronmk
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 10364 aaronmk
1187
1188
--
1189 12298 aaronmk
-- Name: diff(regclass, regclass, anyelement); Type: FUNCTION; Schema: util; Owner: -
1190 12044 aaronmk
--
1191
1192 12298 aaronmk
CREATE FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) RETURNS SETOF record
1193 12427 aaronmk
    LANGUAGE sql
1194 12044 aaronmk
    AS $_$
1195 12653 aaronmk
-- 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 12663 aaronmk
SELECT util.auto_rm_freq(ARRAY[$1, $2]);
1199 12653 aaronmk
1200
SELECT * FROM util.diff($1, $2, $3, has_freq := true)
1201 12298 aaronmk
$_$;
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 12653 aaronmk
1212
col_type_null (*required*): NULL::shared_base_type
1213 12298 aaronmk
';
1214
1215
1216
--
1217 12491 aaronmk
-- Name: diff(text, text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1218 12298 aaronmk
--
1219
1220 12491 aaronmk
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 12429 aaronmk
    LANGUAGE plpgsql
1222
    SET search_path TO pg_temp
1223 12298 aaronmk
    AS $_$
1224 12429 aaronmk
/* 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 12492 aaronmk
`SET LOCAL search_path` to the current function */
1229 12429 aaronmk
BEGIN
1230 12491 aaronmk
	PERFORM util.use_schema($3); -- includes util.%== as default/fallback
1231 12429 aaronmk
1232 12565 aaronmk
	PERFORM util.mk_keys_func(pg_typeof($3));
1233 12429 aaronmk
	RETURN QUERY
1234 12554 aaronmk
		SELECT * FROM util.eval2col_pair(util.mk_diff_query($1, $2,
1235 12479 aaronmk
$$/* need to explicitly cast each side to the return type because this does not
1236 12284 aaronmk
happen automatically even when an implicit cast is available */
1237 12479 aaronmk
  left_::$$||util.typeof($3)||$$
1238
, right_::$$||util.typeof($3)
1239 12496 aaronmk
/* 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 12553 aaronmk
, util._if($4, $$true/*= CROSS JOIN*/$$,
1242
$$ left_::$$||util.typeof($3)||$$
1243 12496 aaronmk
%== right_::$$||util.typeof($3)||$$
1244
	-- refer to EXPLAIN output for expansion of %==$$
1245 12657 aaronmk
)
1246
,     $$         left_::$$||util.typeof($3)||$$
1247
IS DISTINCT FROM right_::$$||util.typeof($3)
1248
), $3)
1249 12429 aaronmk
	;
1250
END;
1251 12044 aaronmk
$_$;
1252
1253
1254
--
1255 12491 aaronmk
-- 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 12280 aaronmk
--
1257
1258 12491 aaronmk
COMMENT ON FUNCTION diff(left__ text, right__ text, col_type_null anyelement, single_row boolean, OUT left_ anyelement, OUT right_ anyelement) IS '
1259 12280 aaronmk
col_type_null (*required*): NULL::col_type
1260 12299 aaronmk
single_row: whether the tables consist of a single row, which should be
1261
	displayed side-by-side
1262 12282 aaronmk
1263 12498 aaronmk
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 12282 aaronmk
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 12280 aaronmk
';
1280
1281
1282
--
1283 12653 aaronmk
-- 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 8200 aaronmk
-- 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 12446 aaronmk
    LANGUAGE sql
1314 8200 aaronmk
    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 12235 aaronmk
COMMENT ON FUNCTION do_optionally_ignore(sql text, ignore boolean) IS '
1324
idempotent
1325
';
1326 8200 aaronmk
1327
1328
--
1329 12292 aaronmk
-- Name: drop_column(col_ref, boolean); Type: FUNCTION; Schema: util; Owner: -
1330 10362 aaronmk
--
1331
1332 12292 aaronmk
CREATE FUNCTION drop_column(col col_ref, force boolean DEFAULT false) RETURNS void
1333 12446 aaronmk
    LANGUAGE sql
1334 10362 aaronmk
    AS $_$
1335
SELECT util.eval($$ALTER TABLE $$||$1.table_||$$ DROP COLUMN IF EXISTS $$||
1336 12292 aaronmk
quote_ident($1.name)||util._if($2, $$ CASCADE$$, ''::text))
1337 10362 aaronmk
$_$;
1338
1339
1340
--
1341 12292 aaronmk
-- Name: FUNCTION drop_column(col col_ref, force boolean); Type: COMMENT; Schema: util; Owner: -
1342 10362 aaronmk
--
1343
1344 12292 aaronmk
COMMENT ON FUNCTION drop_column(col col_ref, force boolean) IS '
1345 12235 aaronmk
idempotent
1346
';
1347 10362 aaronmk
1348
1349
--
1350 12660 aaronmk
-- 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 12686 aaronmk
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 12660 aaronmk
$_$;
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 12587 aaronmk
-- Name: drop_relation(anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1372 12331 aaronmk
--
1373
1374 12587 aaronmk
CREATE FUNCTION drop_relation(relation anyelement, force boolean DEFAULT false) RETURNS void
1375 12331 aaronmk
    LANGUAGE sql
1376
    AS $_$
1377 12353 aaronmk
/* 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 12331 aaronmk
$_$;
1381
1382
1383
--
1384 12343 aaronmk
-- Name: drop_relation(text, text, boolean); Type: FUNCTION; Schema: util; Owner: -
1385
--
1386
1387 12364 aaronmk
CREATE FUNCTION drop_relation(type text, relation_esc text, force boolean DEFAULT false) RETURNS void
1388 12343 aaronmk
    LANGUAGE sql
1389
    AS $_$
1390 12347 aaronmk
SELECT util.eval($$DROP $$||$1||$$ IF EXISTS $$||$2
1391 12343 aaronmk
||util._if($3, $$ CASCADE$$, ''::text))
1392
$_$;
1393
1394
1395
--
1396 12364 aaronmk
-- Name: FUNCTION drop_relation(type text, relation_esc text, force boolean); Type: COMMENT; Schema: util; Owner: -
1397 12343 aaronmk
--
1398
1399 12364 aaronmk
COMMENT ON FUNCTION drop_relation(type text, relation_esc text, force boolean) IS '
1400 12343 aaronmk
idempotent
1401
';
1402
1403
1404
--
1405 12413 aaronmk
-- 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 12502 aaronmk
SELECT util.drop_relations_like($1, util.schema_regexp($2), $3)
1412 12413 aaronmk
$_$;
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 12668 aaronmk
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 12667 aaronmk
;
1426 12413 aaronmk
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 12292 aaronmk
-- Name: drop_table(text, boolean); Type: FUNCTION; Schema: util; Owner: -
1435 10150 aaronmk
--
1436
1437 12292 aaronmk
CREATE FUNCTION drop_table(table_ text, force boolean DEFAULT false) RETURNS void
1438 12446 aaronmk
    LANGUAGE sql
1439 10150 aaronmk
    AS $_$
1440 12347 aaronmk
SELECT util.drop_relation('TABLE', $1, $2)
1441 10150 aaronmk
$_$;
1442
1443
1444
--
1445 12292 aaronmk
-- Name: FUNCTION drop_table(table_ text, force boolean); Type: COMMENT; Schema: util; Owner: -
1446 10150 aaronmk
--
1447
1448 12292 aaronmk
COMMENT ON FUNCTION drop_table(table_ text, force boolean) IS '
1449 12235 aaronmk
idempotent
1450
';
1451 10150 aaronmk
1452
1453
--
1454 12292 aaronmk
-- Name: drop_view(text, boolean); Type: FUNCTION; Schema: util; Owner: -
1455 12229 aaronmk
--
1456
1457 12292 aaronmk
CREATE FUNCTION drop_view(view_ text, force boolean DEFAULT false) RETURNS void
1458 12446 aaronmk
    LANGUAGE sql
1459 12229 aaronmk
    AS $_$
1460 12347 aaronmk
SELECT util.drop_relation('VIEW', $1, $2)
1461 12229 aaronmk
$_$;
1462
1463
1464
--
1465 12292 aaronmk
-- Name: FUNCTION drop_view(view_ text, force boolean); Type: COMMENT; Schema: util; Owner: -
1466 12229 aaronmk
--
1467
1468 12292 aaronmk
COMMENT ON FUNCTION drop_view(view_ text, force boolean) IS '
1469 12235 aaronmk
idempotent
1470
';
1471 12229 aaronmk
1472
1473
--
1474 10322 aaronmk
-- 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 12235 aaronmk
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 10322 aaronmk
1492
1493
--
1494 8183 aaronmk
-- Name: ensure_prefix(text, text); Type: FUNCTION; Schema: util; Owner: -
1495 8086 aaronmk
--
1496
1497
CREATE FUNCTION ensure_prefix(prefix text, str text) RETURNS text
1498 10388 aaronmk
    LANGUAGE sql IMMUTABLE
1499 8086 aaronmk
    AS $_$
1500 8183 aaronmk
SELECT (CASE WHEN util.has_prefix($1, $2) THEN $2 ELSE $1||$2 END)
1501 8086 aaronmk
$_$;
1502
1503
1504
--
1505 10987 aaronmk
-- 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 13455 aaronmk
-- 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 12531 aaronmk
-- Name: eval(text, boolean); Type: FUNCTION; Schema: util; Owner: -
1529 9824 aaronmk
--
1530
1531 12531 aaronmk
CREATE FUNCTION eval(sql text, verbose_ boolean DEFAULT true) RETURNS void
1532 12558 aaronmk
    LANGUAGE plpgsql
1533 9824 aaronmk
    AS $$
1534
BEGIN
1535 12531 aaronmk
	IF verbose_ THEN PERFORM util.debug_print_sql(sql); END IF;
1536 12251 aaronmk
	EXECUTE sql;
1537 9824 aaronmk
END;
1538
$$;
1539
1540
1541
--
1542 12181 aaronmk
-- 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 12251 aaronmk
	PERFORM util.debug_print_sql(sql);
1550 12181 aaronmk
	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 12235 aaronmk
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 12181 aaronmk
1563
1564
--
1565 12301 aaronmk
-- 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 12458 aaronmk
-- Name: eval2set(text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1580 10363 aaronmk
--
1581
1582 12458 aaronmk
CREATE FUNCTION eval2set(sql text, ret_type_null anyelement DEFAULT NULL::text, verbose_ boolean DEFAULT true) RETURNS SETOF anyelement
1583 10363 aaronmk
    LANGUAGE plpgsql
1584
    AS $$
1585
BEGIN
1586 12458 aaronmk
	IF verbose_ THEN PERFORM util.debug_print_sql(sql); END IF;
1587 10363 aaronmk
	RETURN QUERY EXECUTE sql;
1588
END;
1589
$$;
1590
1591
1592
--
1593 10129 aaronmk
-- Name: eval2val(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
1594 10128 aaronmk
--
1595
1596 10129 aaronmk
CREATE FUNCTION eval2val(sql text, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
1597 12514 aaronmk
    LANGUAGE plpgsql STABLE
1598 10128 aaronmk
    AS $$
1599
DECLARE
1600
	ret_val ret_type_null%TYPE;
1601
BEGIN
1602 12251 aaronmk
	PERFORM util.debug_print_sql(sql);
1603 10128 aaronmk
	EXECUTE sql INTO STRICT ret_val;
1604
	RETURN ret_val;
1605
END;
1606
$$;
1607
1608
1609
--
1610 10129 aaronmk
-- Name: FUNCTION eval2val(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
1611 10128 aaronmk
--
1612
1613 12235 aaronmk
COMMENT ON FUNCTION eval2val(sql text, ret_type_null anyelement) IS '
1614
ret_type_null: NULL::ret_type
1615
';
1616 10128 aaronmk
1617
1618
--
1619 10131 aaronmk
-- 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 10132 aaronmk
SELECT util.eval2val($$SELECT $$||$1, $2)
1626 10131 aaronmk
$_$;
1627
1628
1629
--
1630
-- Name: FUNCTION eval_expr(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
1631
--
1632
1633 12235 aaronmk
COMMENT ON FUNCTION eval_expr(sql text, ret_type_null anyelement) IS '
1634
ret_type_null: NULL::ret_type
1635
';
1636 10131 aaronmk
1637
1638
--
1639 10133 aaronmk
-- 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 12235 aaronmk
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 10133 aaronmk
1658
1659
--
1660 8183 aaronmk
-- Name: existing_cols(regclass, text[]); Type: FUNCTION; Schema: util; Owner: -
1661 8182 aaronmk
--
1662
1663
CREATE FUNCTION existing_cols(table_ regclass, VARIADIC col_names text[]) RETURNS SETOF text
1664 12446 aaronmk
    LANGUAGE sql STABLE
1665 8182 aaronmk
    AS $_$
1666
SELECT col_name
1667
FROM unnest($2) s (col_name)
1668 8183 aaronmk
WHERE util.col_exists(($1, col_name))
1669 8182 aaronmk
$_$;
1670
1671
1672
--
1673 11830 aaronmk
-- Name: explain(text); Type: FUNCTION; Schema: util; Owner: -
1674
--
1675
1676
CREATE FUNCTION explain(sql text) RETURNS SETOF text
1677
    LANGUAGE sql
1678 13496 aaronmk
    SET client_min_messages TO 'error'
1679 11830 aaronmk
    AS $_$
1680 13496 aaronmk
/* `client_min_messages = ERROR`: EXPLAIN apparently runs IMMUTABLE functions in
1681
the query, so this prevents displaying any log messages printed by them */
1682 12459 aaronmk
SELECT util.eval2set($$EXPLAIN $$||$1, verbose_ := false)
1683 11830 aaronmk
$_$;
1684
1685
1686
--
1687 11833 aaronmk
-- Name: explain2notice(text); Type: FUNCTION; Schema: util; Owner: -
1688
--
1689
1690
CREATE FUNCTION explain2notice(sql text) RETURNS void
1691 12449 aaronmk
    LANGUAGE sql
1692
    AS $_$
1693 12534 aaronmk
SELECT util.raise('NOTICE', util.explain2notice_msg($1))
1694 12449 aaronmk
$_$;
1695 12448 aaronmk
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 12464 aaronmk
-- newline before and after to visually separate it from other debug info
1705 12756 aaronmk
SELECT COALESCE($$
1706 12464 aaronmk
EXPLAIN:
1707 12756 aaronmk
$$||util.fold_explain_msg(util.explain2str($1))||$$
1708
$$, '')
1709 11833 aaronmk
$_$;
1710
1711
1712
--
1713 12452 aaronmk
-- 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 13403 aaronmk
    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 12452 aaronmk
1729
1730
--
1731 11832 aaronmk
-- 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 11835 aaronmk
SET default_tablespace = '';
1743
1744
SET default_with_oids = false;
1745
1746 11832 aaronmk
--
1747 11835 aaronmk
-- Name: explain; Type: TABLE; Schema: util; Owner: -; Tablespace:
1748 11831 aaronmk
--
1749
1750 11835 aaronmk
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 11831 aaronmk
    LANGUAGE sql
1761
    AS $_$
1762 11835 aaronmk
SELECT util.eval($$INSERT INTO $$||$2||$$ SELECT util.explain(
1763
$$||quote_nullable($1)||$$
1764 11831 aaronmk
)$$)
1765
$_$;
1766
1767
1768
--
1769 11836 aaronmk
-- Name: FUNCTION explain2table(sql text, table_ regclass); Type: COMMENT; Schema: util; Owner: -
1770
--
1771
1772 12235 aaronmk
COMMENT ON FUNCTION explain2table(sql text, table_ regclass) IS '
1773
usage:
1774 11836 aaronmk
PERFORM util.explain2table($$
1775
query
1776 12235 aaronmk
$$);
1777
';
1778 11836 aaronmk
1779
1780
--
1781 12450 aaronmk
-- 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 12461 aaronmk
SELECT match[1] FROM regexp_matches(util.ltrim_nl($1), '^(\S*)') match
1788 12450 aaronmk
$_$;
1789
1790
1791
--
1792 10323 aaronmk
-- Name: fix_array(anyarray); Type: FUNCTION; Schema: util; Owner: -
1793
--
1794
1795
CREATE FUNCTION fix_array("array" anyarray) RETURNS anyarray
1796 10355 aaronmk
    LANGUAGE sql IMMUTABLE
1797 10323 aaronmk
    AS $_$
1798 10355 aaronmk
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 10323 aaronmk
$_$;
1802
1803
1804
--
1805
-- Name: FUNCTION fix_array("array" anyarray); Type: COMMENT; Schema: util; Owner: -
1806
--
1807
1808 12235 aaronmk
COMMENT ON FUNCTION fix_array("array" anyarray) IS '
1809
ensures that an array will always have proper non-NULL dimensions
1810
';
1811 10323 aaronmk
1812
1813
--
1814 12755 aaronmk
-- 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 8321 aaronmk
-- 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 8323 aaronmk
	IF SQLERRM = 'cannot drop columns from view'
1839
	OR SQLERRM LIKE 'cannot change name of view column "%" to "%"'
1840
	THEN
1841 8321 aaronmk
		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 12235 aaronmk
COMMENT ON FUNCTION force_update_view(view_ text, query text) IS '
1854
idempotent
1855
';
1856 8321 aaronmk
1857
1858
--
1859 12654 aaronmk
-- 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 12661 aaronmk
-- 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 11655 aaronmk
-- 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 8183 aaronmk
-- Name: has_prefix(text, text); Type: FUNCTION; Schema: util; Owner: -
1900 8085 aaronmk
--
1901
1902
CREATE FUNCTION has_prefix(prefix text, str text) RETURNS boolean
1903 10388 aaronmk
    LANGUAGE sql IMMUTABLE
1904 8085 aaronmk
    AS $_$
1905
SELECT substring($2 for length($1)) = $1
1906
$_$;
1907
1908
1909
--
1910 12296 aaronmk
-- 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 10307 aaronmk
-- 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 10324 aaronmk
SELECT hstore(util.fix_array($1), util.array_fill($2, util.array_length($1)))
1928 10307 aaronmk
$_$;
1929
1930
1931
--
1932
-- Name: FUNCTION hstore(keys text[], value text); Type: COMMENT; Schema: util; Owner: -
1933
--
1934
1935 12235 aaronmk
COMMENT ON FUNCTION hstore(keys text[], value text) IS '
1936
avoids repeating the same value for each key
1937
';
1938 10307 aaronmk
1939
1940
--
1941 12218 aaronmk
-- 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 12222 aaronmk
SELECT COALESCE($1, $2)
1948 12218 aaronmk
$_$;
1949
1950
1951
--
1952
-- Name: FUNCTION ifnull(value anyelement, null_ anyelement); Type: COMMENT; Schema: util; Owner: -
1953
--
1954
1955 12235 aaronmk
COMMENT ON FUNCTION ifnull(value anyelement, null_ anyelement) IS '
1956
equivalent to MySQL''s IFNULL() (Postgres auto-lowercases the name)
1957
';
1958 12218 aaronmk
1959
1960
--
1961 13452 aaronmk
-- 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 13490 aaronmk
SELECT elem FROM unnest($1) elem ORDER BY row_number() OVER () DESC
1968 13452 aaronmk
$_$;
1969
1970
1971
--
1972 12285 aaronmk
-- 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 13136 aaronmk
-- 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 13139 aaronmk
	-- 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 13136 aaronmk
	RETURN true;
1994
EXCEPTION
1995 13493 aaronmk
WHEN   data_exception
1996
	OR syntax_error_or_access_rule_violation -- eg. ::regclass
1997
	THEN
1998
	RETURN false;
1999 13136 aaronmk
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 13139 aaronmk
passes NULL through. however, if NULL is not valid for the type, false will be
2009
returned instead.
2010
2011 13136 aaronmk
ret_type_null: NULL::ret_type
2012
';
2013
2014
2015
--
2016 10137 aaronmk
-- Name: is_constant(col_ref); Type: FUNCTION; Schema: util; Owner: -
2017
--
2018
2019
CREATE FUNCTION is_constant(col col_ref) RETURNS boolean
2020 12446 aaronmk
    LANGUAGE sql STABLE
2021 10137 aaronmk
    AS $_$
2022 12789 aaronmk
SELECT COALESCE(util.col_comment($1) LIKE '
2023
constant
2024
%', false)
2025 10137 aaronmk
$_$;
2026
2027
2028
--
2029 11659 aaronmk
-- 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 12457 aaronmk
-- 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 12451 aaronmk
-- 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 10391 aaronmk
-- 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 10613 aaronmk
-- 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 12480 aaronmk
-- 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 12330 aaronmk
-- Name: is_table(regclass); Type: FUNCTION; Schema: util; Owner: -
2099
--
2100
2101
CREATE FUNCTION is_table(relation regclass) RETURNS boolean
2102 12332 aaronmk
    LANGUAGE sql STABLE
2103 12330 aaronmk
    AS $_$
2104
SELECT relkind = 'r' FROM pg_class WHERE oid = $1
2105
$_$;
2106
2107
2108
--
2109 12329 aaronmk
-- Name: is_view(regclass); Type: FUNCTION; Schema: util; Owner: -
2110
--
2111
2112
CREATE FUNCTION is_view(relation regclass) RETURNS boolean
2113 12332 aaronmk
    LANGUAGE sql STABLE
2114 12329 aaronmk
    AS $_$
2115
SELECT relkind = 'v' FROM pg_class WHERE oid = $1
2116
$_$;
2117
2118
2119
--
2120 8183 aaronmk
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: util; Owner: -
2121 4009 aaronmk
--
2122
2123 4053 aaronmk
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
2124 12444 aaronmk
    LANGUAGE sql IMMUTABLE STRICT
2125 4009 aaronmk
    AS $_$
2126 4054 aaronmk
SELECT $1 || $3 || $2
2127 2595 aaronmk
$_$;
2128
2129
2130
--
2131 12444 aaronmk
-- 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 12436 aaronmk
-- 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 10989 aaronmk
-- Name: limit2row_num(integer, integer, integer); Type: FUNCTION; Schema: util; Owner: -
2152 10985 aaronmk
--
2153
2154 10989 aaronmk
CREATE FUNCTION limit2row_num(limit_ integer, offset_ integer DEFAULT NULL::integer, min_row_num integer DEFAULT 1) RETURNS integer
2155 10985 aaronmk
    LANGUAGE sql IMMUTABLE
2156
    AS $_$
2157 10989 aaronmk
SELECT COALESCE(util.offset2row_num($2, $3) + $1 - 1, 2147483647)
2158 10985 aaronmk
$_$;
2159
2160
2161
--
2162 13384 aaronmk
-- 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 12275 aaronmk
-- 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 10110 aaronmk
-- 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 8183 aaronmk
-- Name: map_get(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2219 8146 aaronmk
--
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 8149 aaronmk
        INTO value USING key;
2229 8146 aaronmk
    RETURN value;
2230
END;
2231
$_$;
2232
2233
2234
--
2235 10358 aaronmk
-- Name: map_nulls(text[], anyelement); Type: FUNCTION; Schema: util; Owner: -
2236 10325 aaronmk
--
2237
2238 10358 aaronmk
CREATE FUNCTION map_nulls(nulls text[], value anyelement) RETURNS anyelement
2239 10353 aaronmk
    LANGUAGE sql IMMUTABLE
2240 10325 aaronmk
    AS $_$
2241 10374 aaronmk
SELECT util._map(util.nulls_map($1), $2)
2242 10325 aaronmk
$_$;
2243
2244
2245
--
2246 10359 aaronmk
-- Name: FUNCTION map_nulls(nulls text[], value anyelement); Type: COMMENT; Schema: util; Owner: -
2247
--
2248
2249 12235 aaronmk
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 10359 aaronmk
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 10375 aaronmk
both take ~920 ms.
2258 12235 aaronmk
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 10359 aaronmk
2261
2262
--
2263 8183 aaronmk
-- Name: map_values(regclass); Type: FUNCTION; Schema: util; Owner: -
2264 8150 aaronmk
--
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 12228 aaronmk
-- Name: materialize_query(text, text); Type: FUNCTION; Schema: util; Owner: -
2277
--
2278
2279 12262 aaronmk
CREATE FUNCTION materialize_query(table_esc text, sql text) RETURNS void
2280 12228 aaronmk
    LANGUAGE sql
2281
    AS $_$
2282 12262 aaronmk
SELECT util.create_if_not_exists($$CREATE TABLE $$||$1||$$ AS
2283 12321 aaronmk
$$||util.ltrim_nl($2));
2284
-- make sure the created table has the correct estimated row count
2285
SELECT util.analyze_($1);
2286 12470 aaronmk
2287
SELECT util.append_comment($1, '
2288
contents generated from:
2289 13397 aaronmk
'||util.ltrim_nl(util.runnable_sql($2))||';
2290 12470 aaronmk
');
2291 12228 aaronmk
$_$;
2292
2293
2294
--
2295 12262 aaronmk
-- Name: FUNCTION materialize_query(table_esc text, sql text); Type: COMMENT; Schema: util; Owner: -
2296 12228 aaronmk
--
2297
2298 12262 aaronmk
COMMENT ON FUNCTION materialize_query(table_esc text, sql text) IS '
2299 12235 aaronmk
idempotent
2300
';
2301 12228 aaronmk
2302
2303
--
2304 12234 aaronmk
-- Name: materialize_view(text, regclass); Type: FUNCTION; Schema: util; Owner: -
2305
--
2306
2307 12262 aaronmk
CREATE FUNCTION materialize_view(table_esc text, view_ regclass) RETURNS void
2308 12234 aaronmk
    LANGUAGE sql
2309
    AS $_$
2310
SELECT util.materialize_query($1, $$SELECT * FROM $$||$2)
2311
$_$;
2312
2313
2314
--
2315 12262 aaronmk
-- Name: FUNCTION materialize_view(table_esc text, view_ regclass); Type: COMMENT; Schema: util; Owner: -
2316 12234 aaronmk
--
2317
2318 12262 aaronmk
COMMENT ON FUNCTION materialize_view(table_esc text, view_ regclass) IS '
2319 12235 aaronmk
idempotent
2320
';
2321 12234 aaronmk
2322
2323
--
2324 8190 aaronmk
-- 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 12446 aaronmk
    LANGUAGE sql
2329 8190 aaronmk
    AS $_$
2330 10135 aaronmk
SELECT util.create_if_not_exists($$
2331
ALTER TABLE $$||$1.table_||$$ ADD COLUMN $$
2332 8190 aaronmk
||quote_ident($1.name)||$$ $$||pg_typeof($2)||util.type_qual($2)||$$ DEFAULT $$
2333 10135 aaronmk
||quote_literal($2)||$$;
2334 12235 aaronmk
COMMENT ON COLUMN $$||$1.table_||$$.$$||quote_ident($1.name)||$$ IS '
2335
constant
2336
';
2337 10135 aaronmk
$$)
2338 8190 aaronmk
$_$;
2339
2340
2341
--
2342
-- Name: FUNCTION mk_const_col(col col_ref, value anyelement); Type: COMMENT; Schema: util; Owner: -
2343
--
2344
2345 12235 aaronmk
COMMENT ON FUNCTION mk_const_col(col col_ref, value anyelement) IS '
2346
idempotent
2347
';
2348 8190 aaronmk
2349
2350
--
2351 10296 aaronmk
-- Name: mk_derived_col(col_ref, text, boolean); Type: FUNCTION; Schema: util; Owner: -
2352 8187 aaronmk
--
2353
2354 10296 aaronmk
CREATE FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean DEFAULT false) RETURNS void
2355 8187 aaronmk
    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 10296 aaronmk
    PERFORM util.create_if_not_exists((CASE WHEN overwrite THEN '' ELSE $$
2362
ALTER TABLE $$||col.table_||$$ ADD   COLUMN $$||col_name_sql||$$      $$||type||$$;$$ END)||$$
2363 8187 aaronmk
ALTER TABLE $$||col.table_||$$ ALTER COLUMN $$||col_name_sql||$$ TYPE $$||type||$$ USING
2364
$$||expr||$$;
2365
$$);
2366
END;
2367
$_$;
2368
2369
2370
--
2371 10296 aaronmk
-- Name: FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean); Type: COMMENT; Schema: util; Owner: -
2372 8188 aaronmk
--
2373
2374 12235 aaronmk
COMMENT ON FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean) IS '
2375
idempotent
2376
';
2377 8188 aaronmk
2378
2379
--
2380 12554 aaronmk
-- Name: mk_diff_query(text, text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
2381 12475 aaronmk
--
2382
2383 12554 aaronmk
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 12475 aaronmk
    LANGUAGE sql IMMUTABLE
2385
    AS $_$
2386
SELECT
2387 12478 aaronmk
$$SELECT
2388 12554 aaronmk
$$||$3||$$
2389 12555 aaronmk
FROM      $$||$1||$$ left_
2390 12554 aaronmk
FULL JOIN $$||$2||$$ right_
2391
ON $$||$4||$$
2392
WHERE $$||$5||$$
2393 12475 aaronmk
ORDER BY left_, right_
2394
$$
2395
$_$;
2396
2397
2398
--
2399 12564 aaronmk
-- Name: mk_keys_func(regtype); Type: FUNCTION; Schema: util; Owner: -
2400
--
2401
2402
CREATE FUNCTION mk_keys_func(type regtype) RETURNS void
2403 12591 aaronmk
    LANGUAGE sql
2404 12564 aaronmk
    AS $_$
2405 12570 aaronmk
-- keys()
2406 12564 aaronmk
SELECT util.mk_keys_func($1, ARRAY(
2407
SELECT col FROM util.typed_cols($1) col
2408
WHERE (col).type != ANY('{bigint}'::regtype[]) -- not a count col
2409 12570 aaronmk
));
2410
2411 12571 aaronmk
-- values_()
2412 12570 aaronmk
SELECT util.mk_keys_func($1, COALESCE(
2413
	NULLIF(ARRAY(
2414
	SELECT col FROM util.typed_cols($1) col
2415
	WHERE (col).type = ANY('{bigint}'::regtype[]) -- is a count col
2416
	), ARRAY[]::util.col_cast[])
2417
, ARRAY(SELECT util.typed_cols($1))) -- no count cols, so use all cols
2418 12571 aaronmk
, 'values_');
2419 12564 aaronmk
$_$;
2420
2421
2422
--
2423 12569 aaronmk
-- Name: mk_keys_func(regtype, col_cast[], text); Type: FUNCTION; Schema: util; Owner: -
2424 12561 aaronmk
--
2425
2426 12569 aaronmk
CREATE FUNCTION mk_keys_func(type regtype, cols col_cast[], name text DEFAULT 'keys'::text) RETURNS void
2427 12591 aaronmk
    LANGUAGE sql
2428 12561 aaronmk
    AS $_$
2429 12567 aaronmk
SELECT util.create_if_not_exists($$
2430 12577 aaronmk
CREATE TYPE $$||util.prefixed_name($3||'_', $1)||$$ AS
2431
($$||util.mk_typed_cols_list($2)||$$);
2432 12671 aaronmk
COMMENT ON TYPE $$||util.prefixed_name($3||'_', $1)||$$ IS '
2433
autogenerated
2434
';
2435 12594 aaronmk
$$);
2436 12577 aaronmk
2437 12594 aaronmk
SELECT util.mk_keys_func($1, util.prefixed_name($3||'_', $1)::regtype, $3);
2438
$_$;
2439
2440
2441
--
2442
-- Name: mk_keys_func(regtype, regtype, text); Type: FUNCTION; Schema: util; Owner: -
2443
--
2444
2445
CREATE FUNCTION mk_keys_func(type regtype, return_type regtype, name text DEFAULT 'keys'::text) RETURNS void
2446
    LANGUAGE sql
2447
    AS $_$
2448
SELECT util.create_if_not_exists($$
2449 12581 aaronmk
CREATE FUNCTION $$||util.qual_name(util.schema($1), $3)||$$(value $$
2450 12577 aaronmk
||util.qual_name($1)||$$)
2451 12594 aaronmk
  RETURNS $$||util.qual_name($2)||$$ AS
2452 12561 aaronmk
$BODY1$
2453 12577 aaronmk
SELECT ROW($$||
2454 12594 aaronmk
(SELECT COALESCE(string_agg($$$1.$$||quote_ident((col).col_name), ', '), '')
2455
FROM util.typed_cols($2) col) ||$$)::$$||util.qual_name($2)||$$
2456 12561 aaronmk
$BODY1$
2457
  LANGUAGE sql IMMUTABLE
2458
  COST 100;
2459 12594 aaronmk
$$);
2460 12561 aaronmk
$_$;
2461
2462
2463
--
2464 8183 aaronmk
-- Name: mk_map_table(text); Type: FUNCTION; Schema: util; Owner: -
2465 8139 aaronmk
--
2466
2467
CREATE FUNCTION mk_map_table(table_ text) RETURNS void
2468 12446 aaronmk
    LANGUAGE sql
2469 8139 aaronmk
    AS $_$
2470 8183 aaronmk
SELECT util.create_if_not_exists($$
2471 8141 aaronmk
CREATE TABLE $$||$1||$$
2472 8139 aaronmk
(
2473 8183 aaronmk
    LIKE util.map INCLUDING ALL
2474 10110 aaronmk
);
2475
2476
CREATE TRIGGER map_filter_insert
2477
  BEFORE INSERT
2478
  ON $$||$1||$$
2479
  FOR EACH ROW
2480
  EXECUTE PROCEDURE util.map_filter_insert();
2481 8141 aaronmk
$$)
2482 8139 aaronmk
$_$;
2483
2484
2485
--
2486 12725 aaronmk
-- Name: mk_not_null(text); Type: FUNCTION; Schema: util; Owner: -
2487
--
2488
2489
CREATE FUNCTION mk_not_null(text) RETURNS text
2490
    LANGUAGE sql IMMUTABLE
2491
    AS $_$
2492
SELECT COALESCE($1, '<NULL>')
2493
$_$;
2494
2495
2496
--
2497 12556 aaronmk
-- Name: mk_out_params(col_cast[]); Type: FUNCTION; Schema: util; Owner: -
2498
--
2499
2500
CREATE FUNCTION mk_out_params(cols col_cast[]) RETURNS text
2501
    LANGUAGE sql IMMUTABLE
2502
    AS $_$
2503 12559 aaronmk
SELECT COALESCE(string_agg($$, OUT $$||(unnest).col_name||$$ $$||
2504
util.qual_name((unnest).type), ''), '')
2505 12556 aaronmk
FROM unnest($1)
2506
$_$;
2507
2508
2509
--
2510 12236 aaronmk
-- Name: mk_search_path(text[]); Type: FUNCTION; Schema: util; Owner: -
2511
--
2512
2513
CREATE FUNCTION mk_search_path(VARIADIC schemas text[]) RETURNS text
2514
    LANGUAGE sql IMMUTABLE
2515
    AS $_$
2516 12486 aaronmk
SELECT string_agg(quote_ident(unnest), ', ') FROM unnest($1||'util'::text)
2517 12236 aaronmk
$_$;
2518
2519
2520
--
2521 12486 aaronmk
-- Name: FUNCTION mk_search_path(VARIADIC schemas text[]); Type: COMMENT; Schema: util; Owner: -
2522
--
2523
2524
COMMENT ON FUNCTION mk_search_path(VARIADIC schemas text[]) IS '
2525
auto-appends util to the search_path to enable use of util operators
2526
';
2527
2528
2529
--
2530 13474 aaronmk
-- Name: mk_set_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2531
--
2532
2533
CREATE FUNCTION mk_set_comment(table_ regclass, comment text) RETURNS text
2534
    LANGUAGE sql STABLE
2535
    AS $_$
2536 13483 aaronmk
SELECT COALESCE($$COMMENT ON $$||util.relation_type($1)||$$ $$||$1||$$ IS $$
2537 13481 aaronmk
||quote_literal($2)/*pass NULL through*/||$$;$$, ''/*no comment*/)
2538 13474 aaronmk
$_$;
2539
2540
2541
--
2542 13504 aaronmk
-- Name: mk_set_relation_metadata(regclass); Type: FUNCTION; Schema: util; Owner: -
2543
--
2544
2545
CREATE FUNCTION mk_set_relation_metadata(relation regclass) RETURNS text
2546
    LANGUAGE sql STABLE
2547
    AS $_$
2548
SELECT util.show_grants_for($1)
2549
||util.show_set_comment($1)||$$
2550
$$
2551
$_$;
2552
2553
2554
--
2555 12467 aaronmk
-- Name: mk_set_search_path(boolean); Type: FUNCTION; Schema: util; Owner: -
2556
--
2557
2558
CREATE FUNCTION mk_set_search_path(for_printing boolean DEFAULT false) RETURNS text
2559
    LANGUAGE sql IMMUTABLE
2560
    AS $_$
2561
SELECT util.mk_set_search_path(current_setting('search_path'), $1)
2562
$_$;
2563
2564
2565
--
2566 12466 aaronmk
-- Name: mk_set_search_path(text, boolean); Type: FUNCTION; Schema: util; Owner: -
2567 12270 aaronmk
--
2568
2569 12466 aaronmk
CREATE FUNCTION mk_set_search_path(search_path text, for_printing boolean DEFAULT false) RETURNS text
2570 12270 aaronmk
    LANGUAGE sql IMMUTABLE
2571
    AS $_$
2572 12432 aaronmk
/* debug_print_return_value() needed because this function is used with EXECUTE
2573
rather than util.eval() (in order to affect the calling function), so the
2574
search_path would not otherwise be printed */
2575 12487 aaronmk
SELECT $$SET$$||util._if($2, $$ /*LOCAL*/$$::text, $$ LOCAL$$)
2576
||$$ search_path TO $$||$1
2577 12270 aaronmk
$_$;
2578
2579
2580
--
2581 10113 aaronmk
-- Name: mk_source_col(regclass); Type: FUNCTION; Schema: util; Owner: -
2582
--
2583
2584
CREATE FUNCTION mk_source_col(table_ regclass) RETURNS void
2585 12446 aaronmk
    LANGUAGE sql
2586 10113 aaronmk
    AS $_$
2587 12240 aaronmk
SELECT util.mk_const_col(($1, 'source'), util.schema($1))
2588 10113 aaronmk
$_$;
2589
2590
2591
--
2592
-- Name: FUNCTION mk_source_col(table_ regclass); Type: COMMENT; Schema: util; Owner: -
2593
--
2594
2595 12235 aaronmk
COMMENT ON FUNCTION mk_source_col(table_ regclass) IS '
2596
idempotent
2597
';
2598 10113 aaronmk
2599
2600
--
2601 11011 aaronmk
-- Name: mk_subset_by_row_num_func(regclass); Type: FUNCTION; Schema: util; Owner: -
2602
--
2603
2604
CREATE FUNCTION mk_subset_by_row_num_func(view_ regclass) RETURNS void
2605
    LANGUAGE plpgsql STRICT
2606
    AS $_$
2607
DECLARE
2608
	view_qual_name text = util.qual_name(view_);
2609
BEGIN
2610
	EXECUTE $$
2611
CREATE OR REPLACE FUNCTION $$||view_||$$(limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
2612
  RETURNS SETOF $$||view_||$$ AS
2613
$BODY1$
2614
SELECT * FROM $$||view_qual_name||$$
2615
ORDER BY sort_col
2616
LIMIT $1 OFFSET $2
2617
$BODY1$
2618
  LANGUAGE sql STABLE
2619
  COST 100
2620
  ROWS 1000
2621
$$;
2622
2623
	PERFORM util.mk_subset_by_row_num_no_sort_func(view_);
2624
END;
2625
$_$;
2626
2627
2628
--
2629 8325 aaronmk
-- Name: mk_subset_by_row_num_func(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2630
--
2631
2632
CREATE FUNCTION mk_subset_by_row_num_func(view_ regclass, row_num_col text) RETURNS void
2633
    LANGUAGE plpgsql STRICT
2634
    AS $_$
2635 10990 aaronmk
DECLARE
2636
	view_qual_name text = util.qual_name(view_);
2637
	row_num__min__fn text = util.esc_name__append('__row_num__min', view_qual_name);
2638 8325 aaronmk
BEGIN
2639
	EXECUTE $$
2640 10990 aaronmk
CREATE OR REPLACE FUNCTION $$||row_num__min__fn||$$()
2641
  RETURNS integer AS
2642
$BODY1$
2643
SELECT $$||quote_ident(row_num_col)||$$
2644
FROM $$||view_qual_name||$$
2645
ORDER BY $$||quote_ident(row_num_col)||$$ ASC
2646
LIMIT 1
2647
$BODY1$
2648
  LANGUAGE sql STABLE
2649
  COST 100;
2650
$$;
2651
2652
	EXECUTE $$
2653 8325 aaronmk
CREATE OR REPLACE FUNCTION $$||view_||$$(limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
2654
  RETURNS SETOF $$||view_||$$ AS
2655
$BODY1$
2656 10990 aaronmk
SELECT * FROM $$||view_qual_name||$$
2657
WHERE $$||quote_ident(row_num_col)||$$ BETWEEN
2658
	util.offset2row_num(    $2, $$||row_num__min__fn||$$())
2659
AND util.limit2row_num ($1, $2, $$||row_num__min__fn||$$())
2660 10991 aaronmk
ORDER BY $$||quote_ident(row_num_col)||$$
2661 8325 aaronmk
$BODY1$
2662
  LANGUAGE sql STABLE
2663
  COST 100
2664
  ROWS 1000
2665
$$;
2666 11010 aaronmk
2667
	PERFORM util.mk_subset_by_row_num_no_sort_func(view_);
2668
END;
2669
$_$;
2670
2671
2672
--
2673
-- Name: mk_subset_by_row_num_no_sort_func(regclass); Type: FUNCTION; Schema: util; Owner: -
2674
--
2675
2676
CREATE FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass) RETURNS void
2677
    LANGUAGE plpgsql STRICT
2678
    AS $_$
2679
DECLARE
2680
	view_qual_name text = util.qual_name(view_);
2681
BEGIN
2682 8326 aaronmk
	EXECUTE $$
2683
CREATE OR REPLACE FUNCTION $$||view_||$$(no_sort boolean, limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
2684
  RETURNS SETOF $$||view_||$$
2685
  SET enable_sort TO 'off'
2686
  AS
2687
$BODY1$
2688 10990 aaronmk
SELECT * FROM $$||view_qual_name||$$($2, $3)
2689 8326 aaronmk
$BODY1$
2690
  LANGUAGE sql STABLE
2691
  COST 100
2692
  ROWS 1000
2693
;
2694
COMMENT ON FUNCTION $$||view_||$$(no_sort boolean, limit_ integer, offset_ integer) IS '
2695
Use this for limit values greater than ~100,000 to avoid unwanted slow sorts.
2696
If you want to run EXPLAIN and get expanded output, use the regular subset
2697
function instead. (When a config param is set on a function, EXPLAIN produces
2698
just a function scan.)
2699
';
2700
$$;
2701 8325 aaronmk
END;
2702
$_$;
2703
2704
2705
--
2706 11010 aaronmk
-- Name: FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass); Type: COMMENT; Schema: util; Owner: -
2707
--
2708
2709 12235 aaronmk
COMMENT ON FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass) IS '
2710
creates subset function which turns off enable_sort
2711
';
2712 11010 aaronmk
2713
2714
--
2715 12576 aaronmk
-- Name: mk_typed_cols_list(col_cast[]); Type: FUNCTION; Schema: util; Owner: -
2716
--
2717
2718
CREATE FUNCTION mk_typed_cols_list(cols col_cast[]) RETURNS text
2719
    LANGUAGE sql IMMUTABLE
2720
    AS $_$
2721 12579 aaronmk
SELECT COALESCE(string_agg(quote_ident((unnest).col_name)||$$ $$||
2722 12576 aaronmk
util.qual_name((unnest).type), ', '), '')
2723
FROM unnest($1)
2724
$_$;
2725
2726
2727
--
2728 12242 aaronmk
-- Name: name(regclass); Type: FUNCTION; Schema: util; Owner: -
2729
--
2730
2731
CREATE FUNCTION name(table_ regclass) RETURNS text
2732
    LANGUAGE sql STABLE
2733
    AS $_$
2734
SELECT relname::text FROM pg_class WHERE oid = $1
2735
$_$;
2736
2737
2738
--
2739 8183 aaronmk
-- Name: name(regtype); Type: FUNCTION; Schema: util; Owner: -
2740 8083 aaronmk
--
2741
2742
CREATE FUNCTION name(type regtype) RETURNS text
2743 12446 aaronmk
    LANGUAGE sql STABLE
2744 8083 aaronmk
    AS $_$
2745
SELECT typname::text FROM pg_type WHERE oid = $1
2746
$_$;
2747
2748
2749
--
2750 12360 aaronmk
-- Name: name_was_truncated(text, integer); Type: FUNCTION; Schema: util; Owner: -
2751 12355 aaronmk
--
2752
2753 12360 aaronmk
CREATE FUNCTION name_was_truncated(name_ text, max_prefix_len integer DEFAULT 0) RETURNS boolean
2754 12355 aaronmk
    LANGUAGE sql IMMUTABLE
2755
    AS $_$
2756 12360 aaronmk
SELECT octet_length($1) >= util.namedatalen() - $2
2757 12355 aaronmk
$_$;
2758
2759
2760
--
2761 12354 aaronmk
-- Name: namedatalen(); Type: FUNCTION; Schema: util; Owner: -
2762
--
2763
2764
CREATE FUNCTION namedatalen() RETURNS integer
2765
    LANGUAGE sql IMMUTABLE
2766
    AS $$
2767
SELECT octet_length(repeat('_', 1024/*>63*/)::name::text)
2768
$$;
2769
2770
2771
--
2772 9958 aaronmk
-- Name: not_empty(anyarray); Type: FUNCTION; Schema: util; Owner: -
2773
--
2774
2775
CREATE FUNCTION not_empty(value anyarray) RETURNS boolean
2776
    LANGUAGE sql IMMUTABLE
2777
    AS $_$
2778 10329 aaronmk
SELECT $1 IS NOT NULL AND util.array_length($1) > 0
2779 9958 aaronmk
$_$;
2780
2781
2782
--
2783 9956 aaronmk
-- Name: not_null(anyelement); Type: FUNCTION; Schema: util; Owner: -
2784
--
2785
2786
CREATE FUNCTION not_null(value anyelement) RETURNS boolean
2787 9957 aaronmk
    LANGUAGE sql IMMUTABLE
2788 9956 aaronmk
    AS $_$
2789
SELECT $1 IS NOT NULL
2790
$_$;
2791
2792
2793
--
2794 10373 aaronmk
-- Name: nulls_map(text[]); Type: FUNCTION; Schema: util; Owner: -
2795
--
2796
2797
CREATE FUNCTION nulls_map(nulls text[]) RETURNS hstore
2798
    LANGUAGE sql IMMUTABLE
2799
    AS $_$
2800
SELECT util.hstore($1, NULL) || '*=>*'
2801
$_$;
2802
2803
2804
--
2805
-- Name: FUNCTION nulls_map(nulls text[]); Type: COMMENT; Schema: util; Owner: -
2806
--
2807
2808 12235 aaronmk
COMMENT ON FUNCTION nulls_map(nulls text[]) IS '
2809
for use with _map()
2810
';
2811 10373 aaronmk
2812
2813
--
2814 10989 aaronmk
-- Name: offset2row_num(integer, integer); Type: FUNCTION; Schema: util; Owner: -
2815 10984 aaronmk
--
2816
2817 10989 aaronmk
CREATE FUNCTION offset2row_num(offset_ integer, min_row_num integer DEFAULT 1) RETURNS integer
2818 10984 aaronmk
    LANGUAGE sql IMMUTABLE
2819
    AS $_$
2820 10989 aaronmk
SELECT $2 + COALESCE($1, 0)
2821 10984 aaronmk
$_$;
2822
2823
2824
--
2825 12659 aaronmk
-- Name: parent(regclass); Type: FUNCTION; Schema: util; Owner: -
2826
--
2827
2828
CREATE FUNCTION parent(table_ regclass) RETURNS regclass
2829
    LANGUAGE sql STABLE
2830
    AS $_$
2831
SELECT inhparent FROM pg_inherits WHERE inhrelid = $1
2832
$_$;
2833
2834
2835
--
2836 12651 aaronmk
-- Name: populate_table(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2837
--
2838
2839
CREATE FUNCTION populate_table(table_ regclass, sql text) RETURNS void
2840
    LANGUAGE sql
2841
    AS $_$
2842
SELECT util.eval($$INSERT INTO $$||$1||$$
2843
$$||util.ltrim_nl($2));
2844
-- make sure the created table has the correct estimated row count
2845
SELECT util.analyze_($1);
2846
$_$;
2847
2848
2849
--
2850 12575 aaronmk
-- Name: prefixed_name(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
2851
--
2852
2853
CREATE FUNCTION prefixed_name(prefix text, type anyelement) RETURNS text
2854
    LANGUAGE sql IMMUTABLE
2855
    AS $_$
2856
SELECT util.qual_name(util.schema($2), $1||util.name($2))
2857
$_$;
2858
2859
2860
--
2861 12494 aaronmk
-- Name: prepend_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2862
--
2863
2864
CREATE FUNCTION prepend_comment(table_ regclass, comment text) RETURNS void
2865
    LANGUAGE sql
2866
    AS $_$
2867
SELECT util.set_comment($1, concat($2, util.comment($1)))
2868
$_$;
2869
2870
2871
--
2872
-- Name: FUNCTION prepend_comment(table_ regclass, comment text); Type: COMMENT; Schema: util; Owner: -
2873
--
2874
2875
COMMENT ON FUNCTION prepend_comment(table_ regclass, comment text) IS '
2876
comment: must start and end with a newline
2877
';
2878
2879
2880
--
2881 12260 aaronmk
-- Name: qual_name(text[]); Type: FUNCTION; Schema: util; Owner: -
2882
--
2883
2884
CREATE FUNCTION qual_name(VARIADIC elems text[]) RETURNS text
2885
    LANGUAGE sql IMMUTABLE
2886
    AS $_$
2887
SELECT string_agg(quote_ident(unnest), '.') FROM unnest($1)
2888
$_$;
2889
2890
2891
--
2892 10988 aaronmk
-- Name: qual_name(regclass); Type: FUNCTION; Schema: util; Owner: -
2893
--
2894
2895
CREATE FUNCTION qual_name(table_ regclass) RETURNS text
2896 12446 aaronmk
    LANGUAGE sql STABLE
2897 12267 aaronmk
    SET search_path TO pg_temp
2898 10988 aaronmk
    AS $_$
2899 12267 aaronmk
SELECT $1::text
2900 10988 aaronmk
$_$;
2901
2902
2903
--
2904 12267 aaronmk
-- Name: qual_name(regtype); Type: FUNCTION; Schema: util; Owner: -
2905
--
2906
2907
CREATE FUNCTION qual_name(type regtype) RETURNS text
2908 12446 aaronmk
    LANGUAGE sql STABLE
2909 12267 aaronmk
    SET search_path TO pg_temp
2910
    AS $_$
2911
SELECT $1::text
2912
$_$;
2913
2914
2915
--
2916
-- Name: FUNCTION qual_name(type regtype); Type: COMMENT; Schema: util; Owner: -
2917
--
2918
2919
COMMENT ON FUNCTION qual_name(type regtype) IS '
2920
a type''s schema-qualified name
2921
';
2922
2923
2924
--
2925 12268 aaronmk
-- Name: qual_name(unknown); Type: FUNCTION; Schema: util; Owner: -
2926
--
2927
2928
CREATE FUNCTION qual_name(type unknown) RETURNS text
2929 12446 aaronmk
    LANGUAGE sql STABLE
2930 12268 aaronmk
    AS $_$
2931
SELECT util.qual_name($1::text::regtype)
2932
$_$;
2933
2934
2935
--
2936 12376 aaronmk
-- Name: quote_func_call(regprocedure, text[]); Type: FUNCTION; Schema: util; Owner: -
2937
--
2938
2939
CREATE FUNCTION quote_func_call(func regprocedure, VARIADIC args_esc text[]) RETURNS text
2940
    LANGUAGE sql IMMUTABLE
2941
    AS $_$
2942
SELECT util.quote_func_call($1::regproc::text, VARIADIC $2)
2943
$_$;
2944
2945
2946
--
2947
-- Name: quote_func_call(text, text[]); Type: FUNCTION; Schema: util; Owner: -
2948
--
2949
2950
CREATE FUNCTION quote_func_call(func_esc text, VARIADIC args_esc text[]) RETURNS text
2951
    LANGUAGE sql IMMUTABLE
2952
    AS $_$
2953
SELECT $1||'('||concat_ws(', ', VARIADIC $2)||')'
2954
$_$;
2955
2956
2957
--
2958 12371 aaronmk
-- Name: quote_typed(anyelement); Type: FUNCTION; Schema: util; Owner: -
2959
--
2960
2961
CREATE FUNCTION quote_typed(value anyelement) RETURNS text
2962
    LANGUAGE sql IMMUTABLE
2963
    AS $_$
2964 12437 aaronmk
SELECT quote_nullable($1)||$$::$$||util.qual_name(pg_typeof($1))
2965 12371 aaronmk
$_$;
2966
2967
2968
--
2969 12530 aaronmk
-- Name: raise(text, text); Type: FUNCTION; Schema: util; Owner: -
2970
--
2971
2972
CREATE FUNCTION raise(type text, msg text) RETURNS void
2973
    LANGUAGE sql IMMUTABLE
2974 12560 aaronmk
    AS $_X$
2975 12530 aaronmk
SELECT util.eval($$
2976
CREATE OR REPLACE FUNCTION pg_temp.__raise()
2977
  RETURNS void AS
2978 12560 aaronmk
-- $__BODY1$ in case msg contains $BODY1$ (in SQL)
2979
$__BODY1$
2980 12530 aaronmk
BEGIN
2981
	RAISE $$||$1||$$ USING MESSAGE = $$||quote_nullable($2)||$$;
2982
END;
2983 12560 aaronmk
$__BODY1$
2984 12530 aaronmk
  LANGUAGE plpgsql IMMUTABLE
2985
  COST 100;
2986 12532 aaronmk
$$, verbose_ := false);
2987 12530 aaronmk
2988 12532 aaronmk
SELECT util.eval($$SELECT pg_temp.__raise()$$, verbose_ := false);
2989 12560 aaronmk
$_X$;
2990 12530 aaronmk
2991
2992
--
2993 12533 aaronmk
-- Name: FUNCTION raise(type text, msg text); Type: COMMENT; Schema: util; Owner: -
2994
--
2995
2996
COMMENT ON FUNCTION raise(type text, msg text) IS '
2997
type: a log level from
2998
http://www.postgresql.org/docs/9.3/static/plpgsql-errors-and-messages.html
2999
or a condition name from
3000
http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html
3001
';
3002
3003
3004
--
3005 12536 aaronmk
-- Name: raise_error_warning(text); Type: FUNCTION; Schema: util; Owner: -
3006 12311 aaronmk
--
3007
3008 12536 aaronmk
CREATE FUNCTION raise_error_warning(msg text) RETURNS void
3009 12441 aaronmk
    LANGUAGE sql IMMUTABLE
3010 12311 aaronmk
    AS $_$
3011 12536 aaronmk
SELECT util.raise('WARNING', 'ERROR:  '||$1)
3012 12311 aaronmk
$_$;
3013
3014
3015
--
3016 10116 aaronmk
-- Name: raise_undefined_column(col_ref); Type: FUNCTION; Schema: util; Owner: -
3017
--
3018
3019
CREATE FUNCTION raise_undefined_column(col col_ref) RETURNS text
3020
    LANGUAGE plpgsql IMMUTABLE STRICT
3021
    AS $$
3022
BEGIN
3023
	RAISE undefined_column USING MESSAGE = concat('undefined column: ', col.name);
3024
END;
3025
$$;
3026
3027
3028
--
3029 13512 aaronmk
-- Name: recreate(text, text[]); Type: FUNCTION; Schema: util; Owner: -
3030
--
3031
3032
CREATE FUNCTION recreate(cmd text, users text[] DEFAULT NULL::text[]) RETURNS void
3033
    LANGUAGE plpgsql
3034
    AS $_$
3035
DECLARE
3036
	PG_EXCEPTION_DETAIL text;
3037
	restore_views_info util.restore_views_info;
3038
BEGIN
3039
	restore_views_info = util.save_drop_views(users);
3040
	PERFORM util.eval(cmd);
3041
	PERFORM util.restore_views(restore_views_info);
3042
EXCEPTION
3043
WHEN dependent_objects_still_exist THEN
3044
	IF users IS NOT NULL THEN RAISE; END IF; -- save_drop_views() didn't fix it
3045
	GET STACKED DIAGNOSTICS PG_EXCEPTION_DETAIL = PG_EXCEPTION_DETAIL;
3046
	users = array(SELECT * FROM util.regexp_matches_group(
3047
		PG_EXCEPTION_DETAIL, '(?m)^view (.*) depends on [[:lower:]]+ .*$'));
3048
		-- will be in forward dependency order
3049
	PERFORM util.debug_print_var('PG_EXCEPTION_DETAIL', PG_EXCEPTION_DETAIL);
3050
	PERFORM util.debug_print_var('users', users);
3051
	IF util.is_empty(users) THEN RAISE; END IF;
3052
	PERFORM util.recreate(cmd, users);
3053
END;
3054
$_$;
3055
3056
3057
--
3058
-- Name: FUNCTION recreate(cmd text, users text[]); Type: COMMENT; Schema: util; Owner: -
3059
--
3060
3061
COMMENT ON FUNCTION recreate(cmd text, users text[]) IS '
3062
usage:
3063
SELECT util.recreate($$
3064
-- trigger the dependent_objects_still_exist exception
3065
DROP VIEW schemas.main_view; -- *not* CASCADE; it must trigger an exception
3066
3067
CREATE VIEW schemas.main_view AS _;
3068
3069
-- manually restore views that need to be updated for the changes
3070
CREATE VIEW schemas.dependent_view AS _;
3071
$$);
3072
3073
idempotent
3074
3075
users: not necessary to provide this because it will be autopopulated
3076
';
3077
3078
3079
--
3080 11657 aaronmk
-- Name: regexp_matches_group(text, text, integer); Type: FUNCTION; Schema: util; Owner: -
3081
--
3082
3083
CREATE FUNCTION regexp_matches_group(str text, re text, group_ integer DEFAULT 1) RETURNS SETOF text
3084
    LANGUAGE sql IMMUTABLE
3085
    AS $_$
3086
SELECT regexp_matches[$3] FROM regexp_matches($1, $2, 'g')
3087
$_$;
3088
3089
3090
--
3091 12333 aaronmk
-- Name: regexp_quote(text); Type: FUNCTION; Schema: util; Owner: -
3092
--
3093
3094
CREATE FUNCTION regexp_quote(str text) RETURNS text
3095
    LANGUAGE sql IMMUTABLE
3096
    AS $_$
3097
SELECT regexp_replace($1, '\W', /*\char*/'\\\&', 'g')
3098
$_$;
3099
3100
3101
--
3102 12375 aaronmk
-- Name: regprocedure(text); Type: FUNCTION; Schema: util; Owner: -
3103
--
3104
3105
CREATE FUNCTION regprocedure(func text) RETURNS regprocedure
3106
    LANGUAGE sql IMMUTABLE
3107
    AS $_$
3108
SELECT (CASE WHEN right($1, 1) = ')'
3109 12377 aaronmk
THEN $1::regprocedure ELSE $1::regproc::regprocedure END)
3110 12375 aaronmk
$_$;
3111
3112
3113
--
3114 13492 aaronmk
-- Name: relation_exists(text); Type: FUNCTION; Schema: util; Owner: -
3115
--
3116
3117
CREATE FUNCTION relation_exists(relation text) RETURNS boolean
3118
    LANGUAGE sql STABLE
3119
    AS $_$
3120
SELECT $1 IS NOT NULL AND util.is_castable($1, NULL::regclass)
3121
$_$;
3122
3123
3124
--
3125 12344 aaronmk
-- Name: relation_type(regclass); Type: FUNCTION; Schema: util; Owner: -
3126
--
3127
3128
CREATE FUNCTION relation_type(relation regclass) RETURNS text
3129
    LANGUAGE sql STABLE
3130
    AS $_$
3131
SELECT util.relation_type(util.relation_type_char($1))
3132
$_$;
3133
3134
3135
--
3136 12340 aaronmk
-- Name: relation_type("char"); Type: FUNCTION; Schema: util; Owner: -
3137 12339 aaronmk
--
3138
3139 12340 aaronmk
CREATE FUNCTION relation_type(relation_type_char "char") RETURNS text
3140 12339 aaronmk
    LANGUAGE sql IMMUTABLE
3141
    AS $_$
3142 12593 aaronmk
SELECT 'c=>TYPE, r=>TABLE, v=>VIEW'::hstore -> $1
3143 12339 aaronmk
$_$;
3144
3145
3146
--
3147 12588 aaronmk
-- Name: relation_type(regtype); Type: FUNCTION; Schema: util; Owner: -
3148
--
3149
3150
CREATE FUNCTION relation_type(type regtype) RETURNS text
3151
    LANGUAGE sql IMMUTABLE
3152
    AS $$
3153
SELECT 'TYPE'::text
3154
$$;
3155
3156
3157
--
3158 12341 aaronmk
-- Name: relation_type_char(regclass); Type: FUNCTION; Schema: util; Owner: -
3159
--
3160
3161
CREATE FUNCTION relation_type_char(relation regclass) RETURNS "char"
3162
    LANGUAGE sql STABLE
3163
    AS $_$
3164
SELECT relkind FROM pg_class WHERE oid = $1
3165
$_$;
3166
3167
3168
--
3169 12293 aaronmk
-- Name: remake_diff_table(text, regclass, regclass, text); Type: FUNCTION; Schema: util; Owner: -
3170
--
3171
3172
CREATE FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text) RETURNS void
3173
    LANGUAGE sql
3174
    AS $_$
3175
/* can't have in_table/out_table inherit from *each other*, because inheritance
3176
also causes the rows of the parent table to be included in the child table.
3177
instead, they need to inherit from a common, empty table. */
3178 12382 aaronmk
SELECT util.create_if_not_exists($$SELECT $$||util.quote_func_call(
3179
'util.copy_struct', util.quote_typed($2), util.quote_typed($4)));
3180 13098 aaronmk
SELECT util.rm_freq(ARRAY[$4]); -- left/right_table don't have freq yet
3181 12293 aaronmk
SELECT util.inherit($2, $4);
3182
SELECT util.inherit($3, $4);
3183
3184
SELECT util.rematerialize_query($1, $$
3185
SELECT * FROM util.diff(
3186 12419 aaronmk
  $$||util.quote_typed($2)||$$
3187
, $$||util.quote_typed($3)||$$
3188 12293 aaronmk
, NULL::$$||$4||$$)
3189
$$);
3190 12303 aaronmk
3191
/* the table unfortunately cannot be *materialized* in human-readable form,
3192
because this would create column name collisions between the two sides */
3193 12495 aaronmk
SELECT util.prepend_comment($1, '
3194 12303 aaronmk
to view this table in human-readable form (with each side''s tuple column
3195
expanded to its component fields):
3196 12572 aaronmk
SELECT (left_).*, ('||util.schema($4::regclass)||'.values_(right_)).* FROM '||$1||';
3197 13092 aaronmk
3198
to display NULL values that are extra or missing:
3199
SELECT * FROM '||$1||';
3200 12303 aaronmk
');
3201 12293 aaronmk
$_$;
3202
3203
3204
--
3205
-- Name: FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text); Type: COMMENT; Schema: util; Owner: -
3206
--
3207
3208
COMMENT ON FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text) IS '
3209
type_table (*required*): table to create as the shared base type
3210
';
3211
3212
3213
--
3214 12265 aaronmk
-- Name: rematerialize_query(text, text); Type: FUNCTION; Schema: util; Owner: -
3215
--
3216
3217
CREATE FUNCTION rematerialize_query(table_esc text, sql text) RETURNS void
3218
    LANGUAGE sql
3219
    AS $_$
3220
SELECT util.drop_table($1);
3221
SELECT util.materialize_query($1, $2);
3222
$_$;
3223
3224
3225
--
3226
-- Name: FUNCTION rematerialize_query(table_esc text, sql text); Type: COMMENT; Schema: util; Owner: -
3227
--
3228
3229
COMMENT ON FUNCTION rematerialize_query(table_esc text, sql text) IS '
3230
idempotent, but repeats action each time
3231
';
3232
3233
3234
--
3235 12247 aaronmk
-- Name: rematerialize_view(text, regclass); Type: FUNCTION; Schema: util; Owner: -
3236
--
3237
3238 12262 aaronmk
CREATE FUNCTION rematerialize_view(table_esc text, view_ regclass) RETURNS void
3239 12247 aaronmk
    LANGUAGE sql
3240
    AS $_$
3241
SELECT util.drop_table($1);
3242
SELECT util.materialize_view($1, $2);
3243
$_$;
3244
3245
3246
--
3247 12262 aaronmk
-- Name: FUNCTION rematerialize_view(table_esc text, view_ regclass); Type: COMMENT; Schema: util; Owner: -
3248 12247 aaronmk
--
3249
3250 12262 aaronmk
COMMENT ON FUNCTION rematerialize_view(table_esc text, view_ regclass) IS '
3251 12247 aaronmk
idempotent, but repeats action each time
3252
';
3253
3254
3255
--
3256 8183 aaronmk
-- Name: rename_cols(regclass, anyelement); Type: FUNCTION; Schema: util; Owner: -
3257 8137 aaronmk
--
3258
3259 8148 aaronmk
CREATE FUNCTION rename_cols(table_ regclass, renames anyelement) RETURNS void
3260 12446 aaronmk
    LANGUAGE sql
3261 8137 aaronmk
    AS $_$
3262 8212 aaronmk
SELECT util.try_create($$ALTER TABLE $$||$1||$$ RENAME $$
3263 8137 aaronmk
||quote_ident(name)||$$ TO $$||quote_ident($2 -> name))
3264 10309 aaronmk
FROM util.col_names($1::text::regtype) f (name);
3265
SELECT NULL::void; -- don't fold away functions called in previous query
3266 8137 aaronmk
$_$;
3267
3268
3269
--
3270 8183 aaronmk
-- Name: FUNCTION rename_cols(table_ regclass, renames anyelement); Type: COMMENT; Schema: util; Owner: -
3271 8137 aaronmk
--
3272
3273 12235 aaronmk
COMMENT ON FUNCTION rename_cols(table_ regclass, renames anyelement) IS '
3274
idempotent
3275
';
3276 8137 aaronmk
3277
3278
--
3279 12349 aaronmk
-- Name: rename_relation(regclass, text); Type: FUNCTION; Schema: util; Owner: -
3280
--
3281
3282
CREATE FUNCTION rename_relation(from_ regclass, to_ text) RETURNS void
3283
    LANGUAGE sql
3284
    AS $_$
3285 12353 aaronmk
/* use util.qual_name() instead of ::text so that the schema qualifier is always
3286
included in the debug SQL */
3287
SELECT util.rename_relation(util.qual_name($1), $2)
3288 12349 aaronmk
$_$;
3289
3290
3291
--
3292
-- Name: rename_relation(text, text); Type: FUNCTION; Schema: util; Owner: -
3293
--
3294
3295 12364 aaronmk
CREATE FUNCTION rename_relation(from_esc text, to_name text) RETURNS void
3296 12349 aaronmk
    LANGUAGE sql
3297
    AS $_$
3298
/* 'ALTER TABLE can be used with views too'
3299
(http://www.postgresql.org/docs/9.3/static/sql-alterview.html) */
3300 12363 aaronmk
SELECT util.eval($$ALTER TABLE IF EXISTS $$||$1||$$ RENAME TO $$
3301
||quote_ident($2))
3302 12349 aaronmk
$_$;
3303
3304
3305
--
3306 12364 aaronmk
-- Name: FUNCTION rename_relation(from_esc text, to_name text); Type: COMMENT; Schema: util; Owner: -
3307 12349 aaronmk
--
3308
3309 12364 aaronmk
COMMENT ON FUNCTION rename_relation(from_esc text, to_name text) IS '
3310 12349 aaronmk
idempotent
3311
';
3312
3313
3314
--
3315 12358 aaronmk
-- Name: replace_suffix(text, text, text, integer); Type: FUNCTION; Schema: util; Owner: -
3316 12350 aaronmk
--
3317
3318 12358 aaronmk
CREATE FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer DEFAULT 0) RETURNS text
3319 12350 aaronmk
    LANGUAGE sql IMMUTABLE
3320
    AS $_$
3321 12358 aaronmk
SELECT regexp_replace($1, util.truncated_prefixed_name_regexp($2, $4), '\1'||$3)
3322 12350 aaronmk
$_$;
3323
3324
3325
--
3326 12358 aaronmk
-- Name: FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer); Type: COMMENT; Schema: util; Owner: -
3327
--
3328
3329
COMMENT ON FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer) IS '
3330
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
3331
';
3332
3333
3334
--
3335 10297 aaronmk
-- Name: reset_col_names(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
3336
--
3337
3338
CREATE FUNCTION reset_col_names(table_ regclass, names regclass) RETURNS void
3339 12446 aaronmk
    LANGUAGE sql
3340 10297 aaronmk
    AS $_$
3341 10596 aaronmk
SELECT util.eval($$DELETE FROM $$||$2||$$ WHERE "from" LIKE ':%'$$);
3342
SELECT util.mk_derived_col(($2, 'to'), $$"from"$$, overwrite := true);
3343 10297 aaronmk
SELECT util.set_col_names($1, $2);
3344
$_$;
3345
3346
3347
--
3348
-- Name: FUNCTION reset_col_names(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
3349
--
3350
3351 12235 aaronmk
COMMENT ON FUNCTION reset_col_names(table_ regclass, names regclass) IS '
3352
idempotent.
3353
alters the names table, so it will need to be repopulated after running this function.
3354
';
3355 10297 aaronmk
3356
3357
--
3358 8183 aaronmk
-- Name: reset_map_table(text); Type: FUNCTION; Schema: util; Owner: -
3359 8143 aaronmk
--
3360
3361
CREATE FUNCTION reset_map_table(table_ text) RETURNS void
3362 12446 aaronmk
    LANGUAGE sql
3363 8143 aaronmk
    AS $_$
3364 10152 aaronmk
SELECT util.drop_table($1);
3365 8183 aaronmk
SELECT util.mk_map_table($1);
3366 8143 aaronmk
$_$;
3367
3368
3369
--
3370 13488 aaronmk
-- Name: restore_views(restore_views_info); Type: FUNCTION; Schema: util; Owner: -
3371 13486 aaronmk
--
3372
3373 13488 aaronmk
CREATE FUNCTION restore_views(restore_views_info) RETURNS void
3374 13486 aaronmk
    LANGUAGE sql
3375
    AS $_$
3376
SELECT util.debug_print_var('views', $1);
3377 13497 aaronmk
SELECT util.create_if_not_exists((view_).def, (view_).path)
3378
	/* need to specify view name for manual existence check, in case view def
3379
	becomes invalid, which would produce nonstandard (uncatchable) exception */
3380 13491 aaronmk
FROM unnest($1.views) view_; -- in forward dependency order
3381 13486 aaronmk
	/* create_if_not_exists() rather than eval(), because cmd might manually
3382
	re-create a deleted dependent view, causing it to already exist */
3383
SELECT NULL::void; -- don't fold away functions called in previous query
3384
$_$;
3385
3386
3387
--
3388 13096 aaronmk
-- Name: rm_freq(regclass[], text); Type: FUNCTION; Schema: util; Owner: -
3389
--
3390
3391
CREATE FUNCTION rm_freq(tables regclass[], freq_col text DEFAULT 'copies'::text) RETURNS void
3392
    LANGUAGE sql
3393
    AS $_$
3394
SELECT util.drop_column($1, $2, force := true)
3395
$_$;
3396
3397
3398
--
3399 12356 aaronmk
-- Name: rtrim_n(text, integer); Type: FUNCTION; Schema: util; Owner: -
3400
--
3401
3402
CREATE FUNCTION rtrim_n(str text, count integer) RETURNS text
3403
    LANGUAGE sql IMMUTABLE
3404
    AS $_$
3405
SELECT (CASE WHEN $2 <= 0 THEN $1 ELSE left($1, -$2) END)
3406
$_$;
3407
3408
3409
--
3410 12473 aaronmk
-- Name: runnable_sql(text); Type: FUNCTION; Schema: util; Owner: -
3411
--
3412
3413
CREATE FUNCTION runnable_sql(sql text) RETURNS text
3414
    LANGUAGE sql IMMUTABLE
3415
    AS $_$
3416 12481 aaronmk
SELECT (CASE WHEN util.is_set_stmt($1) THEN ''
3417
ELSE util.mk_set_search_path(for_printing := true)||$$;
3418
$$ END)||$1
3419 12473 aaronmk
$_$;
3420
3421
3422
--
3423 11652 aaronmk
-- Name: save_drop_view(text); Type: FUNCTION; Schema: util; Owner: -
3424
--
3425
3426
CREATE FUNCTION save_drop_view(view_ text) RETURNS text
3427
    LANGUAGE plpgsql STRICT
3428 13467 aaronmk
    AS $$
3429 11652 aaronmk
DECLARE
3430
	result text = NULL;
3431
BEGIN
3432
	BEGIN
3433 13470 aaronmk
		result = util.show_create_view(view_, replace := false);
3434
			/* replace: no `OR REPLACE` because that causes nonuniform errors
3435
			(eg. invalid_table_definition), instead of the standard
3436
			duplicate_table exception caught by util.create_if_not_exists() */
3437 13467 aaronmk
		PERFORM util.drop_view(view_);
3438 11652 aaronmk
	EXCEPTION
3439
		WHEN undefined_table THEN NULL;
3440
	END;
3441
	RETURN result;
3442
END;
3443 13467 aaronmk
$$;
3444 11652 aaronmk
3445
3446
--
3447 11660 aaronmk
-- Name: save_drop_views(text[]); Type: FUNCTION; Schema: util; Owner: -
3448
--
3449
3450 13488 aaronmk
CREATE FUNCTION save_drop_views(views text[]) RETURNS restore_views_info
3451 11660 aaronmk
    LANGUAGE sql
3452
    AS $_$
3453 13488 aaronmk
SELECT ROW(/*return in forward dependency order*/util.array_reverse(array(
3454 13491 aaronmk
SELECT (view_, util.save_drop_view(view_))::util.db_item
3455 13485 aaronmk
FROM unnest(/*drop in reverse dependency order*/util.array_reverse($1)) view_
3456 13488 aaronmk
)))::util.restore_views_info
3457 11660 aaronmk
$_$;
3458
3459
3460
--
3461 12244 aaronmk
-- Name: schema(oid); Type: FUNCTION; Schema: util; Owner: -
3462
--
3463
3464
CREATE FUNCTION schema(pg_namespace_oid oid) RETURNS text
3465
    LANGUAGE sql STABLE
3466
    AS $_$
3467
SELECT nspname::text FROM pg_namespace WHERE pg_namespace.oid = $1
3468
$_$;
3469
3470
3471
--
3472 12242 aaronmk
-- Name: schema(regclass); Type: FUNCTION; Schema: util; Owner: -
3473
--
3474
3475
CREATE FUNCTION schema(table_ regclass) RETURNS text
3476
    LANGUAGE sql STABLE
3477
    AS $_$
3478 12245 aaronmk
SELECT util.schema(relnamespace) FROM pg_class WHERE oid = $1
3479 12242 aaronmk
$_$;
3480
3481
3482
--
3483 10794 aaronmk
-- Name: schema(regtype); Type: FUNCTION; Schema: util; Owner: -
3484
--
3485
3486
CREATE FUNCTION schema(type regtype) RETURNS text
3487
    LANGUAGE sql STABLE
3488
    AS $_$
3489 12245 aaronmk
SELECT util.schema(typnamespace) FROM pg_type WHERE oid = $1
3490 10794 aaronmk
$_$;
3491
3492
3493
--
3494
-- Name: schema(anyelement); Type: FUNCTION; Schema: util; Owner: -
3495
--
3496
3497
CREATE FUNCTION schema(type_null anyelement) RETURNS text
3498
    LANGUAGE sql STABLE
3499
    AS $_$
3500
SELECT util.schema(pg_typeof($1))
3501
$_$;
3502
3503
3504
--
3505 12134 aaronmk
-- Name: schema_bundle_get_schemas(text); Type: FUNCTION; Schema: util; Owner: -
3506
--
3507
3508
CREATE FUNCTION schema_bundle_get_schemas(schema_bundle text) RETURNS SETOF text
3509
    LANGUAGE sql STABLE
3510
    AS $_$
3511
SELECT nspname::text FROM pg_namespace WHERE nspname ~ ('^'||$1||'(?=\y|_)')
3512
$_$;
3513
3514
3515
--
3516 12135 aaronmk
-- Name: FUNCTION schema_bundle_get_schemas(schema_bundle text); Type: COMMENT; Schema: util; Owner: -
3517
--
3518
3519 12235 aaronmk
COMMENT ON FUNCTION schema_bundle_get_schemas(schema_bundle text) IS '
3520
a schema bundle is a group of schemas with a common prefix
3521
';
3522 12135 aaronmk
3523
3524
--
3525
-- Name: schema_bundle_rename(text, text); Type: FUNCTION; Schema: util; Owner: -
3526
--
3527
3528
CREATE FUNCTION schema_bundle_rename(old text, new text) RETURNS void
3529
    LANGUAGE sql
3530
    AS $_$
3531
SELECT util.schema_rename(old_schema,
3532
	overlay(old_schema placing new from 1 for length(old))) -- replace prefix
3533
FROM util.schema_bundle_get_schemas($1) f (old_schema);
3534
SELECT NULL::void; -- don't fold away functions called in previous query
3535
$_$;
3536
3537
3538
--
3539
-- Name: schema_bundle_replace(text, text); Type: FUNCTION; Schema: util; Owner: -
3540
--
3541
3542
CREATE FUNCTION schema_bundle_replace(replace text, with_ text) RETURNS void
3543
    LANGUAGE plpgsql
3544
    AS $$
3545
BEGIN
3546
	-- don't schema_bundle_rm() the schema_bundle to keep!
3547
	IF replace = with_ THEN RETURN; END IF;
3548
3549
	PERFORM util.schema_bundle_rm(replace);
3550
	PERFORM util.schema_bundle_rename(with_, replace);
3551
END;
3552
$$;
3553
3554
3555
--
3556
-- Name: schema_bundle_rm(text); Type: FUNCTION; Schema: util; Owner: -
3557
--
3558
3559
CREATE FUNCTION schema_bundle_rm(schema_bundle text) RETURNS void
3560
    LANGUAGE sql
3561
    AS $_$
3562
SELECT util.schema_rm(schema)
3563
FROM util.schema_bundle_get_schemas($1) f (schema);
3564
SELECT NULL::void; -- don't fold away functions called in previous query
3565
$_$;
3566
3567
3568
--
3569 12238 aaronmk
-- Name: schema_esc(anyelement); Type: FUNCTION; Schema: util; Owner: -
3570 10795 aaronmk
--
3571
3572 12238 aaronmk
CREATE FUNCTION schema_esc(type_null anyelement) RETURNS text
3573 10795 aaronmk
    LANGUAGE sql STABLE
3574
    AS $_$
3575
SELECT quote_ident(util.schema($1))
3576
$_$;
3577
3578
3579
--
3580 12324 aaronmk
-- Name: schema_matches(text, text); Type: FUNCTION; Schema: util; Owner: -
3581
--
3582
3583
CREATE FUNCTION schema_matches(schema text, schema_regexp text) RETURNS boolean
3584 12334 aaronmk
    LANGUAGE sql IMMUTABLE
3585 12324 aaronmk
    AS $_$
3586
SELECT $1 ~ $2 AND /*in userspace*/$1 !~ '^(?:information_schema|pg_.*)$'
3587
$_$;
3588
3589
3590
--
3591 12304 aaronmk
-- Name: schema_oid(text); Type: FUNCTION; Schema: util; Owner: -
3592
--
3593
3594
CREATE FUNCTION schema_oid(schema text) RETURNS oid
3595
    LANGUAGE sql STABLE
3596
    AS $_$
3597
SELECT oid FROM pg_namespace WHERE nspname = $1
3598
$_$;
3599
3600
3601
--
3602 12504 aaronmk
-- Name: schema_regexp(regclass); Type: FUNCTION; Schema: util; Owner: -
3603
--
3604
3605
CREATE FUNCTION schema_regexp(relation regclass) RETURNS text
3606
    LANGUAGE sql IMMUTABLE
3607
    AS $_$
3608
SELECT util.schema_regexp(schema_anchor := $1)
3609
$_$;
3610
3611
3612
--
3613 12501 aaronmk
-- Name: schema_regexp(anyelement); Type: FUNCTION; Schema: util; Owner: -
3614
--
3615
3616
CREATE FUNCTION schema_regexp(schema_anchor anyelement) RETURNS text
3617
    LANGUAGE sql IMMUTABLE
3618
    AS $_$
3619
SELECT util.str_equality_regexp(util.schema($1))
3620
$_$;
3621
3622
3623
--
3624 12132 aaronmk
-- Name: schema_rename(text, text); Type: FUNCTION; Schema: util; Owner: -
3625
--
3626
3627
CREATE FUNCTION schema_rename(old text, new text) RETURNS void
3628
    LANGUAGE sql
3629
    AS $_$
3630
SELECT util.eval($$ALTER SCHEMA $$||quote_ident($1)||$$ RENAME TO $$||quote_ident($2));
3631
$_$;
3632
3633
3634
--
3635 12133 aaronmk
-- Name: schema_replace(text, text); Type: FUNCTION; Schema: util; Owner: -
3636
--
3637
3638
CREATE FUNCTION schema_replace(replace text, with_ text) RETURNS void
3639
    LANGUAGE plpgsql
3640
    AS $$
3641
BEGIN
3642
	-- don't schema_rm() the schema to keep!
3643
	IF replace = with_ THEN RETURN; END IF;
3644
3645
	PERFORM util.schema_rm(replace);
3646
	PERFORM util.schema_rename(with_, replace);
3647
END;
3648
$$;
3649
3650
3651
--
3652 12132 aaronmk
-- Name: schema_rm(text); Type: FUNCTION; Schema: util; Owner: -
3653
--
3654
3655
CREATE FUNCTION schema_rm(schema text) RETURNS void
3656
    LANGUAGE sql
3657
    AS $_$
3658
SELECT util.eval($$DROP SCHEMA IF EXISTS $$||quote_ident($1)||$$ CASCADE$$);
3659
$_$;
3660
3661
3662
--
3663 9825 aaronmk
-- Name: search_path_append(text); Type: FUNCTION; Schema: util; Owner: -
3664
--
3665
3666
CREATE FUNCTION search_path_append(schemas text) RETURNS void
3667 12446 aaronmk
    LANGUAGE sql
3668 9825 aaronmk
    AS $_$
3669
SELECT util.eval(
3670
$$SET search_path TO $$||current_setting('search_path')||$$, $$||$1);
3671
$_$;
3672
3673
3674
--
3675 8183 aaronmk
-- Name: set_col_names(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
3676 8153 aaronmk
--
3677
3678
CREATE FUNCTION set_col_names(table_ regclass, names regclass) RETURNS void
3679
    LANGUAGE plpgsql STRICT
3680
    AS $_$
3681
DECLARE
3682 8183 aaronmk
    old text[] = ARRAY(SELECT util.col_names(table_));
3683
    new text[] = ARRAY(SELECT util.map_values(names));
3684 8153 aaronmk
BEGIN
3685
    old = old[1:array_length(new, 1)]; -- truncate to same length
3686 10345 aaronmk
    PERFORM util.eval($$ALTER TABLE $$||$1||$$ RENAME $$||quote_ident(key)
3687
||$$ TO $$||quote_ident(value))
3688 10149 aaronmk
    FROM each(hstore(old, new))
3689
    WHERE value != key -- not same name
3690
    ;
3691 8153 aaronmk
END;
3692
$_$;
3693
3694
3695
--
3696 8183 aaronmk
-- Name: FUNCTION set_col_names(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
3697 8153 aaronmk
--
3698
3699 12235 aaronmk
COMMENT ON FUNCTION set_col_names(table_ regclass, names regclass) IS '
3700
idempotent
3701
';
3702 8153 aaronmk
3703
3704
--
3705 10145 aaronmk
-- Name: set_col_names_with_metadata(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
3706
--
3707
3708
CREATE FUNCTION set_col_names_with_metadata(table_ regclass, names regclass) RETURNS void
3709
    LANGUAGE plpgsql STRICT
3710
    AS $_$
3711
DECLARE
3712
	row_ util.map;
3713
BEGIN
3714 10715 aaronmk
	-- rename any metadata cols rather than re-adding them with new names
3715
	BEGIN
3716
		PERFORM util.set_col_names(table_, names);
3717
	EXCEPTION
3718
		WHEN array_subscript_error THEN -- selective suppress
3719
			IF SQLERRM LIKE 'arrays must have same bounds' THEN NULL;
3720
				-- metadata cols not yet added
3721 12568 aaronmk
			ELSE RAISE;
3722 10715 aaronmk
			END IF;
3723
	END;
3724
3725 10157 aaronmk
	FOR row_ IN EXECUTE $$SELECT * FROM $$||names||$$ WHERE "from" LIKE ':%'$$
3726 10145 aaronmk
	LOOP
3727 10147 aaronmk
		PERFORM util.mk_const_col((table_, row_."to"),
3728
			substring(row_."from" from 2));
3729 10145 aaronmk
	END LOOP;
3730
3731
	PERFORM util.set_col_names(table_, names);
3732
END;
3733
$_$;
3734
3735
3736
--
3737
-- Name: FUNCTION set_col_names_with_metadata(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
3738
--
3739
3740 12235 aaronmk
COMMENT ON FUNCTION set_col_names_with_metadata(table_ regclass, names regclass) IS '
3741
idempotent.
3742
the metadata mappings must be *last* in the names table.
3743
';
3744 10145 aaronmk
3745
3746
--
3747 8183 aaronmk
-- Name: set_col_types(regclass, col_cast[]); Type: FUNCTION; Schema: util; Owner: -
3748 8107 aaronmk
--
3749
3750
CREATE FUNCTION set_col_types(table_ regclass, col_casts col_cast[]) RETURNS void
3751 12733 aaronmk
    LANGUAGE sql
3752 8107 aaronmk
    AS $_$
3753 12734 aaronmk
SELECT util.eval(COALESCE(
3754
$$ALTER TABLE $$||$1||$$
3755 12732 aaronmk
$$||(
3756
	SELECT
3757
	string_agg($$ALTER COLUMN $$||col_name_sql||$$ TYPE $$||target_type
3758
	||$$ USING $$||col_name_sql||$$::$$||target_type, $$
3759
, $$)
3760
	FROM
3761
	(
3762
		SELECT
3763
		  quote_ident(col_name) AS col_name_sql
3764 12733 aaronmk
		, util.col_type(($1, col_name)) AS curr_type
3765 12732 aaronmk
		, type AS target_type
3766 12733 aaronmk
		FROM unnest($2)
3767 12732 aaronmk
	) s
3768
	WHERE curr_type != target_type
3769 12734 aaronmk
), ''))
3770 8107 aaronmk
$_$;
3771
3772
3773
--
3774 8183 aaronmk
-- Name: FUNCTION set_col_types(table_ regclass, col_casts col_cast[]); Type: COMMENT; Schema: util; Owner: -
3775 8107 aaronmk
--
3776
3777 12235 aaronmk
COMMENT ON FUNCTION set_col_types(table_ regclass, col_casts col_cast[]) IS '
3778
idempotent
3779
';
3780 8107 aaronmk
3781
3782
--
3783 12302 aaronmk
-- Name: set_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
3784
--
3785
3786
CREATE FUNCTION set_comment(table_ regclass, comment text) RETURNS void
3787 12446 aaronmk
    LANGUAGE sql
3788 12302 aaronmk
    AS $_$
3789 13477 aaronmk
SELECT util.eval(util.mk_set_comment($1, $2))
3790 12302 aaronmk
$_$;
3791
3792
3793
--
3794 12482 aaronmk
-- Name: set_search_path(text, boolean); Type: FUNCTION; Schema: util; Owner: -
3795
--
3796
3797
CREATE FUNCTION set_search_path(search_path text, for_session boolean DEFAULT false) RETURNS void
3798
    LANGUAGE sql
3799
    AS $_$
3800
SELECT util.eval(util.mk_set_search_path($1, $2))
3801
$_$;
3802
3803
3804
--
3805 13468 aaronmk
-- Name: show_create_view(regclass, boolean); Type: FUNCTION; Schema: util; Owner: -
3806 11651 aaronmk
--
3807
3808 13468 aaronmk
CREATE FUNCTION show_create_view(view_ regclass, replace boolean DEFAULT true) RETURNS text
3809 11651 aaronmk
    LANGUAGE sql STABLE
3810
    AS $_$
3811 13468 aaronmk
SELECT $$CREATE$$||(CASE WHEN $2 THEN $$ OR REPLACE$$ ELSE '' END)||$$ VIEW $$
3812
||$1||$$ AS
3813 13482 aaronmk
$$||pg_get_viewdef($1)/*no ; because pg_get_viewdef() includes one*/||$$
3814 13505 aaronmk
$$||util.mk_set_relation_metadata($1)
3815 11651 aaronmk
$_$;
3816
3817
3818
--
3819 11655 aaronmk
-- Name: show_grants_for(regclass); Type: FUNCTION; Schema: util; Owner: -
3820
--
3821
3822
CREATE FUNCTION show_grants_for(table_ regclass) RETURNS text
3823
    LANGUAGE sql STABLE
3824
    AS $_$
3825 12269 aaronmk
SELECT string_agg(cmd, '')
3826 11655 aaronmk
FROM
3827
(
3828
	SELECT (CASE WHEN has_table_privilege(user_, $1, 'SELECT') THEN
3829
$$GRANT SELECT ON TABLE $$||$1||$$ TO $$||quote_ident(user_)||$$;
3830
$$ ELSE '' END) AS cmd
3831
	FROM util.grants_users() f (user_)
3832
) s
3833
$_$;
3834
3835
3836
--
3837 12325 aaronmk
-- Name: show_relations_like(text, text, character[]); Type: FUNCTION; Schema: util; Owner: -
3838
--
3839
3840 12670 aaronmk
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
3841 12325 aaronmk
    LANGUAGE sql STABLE
3842
    AS $_$
3843
SELECT oid FROM pg_class
3844
WHERE relkind = ANY($3) AND relname ~ $1
3845
AND util.schema_matches(util.schema(relnamespace), $2)
3846
ORDER BY relname
3847
$_$;
3848
3849
3850
--
3851 13478 aaronmk
-- Name: show_set_comment(regclass); Type: FUNCTION; Schema: util; Owner: -
3852
--
3853
3854
CREATE FUNCTION show_set_comment(table_ regclass) RETURNS text
3855
    LANGUAGE sql STABLE
3856
    AS $_$
3857 13480 aaronmk
SELECT util.mk_set_comment($1, util.comment($1))
3858 13478 aaronmk
$_$;
3859
3860
3861
--
3862 12592 aaronmk
-- Name: show_types_like(text, text); Type: FUNCTION; Schema: util; Owner: -
3863
--
3864
3865
CREATE FUNCTION show_types_like(name_regexp text DEFAULT ''::text, schema_regexp text DEFAULT ''::text) RETURNS SETOF regtype
3866
    LANGUAGE sql STABLE
3867
    AS $_$
3868
SELECT oid
3869
FROM pg_type
3870
WHERE typname ~ $1 AND util.schema_matches(util.schema(typnamespace), $2)
3871
ORDER BY typname
3872
$_$;
3873
3874
3875
--
3876 12305 aaronmk
-- Name: show_views_like(text, text); Type: FUNCTION; Schema: util; Owner: -
3877
--
3878
3879 12385 aaronmk
CREATE FUNCTION show_views_like(name_regexp text, schema_regexp text DEFAULT ''::text) RETURNS SETOF regclass
3880 12305 aaronmk
    LANGUAGE sql STABLE
3881
    AS $_$
3882 12385 aaronmk
SELECT * FROM util.show_relations_like($1, $2, ARRAY['v'])
3883 12305 aaronmk
$_$;
3884
3885
3886
--
3887 12384 aaronmk
-- Name: str_equality_regexp(text); Type: FUNCTION; Schema: util; Owner: -
3888
--
3889
3890
CREATE FUNCTION str_equality_regexp(literal text) RETURNS text
3891
    LANGUAGE sql IMMUTABLE
3892
    AS $_$
3893
SELECT '^'||util.regexp_quote($1)||'$'
3894
$_$;
3895
3896
3897
--
3898 8183 aaronmk
-- Name: table2hstore(regclass); Type: FUNCTION; Schema: util; Owner: -
3899 8144 aaronmk
--
3900
3901
CREATE FUNCTION table2hstore(table_ regclass) RETURNS hstore
3902 8145 aaronmk
    LANGUAGE plpgsql STABLE STRICT
3903 8144 aaronmk
    AS $_$
3904
DECLARE
3905
    hstore hstore;
3906
BEGIN
3907
    EXECUTE $$SELECT hstore(ARRAY(SELECT unnest(ARRAY["from", "to"]) FROM $$||
3908
        table_||$$))$$ INTO STRICT hstore;
3909
    RETURN hstore;
3910
END;
3911
$_$;
3912
3913
3914
--
3915 10184 aaronmk
-- Name: table_flag__get(regclass, text); Type: FUNCTION; Schema: util; Owner: -
3916
--
3917
3918
CREATE FUNCTION table_flag__get(table_ regclass, flag text) RETURNS boolean
3919 12446 aaronmk
    LANGUAGE sql STABLE
3920 10184 aaronmk
    AS $_$
3921
SELECT COUNT(*) > 0 FROM pg_constraint
3922
WHERE conrelid = $1 AND contype = 'c' AND conname = $2
3923
$_$;
3924
3925
3926
--
3927
-- Name: FUNCTION table_flag__get(table_ regclass, flag text); Type: COMMENT; Schema: util; Owner: -
3928
--
3929
3930 12235 aaronmk
COMMENT ON FUNCTION table_flag__get(table_ regclass, flag text) IS '
3931
gets whether a status flag is set by the presence of a table constraint
3932
';
3933 10184 aaronmk
3934
3935
--
3936 10182 aaronmk
-- Name: table_flag__set(regclass, text); Type: FUNCTION; Schema: util; Owner: -
3937
--
3938
3939
CREATE FUNCTION table_flag__set(table_ regclass, flag text) RETURNS void
3940 12446 aaronmk
    LANGUAGE sql
3941 10182 aaronmk
    AS $_$
3942
SELECT util.create_if_not_exists($$ALTER TABLE $$||$1||$$ ADD CONSTRAINT $$
3943
||quote_ident($2)||$$ CHECK (true)$$)
3944
$_$;
3945
3946
3947
--
3948
-- Name: FUNCTION table_flag__set(table_ regclass, flag text); Type: COMMENT; Schema: util; Owner: -
3949
--
3950
3951 12235 aaronmk
COMMENT ON FUNCTION table_flag__set(table_ regclass, flag text) IS '
3952
stores a status flag by the presence of a table constraint.
3953
idempotent.
3954
';
3955 10182 aaronmk
3956
3957
--
3958 10185 aaronmk
-- Name: table_nulls_mapped__get(regclass); Type: FUNCTION; Schema: util; Owner: -
3959
--
3960
3961
CREATE FUNCTION table_nulls_mapped__get(table_ regclass) RETURNS boolean
3962 12446 aaronmk
    LANGUAGE sql STABLE
3963 10185 aaronmk
    AS $_$
3964
SELECT util.table_flag__get($1, 'nulls_mapped')
3965
$_$;
3966
3967
3968
--
3969
-- Name: FUNCTION table_nulls_mapped__get(table_ regclass); Type: COMMENT; Schema: util; Owner: -
3970
--
3971
3972 12235 aaronmk
COMMENT ON FUNCTION table_nulls_mapped__get(table_ regclass) IS '
3973
gets whether a table''s NULL-equivalent strings have been replaced with NULL
3974
';
3975 10185 aaronmk
3976
3977
--
3978 10183 aaronmk
-- Name: table_nulls_mapped__set(regclass); Type: FUNCTION; Schema: util; Owner: -
3979
--
3980
3981
CREATE FUNCTION table_nulls_mapped__set(table_ regclass) RETURNS void
3982 12446 aaronmk
    LANGUAGE sql
3983 10183 aaronmk
    AS $_$
3984
SELECT util.table_flag__set($1, 'nulls_mapped')
3985
$_$;
3986
3987
3988
--
3989
-- Name: FUNCTION table_nulls_mapped__set(table_ regclass); Type: COMMENT; Schema: util; Owner: -
3990
--
3991
3992 12235 aaronmk
COMMENT ON FUNCTION table_nulls_mapped__set(table_ regclass) IS '
3993
sets that a table''s NULL-equivalent strings have been replaced with NULL.
3994
idempotent.
3995
';
3996 10183 aaronmk
3997
3998
--
3999 12652 aaronmk
-- Name: to_freq(regclass); Type: FUNCTION; Schema: util; Owner: -
4000
--
4001
4002
CREATE FUNCTION to_freq(table_ regclass) RETURNS void
4003
    LANGUAGE sql
4004
    AS $_$
4005
-- save data before truncating main table
4006
SELECT util.copy_types_and_data($1, 'pg_temp.__copy');
4007
4008
-- repopulate main table w/ copies column
4009
SELECT util.truncate($1);
4010
SELECT util.eval($$ALTER TABLE $$||$1||$$ ADD COLUMN copies bigint NOT NULL$$);
4011
SELECT util.populate_table($1, $$
4012
SELECT (table_).*, copies
4013
FROM (
4014
	SELECT table_, COUNT(*) AS copies
4015
	FROM pg_temp.__copy table_
4016
	GROUP BY table_
4017
) s
4018
$$);
4019
4020
-- delete temp table so it doesn't stay around until end of connection
4021
SELECT util.drop_table('pg_temp.__copy');
4022
$_$;
4023
4024
4025
--
4026 8183 aaronmk
-- Name: to_global_col_names(regclass); Type: FUNCTION; Schema: util; Owner: -
4027 8088 aaronmk
--
4028
4029
CREATE FUNCTION to_global_col_names(table_ regclass) RETURNS void
4030
    LANGUAGE plpgsql STRICT
4031
    AS $_$
4032
DECLARE
4033
    row record;
4034
BEGIN
4035 8183 aaronmk
    FOR row IN SELECT * FROM util.col_global_names(table_::text::regtype)
4036 8088 aaronmk
    LOOP
4037
        IF row.global_name != row.name THEN
4038
            EXECUTE $$ALTER TABLE $$||table_||$$ RENAME $$
4039
                ||quote_ident(row.name)||$$ TO $$||quote_ident(row.global_name);
4040
        END IF;
4041
    END LOOP;
4042
END;
4043
$_$;
4044
4045
4046
--
4047 8183 aaronmk
-- Name: FUNCTION to_global_col_names(table_ regclass); Type: COMMENT; Schema: util; Owner: -
4048 8088 aaronmk
--
4049
4050 12235 aaronmk
COMMENT ON FUNCTION to_global_col_names(table_ regclass) IS '
4051
idempotent
4052
';
4053 8088 aaronmk
4054
4055
--
4056 12874 aaronmk
-- Name: trim(regclass, regclass, boolean); Type: FUNCTION; Schema: util; Owner: -
4057 10365 aaronmk
--
4058
4059 12874 aaronmk
CREATE FUNCTION "trim"(table_ regclass, names regclass, force boolean DEFAULT true) RETURNS void
4060 12446 aaronmk
    LANGUAGE sql
4061 10365 aaronmk
    AS $_$
4062 12874 aaronmk
SELECT util.drop_column(($1, col), $3) FROM util.added_cols($1, $2) f (col);
4063 10365 aaronmk
SELECT NULL::void; -- don't fold away functions called in previous query
4064
$_$;
4065
4066
4067
--
4068 12874 aaronmk
-- Name: FUNCTION "trim"(table_ regclass, names regclass, force boolean); Type: COMMENT; Schema: util; Owner: -
4069 10365 aaronmk
--
4070
4071 12874 aaronmk
COMMENT ON FUNCTION "trim"(table_ regclass, names regclass, force boolean) IS '
4072
trims table_ to include only columns in the original data
4073
4074
by default, cascadingly drops dependent columns so that they don''t prevent
4075
trim() from succeeding. note that this requires the dependent columns to then be
4076
manually re-created.
4077
4078
idempotent
4079 12235 aaronmk
';
4080 10365 aaronmk
4081
4082
--
4083 8183 aaronmk
-- Name: truncate(regclass); Type: FUNCTION; Schema: util; Owner: -
4084 8142 aaronmk
--
4085
4086
CREATE FUNCTION truncate(table_ regclass) RETURNS void
4087
    LANGUAGE plpgsql STRICT
4088
    AS $_$
4089
BEGIN
4090
    EXECUTE $$TRUNCATE $$||table_||$$ CASCADE$$;
4091
END;
4092
$_$;
4093
4094
4095
--
4096 8183 aaronmk
-- Name: FUNCTION truncate(table_ regclass); Type: COMMENT; Schema: util; Owner: -
4097 8142 aaronmk
--
4098
4099 12235 aaronmk
COMMENT ON FUNCTION truncate(table_ regclass) IS '
4100
idempotent
4101
';
4102 8142 aaronmk
4103
4104
--
4105 12357 aaronmk
-- Name: truncated_prefixed_name_regexp(text, integer); Type: FUNCTION; Schema: util; Owner: -
4106
--
4107
4108
CREATE FUNCTION truncated_prefixed_name_regexp(name text, max_prefix_len integer) RETURNS text
4109
    LANGUAGE sql IMMUTABLE
4110
    AS $_$
4111 12361 aaronmk
SELECT '^(.*)'||util._if(util.name_was_truncated($1, $2),
4112 12362 aaronmk
util.regexp_quote(util.rtrim_n($1, $2))||'.*', util.regexp_quote($1)) ||'$'
4113 12357 aaronmk
$_$;
4114
4115
4116
--
4117 13135 aaronmk
-- Name: try_cast(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
4118
--
4119
4120
CREATE FUNCTION try_cast(value text, ret_type_null anyelement) RETURNS anyelement
4121
    LANGUAGE plpgsql IMMUTABLE
4122
    AS $$
4123
BEGIN
4124
	/* need explicit cast because some types not implicitly-castable, and also
4125
	to make the cast happen inside the try block. (*implicit* casts to the
4126
	return type happen at the end of the function, outside any block.) */
4127
	RETURN util.cast(value, ret_type_null);
4128
EXCEPTION
4129 13493 aaronmk
WHEN   data_exception
4130
	OR syntax_error_or_access_rule_violation -- eg. ::regclass
4131
	THEN
4132 13135 aaronmk
	PERFORM util.raise('WARNING', SQLERRM);
4133
	RETURN NULL;
4134
END;
4135
$$;
4136
4137
4138
--
4139
-- Name: FUNCTION try_cast(value text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
4140
--
4141
4142
COMMENT ON FUNCTION try_cast(value text, ret_type_null anyelement) IS '
4143
ret_type_null: NULL::ret_type
4144
';
4145
4146
4147
--
4148 8199 aaronmk
-- Name: try_create(text); Type: FUNCTION; Schema: util; Owner: -
4149
--
4150
4151
CREATE FUNCTION try_create(sql text) RETURNS void
4152
    LANGUAGE plpgsql STRICT
4153
    AS $$
4154
BEGIN
4155 12658 aaronmk
	PERFORM util.eval(sql);
4156 8199 aaronmk
EXCEPTION
4157 12676 aaronmk
WHEN   not_null_violation
4158
		/* trying to add NOT NULL column to parent table, which cascades to
4159
		child table whose values for the new column will be NULL */
4160
	OR wrong_object_type -- trying to alter a view's columns
4161
	OR undefined_column
4162
	OR duplicate_column
4163
THEN NULL;
4164 12684 aaronmk
WHEN datatype_mismatch THEN
4165
	IF SQLERRM LIKE 'child table is missing column %' THEN NULL;
4166
	ELSE RAISE; -- rethrow
4167
	END IF;
4168 8199 aaronmk
END;
4169
$$;
4170
4171
4172
--
4173
-- Name: FUNCTION try_create(sql text); Type: COMMENT; Schema: util; Owner: -
4174
--
4175
4176 12235 aaronmk
COMMENT ON FUNCTION try_create(sql text) IS '
4177
idempotent
4178
';
4179 8199 aaronmk
4180
4181
--
4182 8209 aaronmk
-- Name: try_mk_derived_col(col_ref, text); Type: FUNCTION; Schema: util; Owner: -
4183
--
4184
4185
CREATE FUNCTION try_mk_derived_col(col col_ref, expr text) RETURNS void
4186 12446 aaronmk
    LANGUAGE sql
4187 8209 aaronmk
    AS $_$
4188
SELECT util.try_create($$SELECT util.mk_derived_col($$||quote_literal($1)||$$, $$||quote_literal($2)||$$)$$)
4189
$_$;
4190
4191
4192
--
4193
-- Name: FUNCTION try_mk_derived_col(col col_ref, expr text); Type: COMMENT; Schema: util; Owner: -
4194
--
4195
4196 12235 aaronmk
COMMENT ON FUNCTION try_mk_derived_col(col col_ref, expr text) IS '
4197
idempotent
4198
';
4199 8209 aaronmk
4200
4201
--
4202 8189 aaronmk
-- Name: type_qual(anyelement); Type: FUNCTION; Schema: util; Owner: -
4203
--
4204
4205
CREATE FUNCTION type_qual(value anyelement) RETURNS text
4206
    LANGUAGE sql IMMUTABLE
4207
    AS $_$
4208
SELECT CASE WHEN $1 IS NULL THEN '' ELSE $$ NOT NULL$$ END
4209
$_$;
4210
4211
4212
--
4213 10161 aaronmk
-- Name: FUNCTION type_qual(value anyelement); Type: COMMENT; Schema: util; Owner: -
4214
--
4215
4216 12235 aaronmk
COMMENT ON FUNCTION type_qual(value anyelement) IS '
4217
a type''s NOT NULL qualifier
4218
';
4219 10161 aaronmk
4220
4221
--
4222 12562 aaronmk
-- Name: typed_cols(regtype); Type: FUNCTION; Schema: util; Owner: -
4223
--
4224
4225
CREATE FUNCTION typed_cols(type regtype) RETURNS SETOF col_cast
4226 12590 aaronmk
    LANGUAGE sql STABLE
4227 12562 aaronmk
    AS $_$
4228
SELECT (attname::text, atttypid)::util.col_cast
4229
FROM pg_attribute
4230
WHERE attrelid = $1::text::regclass AND attnum >= 1 AND NOT attisdropped
4231
ORDER BY attnum
4232
$_$;
4233
4234
4235
--
4236 12438 aaronmk
-- Name: typeof(anyelement); Type: FUNCTION; Schema: util; Owner: -
4237
--
4238
4239
CREATE FUNCTION typeof(value anyelement) RETURNS text
4240
    LANGUAGE sql IMMUTABLE
4241
    AS $_$
4242
SELECT util.qual_name(pg_typeof($1))
4243
$_$;
4244
4245
4246
--
4247 8185 aaronmk
-- Name: typeof(text, regtype); Type: FUNCTION; Schema: util; Owner: -
4248
--
4249
4250 10160 aaronmk
CREATE FUNCTION typeof(expr text, table_ regtype DEFAULT NULL::regtype) RETURNS regtype
4251
    LANGUAGE plpgsql STABLE
4252 8185 aaronmk
    AS $_$
4253
DECLARE
4254
    type regtype;
4255
BEGIN
4256 10160 aaronmk
    EXECUTE $$SELECT pg_typeof($$||expr||$$)$$||
4257
COALESCE($$ FROM (SELECT (NULL::$$||table_||$$).*) _s$$, '') INTO STRICT type;
4258 8185 aaronmk
    RETURN type;
4259
END;
4260
$_$;
4261
4262
4263
--
4264 12490 aaronmk
-- Name: use_schema(anyelement); Type: FUNCTION; Schema: util; Owner: -
4265 12483 aaronmk
--
4266
4267 12490 aaronmk
CREATE FUNCTION use_schema(schema_anchor anyelement) RETURNS void
4268 12483 aaronmk
    LANGUAGE sql
4269
    AS $_$
4270 12488 aaronmk
SELECT util.set_search_path(util.mk_search_path(util.schema($1)))
4271 12483 aaronmk
$_$;
4272
4273
4274
--
4275 12490 aaronmk
-- Name: FUNCTION use_schema(schema_anchor anyelement); Type: COMMENT; Schema: util; Owner: -
4276 12488 aaronmk
--
4277
4278 12490 aaronmk
COMMENT ON FUNCTION use_schema(schema_anchor anyelement) IS '
4279 12488 aaronmk
auto-appends util to the search_path to enable use of util operators
4280
';
4281
4282
4283
--
4284 9959 aaronmk
-- Name: all_same(anyelement); Type: AGGREGATE; Schema: util; Owner: -
4285
--
4286
4287
CREATE AGGREGATE all_same(anyelement) (
4288
    SFUNC = all_same_transform,
4289
    STYPE = anyarray,
4290
    FINALFUNC = all_same_final
4291
);
4292
4293
4294
--
4295
-- Name: AGGREGATE all_same(anyelement); Type: COMMENT; Schema: util; Owner: -
4296
--
4297
4298 12235 aaronmk
COMMENT ON AGGREGATE all_same(anyelement) IS '
4299
includes NULLs in comparison
4300
';
4301 9959 aaronmk
4302
4303
--
4304 8183 aaronmk
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: util; Owner: -
4305 2595 aaronmk
--
4306
4307
CREATE AGGREGATE join_strs(text, text) (
4308 4052 aaronmk
    SFUNC = join_strs_transform,
4309 4010 aaronmk
    STYPE = text
4310 2595 aaronmk
);
4311
4312
4313 8147 aaronmk
--
4314 12423 aaronmk
-- Name: %==; Type: OPERATOR; Schema: util; Owner: -
4315
--
4316
4317
CREATE OPERATOR %== (
4318
    PROCEDURE = "%==",
4319
    LEFTARG = anyelement,
4320
    RIGHTARG = anyelement
4321
);
4322
4323
4324
--
4325
-- Name: OPERATOR %== (anyelement, anyelement); Type: COMMENT; Schema: util; Owner: -
4326
--
4327
4328
COMMENT ON OPERATOR %== (anyelement, anyelement) IS '
4329
returns whether the map-keys of the compared values are the same
4330
(mnemonic: % is the Perl symbol for a hash map)
4331
4332
should be overridden for types that store both keys and values
4333
4334
used in a FULL JOIN to select which columns to join on
4335
';
4336
4337
4338
--
4339 8183 aaronmk
-- Name: ->; Type: OPERATOR; Schema: util; Owner: -
4340 8147 aaronmk
--
4341
4342
CREATE OPERATOR -> (
4343
    PROCEDURE = map_get,
4344
    LEFTARG = regclass,
4345
    RIGHTARG = text
4346
);
4347
4348
4349 10308 aaronmk
--
4350
-- Name: =>; Type: OPERATOR; Schema: util; Owner: -
4351
--
4352
4353
CREATE OPERATOR => (
4354
    PROCEDURE = hstore,
4355 10357 aaronmk
    LEFTARG = text[],
4356 10608 aaronmk
    RIGHTARG = text
4357 10308 aaronmk
);
4358
4359
4360
--
4361 10608 aaronmk
-- Name: OPERATOR => (text[], text); Type: COMMENT; Schema: util; Owner: -
4362 10308 aaronmk
--
4363
4364 12235 aaronmk
COMMENT ON OPERATOR => (text[], text) IS '
4365
usage: array[''key1'', ...]::text[] => ''value''
4366
';
4367 10308 aaronmk
4368
4369 10391 aaronmk
--
4370 10613 aaronmk
-- Name: ?*>=; Type: OPERATOR; Schema: util; Owner: -
4371
--
4372
4373
CREATE OPERATOR ?*>= (
4374
    PROCEDURE = is_populated_more_often_than,
4375
    LEFTARG = anyelement,
4376
    RIGHTARG = anyelement
4377
);
4378
4379
4380
--
4381 10391 aaronmk
-- Name: ?>=; Type: OPERATOR; Schema: util; Owner: -
4382
--
4383
4384
CREATE OPERATOR ?>= (
4385
    PROCEDURE = is_more_complete_than,
4386
    LEFTARG = anyelement,
4387
    RIGHTARG = anyelement
4388
);
4389
4390
4391 11005 aaronmk
--
4392
-- Name: ||%; Type: OPERATOR; Schema: util; Owner: -
4393
--
4394
4395
CREATE OPERATOR ||% (
4396
    PROCEDURE = concat_esc,
4397
    LEFTARG = text,
4398
    RIGHTARG = text
4399
);
4400
4401
4402
--
4403
-- Name: OPERATOR ||% (text, text); Type: COMMENT; Schema: util; Owner: -
4404
--
4405
4406 12235 aaronmk
COMMENT ON OPERATOR ||% (text, text) IS '
4407
% indicates an identifier, as in Perl hashes and one of the x86 assembler syntaxes for registers
4408
';
4409 11005 aaronmk
4410
4411 2107 aaronmk
--
4412 8183 aaronmk
-- Name: map; Type: TABLE; Schema: util; Owner: -; Tablespace:
4413 8140 aaronmk
--
4414
4415
CREATE TABLE map (
4416
    "from" text NOT NULL,
4417 8158 aaronmk
    "to" text,
4418
    filter text,
4419
    notes text
4420 8140 aaronmk
);
4421
4422
4423
--
4424 11834 aaronmk
-- Data for Name: explain; Type: TABLE DATA; Schema: util; Owner: -
4425
--
4426
4427
4428
4429
--
4430 8183 aaronmk
-- Data for Name: map; Type: TABLE DATA; Schema: util; Owner: -
4431 8140 aaronmk
--
4432
4433
4434
4435
--
4436 10342 aaronmk
-- Name: map__unique__from; Type: CONSTRAINT; Schema: util; Owner: -; Tablespace:
4437 8140 aaronmk
--
4438
4439
ALTER TABLE ONLY map
4440 10342 aaronmk
    ADD CONSTRAINT map__unique__from UNIQUE ("from");
4441 8140 aaronmk
4442
4443
--
4444 10343 aaronmk
-- Name: map__unique__to; Type: CONSTRAINT; Schema: util; Owner: -; Tablespace:
4445
--
4446
4447
ALTER TABLE ONLY map
4448
    ADD CONSTRAINT map__unique__to UNIQUE ("to");
4449
4450
4451
--
4452 10110 aaronmk
-- Name: map_filter_insert; Type: TRIGGER; Schema: util; Owner: -
4453
--
4454
4455
CREATE TRIGGER map_filter_insert BEFORE INSERT ON map FOR EACH ROW EXECUTE PROCEDURE map_filter_insert();
4456
4457
4458
--
4459 2136 aaronmk
-- PostgreSQL database dump complete
4460
--