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 13599 aaronmk
-- Name: copy(regclass, text); Type: FUNCTION; Schema: util; Owner: -
1032
--
1033
1034
CREATE FUNCTION copy(from_ regclass, to_ text) RETURNS void
1035
    LANGUAGE sql
1036
    AS $_$
1037
SELECT util.copy_struct($1, $2);
1038
SELECT util.copy_data($1, $2);
1039
$_$;
1040
1041
1042
--
1043 13598 aaronmk
-- Name: copy_data(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
1044
--
1045
1046
CREATE FUNCTION copy_data(from_ regclass, to_ regclass) RETURNS void
1047
    LANGUAGE sql
1048
    AS $_$
1049
SELECT util.eval($$INSERT INTO $$||$2||$$ SELECT * FROM $$||$1)
1050
$_$;
1051
1052
1053
--
1054 12288 aaronmk
-- Name: copy_struct(regclass, text); Type: FUNCTION; Schema: util; Owner: -
1055
--
1056
1057
CREATE FUNCTION copy_struct(from_ regclass, to_ text) RETURNS void
1058
    LANGUAGE sql
1059
    AS $_$
1060
SELECT util.eval($$CREATE TABLE $$||$2||$$ (LIKE $$||$1||$$ INCLUDING ALL)$$)
1061
$_$;
1062
1063
1064
--
1065 12649 aaronmk
-- Name: copy_types_and_data(regclass, text); Type: FUNCTION; Schema: util; Owner: -
1066
--
1067
1068
CREATE FUNCTION copy_types_and_data(from_ regclass, to_ text) RETURNS void
1069
    LANGUAGE sql
1070
    AS $_$
1071
SELECT util.materialize_view($2, $1)
1072
$_$;
1073
1074
1075
--
1076 13495 aaronmk
-- Name: create_if_not_exists(text, text); Type: FUNCTION; Schema: util; Owner: -
1077 8094 aaronmk
--
1078
1079 13495 aaronmk
CREATE FUNCTION create_if_not_exists(sql text, relation text DEFAULT NULL::text) RETURNS void
1080
    LANGUAGE plpgsql
1081 8094 aaronmk
    AS $$
1082
BEGIN
1083 13495 aaronmk
	/* always generate standard exception if exists, even if table definition
1084
	would be invalid (which generates a variety of exceptions) */
1085 13529 aaronmk
	IF util.relation_exists(relation) THEN
1086
		PERFORM util.raise('NOTICE', relation||' already exists, skipping');
1087
		RAISE duplicate_table;
1088
	END IF;
1089 12595 aaronmk
	PERFORM util.eval(sql);
1090 8094 aaronmk
EXCEPTION
1091 12676 aaronmk
WHEN   duplicate_table
1092
	OR duplicate_object -- eg. constraint
1093
	OR duplicate_column
1094
	OR duplicate_function
1095
THEN NULL;
1096 12595 aaronmk
WHEN invalid_table_definition THEN
1097
	IF SQLERRM LIKE 'multiple primary keys for table % are not allowed' THEN NULL;
1098
	ELSE RAISE;
1099
	END IF;
1100 8094 aaronmk
END;
1101
$$;
1102
1103
1104
--
1105 13495 aaronmk
-- Name: FUNCTION create_if_not_exists(sql text, relation text); Type: COMMENT; Schema: util; Owner: -
1106 8094 aaronmk
--
1107
1108 13495 aaronmk
COMMENT ON FUNCTION create_if_not_exists(sql text, relation text) IS '
1109 12235 aaronmk
idempotent
1110
';
1111 8094 aaronmk
1112
1113
--
1114 12378 aaronmk
-- Name: curr_func(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
1115
--
1116
1117
CREATE FUNCTION curr_func(func text, schema_anchor anyelement) RETURNS text
1118 12590 aaronmk
    LANGUAGE sql STABLE
1119 12378 aaronmk
    AS $$
1120
SELECT util.schema_esc(schema_anchor)||'.'||quote_ident(func)
1121
$$;
1122
1123
1124
--
1125 12668 aaronmk
-- Name: debug_print_func_call(text); Type: FUNCTION; Schema: util; Owner: -
1126
--
1127
1128
CREATE FUNCTION debug_print_func_call(func_call text) RETURNS void
1129
    LANGUAGE sql IMMUTABLE
1130
    AS $_$
1131
SELECT util.raise('NOTICE', $$SELECT $$||$1)
1132
$_$;
1133
1134
1135
--
1136 12433 aaronmk
-- Name: debug_print_return_value(anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1137 12430 aaronmk
--
1138
1139 12433 aaronmk
CREATE FUNCTION debug_print_return_value(value anyelement, encode boolean DEFAULT false) RETURNS anyelement
1140 12430 aaronmk
    LANGUAGE sql IMMUTABLE
1141
    AS $_$
1142 13447 aaronmk
SELECT util.debug_print_value('returns: ', $1, $2);
1143 12430 aaronmk
SELECT $1;
1144
$_$;
1145
1146
1147
--
1148 12250 aaronmk
-- Name: debug_print_sql(text); Type: FUNCTION; Schema: util; Owner: -
1149
--
1150
1151
CREATE FUNCTION debug_print_sql(sql text) RETURNS void
1152
    LANGUAGE sql IMMUTABLE
1153
    AS $_$
1154
/* newline before so the query starts at the beginning of the line.
1155
newline after to visually separate queries from one another. */
1156 12534 aaronmk
SELECT util.raise('NOTICE', $$
1157 12474 aaronmk
$$||util.runnable_sql($1)||$$
1158 12464 aaronmk
$$||COALESCE(util.explain2notice_msg_if_can($1), ''))
1159 12250 aaronmk
$_$;
1160
1161
1162
--
1163 13446 aaronmk
-- Name: debug_print_value(text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1164
--
1165
1166
CREATE FUNCTION debug_print_value(label text, value anyelement, encode boolean DEFAULT false) RETURNS void
1167
    LANGUAGE sql IMMUTABLE
1168
    AS $_$
1169
SELECT util.raise('NOTICE', concat($1,
1170 13449 aaronmk
(CASE WHEN $3 THEN util.quote_typed($2) ELSE $2::text END))||$$
1171
$$)
1172 13446 aaronmk
$_$;
1173
1174
1175
--
1176 13448 aaronmk
-- Name: debug_print_var(text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1177
--
1178
1179
CREATE FUNCTION debug_print_var(var text, value anyelement, encode boolean DEFAULT false) RETURNS void
1180
    LANGUAGE sql IMMUTABLE
1181
    AS $_$
1182
/* can't use EXECUTE in the caller because "No substitution of PL/pgSQL
1183
variables is done on the computed command string"
1184
(http://www.postgresql.org/docs/9.3/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN) */
1185
SELECT util.debug_print_value($1||' = ', $2, $3)
1186
$_$;
1187
1188
1189
--
1190 10364 aaronmk
-- Name: derived_cols(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
1191
--
1192
1193
CREATE FUNCTION derived_cols(table_ regclass, names regclass) RETURNS SETOF text
1194 12446 aaronmk
    LANGUAGE sql STABLE
1195 10364 aaronmk
    AS $_$
1196
SELECT util.eval2set($$
1197
SELECT col
1198
FROM util.col_names($$||quote_nullable($1)||$$::regclass) f (col)
1199
LEFT JOIN $$||$2||$$ ON "to" = col
1200
WHERE "from" IS NULL
1201
$$, NULL::text)
1202
$_$;
1203
1204
1205
--
1206
-- Name: FUNCTION derived_cols(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
1207
--
1208
1209 12235 aaronmk
COMMENT ON FUNCTION derived_cols(table_ regclass, names regclass) IS '
1210
gets table_''s derived columns (all the columns not in the names table)
1211
';
1212 10364 aaronmk
1213
1214
--
1215 12298 aaronmk
-- Name: diff(regclass, regclass, anyelement); Type: FUNCTION; Schema: util; Owner: -
1216 12044 aaronmk
--
1217
1218 12298 aaronmk
CREATE FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) RETURNS SETOF record
1219 12427 aaronmk
    LANGUAGE sql
1220 12044 aaronmk
    AS $_$
1221 12653 aaronmk
-- create a diff when the # of copies of a row differs between the tables
1222
SELECT util.to_freq($1);
1223
SELECT util.to_freq($2);
1224 12663 aaronmk
SELECT util.auto_rm_freq(ARRAY[$1, $2]);
1225 12653 aaronmk
1226
SELECT * FROM util.diff($1, $2, $3, has_freq := true)
1227 12298 aaronmk
$_$;
1228
1229
1230
--
1231
-- Name: FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement); Type: COMMENT; Schema: util; Owner: -
1232
--
1233
1234
COMMENT ON FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) IS '
1235
usage:
1236
SELECT * FROM util.diff(''"left_table"''::regclass, ''"right_table"''::regclass, NULL::shared_base_type)
1237 12653 aaronmk
1238
col_type_null (*required*): NULL::shared_base_type
1239 12298 aaronmk
';
1240
1241
1242
--
1243 12491 aaronmk
-- Name: diff(text, text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1244 12298 aaronmk
--
1245
1246 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
1247 12429 aaronmk
    LANGUAGE plpgsql
1248
    SET search_path TO pg_temp
1249 12298 aaronmk
    AS $_$
1250 12429 aaronmk
/* must use LANGUAGE plpgsql because LANGUAGE sql does not support runtime
1251
changes of search_path (schema elements are bound at inline time rather than
1252
runtime) */
1253
/* function option search_path is needed to limit the effects of
1254 12492 aaronmk
`SET LOCAL search_path` to the current function */
1255 12429 aaronmk
BEGIN
1256 12491 aaronmk
	PERFORM util.use_schema($3); -- includes util.%== as default/fallback
1257 12429 aaronmk
1258 12565 aaronmk
	PERFORM util.mk_keys_func(pg_typeof($3));
1259 12429 aaronmk
	RETURN QUERY
1260 12554 aaronmk
		SELECT * FROM util.eval2col_pair(util.mk_diff_query($1, $2,
1261 12479 aaronmk
$$/* need to explicitly cast each side to the return type because this does not
1262 12284 aaronmk
happen automatically even when an implicit cast is available */
1263 12479 aaronmk
  left_::$$||util.typeof($3)||$$
1264
, right_::$$||util.typeof($3)
1265 12496 aaronmk
/* when using the util.%==(anyelement, anyelement) operator, you must cast to
1266
the *same* base type, *even though* this is optional when using a custom %== */
1267 12553 aaronmk
, util._if($4, $$true/*= CROSS JOIN*/$$,
1268
$$ left_::$$||util.typeof($3)||$$
1269 12496 aaronmk
%== right_::$$||util.typeof($3)||$$
1270
	-- refer to EXPLAIN output for expansion of %==$$
1271 12657 aaronmk
)
1272
,     $$         left_::$$||util.typeof($3)||$$
1273
IS DISTINCT FROM right_::$$||util.typeof($3)
1274
), $3)
1275 12429 aaronmk
	;
1276
END;
1277 12044 aaronmk
$_$;
1278
1279
1280
--
1281 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: -
1282 12280 aaronmk
--
1283
1284 12491 aaronmk
COMMENT ON FUNCTION diff(left__ text, right__ text, col_type_null anyelement, single_row boolean, OUT left_ anyelement, OUT right_ anyelement) IS '
1285 12280 aaronmk
col_type_null (*required*): NULL::col_type
1286 12299 aaronmk
single_row: whether the tables consist of a single row, which should be
1287
	displayed side-by-side
1288 12282 aaronmk
1289 12498 aaronmk
to match up rows using a subset of the columns, create a custom keys() function
1290
which returns this subset as a record:
1291
-- note that OUT parameters for the returned fields are *not* needed
1292
CREATE OR REPLACE FUNCTION [schema].keys(value [schema].[base_type])
1293
  RETURNS record AS
1294
$BODY$
1295
SELECT ($1.key_field_0, $1.key_field_1)
1296
$BODY$
1297
  LANGUAGE sql IMMUTABLE
1298
  COST 100;
1299
1300
1301 12282 aaronmk
to run EXPLAIN on the FULL JOIN query:
1302
# run this function
1303
# look for a NOTICE containing the expanded query that it ran
1304
# run EXPLAIN on this expanded query
1305 12280 aaronmk
';
1306
1307
1308
--
1309 12653 aaronmk
-- Name: diff(regclass, regclass, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1310
--
1311
1312
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
1313
    LANGUAGE sql
1314
    AS $_$
1315
SELECT * FROM util.diff($1::text, $2::text, $3,
1316
	single_row := util.has_single_row($1) AND util.has_single_row($2))
1317
$_$;
1318
1319
1320
--
1321
-- 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: -
1322
--
1323
1324
COMMENT ON FUNCTION diff(left_table regclass, right_table regclass, col_type_null anyelement, has_freq boolean, OUT left_ anyelement, OUT right_ anyelement) IS '
1325
helper function used by diff(regclass, regclass)
1326
1327
usage:
1328
SELECT * FROM util.diff(''"left_freq_table"''::regclass, ''"right_freq_table"''::regclass, NULL::shared_base_type, has_freq := true)
1329
1330
col_type_null (*required*): NULL::shared_base_type
1331
';
1332
1333
1334
--
1335 8200 aaronmk
-- Name: do_optionally_ignore(text, boolean); Type: FUNCTION; Schema: util; Owner: -
1336
--
1337
1338
CREATE FUNCTION do_optionally_ignore(sql text, ignore boolean) RETURNS void
1339 12446 aaronmk
    LANGUAGE sql
1340 8200 aaronmk
    AS $_$
1341
SELECT CASE WHEN $2 THEN util.try_create($1) ELSE util.create_if_not_exists($1) END
1342
$_$;
1343
1344
1345
--
1346
-- Name: FUNCTION do_optionally_ignore(sql text, ignore boolean); Type: COMMENT; Schema: util; Owner: -
1347
--
1348
1349 12235 aaronmk
COMMENT ON FUNCTION do_optionally_ignore(sql text, ignore boolean) IS '
1350
idempotent
1351
';
1352 8200 aaronmk
1353
1354
--
1355 12292 aaronmk
-- Name: drop_column(col_ref, boolean); Type: FUNCTION; Schema: util; Owner: -
1356 10362 aaronmk
--
1357
1358 12292 aaronmk
CREATE FUNCTION drop_column(col col_ref, force boolean DEFAULT false) RETURNS void
1359 12446 aaronmk
    LANGUAGE sql
1360 10362 aaronmk
    AS $_$
1361
SELECT util.eval($$ALTER TABLE $$||$1.table_||$$ DROP COLUMN IF EXISTS $$||
1362 12292 aaronmk
quote_ident($1.name)||util._if($2, $$ CASCADE$$, ''::text))
1363 10362 aaronmk
$_$;
1364
1365
1366
--
1367 12292 aaronmk
-- Name: FUNCTION drop_column(col col_ref, force boolean); Type: COMMENT; Schema: util; Owner: -
1368 10362 aaronmk
--
1369
1370 12292 aaronmk
COMMENT ON FUNCTION drop_column(col col_ref, force boolean) IS '
1371 12235 aaronmk
idempotent
1372
';
1373 10362 aaronmk
1374
1375
--
1376 12660 aaronmk
-- Name: drop_column(regclass[], text, boolean); Type: FUNCTION; Schema: util; Owner: -
1377
--
1378
1379
CREATE FUNCTION drop_column(tables regclass[], col text, force boolean DEFAULT false) RETURNS void
1380
    LANGUAGE sql
1381
    AS $_$
1382 12686 aaronmk
SELECT util.drop_column((table_, $2), $3) FROM unnest($1) table_;
1383
SELECT NULL::void; -- don't fold away functions called in previous query
1384 12660 aaronmk
$_$;
1385
1386
1387
--
1388
-- Name: FUNCTION drop_column(tables regclass[], col text, force boolean); Type: COMMENT; Schema: util; Owner: -
1389
--
1390
1391
COMMENT ON FUNCTION drop_column(tables regclass[], col text, force boolean) IS '
1392
idempotent
1393
';
1394
1395
1396
--
1397 12587 aaronmk
-- Name: drop_relation(anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1398 12331 aaronmk
--
1399
1400 12587 aaronmk
CREATE FUNCTION drop_relation(relation anyelement, force boolean DEFAULT false) RETURNS void
1401 12331 aaronmk
    LANGUAGE sql
1402
    AS $_$
1403 12353 aaronmk
/* use util.qual_name() instead of ::text so that the schema qualifier is always
1404
included in the debug SQL */
1405
SELECT util.drop_relation(util.relation_type($1), util.qual_name($1), $2)
1406 12331 aaronmk
$_$;
1407
1408
1409
--
1410 12343 aaronmk
-- Name: drop_relation(text, text, boolean); Type: FUNCTION; Schema: util; Owner: -
1411
--
1412
1413 12364 aaronmk
CREATE FUNCTION drop_relation(type text, relation_esc text, force boolean DEFAULT false) RETURNS void
1414 12343 aaronmk
    LANGUAGE sql
1415
    AS $_$
1416 12347 aaronmk
SELECT util.eval($$DROP $$||$1||$$ IF EXISTS $$||$2
1417 12343 aaronmk
||util._if($3, $$ CASCADE$$, ''::text))
1418
$_$;
1419
1420
1421
--
1422 12364 aaronmk
-- Name: FUNCTION drop_relation(type text, relation_esc text, force boolean); Type: COMMENT; Schema: util; Owner: -
1423 12343 aaronmk
--
1424
1425 12364 aaronmk
COMMENT ON FUNCTION drop_relation(type text, relation_esc text, force boolean) IS '
1426 12343 aaronmk
idempotent
1427
';
1428
1429
1430
--
1431 12413 aaronmk
-- Name: drop_relations_like(text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1432
--
1433
1434
CREATE FUNCTION drop_relations_like(name_regexp text, schema_anchor anyelement, force boolean DEFAULT false) RETURNS void
1435
    LANGUAGE sql
1436
    AS $_$
1437 12502 aaronmk
SELECT util.drop_relations_like($1, util.schema_regexp($2), $3)
1438 12413 aaronmk
$_$;
1439
1440
1441
--
1442
-- Name: drop_relations_like(text, text, boolean); Type: FUNCTION; Schema: util; Owner: -
1443
--
1444
1445
CREATE FUNCTION drop_relations_like(name_regexp text, schema_regexp text DEFAULT ''::text, force boolean DEFAULT false) RETURNS void
1446
    LANGUAGE sql
1447
    AS $_$
1448 12668 aaronmk
SELECT util.debug_print_func_call(util.quote_func_call(
1449
'util.drop_relations_like', util.quote_typed($1), util.quote_typed($2),
1450
util.quote_typed($3)))
1451 12667 aaronmk
;
1452 12413 aaronmk
SELECT util.drop_relation(relation, $3)
1453
FROM util.show_relations_like($1, $2) relation
1454
;
1455
SELECT NULL::void; -- don't fold away functions called in previous query
1456
$_$;
1457
1458
1459
--
1460 12292 aaronmk
-- Name: drop_table(text, boolean); Type: FUNCTION; Schema: util; Owner: -
1461 10150 aaronmk
--
1462
1463 12292 aaronmk
CREATE FUNCTION drop_table(table_ text, force boolean DEFAULT false) RETURNS void
1464 12446 aaronmk
    LANGUAGE sql
1465 10150 aaronmk
    AS $_$
1466 12347 aaronmk
SELECT util.drop_relation('TABLE', $1, $2)
1467 10150 aaronmk
$_$;
1468
1469
1470
--
1471 12292 aaronmk
-- Name: FUNCTION drop_table(table_ text, force boolean); Type: COMMENT; Schema: util; Owner: -
1472 10150 aaronmk
--
1473
1474 12292 aaronmk
COMMENT ON FUNCTION drop_table(table_ text, force boolean) IS '
1475 12235 aaronmk
idempotent
1476
';
1477 10150 aaronmk
1478
1479
--
1480 12292 aaronmk
-- Name: drop_view(text, boolean); Type: FUNCTION; Schema: util; Owner: -
1481 12229 aaronmk
--
1482
1483 12292 aaronmk
CREATE FUNCTION drop_view(view_ text, force boolean DEFAULT false) RETURNS void
1484 12446 aaronmk
    LANGUAGE sql
1485 12229 aaronmk
    AS $_$
1486 12347 aaronmk
SELECT util.drop_relation('VIEW', $1, $2)
1487 12229 aaronmk
$_$;
1488
1489
1490
--
1491 12292 aaronmk
-- Name: FUNCTION drop_view(view_ text, force boolean); Type: COMMENT; Schema: util; Owner: -
1492 12229 aaronmk
--
1493
1494 12292 aaronmk
COMMENT ON FUNCTION drop_view(view_ text, force boolean) IS '
1495 12235 aaronmk
idempotent
1496
';
1497 12229 aaronmk
1498
1499
--
1500 10322 aaronmk
-- Name: empty_array(anyelement); Type: FUNCTION; Schema: util; Owner: -
1501
--
1502
1503
CREATE FUNCTION empty_array(elem_type_null anyelement DEFAULT NULL::text) RETURNS anyarray
1504
    LANGUAGE sql IMMUTABLE
1505
    AS $_$
1506
SELECT util.array_fill($1, 0)
1507
$_$;
1508
1509
1510
--
1511
-- Name: FUNCTION empty_array(elem_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
1512
--
1513
1514 12235 aaronmk
COMMENT ON FUNCTION empty_array(elem_type_null anyelement) IS '
1515
constructs proper empty 1-dimensional array whose dimensions are not NULL ( ''{}''::text[] does not do this)
1516
';
1517 10322 aaronmk
1518
1519
--
1520 8183 aaronmk
-- Name: ensure_prefix(text, text); Type: FUNCTION; Schema: util; Owner: -
1521 8086 aaronmk
--
1522
1523
CREATE FUNCTION ensure_prefix(prefix text, str text) RETURNS text
1524 10388 aaronmk
    LANGUAGE sql IMMUTABLE
1525 8086 aaronmk
    AS $_$
1526 8183 aaronmk
SELECT (CASE WHEN util.has_prefix($1, $2) THEN $2 ELSE $1||$2 END)
1527 8086 aaronmk
$_$;
1528
1529
1530
--
1531 10987 aaronmk
-- Name: esc_name__append(text, text); Type: FUNCTION; Schema: util; Owner: -
1532
--
1533
1534
CREATE FUNCTION esc_name__append(suffix text, esc_name text) RETURNS text
1535
    LANGUAGE sql IMMUTABLE
1536
    AS $_$
1537
SELECT regexp_replace($2, '("?)$', $1||'\1')
1538
$_$;
1539
1540
1541
--
1542 13455 aaronmk
-- Name: eval(text[]); Type: FUNCTION; Schema: util; Owner: -
1543
--
1544
1545
CREATE FUNCTION eval(queries text[]) RETURNS void
1546
    LANGUAGE sql
1547
    AS $_$
1548
SELECT util.eval(query) FROM unnest($1) query;
1549
SELECT NULL::void; -- don't fold away functions called in previous query
1550
$_$;
1551
1552
1553
--
1554 12531 aaronmk
-- Name: eval(text, boolean); Type: FUNCTION; Schema: util; Owner: -
1555 9824 aaronmk
--
1556
1557 12531 aaronmk
CREATE FUNCTION eval(sql text, verbose_ boolean DEFAULT true) RETURNS void
1558 12558 aaronmk
    LANGUAGE plpgsql
1559 9824 aaronmk
    AS $$
1560
BEGIN
1561 13641 aaronmk
	sql = util.view_def_to_orig(sql); -- restore user's intent
1562 12531 aaronmk
	IF verbose_ THEN PERFORM util.debug_print_sql(sql); END IF;
1563 12251 aaronmk
	EXECUTE sql;
1564 9824 aaronmk
END;
1565
$$;
1566
1567
1568
--
1569 12181 aaronmk
-- Name: eval2col_pair(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
1570
--
1571
1572
CREATE FUNCTION eval2col_pair(sql text, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) RETURNS SETOF record
1573
    LANGUAGE plpgsql
1574
    AS $$
1575
BEGIN
1576 12251 aaronmk
	PERFORM util.debug_print_sql(sql);
1577 12181 aaronmk
	RETURN QUERY EXECUTE sql;
1578
END;
1579
$$;
1580
1581
1582
--
1583
-- Name: FUNCTION eval2col_pair(sql text, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement); Type: COMMENT; Schema: util; Owner: -
1584
--
1585
1586 12235 aaronmk
COMMENT ON FUNCTION eval2col_pair(sql text, col_type_null anyelement, OUT left_ anyelement, OUT right_ anyelement) IS '
1587
col_type_null (*required*): NULL::col_type
1588
';
1589 12181 aaronmk
1590
1591
--
1592 12301 aaronmk
-- Name: eval2records(text); Type: FUNCTION; Schema: util; Owner: -
1593
--
1594
1595
CREATE FUNCTION eval2records(sql text) RETURNS SETOF record
1596
    LANGUAGE plpgsql
1597
    AS $$
1598
BEGIN
1599
	PERFORM util.debug_print_sql(sql);
1600
	RETURN QUERY EXECUTE sql;
1601
END;
1602
$$;
1603
1604
1605
--
1606 12458 aaronmk
-- Name: eval2set(text, anyelement, boolean); Type: FUNCTION; Schema: util; Owner: -
1607 10363 aaronmk
--
1608
1609 12458 aaronmk
CREATE FUNCTION eval2set(sql text, ret_type_null anyelement DEFAULT NULL::text, verbose_ boolean DEFAULT true) RETURNS SETOF anyelement
1610 10363 aaronmk
    LANGUAGE plpgsql
1611
    AS $$
1612
BEGIN
1613 12458 aaronmk
	IF verbose_ THEN PERFORM util.debug_print_sql(sql); END IF;
1614 10363 aaronmk
	RETURN QUERY EXECUTE sql;
1615
END;
1616
$$;
1617
1618
1619
--
1620 10129 aaronmk
-- Name: eval2val(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
1621 10128 aaronmk
--
1622
1623 10129 aaronmk
CREATE FUNCTION eval2val(sql text, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
1624 12514 aaronmk
    LANGUAGE plpgsql STABLE
1625 10128 aaronmk
    AS $$
1626
DECLARE
1627
	ret_val ret_type_null%TYPE;
1628
BEGIN
1629 12251 aaronmk
	PERFORM util.debug_print_sql(sql);
1630 10128 aaronmk
	EXECUTE sql INTO STRICT ret_val;
1631
	RETURN ret_val;
1632
END;
1633
$$;
1634
1635
1636
--
1637 10129 aaronmk
-- Name: FUNCTION eval2val(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
1638 10128 aaronmk
--
1639
1640 12235 aaronmk
COMMENT ON FUNCTION eval2val(sql text, ret_type_null anyelement) IS '
1641
ret_type_null: NULL::ret_type
1642
';
1643 10128 aaronmk
1644
1645
--
1646 10131 aaronmk
-- Name: eval_expr(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
1647
--
1648
1649
CREATE FUNCTION eval_expr(sql text, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
1650
    LANGUAGE sql
1651
    AS $_$
1652 10132 aaronmk
SELECT util.eval2val($$SELECT $$||$1, $2)
1653 10131 aaronmk
$_$;
1654
1655
1656
--
1657
-- Name: FUNCTION eval_expr(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
1658
--
1659
1660 12235 aaronmk
COMMENT ON FUNCTION eval_expr(sql text, ret_type_null anyelement) IS '
1661
ret_type_null: NULL::ret_type
1662
';
1663 10131 aaronmk
1664
1665
--
1666 10133 aaronmk
-- Name: eval_expr_passthru(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
1667
--
1668
1669
CREATE FUNCTION eval_expr_passthru(sql text, ret_type_null anyelement DEFAULT NULL::text) RETURNS anyelement
1670
    LANGUAGE sql
1671
    AS $_$
1672
SELECT CASE WHEN $1 IS NULL THEN NULL ELSE util.eval_expr($1, $2) END
1673
$_$;
1674
1675
1676
--
1677
-- Name: FUNCTION eval_expr_passthru(sql text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
1678
--
1679
1680 12235 aaronmk
COMMENT ON FUNCTION eval_expr_passthru(sql text, ret_type_null anyelement) IS '
1681
sql: can be NULL, which will be passed through
1682
ret_type_null: NULL::ret_type
1683
';
1684 10133 aaronmk
1685
1686
--
1687 8183 aaronmk
-- Name: existing_cols(regclass, text[]); Type: FUNCTION; Schema: util; Owner: -
1688 8182 aaronmk
--
1689
1690
CREATE FUNCTION existing_cols(table_ regclass, VARIADIC col_names text[]) RETURNS SETOF text
1691 12446 aaronmk
    LANGUAGE sql STABLE
1692 8182 aaronmk
    AS $_$
1693
SELECT col_name
1694
FROM unnest($2) s (col_name)
1695 8183 aaronmk
WHERE util.col_exists(($1, col_name))
1696 8182 aaronmk
$_$;
1697
1698
1699
--
1700 11830 aaronmk
-- Name: explain(text); Type: FUNCTION; Schema: util; Owner: -
1701
--
1702
1703
CREATE FUNCTION explain(sql text) RETURNS SETOF text
1704
    LANGUAGE sql
1705 13496 aaronmk
    SET client_min_messages TO 'error'
1706 11830 aaronmk
    AS $_$
1707 13496 aaronmk
/* `client_min_messages = ERROR`: EXPLAIN apparently runs IMMUTABLE functions in
1708
the query, so this prevents displaying any log messages printed by them */
1709 12459 aaronmk
SELECT util.eval2set($$EXPLAIN $$||$1, verbose_ := false)
1710 11830 aaronmk
$_$;
1711
1712
1713
--
1714 11833 aaronmk
-- Name: explain2notice(text); Type: FUNCTION; Schema: util; Owner: -
1715
--
1716
1717
CREATE FUNCTION explain2notice(sql text) RETURNS void
1718 12449 aaronmk
    LANGUAGE sql
1719
    AS $_$
1720 12534 aaronmk
SELECT util.raise('NOTICE', util.explain2notice_msg($1))
1721 12449 aaronmk
$_$;
1722 12448 aaronmk
1723
1724
--
1725
-- Name: explain2notice_msg(text); Type: FUNCTION; Schema: util; Owner: -
1726
--
1727
1728
CREATE FUNCTION explain2notice_msg(sql text) RETURNS text
1729
    LANGUAGE sql
1730
    AS $_$
1731 12464 aaronmk
-- newline before and after to visually separate it from other debug info
1732 12756 aaronmk
SELECT COALESCE($$
1733 12464 aaronmk
EXPLAIN:
1734 12756 aaronmk
$$||util.fold_explain_msg(util.explain2str($1))||$$
1735
$$, '')
1736 11833 aaronmk
$_$;
1737
1738
1739
--
1740 12452 aaronmk
-- Name: explain2notice_msg_if_can(text); Type: FUNCTION; Schema: util; Owner: -
1741
--
1742
1743
CREATE FUNCTION explain2notice_msg_if_can(sql text) RETURNS text
1744 13403 aaronmk
    LANGUAGE plpgsql
1745
    AS $$
1746
BEGIN
1747
	RETURN util.explain2notice_msg(sql);
1748
EXCEPTION
1749 13601 aaronmk
WHEN   syntax_error
1750
	OR invalid_cursor_definition -- "cannot open multi-query plan as cursor"
1751
	THEN RETURN NULL; -- non-explainable query
1752 13403 aaronmk
	/* don't use util.is_explainable() because the list provided by Postgres
1753
	(http://www.postgresql.org/docs/9.3/static/sql-explain.html#AEN77691)
1754
	excludes some query types that are in fact EXPLAIN-able */
1755
END;
1756
$$;
1757 12452 aaronmk
1758
1759
--
1760 11832 aaronmk
-- Name: explain2str(text); Type: FUNCTION; Schema: util; Owner: -
1761
--
1762
1763
CREATE FUNCTION explain2str(sql text) RETURNS text
1764
    LANGUAGE sql
1765
    AS $_$
1766
SELECT util.join_strs(explain, $$
1767
$$) FROM util.explain($1)
1768
$_$;
1769
1770
1771 11835 aaronmk
SET default_tablespace = '';
1772
1773
SET default_with_oids = false;
1774
1775 11832 aaronmk
--
1776 11835 aaronmk
-- Name: explain; Type: TABLE; Schema: util; Owner: -; Tablespace:
1777 11831 aaronmk
--
1778
1779 11835 aaronmk
CREATE TABLE explain (
1780
    line text NOT NULL
1781
);
1782
1783
1784
--
1785
-- Name: explain2table(text, regclass); Type: FUNCTION; Schema: util; Owner: -
1786
--
1787
1788
CREATE FUNCTION explain2table(sql text, table_ regclass DEFAULT 'explain'::regclass) RETURNS void
1789 11831 aaronmk
    LANGUAGE sql
1790
    AS $_$
1791 11835 aaronmk
SELECT util.eval($$INSERT INTO $$||$2||$$ SELECT util.explain(
1792
$$||quote_nullable($1)||$$
1793 11831 aaronmk
)$$)
1794
$_$;
1795
1796
1797
--
1798 11836 aaronmk
-- Name: FUNCTION explain2table(sql text, table_ regclass); Type: COMMENT; Schema: util; Owner: -
1799
--
1800
1801 12235 aaronmk
COMMENT ON FUNCTION explain2table(sql text, table_ regclass) IS '
1802
usage:
1803 11836 aaronmk
PERFORM util.explain2table($$
1804
query
1805 12235 aaronmk
$$);
1806
';
1807 11836 aaronmk
1808
1809
--
1810 12450 aaronmk
-- Name: first_word(text); Type: FUNCTION; Schema: util; Owner: -
1811
--
1812
1813
CREATE FUNCTION first_word(str text) RETURNS text
1814
    LANGUAGE sql IMMUTABLE
1815
    AS $_$
1816 12461 aaronmk
SELECT match[1] FROM regexp_matches(util.ltrim_nl($1), '^(\S*)') match
1817 12450 aaronmk
$_$;
1818
1819
1820
--
1821 10323 aaronmk
-- Name: fix_array(anyarray); Type: FUNCTION; Schema: util; Owner: -
1822
--
1823
1824
CREATE FUNCTION fix_array("array" anyarray) RETURNS anyarray
1825 10355 aaronmk
    LANGUAGE sql IMMUTABLE
1826 10323 aaronmk
    AS $_$
1827 10355 aaronmk
SELECT CASE WHEN $1 IS NULL THEN NULL ELSE (
1828
	CASE WHEN pg_catalog.array_ndims($1) IS NULL THEN util.empty_array($1[1]) ELSE $1 END
1829
) END
1830 10323 aaronmk
$_$;
1831
1832
1833
--
1834
-- Name: FUNCTION fix_array("array" anyarray); Type: COMMENT; Schema: util; Owner: -
1835
--
1836
1837 12235 aaronmk
COMMENT ON FUNCTION fix_array("array" anyarray) IS '
1838
ensures that an array will always have proper non-NULL dimensions
1839
';
1840 10323 aaronmk
1841
1842
--
1843 12755 aaronmk
-- Name: fold_explain_msg(text); Type: FUNCTION; Schema: util; Owner: -
1844
--
1845
1846
CREATE FUNCTION fold_explain_msg(explain text) RETURNS text
1847
    LANGUAGE sql IMMUTABLE
1848
    AS $_$
1849
SELECT (CASE WHEN util.first_word($1) = 'Result' THEN NULL ELSE $1 END)
1850
$_$;
1851
1852
1853
--
1854 8321 aaronmk
-- Name: force_update_view(text, text); Type: FUNCTION; Schema: util; Owner: -
1855
--
1856
1857
CREATE FUNCTION force_update_view(view_ text, query text) RETURNS void
1858
    LANGUAGE plpgsql STRICT
1859
    AS $_$
1860
DECLARE
1861
	mk_view text = $$CREATE OR REPLACE VIEW $$||view_||$$ AS
1862
$$||query;
1863
BEGIN
1864
	EXECUTE mk_view;
1865
EXCEPTION
1866
WHEN invalid_table_definition THEN
1867 8323 aaronmk
	IF SQLERRM = 'cannot drop columns from view'
1868
	OR SQLERRM LIKE 'cannot change name of view column "%" to "%"'
1869
	THEN
1870 8321 aaronmk
		EXECUTE $$DROP VIEW $$||view_||$$ CASCADE$$;
1871
		EXECUTE mk_view;
1872
	ELSE RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
1873
	END IF;
1874
END;
1875
$_$;
1876
1877
1878
--
1879
-- Name: FUNCTION force_update_view(view_ text, query text); Type: COMMENT; Schema: util; Owner: -
1880
--
1881
1882 12235 aaronmk
COMMENT ON FUNCTION force_update_view(view_ text, query text) IS '
1883
idempotent
1884
';
1885 8321 aaronmk
1886
1887
--
1888 12654 aaronmk
-- Name: freq_always_1(regclass, text); Type: FUNCTION; Schema: util; Owner: -
1889
--
1890
1891
CREATE FUNCTION freq_always_1(table_ regclass, freq_col text DEFAULT 'copies'::text) RETURNS boolean
1892
    LANGUAGE sql STABLE
1893
    AS $_$
1894
SELECT util.eval2val(
1895
$$SELECT NOT EXISTS( -- there is no row that is != 1
1896
	SELECT NULL
1897
	FROM $$||$1||$$
1898
	WHERE $$||quote_ident(freq_col)||$$ IS DISTINCT FROM 1
1899
	LIMIT 1
1900
)
1901
$$, NULL::boolean)
1902
$_$;
1903
1904
1905
--
1906 12661 aaronmk
-- Name: freq_always_1(regclass[], text); Type: FUNCTION; Schema: util; Owner: -
1907
--
1908
1909
CREATE FUNCTION freq_always_1(tables regclass[], freq_col text DEFAULT 'copies'::text) RETURNS boolean
1910
    LANGUAGE sql STABLE
1911
    AS $_$
1912
SELECT bool_and(util.freq_always_1(table_, $2)) FROM unnest($1) table_
1913
$_$;
1914
1915
1916
--
1917 11655 aaronmk
-- Name: grants_users(); Type: FUNCTION; Schema: util; Owner: -
1918
--
1919
1920
CREATE FUNCTION grants_users() RETURNS SETOF text
1921
    LANGUAGE sql IMMUTABLE
1922
    AS $$
1923
VALUES ('bien_read'), ('public_')
1924
$$;
1925
1926
1927
--
1928 8183 aaronmk
-- Name: has_prefix(text, text); Type: FUNCTION; Schema: util; Owner: -
1929 8085 aaronmk
--
1930
1931
CREATE FUNCTION has_prefix(prefix text, str text) RETURNS boolean
1932 10388 aaronmk
    LANGUAGE sql IMMUTABLE
1933 8085 aaronmk
    AS $_$
1934
SELECT substring($2 for length($1)) = $1
1935
$_$;
1936
1937
1938
--
1939 12296 aaronmk
-- Name: has_single_row(regclass); Type: FUNCTION; Schema: util; Owner: -
1940
--
1941
1942
CREATE FUNCTION has_single_row(table_ regclass) RETURNS boolean
1943
    LANGUAGE sql STABLE
1944
    AS $_$
1945
SELECT util.eval2val($$SELECT COUNT(*) = 1 FROM $$||$1, NULL::boolean)
1946
$_$;
1947
1948
1949
--
1950 10307 aaronmk
-- Name: hstore(text[], text); Type: FUNCTION; Schema: util; Owner: -
1951
--
1952
1953
CREATE FUNCTION hstore(keys text[], value text) RETURNS hstore
1954
    LANGUAGE sql IMMUTABLE
1955
    AS $_$
1956 10324 aaronmk
SELECT hstore(util.fix_array($1), util.array_fill($2, util.array_length($1)))
1957 10307 aaronmk
$_$;
1958
1959
1960
--
1961
-- Name: FUNCTION hstore(keys text[], value text); Type: COMMENT; Schema: util; Owner: -
1962
--
1963
1964 12235 aaronmk
COMMENT ON FUNCTION hstore(keys text[], value text) IS '
1965
avoids repeating the same value for each key
1966
';
1967 10307 aaronmk
1968
1969
--
1970 12218 aaronmk
-- Name: ifnull(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
1971
--
1972
1973
CREATE FUNCTION ifnull(value anyelement, null_ anyelement) RETURNS anyelement
1974
    LANGUAGE sql IMMUTABLE
1975
    AS $_$
1976 12222 aaronmk
SELECT COALESCE($1, $2)
1977 12218 aaronmk
$_$;
1978
1979
1980
--
1981
-- Name: FUNCTION ifnull(value anyelement, null_ anyelement); Type: COMMENT; Schema: util; Owner: -
1982
--
1983
1984 12235 aaronmk
COMMENT ON FUNCTION ifnull(value anyelement, null_ anyelement) IS '
1985
equivalent to MySQL''s IFNULL() (Postgres auto-lowercases the name)
1986
';
1987 12218 aaronmk
1988
1989
--
1990 13452 aaronmk
-- Name: in_reverse(anyarray); Type: FUNCTION; Schema: util; Owner: -
1991
--
1992
1993
CREATE FUNCTION in_reverse("array" anyarray) RETURNS SETOF anyelement
1994
    LANGUAGE sql IMMUTABLE
1995
    AS $_$
1996 13490 aaronmk
SELECT elem FROM unnest($1) elem ORDER BY row_number() OVER () DESC
1997 13452 aaronmk
$_$;
1998
1999
2000
--
2001 12285 aaronmk
-- Name: inherit(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
2002
--
2003
2004
CREATE FUNCTION inherit(derived regclass, base regclass) RETURNS void
2005
    LANGUAGE sql
2006
    AS $_$
2007
SELECT util.eval($$ALTER TABLE $$||$1||$$ INHERIT $$||$2)
2008
$_$;
2009
2010
2011
--
2012 13136 aaronmk
-- Name: is_castable(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
2013
--
2014
2015
CREATE FUNCTION is_castable(value text, ret_type_null anyelement) RETURNS boolean
2016
    LANGUAGE plpgsql IMMUTABLE
2017
    AS $$
2018
BEGIN
2019
	PERFORM util.cast(value, ret_type_null);
2020 13139 aaronmk
	-- must happen *after* cast check, because NULL is not valid for some types
2021
	IF value IS NULL THEN RETURN NULL; END IF; -- pass NULL through
2022 13136 aaronmk
	RETURN true;
2023
EXCEPTION
2024 13493 aaronmk
WHEN   data_exception
2025 13564 aaronmk
	OR invalid_schema_name -- eg. 'pg_temp.__'::regclass
2026 13493 aaronmk
	OR syntax_error_or_access_rule_violation -- eg. ::regclass
2027
	THEN
2028
	RETURN false;
2029 13136 aaronmk
END;
2030
$$;
2031
2032
2033
--
2034
-- Name: FUNCTION is_castable(value text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
2035
--
2036
2037
COMMENT ON FUNCTION is_castable(value text, ret_type_null anyelement) IS '
2038 13139 aaronmk
passes NULL through. however, if NULL is not valid for the type, false will be
2039
returned instead.
2040
2041 13136 aaronmk
ret_type_null: NULL::ret_type
2042
';
2043
2044
2045
--
2046 10137 aaronmk
-- Name: is_constant(col_ref); Type: FUNCTION; Schema: util; Owner: -
2047
--
2048
2049
CREATE FUNCTION is_constant(col col_ref) RETURNS boolean
2050 12446 aaronmk
    LANGUAGE sql STABLE
2051 10137 aaronmk
    AS $_$
2052 12789 aaronmk
SELECT COALESCE(util.col_comment($1) LIKE '
2053
constant
2054
%', false)
2055 10137 aaronmk
$_$;
2056
2057
2058
--
2059 11659 aaronmk
-- Name: is_empty(anyarray); Type: FUNCTION; Schema: util; Owner: -
2060
--
2061
2062
CREATE FUNCTION is_empty("array" anyarray) RETURNS boolean
2063
    LANGUAGE sql IMMUTABLE
2064
    AS $_$
2065
SELECT util.array_length($1) = 0
2066
$_$;
2067
2068
2069
--
2070 12457 aaronmk
-- Name: is_explain(text); Type: FUNCTION; Schema: util; Owner: -
2071
--
2072
2073
CREATE FUNCTION is_explain(sql text) RETURNS boolean
2074
    LANGUAGE sql IMMUTABLE
2075
    AS $_$
2076
SELECT upper(util.first_word($1)) = 'EXPLAIN'
2077
$_$;
2078
2079
2080
--
2081 12451 aaronmk
-- Name: is_explainable(text); Type: FUNCTION; Schema: util; Owner: -
2082
--
2083
2084
CREATE FUNCTION is_explainable(sql text) RETURNS boolean
2085
    LANGUAGE sql IMMUTABLE
2086
    AS $_$
2087
SELECT upper(util.first_word($1)) = ANY(
2088
'{SELECT,INSERT,UPDATE,DELETE,VALUES,EXECUTE,DECLARE}'::text[]
2089
/*from http://www.postgresql.org/docs/9.3/static/sql-explain.html#AEN77691*/
2090
)
2091
$_$;
2092
2093
2094
--
2095 10391 aaronmk
-- Name: is_more_complete_than(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
2096
--
2097
2098
CREATE FUNCTION is_more_complete_than("left" anyelement, "right" anyelement) RETURNS boolean
2099
    LANGUAGE sql IMMUTABLE
2100
    AS $_$
2101
SELECT $1 IS NOT DISTINCT FROM $2 OR ($1 IS NOT NULL AND $2 IS NULL)
2102
$_$;
2103
2104
2105
--
2106 10613 aaronmk
-- Name: is_populated_more_often_than(anyelement, anyelement); Type: FUNCTION; Schema: util; Owner: -
2107
--
2108
2109
CREATE FUNCTION is_populated_more_often_than("left" anyelement, "right" anyelement) RETURNS boolean
2110
    LANGUAGE sql IMMUTABLE
2111
    AS $_$
2112
SELECT $1 IS NOT NULL >= $2 IS NOT NULL -- true > false
2113
$_$;
2114
2115
2116
--
2117 12480 aaronmk
-- Name: is_set_stmt(text); Type: FUNCTION; Schema: util; Owner: -
2118
--
2119
2120
CREATE FUNCTION is_set_stmt(sql text) RETURNS boolean
2121
    LANGUAGE sql IMMUTABLE
2122
    AS $_$
2123
SELECT upper(util.first_word($1)) = 'SET'
2124
$_$;
2125
2126
2127
--
2128 12330 aaronmk
-- Name: is_table(regclass); Type: FUNCTION; Schema: util; Owner: -
2129
--
2130
2131
CREATE FUNCTION is_table(relation regclass) RETURNS boolean
2132 12332 aaronmk
    LANGUAGE sql STABLE
2133 12330 aaronmk
    AS $_$
2134
SELECT relkind = 'r' FROM pg_class WHERE oid = $1
2135
$_$;
2136
2137
2138
--
2139 12329 aaronmk
-- Name: is_view(regclass); Type: FUNCTION; Schema: util; Owner: -
2140
--
2141
2142
CREATE FUNCTION is_view(relation regclass) RETURNS boolean
2143 12332 aaronmk
    LANGUAGE sql STABLE
2144 12329 aaronmk
    AS $_$
2145
SELECT relkind = 'v' FROM pg_class WHERE oid = $1
2146
$_$;
2147
2148
2149
--
2150 8183 aaronmk
-- Name: join_strs_transform(text, text, text); Type: FUNCTION; Schema: util; Owner: -
2151 4009 aaronmk
--
2152
2153 4053 aaronmk
CREATE FUNCTION join_strs_transform(state text, value text, delim text) RETURNS text
2154 12444 aaronmk
    LANGUAGE sql IMMUTABLE STRICT
2155 4009 aaronmk
    AS $_$
2156 4054 aaronmk
SELECT $1 || $3 || $2
2157 2595 aaronmk
$_$;
2158
2159
2160
--
2161 12444 aaronmk
-- Name: FUNCTION join_strs_transform(state text, value text, delim text); Type: COMMENT; Schema: util; Owner: -
2162
--
2163
2164
COMMENT ON FUNCTION join_strs_transform(state text, value text, delim text) IS '
2165
must be declared STRICT to use the special handling of STRICT aggregating functions
2166
';
2167
2168
2169
--
2170 12436 aaronmk
-- Name: keys(anyelement); Type: FUNCTION; Schema: util; Owner: -
2171
--
2172
2173
CREATE FUNCTION keys(value anyelement) RETURNS anyelement
2174
    LANGUAGE sql IMMUTABLE
2175
    AS $_$
2176
SELECT $1 -- compare on the entire value
2177
$_$;
2178
2179
2180
--
2181 10989 aaronmk
-- Name: limit2row_num(integer, integer, integer); Type: FUNCTION; Schema: util; Owner: -
2182 10985 aaronmk
--
2183
2184 10989 aaronmk
CREATE FUNCTION limit2row_num(limit_ integer, offset_ integer DEFAULT NULL::integer, min_row_num integer DEFAULT 1) RETURNS integer
2185 10985 aaronmk
    LANGUAGE sql IMMUTABLE
2186
    AS $_$
2187 10989 aaronmk
SELECT COALESCE(util.offset2row_num($2, $3) + $1 - 1, 2147483647)
2188 10985 aaronmk
$_$;
2189
2190
2191
--
2192 13384 aaronmk
-- Name: loop_ignore_errors(text, text, anyelement); Type: FUNCTION; Schema: util; Owner: -
2193
--
2194
2195
CREATE FUNCTION loop_ignore_errors(iter_sql text, loop_body_sql text, loop_type_null anyelement DEFAULT NULL::text) RETURNS void
2196
    LANGUAGE plpgsql
2197
    AS $$
2198
DECLARE
2199
	errors_ct integer = 0;
2200
	loop_var loop_type_null%TYPE;
2201
BEGIN
2202
	FOR loop_var IN SELECT * FROM util.eval2set(iter_sql, loop_type_null)
2203
	LOOP
2204
		BEGIN
2205
			EXECUTE loop_body_sql USING loop_var;
2206
		EXCEPTION
2207
		WHEN OTHERS THEN
2208
			errors_ct = errors_ct+1;
2209
			PERFORM util.raise_error_warning(SQLERRM);
2210
		END;
2211
	END LOOP;
2212
	IF errors_ct > 0 THEN
2213
		-- can't raise exception because this would roll back the transaction
2214
		PERFORM util.raise_error_warning('there were '||errors_ct
2215
			||' errors: see the WARNINGs for details');
2216
	END IF;
2217
END;
2218
$$;
2219
2220
2221
--
2222 12275 aaronmk
-- Name: ltrim_nl(text); Type: FUNCTION; Schema: util; Owner: -
2223
--
2224
2225
CREATE FUNCTION ltrim_nl(str text) RETURNS text
2226
    LANGUAGE sql IMMUTABLE
2227
    AS $_$
2228
SELECT ltrim($1, $$
2229
$$)
2230
$_$;
2231
2232
2233
--
2234 10110 aaronmk
-- Name: map_filter_insert(); Type: FUNCTION; Schema: util; Owner: -
2235
--
2236
2237
CREATE FUNCTION map_filter_insert() RETURNS trigger
2238
    LANGUAGE plpgsql
2239
    AS $$
2240
BEGIN
2241
	IF new."from" LIKE ':%' THEN RETURN NULL; END IF; -- exclude metadata values
2242
	RETURN new;
2243
END;
2244
$$;
2245
2246
2247
--
2248 8183 aaronmk
-- Name: map_get(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2249 8146 aaronmk
--
2250
2251
CREATE FUNCTION map_get(map regclass, key text) RETURNS text
2252
    LANGUAGE plpgsql STABLE STRICT
2253
    AS $_$
2254
DECLARE
2255
    value text;
2256
BEGIN
2257
    EXECUTE $$SELECT "to" FROM $$||map||$$ WHERE "from" = $1$$
2258 8149 aaronmk
        INTO value USING key;
2259 8146 aaronmk
    RETURN value;
2260
END;
2261
$_$;
2262
2263
2264
--
2265 10358 aaronmk
-- Name: map_nulls(text[], anyelement); Type: FUNCTION; Schema: util; Owner: -
2266 10325 aaronmk
--
2267
2268 10358 aaronmk
CREATE FUNCTION map_nulls(nulls text[], value anyelement) RETURNS anyelement
2269 10353 aaronmk
    LANGUAGE sql IMMUTABLE
2270 10325 aaronmk
    AS $_$
2271 10374 aaronmk
SELECT util._map(util.nulls_map($1), $2)
2272 10325 aaronmk
$_$;
2273
2274
2275
--
2276 10359 aaronmk
-- Name: FUNCTION map_nulls(nulls text[], value anyelement); Type: COMMENT; Schema: util; Owner: -
2277
--
2278
2279 12235 aaronmk
COMMENT ON FUNCTION map_nulls(nulls text[], value anyelement) IS '
2280
due to dynamic inlining[1], this is just as fast as util._map() which it wraps[2].
2281 10359 aaronmk
2282
[1] inlining of function calls, which is different from constant folding
2283
[2] _map()''s profiling query
2284
SELECT util._map(''"1"=>NULL, "2"=>NULL, "3"=>NULL, *=>*'', v) FROM unnest(array_fill(1, array[100000])) f (v)
2285
and map_nulls()''s profiling query
2286
SELECT util.map_nulls(array[1, 2, 3]::text[], v) FROM unnest(array_fill(1, array[100000])) f (v)
2287 10375 aaronmk
both take ~920 ms.
2288 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.
2289
';
2290 10359 aaronmk
2291
2292
--
2293 8183 aaronmk
-- Name: map_values(regclass); Type: FUNCTION; Schema: util; Owner: -
2294 8150 aaronmk
--
2295
2296
CREATE FUNCTION map_values(map regclass) RETURNS SETOF text
2297
    LANGUAGE plpgsql STABLE STRICT
2298
    AS $_$
2299
BEGIN
2300
    RETURN QUERY EXECUTE $$SELECT "to" FROM $$||map;
2301
END;
2302
$_$;
2303
2304
2305
--
2306 12228 aaronmk
-- Name: materialize_query(text, text); Type: FUNCTION; Schema: util; Owner: -
2307
--
2308
2309 12262 aaronmk
CREATE FUNCTION materialize_query(table_esc text, sql text) RETURNS void
2310 12228 aaronmk
    LANGUAGE sql
2311
    AS $_$
2312 12262 aaronmk
SELECT util.create_if_not_exists($$CREATE TABLE $$||$1||$$ AS
2313 12321 aaronmk
$$||util.ltrim_nl($2));
2314
-- make sure the created table has the correct estimated row count
2315
SELECT util.analyze_($1);
2316 12470 aaronmk
2317
SELECT util.append_comment($1, '
2318
contents generated from:
2319 13397 aaronmk
'||util.ltrim_nl(util.runnable_sql($2))||';
2320 12470 aaronmk
');
2321 12228 aaronmk
$_$;
2322
2323
2324
--
2325 12262 aaronmk
-- Name: FUNCTION materialize_query(table_esc text, sql text); Type: COMMENT; Schema: util; Owner: -
2326 12228 aaronmk
--
2327
2328 12262 aaronmk
COMMENT ON FUNCTION materialize_query(table_esc text, sql text) IS '
2329 12235 aaronmk
idempotent
2330
';
2331 12228 aaronmk
2332
2333
--
2334 12234 aaronmk
-- Name: materialize_view(text, regclass); Type: FUNCTION; Schema: util; Owner: -
2335
--
2336
2337 12262 aaronmk
CREATE FUNCTION materialize_view(table_esc text, view_ regclass) RETURNS void
2338 12234 aaronmk
    LANGUAGE sql
2339
    AS $_$
2340
SELECT util.materialize_query($1, $$SELECT * FROM $$||$2)
2341
$_$;
2342
2343
2344
--
2345 12262 aaronmk
-- Name: FUNCTION materialize_view(table_esc text, view_ regclass); Type: COMMENT; Schema: util; Owner: -
2346 12234 aaronmk
--
2347
2348 12262 aaronmk
COMMENT ON FUNCTION materialize_view(table_esc text, view_ regclass) IS '
2349 12235 aaronmk
idempotent
2350
';
2351 12234 aaronmk
2352
2353
--
2354 8190 aaronmk
-- Name: mk_const_col(col_ref, anyelement); Type: FUNCTION; Schema: util; Owner: -
2355
--
2356
2357
CREATE FUNCTION mk_const_col(col col_ref, value anyelement) RETURNS void
2358 12446 aaronmk
    LANGUAGE sql
2359 8190 aaronmk
    AS $_$
2360 10135 aaronmk
SELECT util.create_if_not_exists($$
2361
ALTER TABLE $$||$1.table_||$$ ADD COLUMN $$
2362 8190 aaronmk
||quote_ident($1.name)||$$ $$||pg_typeof($2)||util.type_qual($2)||$$ DEFAULT $$
2363 10135 aaronmk
||quote_literal($2)||$$;
2364 12235 aaronmk
COMMENT ON COLUMN $$||$1.table_||$$.$$||quote_ident($1.name)||$$ IS '
2365
constant
2366
';
2367 10135 aaronmk
$$)
2368 8190 aaronmk
$_$;
2369
2370
2371
--
2372
-- Name: FUNCTION mk_const_col(col col_ref, value anyelement); Type: COMMENT; Schema: util; Owner: -
2373
--
2374
2375 12235 aaronmk
COMMENT ON FUNCTION mk_const_col(col col_ref, value anyelement) IS '
2376
idempotent
2377
';
2378 8190 aaronmk
2379
2380
--
2381 10296 aaronmk
-- Name: mk_derived_col(col_ref, text, boolean); Type: FUNCTION; Schema: util; Owner: -
2382 8187 aaronmk
--
2383
2384 10296 aaronmk
CREATE FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean DEFAULT false) RETURNS void
2385 8187 aaronmk
    LANGUAGE plpgsql STRICT
2386
    AS $_$
2387
DECLARE
2388
    type regtype = util.typeof(expr, col.table_::text::regtype);
2389
    col_name_sql text = quote_ident(col.name);
2390
BEGIN
2391 10296 aaronmk
    PERFORM util.create_if_not_exists((CASE WHEN overwrite THEN '' ELSE $$
2392
ALTER TABLE $$||col.table_||$$ ADD   COLUMN $$||col_name_sql||$$      $$||type||$$;$$ END)||$$
2393 8187 aaronmk
ALTER TABLE $$||col.table_||$$ ALTER COLUMN $$||col_name_sql||$$ TYPE $$||type||$$ USING
2394
$$||expr||$$;
2395
$$);
2396
END;
2397
$_$;
2398
2399
2400
--
2401 10296 aaronmk
-- Name: FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean); Type: COMMENT; Schema: util; Owner: -
2402 8188 aaronmk
--
2403
2404 12235 aaronmk
COMMENT ON FUNCTION mk_derived_col(col col_ref, expr text, overwrite boolean) IS '
2405
idempotent
2406
';
2407 8188 aaronmk
2408
2409
--
2410 12554 aaronmk
-- Name: mk_diff_query(text, text, text, text, text); Type: FUNCTION; Schema: util; Owner: -
2411 12475 aaronmk
--
2412
2413 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
2414 12475 aaronmk
    LANGUAGE sql IMMUTABLE
2415
    AS $_$
2416
SELECT
2417 12478 aaronmk
$$SELECT
2418 12554 aaronmk
$$||$3||$$
2419 12555 aaronmk
FROM      $$||$1||$$ left_
2420 12554 aaronmk
FULL JOIN $$||$2||$$ right_
2421
ON $$||$4||$$
2422
WHERE $$||$5||$$
2423 12475 aaronmk
ORDER BY left_, right_
2424
$$
2425
$_$;
2426
2427
2428
--
2429 13515 aaronmk
-- Name: mk_drop_from_create(text); Type: FUNCTION; Schema: util; Owner: -
2430
--
2431
2432
CREATE FUNCTION mk_drop_from_create(create_cmd text) RETURNS text
2433
    LANGUAGE sql IMMUTABLE
2434
    AS $_$
2435 13516 aaronmk
SELECT $$DROP $$||(util.regexp_match($1,
2436 13603 aaronmk
-- match first CREATE, *if* no DROP came before it
2437 13608 aaronmk
'(?m)\A(?:^(?!DROP\y).*\n)*?^CREATE(?: OR REPLACE)? ((?:)??[[:upper:]]+ .*?)(?: AS(?: (?:SELECT\y.*)?)?)?$'
2438 13515 aaronmk
	/* (?:)?? makes the surrounding () group non-greedy, so that (?: AS ?)?
2439
	works properly (due to nonstandard Postgres regexp behavior:
2440
	http://www.postgresql.org/docs/9.3/static/functions-matching.html#POSIX-MATCHING-RULES) */
2441 13603 aaronmk
))[1]||$$;$$
2442 13515 aaronmk
$_$;
2443
2444
2445
--
2446 12564 aaronmk
-- Name: mk_keys_func(regtype); Type: FUNCTION; Schema: util; Owner: -
2447
--
2448
2449
CREATE FUNCTION mk_keys_func(type regtype) RETURNS void
2450 12591 aaronmk
    LANGUAGE sql
2451 12564 aaronmk
    AS $_$
2452 12570 aaronmk
-- keys()
2453 12564 aaronmk
SELECT util.mk_keys_func($1, ARRAY(
2454
SELECT col FROM util.typed_cols($1) col
2455
WHERE (col).type != ANY('{bigint}'::regtype[]) -- not a count col
2456 12570 aaronmk
));
2457
2458 12571 aaronmk
-- values_()
2459 12570 aaronmk
SELECT util.mk_keys_func($1, COALESCE(
2460
	NULLIF(ARRAY(
2461
	SELECT col FROM util.typed_cols($1) col
2462
	WHERE (col).type = ANY('{bigint}'::regtype[]) -- is a count col
2463
	), ARRAY[]::util.col_cast[])
2464
, ARRAY(SELECT util.typed_cols($1))) -- no count cols, so use all cols
2465 12571 aaronmk
, 'values_');
2466 12564 aaronmk
$_$;
2467
2468
2469
--
2470 12569 aaronmk
-- Name: mk_keys_func(regtype, col_cast[], text); Type: FUNCTION; Schema: util; Owner: -
2471 12561 aaronmk
--
2472
2473 12569 aaronmk
CREATE FUNCTION mk_keys_func(type regtype, cols col_cast[], name text DEFAULT 'keys'::text) RETURNS void
2474 12591 aaronmk
    LANGUAGE sql
2475 12561 aaronmk
    AS $_$
2476 12567 aaronmk
SELECT util.create_if_not_exists($$
2477 12577 aaronmk
CREATE TYPE $$||util.prefixed_name($3||'_', $1)||$$ AS
2478
($$||util.mk_typed_cols_list($2)||$$);
2479 12671 aaronmk
COMMENT ON TYPE $$||util.prefixed_name($3||'_', $1)||$$ IS '
2480
autogenerated
2481
';
2482 12594 aaronmk
$$);
2483 12577 aaronmk
2484 12594 aaronmk
SELECT util.mk_keys_func($1, util.prefixed_name($3||'_', $1)::regtype, $3);
2485
$_$;
2486
2487
2488
--
2489
-- Name: mk_keys_func(regtype, regtype, text); Type: FUNCTION; Schema: util; Owner: -
2490
--
2491
2492
CREATE FUNCTION mk_keys_func(type regtype, return_type regtype, name text DEFAULT 'keys'::text) RETURNS void
2493
    LANGUAGE sql
2494
    AS $_$
2495
SELECT util.create_if_not_exists($$
2496 12581 aaronmk
CREATE FUNCTION $$||util.qual_name(util.schema($1), $3)||$$(value $$
2497 12577 aaronmk
||util.qual_name($1)||$$)
2498 12594 aaronmk
  RETURNS $$||util.qual_name($2)||$$ AS
2499 12561 aaronmk
$BODY1$
2500 12577 aaronmk
SELECT ROW($$||
2501 12594 aaronmk
(SELECT COALESCE(string_agg($$$1.$$||quote_ident((col).col_name), ', '), '')
2502
FROM util.typed_cols($2) col) ||$$)::$$||util.qual_name($2)||$$
2503 12561 aaronmk
$BODY1$
2504
  LANGUAGE sql IMMUTABLE
2505
  COST 100;
2506 12594 aaronmk
$$);
2507 12561 aaronmk
$_$;
2508
2509
2510
--
2511 8183 aaronmk
-- Name: mk_map_table(text); Type: FUNCTION; Schema: util; Owner: -
2512 8139 aaronmk
--
2513
2514
CREATE FUNCTION mk_map_table(table_ text) RETURNS void
2515 12446 aaronmk
    LANGUAGE sql
2516 8139 aaronmk
    AS $_$
2517 8183 aaronmk
SELECT util.create_if_not_exists($$
2518 8141 aaronmk
CREATE TABLE $$||$1||$$
2519 8139 aaronmk
(
2520 8183 aaronmk
    LIKE util.map INCLUDING ALL
2521 10110 aaronmk
);
2522
2523
CREATE TRIGGER map_filter_insert
2524
  BEFORE INSERT
2525
  ON $$||$1||$$
2526
  FOR EACH ROW
2527
  EXECUTE PROCEDURE util.map_filter_insert();
2528 8141 aaronmk
$$)
2529 8139 aaronmk
$_$;
2530
2531
2532
--
2533 12725 aaronmk
-- Name: mk_not_null(text); Type: FUNCTION; Schema: util; Owner: -
2534
--
2535
2536
CREATE FUNCTION mk_not_null(text) RETURNS text
2537
    LANGUAGE sql IMMUTABLE
2538
    AS $_$
2539
SELECT COALESCE($1, '<NULL>')
2540
$_$;
2541
2542
2543
--
2544 12556 aaronmk
-- Name: mk_out_params(col_cast[]); Type: FUNCTION; Schema: util; Owner: -
2545
--
2546
2547
CREATE FUNCTION mk_out_params(cols col_cast[]) RETURNS text
2548
    LANGUAGE sql IMMUTABLE
2549
    AS $_$
2550 12559 aaronmk
SELECT COALESCE(string_agg($$, OUT $$||(unnest).col_name||$$ $$||
2551
util.qual_name((unnest).type), ''), '')
2552 12556 aaronmk
FROM unnest($1)
2553
$_$;
2554
2555
2556
--
2557 12236 aaronmk
-- Name: mk_search_path(text[]); Type: FUNCTION; Schema: util; Owner: -
2558
--
2559
2560
CREATE FUNCTION mk_search_path(VARIADIC schemas text[]) RETURNS text
2561
    LANGUAGE sql IMMUTABLE
2562
    AS $_$
2563 12486 aaronmk
SELECT string_agg(quote_ident(unnest), ', ') FROM unnest($1||'util'::text)
2564 12236 aaronmk
$_$;
2565
2566
2567
--
2568 12486 aaronmk
-- Name: FUNCTION mk_search_path(VARIADIC schemas text[]); Type: COMMENT; Schema: util; Owner: -
2569
--
2570
2571
COMMENT ON FUNCTION mk_search_path(VARIADIC schemas text[]) IS '
2572
auto-appends util to the search_path to enable use of util operators
2573
';
2574
2575
2576
--
2577 13474 aaronmk
-- Name: mk_set_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2578
--
2579
2580
CREATE FUNCTION mk_set_comment(table_ regclass, comment text) RETURNS text
2581
    LANGUAGE sql STABLE
2582
    AS $_$
2583 13483 aaronmk
SELECT COALESCE($$COMMENT ON $$||util.relation_type($1)||$$ $$||$1||$$ IS $$
2584 13481 aaronmk
||quote_literal($2)/*pass NULL through*/||$$;$$, ''/*no comment*/)
2585 13474 aaronmk
$_$;
2586
2587
2588
--
2589 13504 aaronmk
-- Name: mk_set_relation_metadata(regclass); Type: FUNCTION; Schema: util; Owner: -
2590
--
2591
2592
CREATE FUNCTION mk_set_relation_metadata(relation regclass) RETURNS text
2593
    LANGUAGE sql STABLE
2594
    AS $_$
2595
SELECT util.show_grants_for($1)
2596
||util.show_set_comment($1)||$$
2597
$$
2598
$_$;
2599
2600
2601
--
2602 12467 aaronmk
-- Name: mk_set_search_path(boolean); Type: FUNCTION; Schema: util; Owner: -
2603
--
2604
2605
CREATE FUNCTION mk_set_search_path(for_printing boolean DEFAULT false) RETURNS text
2606
    LANGUAGE sql IMMUTABLE
2607
    AS $_$
2608
SELECT util.mk_set_search_path(current_setting('search_path'), $1)
2609
$_$;
2610
2611
2612
--
2613 12466 aaronmk
-- Name: mk_set_search_path(text, boolean); Type: FUNCTION; Schema: util; Owner: -
2614 12270 aaronmk
--
2615
2616 12466 aaronmk
CREATE FUNCTION mk_set_search_path(search_path text, for_printing boolean DEFAULT false) RETURNS text
2617 12270 aaronmk
    LANGUAGE sql IMMUTABLE
2618
    AS $_$
2619 12432 aaronmk
/* debug_print_return_value() needed because this function is used with EXECUTE
2620
rather than util.eval() (in order to affect the calling function), so the
2621
search_path would not otherwise be printed */
2622 12487 aaronmk
SELECT $$SET$$||util._if($2, $$ /*LOCAL*/$$::text, $$ LOCAL$$)
2623
||$$ search_path TO $$||$1
2624 12270 aaronmk
$_$;
2625
2626
2627
--
2628 10113 aaronmk
-- Name: mk_source_col(regclass); Type: FUNCTION; Schema: util; Owner: -
2629
--
2630
2631
CREATE FUNCTION mk_source_col(table_ regclass) RETURNS void
2632 12446 aaronmk
    LANGUAGE sql
2633 10113 aaronmk
    AS $_$
2634 12240 aaronmk
SELECT util.mk_const_col(($1, 'source'), util.schema($1))
2635 10113 aaronmk
$_$;
2636
2637
2638
--
2639
-- Name: FUNCTION mk_source_col(table_ regclass); Type: COMMENT; Schema: util; Owner: -
2640
--
2641
2642 12235 aaronmk
COMMENT ON FUNCTION mk_source_col(table_ regclass) IS '
2643
idempotent
2644
';
2645 10113 aaronmk
2646
2647
--
2648 11011 aaronmk
-- Name: mk_subset_by_row_num_func(regclass); Type: FUNCTION; Schema: util; Owner: -
2649
--
2650
2651
CREATE FUNCTION mk_subset_by_row_num_func(view_ regclass) RETURNS void
2652
    LANGUAGE plpgsql STRICT
2653
    AS $_$
2654
DECLARE
2655
	view_qual_name text = util.qual_name(view_);
2656
BEGIN
2657
	EXECUTE $$
2658
CREATE OR REPLACE FUNCTION $$||view_||$$(limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
2659
  RETURNS SETOF $$||view_||$$ AS
2660
$BODY1$
2661
SELECT * FROM $$||view_qual_name||$$
2662
ORDER BY sort_col
2663
LIMIT $1 OFFSET $2
2664
$BODY1$
2665
  LANGUAGE sql STABLE
2666
  COST 100
2667
  ROWS 1000
2668
$$;
2669
2670
	PERFORM util.mk_subset_by_row_num_no_sort_func(view_);
2671
END;
2672
$_$;
2673
2674
2675
--
2676 8325 aaronmk
-- Name: mk_subset_by_row_num_func(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2677
--
2678
2679
CREATE FUNCTION mk_subset_by_row_num_func(view_ regclass, row_num_col text) RETURNS void
2680
    LANGUAGE plpgsql STRICT
2681
    AS $_$
2682 10990 aaronmk
DECLARE
2683
	view_qual_name text = util.qual_name(view_);
2684
	row_num__min__fn text = util.esc_name__append('__row_num__min', view_qual_name);
2685 8325 aaronmk
BEGIN
2686
	EXECUTE $$
2687 10990 aaronmk
CREATE OR REPLACE FUNCTION $$||row_num__min__fn||$$()
2688
  RETURNS integer AS
2689
$BODY1$
2690
SELECT $$||quote_ident(row_num_col)||$$
2691
FROM $$||view_qual_name||$$
2692
ORDER BY $$||quote_ident(row_num_col)||$$ ASC
2693
LIMIT 1
2694
$BODY1$
2695
  LANGUAGE sql STABLE
2696
  COST 100;
2697
$$;
2698
2699
	EXECUTE $$
2700 8325 aaronmk
CREATE OR REPLACE FUNCTION $$||view_||$$(limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
2701
  RETURNS SETOF $$||view_||$$ AS
2702
$BODY1$
2703 10990 aaronmk
SELECT * FROM $$||view_qual_name||$$
2704
WHERE $$||quote_ident(row_num_col)||$$ BETWEEN
2705
	util.offset2row_num(    $2, $$||row_num__min__fn||$$())
2706
AND util.limit2row_num ($1, $2, $$||row_num__min__fn||$$())
2707 10991 aaronmk
ORDER BY $$||quote_ident(row_num_col)||$$
2708 8325 aaronmk
$BODY1$
2709
  LANGUAGE sql STABLE
2710
  COST 100
2711
  ROWS 1000
2712
$$;
2713 11010 aaronmk
2714
	PERFORM util.mk_subset_by_row_num_no_sort_func(view_);
2715
END;
2716
$_$;
2717
2718
2719
--
2720
-- Name: mk_subset_by_row_num_no_sort_func(regclass); Type: FUNCTION; Schema: util; Owner: -
2721
--
2722
2723
CREATE FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass) RETURNS void
2724
    LANGUAGE plpgsql STRICT
2725
    AS $_$
2726
DECLARE
2727
	view_qual_name text = util.qual_name(view_);
2728
BEGIN
2729 8326 aaronmk
	EXECUTE $$
2730
CREATE OR REPLACE FUNCTION $$||view_||$$(no_sort boolean, limit_ integer DEFAULT NULL::integer, offset_ integer DEFAULT NULL)
2731
  RETURNS SETOF $$||view_||$$
2732
  SET enable_sort TO 'off'
2733
  AS
2734
$BODY1$
2735 10990 aaronmk
SELECT * FROM $$||view_qual_name||$$($2, $3)
2736 8326 aaronmk
$BODY1$
2737
  LANGUAGE sql STABLE
2738
  COST 100
2739
  ROWS 1000
2740
;
2741
COMMENT ON FUNCTION $$||view_||$$(no_sort boolean, limit_ integer, offset_ integer) IS '
2742
Use this for limit values greater than ~100,000 to avoid unwanted slow sorts.
2743
If you want to run EXPLAIN and get expanded output, use the regular subset
2744
function instead. (When a config param is set on a function, EXPLAIN produces
2745
just a function scan.)
2746
';
2747
$$;
2748 8325 aaronmk
END;
2749
$_$;
2750
2751
2752
--
2753 11010 aaronmk
-- Name: FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass); Type: COMMENT; Schema: util; Owner: -
2754
--
2755
2756 12235 aaronmk
COMMENT ON FUNCTION mk_subset_by_row_num_no_sort_func(view_ regclass) IS '
2757
creates subset function which turns off enable_sort
2758
';
2759 11010 aaronmk
2760
2761
--
2762 12576 aaronmk
-- Name: mk_typed_cols_list(col_cast[]); Type: FUNCTION; Schema: util; Owner: -
2763
--
2764
2765
CREATE FUNCTION mk_typed_cols_list(cols col_cast[]) RETURNS text
2766
    LANGUAGE sql IMMUTABLE
2767
    AS $_$
2768 12579 aaronmk
SELECT COALESCE(string_agg(quote_ident((unnest).col_name)||$$ $$||
2769 12576 aaronmk
util.qual_name((unnest).type), ', '), '')
2770
FROM unnest($1)
2771
$_$;
2772
2773
2774
--
2775 12242 aaronmk
-- Name: name(regclass); Type: FUNCTION; Schema: util; Owner: -
2776
--
2777
2778
CREATE FUNCTION name(table_ regclass) RETURNS text
2779
    LANGUAGE sql STABLE
2780
    AS $_$
2781
SELECT relname::text FROM pg_class WHERE oid = $1
2782
$_$;
2783
2784
2785
--
2786 8183 aaronmk
-- Name: name(regtype); Type: FUNCTION; Schema: util; Owner: -
2787 8083 aaronmk
--
2788
2789
CREATE FUNCTION name(type regtype) RETURNS text
2790 12446 aaronmk
    LANGUAGE sql STABLE
2791 8083 aaronmk
    AS $_$
2792
SELECT typname::text FROM pg_type WHERE oid = $1
2793
$_$;
2794
2795
2796
--
2797 12360 aaronmk
-- Name: name_was_truncated(text, integer); Type: FUNCTION; Schema: util; Owner: -
2798 12355 aaronmk
--
2799
2800 12360 aaronmk
CREATE FUNCTION name_was_truncated(name_ text, max_prefix_len integer DEFAULT 0) RETURNS boolean
2801 12355 aaronmk
    LANGUAGE sql IMMUTABLE
2802
    AS $_$
2803 12360 aaronmk
SELECT octet_length($1) >= util.namedatalen() - $2
2804 12355 aaronmk
$_$;
2805
2806
2807
--
2808 12354 aaronmk
-- Name: namedatalen(); Type: FUNCTION; Schema: util; Owner: -
2809
--
2810
2811
CREATE FUNCTION namedatalen() RETURNS integer
2812
    LANGUAGE sql IMMUTABLE
2813
    AS $$
2814
SELECT octet_length(repeat('_', 1024/*>63*/)::name::text)
2815
$$;
2816
2817
2818
--
2819 9958 aaronmk
-- Name: not_empty(anyarray); Type: FUNCTION; Schema: util; Owner: -
2820
--
2821
2822
CREATE FUNCTION not_empty(value anyarray) RETURNS boolean
2823
    LANGUAGE sql IMMUTABLE
2824
    AS $_$
2825 10329 aaronmk
SELECT $1 IS NOT NULL AND util.array_length($1) > 0
2826 9958 aaronmk
$_$;
2827
2828
2829
--
2830 9956 aaronmk
-- Name: not_null(anyelement); Type: FUNCTION; Schema: util; Owner: -
2831
--
2832
2833
CREATE FUNCTION not_null(value anyelement) RETURNS boolean
2834 9957 aaronmk
    LANGUAGE sql IMMUTABLE
2835 9956 aaronmk
    AS $_$
2836
SELECT $1 IS NOT NULL
2837
$_$;
2838
2839
2840
--
2841 10373 aaronmk
-- Name: nulls_map(text[]); Type: FUNCTION; Schema: util; Owner: -
2842
--
2843
2844
CREATE FUNCTION nulls_map(nulls text[]) RETURNS hstore
2845
    LANGUAGE sql IMMUTABLE
2846
    AS $_$
2847
SELECT util.hstore($1, NULL) || '*=>*'
2848
$_$;
2849
2850
2851
--
2852
-- Name: FUNCTION nulls_map(nulls text[]); Type: COMMENT; Schema: util; Owner: -
2853
--
2854
2855 12235 aaronmk
COMMENT ON FUNCTION nulls_map(nulls text[]) IS '
2856
for use with _map()
2857
';
2858 10373 aaronmk
2859
2860
--
2861 10989 aaronmk
-- Name: offset2row_num(integer, integer); Type: FUNCTION; Schema: util; Owner: -
2862 10984 aaronmk
--
2863
2864 10989 aaronmk
CREATE FUNCTION offset2row_num(offset_ integer, min_row_num integer DEFAULT 1) RETURNS integer
2865 10984 aaronmk
    LANGUAGE sql IMMUTABLE
2866
    AS $_$
2867 10989 aaronmk
SELECT $2 + COALESCE($1, 0)
2868 10984 aaronmk
$_$;
2869
2870
2871
--
2872 12659 aaronmk
-- Name: parent(regclass); Type: FUNCTION; Schema: util; Owner: -
2873
--
2874
2875
CREATE FUNCTION parent(table_ regclass) RETURNS regclass
2876
    LANGUAGE sql STABLE
2877
    AS $_$
2878
SELECT inhparent FROM pg_inherits WHERE inhrelid = $1
2879
$_$;
2880
2881
2882
--
2883 13637 aaronmk
-- Name: pg_get_viewdef(regclass); Type: FUNCTION; Schema: util; Owner: -
2884
--
2885
2886
CREATE FUNCTION pg_get_viewdef(view_ regclass) RETURNS text
2887 13638 aaronmk
    LANGUAGE sql STABLE
2888 13637 aaronmk
    AS $_$
2889 13640 aaronmk
SELECT util.view_def_to_orig(pg_catalog.pg_get_viewdef($1))
2890 13637 aaronmk
$_$;
2891
2892
2893
--
2894 12651 aaronmk
-- Name: populate_table(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2895
--
2896
2897
CREATE FUNCTION populate_table(table_ regclass, sql text) RETURNS void
2898
    LANGUAGE sql
2899
    AS $_$
2900
SELECT util.eval($$INSERT INTO $$||$1||$$
2901
$$||util.ltrim_nl($2));
2902
-- make sure the created table has the correct estimated row count
2903
SELECT util.analyze_($1);
2904
$_$;
2905
2906
2907
--
2908 12575 aaronmk
-- Name: prefixed_name(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
2909
--
2910
2911
CREATE FUNCTION prefixed_name(prefix text, type anyelement) RETURNS text
2912
    LANGUAGE sql IMMUTABLE
2913
    AS $_$
2914
SELECT util.qual_name(util.schema($2), $1||util.name($2))
2915
$_$;
2916
2917
2918
--
2919 12494 aaronmk
-- Name: prepend_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
2920
--
2921
2922
CREATE FUNCTION prepend_comment(table_ regclass, comment text) RETURNS void
2923
    LANGUAGE sql
2924
    AS $_$
2925
SELECT util.set_comment($1, concat($2, util.comment($1)))
2926
$_$;
2927
2928
2929
--
2930
-- Name: FUNCTION prepend_comment(table_ regclass, comment text); Type: COMMENT; Schema: util; Owner: -
2931
--
2932
2933
COMMENT ON FUNCTION prepend_comment(table_ regclass, comment text) IS '
2934
comment: must start and end with a newline
2935
';
2936
2937
2938
--
2939 12260 aaronmk
-- Name: qual_name(text[]); Type: FUNCTION; Schema: util; Owner: -
2940
--
2941
2942
CREATE FUNCTION qual_name(VARIADIC elems text[]) RETURNS text
2943
    LANGUAGE sql IMMUTABLE
2944
    AS $_$
2945
SELECT string_agg(quote_ident(unnest), '.') FROM unnest($1)
2946
$_$;
2947
2948
2949
--
2950 10988 aaronmk
-- Name: qual_name(regclass); Type: FUNCTION; Schema: util; Owner: -
2951
--
2952
2953
CREATE FUNCTION qual_name(table_ regclass) RETURNS text
2954 12446 aaronmk
    LANGUAGE sql STABLE
2955 12267 aaronmk
    SET search_path TO pg_temp
2956 10988 aaronmk
    AS $_$
2957 12267 aaronmk
SELECT $1::text
2958 10988 aaronmk
$_$;
2959
2960
2961
--
2962 12267 aaronmk
-- Name: qual_name(regtype); Type: FUNCTION; Schema: util; Owner: -
2963
--
2964
2965
CREATE FUNCTION qual_name(type regtype) RETURNS text
2966 12446 aaronmk
    LANGUAGE sql STABLE
2967 12267 aaronmk
    SET search_path TO pg_temp
2968
    AS $_$
2969
SELECT $1::text
2970
$_$;
2971
2972
2973
--
2974
-- Name: FUNCTION qual_name(type regtype); Type: COMMENT; Schema: util; Owner: -
2975
--
2976
2977
COMMENT ON FUNCTION qual_name(type regtype) IS '
2978
a type''s schema-qualified name
2979
';
2980
2981
2982
--
2983 12268 aaronmk
-- Name: qual_name(unknown); Type: FUNCTION; Schema: util; Owner: -
2984
--
2985
2986
CREATE FUNCTION qual_name(type unknown) RETURNS text
2987 12446 aaronmk
    LANGUAGE sql STABLE
2988 12268 aaronmk
    AS $_$
2989
SELECT util.qual_name($1::text::regtype)
2990
$_$;
2991
2992
2993
--
2994 12376 aaronmk
-- Name: quote_func_call(regprocedure, text[]); Type: FUNCTION; Schema: util; Owner: -
2995
--
2996
2997
CREATE FUNCTION quote_func_call(func regprocedure, VARIADIC args_esc text[]) RETURNS text
2998
    LANGUAGE sql IMMUTABLE
2999
    AS $_$
3000
SELECT util.quote_func_call($1::regproc::text, VARIADIC $2)
3001
$_$;
3002
3003
3004
--
3005
-- Name: quote_func_call(text, text[]); Type: FUNCTION; Schema: util; Owner: -
3006
--
3007
3008
CREATE FUNCTION quote_func_call(func_esc text, VARIADIC args_esc text[]) RETURNS text
3009
    LANGUAGE sql IMMUTABLE
3010
    AS $_$
3011
SELECT $1||'('||concat_ws(', ', VARIADIC $2)||')'
3012
$_$;
3013
3014
3015
--
3016 12371 aaronmk
-- Name: quote_typed(anyelement); Type: FUNCTION; Schema: util; Owner: -
3017
--
3018
3019
CREATE FUNCTION quote_typed(value anyelement) RETURNS text
3020
    LANGUAGE sql IMMUTABLE
3021
    AS $_$
3022 12437 aaronmk
SELECT quote_nullable($1)||$$::$$||util.qual_name(pg_typeof($1))
3023 12371 aaronmk
$_$;
3024
3025
3026
--
3027 12530 aaronmk
-- Name: raise(text, text); Type: FUNCTION; Schema: util; Owner: -
3028
--
3029
3030
CREATE FUNCTION raise(type text, msg text) RETURNS void
3031
    LANGUAGE sql IMMUTABLE
3032 12560 aaronmk
    AS $_X$
3033 12530 aaronmk
SELECT util.eval($$
3034
CREATE OR REPLACE FUNCTION pg_temp.__raise()
3035
  RETURNS void AS
3036 12560 aaronmk
-- $__BODY1$ in case msg contains $BODY1$ (in SQL)
3037
$__BODY1$
3038 12530 aaronmk
BEGIN
3039
	RAISE $$||$1||$$ USING MESSAGE = $$||quote_nullable($2)||$$;
3040
END;
3041 12560 aaronmk
$__BODY1$
3042 12530 aaronmk
  LANGUAGE plpgsql IMMUTABLE
3043
  COST 100;
3044 12532 aaronmk
$$, verbose_ := false);
3045 12530 aaronmk
3046 12532 aaronmk
SELECT util.eval($$SELECT pg_temp.__raise()$$, verbose_ := false);
3047 12560 aaronmk
$_X$;
3048 12530 aaronmk
3049
3050
--
3051 12533 aaronmk
-- Name: FUNCTION raise(type text, msg text); Type: COMMENT; Schema: util; Owner: -
3052
--
3053
3054
COMMENT ON FUNCTION raise(type text, msg text) IS '
3055
type: a log level from
3056
http://www.postgresql.org/docs/9.3/static/plpgsql-errors-and-messages.html
3057
or a condition name from
3058
http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html
3059
';
3060
3061
3062
--
3063 12536 aaronmk
-- Name: raise_error_warning(text); Type: FUNCTION; Schema: util; Owner: -
3064 12311 aaronmk
--
3065
3066 12536 aaronmk
CREATE FUNCTION raise_error_warning(msg text) RETURNS void
3067 12441 aaronmk
    LANGUAGE sql IMMUTABLE
3068 12311 aaronmk
    AS $_$
3069 12536 aaronmk
SELECT util.raise('WARNING', 'ERROR:  '||$1)
3070 12311 aaronmk
$_$;
3071
3072
3073
--
3074 10116 aaronmk
-- Name: raise_undefined_column(col_ref); Type: FUNCTION; Schema: util; Owner: -
3075
--
3076
3077
CREATE FUNCTION raise_undefined_column(col col_ref) RETURNS text
3078
    LANGUAGE plpgsql IMMUTABLE STRICT
3079
    AS $$
3080
BEGIN
3081
	RAISE undefined_column USING MESSAGE = concat('undefined column: ', col.name);
3082
END;
3083
$$;
3084
3085
3086
--
3087 13512 aaronmk
-- Name: recreate(text, text[]); Type: FUNCTION; Schema: util; Owner: -
3088
--
3089
3090
CREATE FUNCTION recreate(cmd text, users text[] DEFAULT NULL::text[]) RETURNS void
3091
    LANGUAGE plpgsql
3092
    AS $_$
3093
DECLARE
3094
	PG_EXCEPTION_DETAIL text;
3095
	restore_views_info util.restore_views_info;
3096
BEGIN
3097
	restore_views_info = util.save_drop_views(users);
3098 13517 aaronmk
3099
	-- trigger the dependent_objects_still_exist exception
3100 13604 aaronmk
	PERFORM util.eval(COALESCE(util.mk_drop_from_create(cmd), '')||cmd);
3101 13517 aaronmk
		-- *not* CASCADE; it must trigger an exception
3102
3103 13512 aaronmk
	PERFORM util.restore_views(restore_views_info);
3104
EXCEPTION
3105
WHEN dependent_objects_still_exist THEN
3106
	IF users IS NOT NULL THEN RAISE; END IF; -- save_drop_views() didn't fix it
3107
	GET STACKED DIAGNOSTICS PG_EXCEPTION_DETAIL = PG_EXCEPTION_DETAIL;
3108
	users = array(SELECT * FROM util.regexp_matches_group(
3109
		PG_EXCEPTION_DETAIL, '(?m)^view (.*) depends on [[:lower:]]+ .*$'));
3110
		-- will be in forward dependency order
3111
	PERFORM util.debug_print_var('PG_EXCEPTION_DETAIL', PG_EXCEPTION_DETAIL);
3112
	PERFORM util.debug_print_var('users', users);
3113
	IF util.is_empty(users) THEN RAISE; END IF;
3114
	PERFORM util.recreate(cmd, users);
3115
END;
3116
$_$;
3117
3118
3119
--
3120
-- Name: FUNCTION recreate(cmd text, users text[]); Type: COMMENT; Schema: util; Owner: -
3121
--
3122
3123
COMMENT ON FUNCTION recreate(cmd text, users text[]) IS '
3124 13517 aaronmk
the appropriate drop statement will be added automatically.
3125
3126 13512 aaronmk
usage:
3127
SELECT util.recreate($$
3128 13524 aaronmk
CREATE VIEW schema.main_view AS _;
3129 13512 aaronmk
3130
-- manually restore views that need to be updated for the changes
3131 13524 aaronmk
CREATE VIEW schema.dependent_view AS _;
3132 13512 aaronmk
$$);
3133
3134
idempotent
3135
3136
users: not necessary to provide this because it will be autopopulated
3137
';
3138
3139
3140
--
3141 13525 aaronmk
-- Name: recreate_view(regclass, text, text); Type: FUNCTION; Schema: util; Owner: -
3142
--
3143
3144 13606 aaronmk
CREATE FUNCTION recreate_view(view_ regclass, view_query text DEFAULT NULL::text, dependent_view_changes text DEFAULT ''::text) RETURNS void
3145 13525 aaronmk
    LANGUAGE sql
3146
    AS $_$
3147
SELECT util.recreate($$
3148
CREATE VIEW $$||$1||$$ AS
3149 13606 aaronmk
$$||COALESCE($2, pg_get_viewdef($1))||$$;
3150 13525 aaronmk
$$||util.mk_set_relation_metadata($1)||$$
3151
3152
-- manually restore views that need to be updated for the changes
3153
$$||$3||$$
3154
$$);
3155
$_$;
3156
3157
3158
--
3159
-- Name: FUNCTION recreate_view(view_ regclass, view_query text, dependent_view_changes text); Type: COMMENT; Schema: util; Owner: -
3160
--
3161
3162
COMMENT ON FUNCTION recreate_view(view_ regclass, view_query text, dependent_view_changes text) IS '
3163
usage:
3164
SELECT util.recreate_view(''schema.main_view'', $$
3165
SELECT __
3166
$$, $$
3167
CREATE VIEW schema.dependent_view AS
3168
__;
3169
$$||util.mk_set_relation_metadata(''schema.dependent_view'')||$$
3170
$$);
3171
3172 13606 aaronmk
if view has already been modified:
3173
SELECT util.recreate_view(''schema.main_view'', dependent_view_changes := $$
3174
CREATE VIEW schema.dependent_view AS
3175
__;
3176
$$||util.mk_set_relation_metadata(''schema.dependent_view'')||$$
3177
$$);
3178
3179 13525 aaronmk
idempotent
3180
';
3181
3182
3183
--
3184 13514 aaronmk
-- Name: regexp_match(text, text); Type: FUNCTION; Schema: util; Owner: -
3185
--
3186
3187
CREATE FUNCTION regexp_match(str text, re text) RETURNS text[]
3188
    LANGUAGE sql IMMUTABLE
3189
    AS $_$
3190
SELECT match FROM regexp_matches($1, $2) match LIMIT 1/*only 1st match*/
3191
$_$;
3192
3193
3194
--
3195 11657 aaronmk
-- Name: regexp_matches_group(text, text, integer); Type: FUNCTION; Schema: util; Owner: -
3196
--
3197
3198
CREATE FUNCTION regexp_matches_group(str text, re text, group_ integer DEFAULT 1) RETURNS SETOF text
3199
    LANGUAGE sql IMMUTABLE
3200
    AS $_$
3201
SELECT regexp_matches[$3] FROM regexp_matches($1, $2, 'g')
3202
$_$;
3203
3204
3205
--
3206 12333 aaronmk
-- Name: regexp_quote(text); Type: FUNCTION; Schema: util; Owner: -
3207
--
3208
3209
CREATE FUNCTION regexp_quote(str text) RETURNS text
3210
    LANGUAGE sql IMMUTABLE
3211
    AS $_$
3212
SELECT regexp_replace($1, '\W', /*\char*/'\\\&', 'g')
3213
$_$;
3214
3215
3216
--
3217 12375 aaronmk
-- Name: regprocedure(text); Type: FUNCTION; Schema: util; Owner: -
3218
--
3219
3220
CREATE FUNCTION regprocedure(func text) RETURNS regprocedure
3221
    LANGUAGE sql IMMUTABLE
3222
    AS $_$
3223
SELECT (CASE WHEN right($1, 1) = ')'
3224 12377 aaronmk
THEN $1::regprocedure ELSE $1::regproc::regprocedure END)
3225 12375 aaronmk
$_$;
3226
3227
3228
--
3229 13492 aaronmk
-- Name: relation_exists(text); Type: FUNCTION; Schema: util; Owner: -
3230
--
3231
3232
CREATE FUNCTION relation_exists(relation text) RETURNS boolean
3233
    LANGUAGE sql STABLE
3234
    AS $_$
3235
SELECT $1 IS NOT NULL AND util.is_castable($1, NULL::regclass)
3236
$_$;
3237
3238
3239
--
3240 12344 aaronmk
-- Name: relation_type(regclass); Type: FUNCTION; Schema: util; Owner: -
3241
--
3242
3243
CREATE FUNCTION relation_type(relation regclass) RETURNS text
3244
    LANGUAGE sql STABLE
3245
    AS $_$
3246
SELECT util.relation_type(util.relation_type_char($1))
3247
$_$;
3248
3249
3250
--
3251 12340 aaronmk
-- Name: relation_type("char"); Type: FUNCTION; Schema: util; Owner: -
3252 12339 aaronmk
--
3253
3254 12340 aaronmk
CREATE FUNCTION relation_type(relation_type_char "char") RETURNS text
3255 12339 aaronmk
    LANGUAGE sql IMMUTABLE
3256
    AS $_$
3257 12593 aaronmk
SELECT 'c=>TYPE, r=>TABLE, v=>VIEW'::hstore -> $1
3258 12339 aaronmk
$_$;
3259
3260
3261
--
3262 12588 aaronmk
-- Name: relation_type(regtype); Type: FUNCTION; Schema: util; Owner: -
3263
--
3264
3265
CREATE FUNCTION relation_type(type regtype) RETURNS text
3266
    LANGUAGE sql IMMUTABLE
3267
    AS $$
3268
SELECT 'TYPE'::text
3269
$$;
3270
3271
3272
--
3273 12341 aaronmk
-- Name: relation_type_char(regclass); Type: FUNCTION; Schema: util; Owner: -
3274
--
3275
3276
CREATE FUNCTION relation_type_char(relation regclass) RETURNS "char"
3277
    LANGUAGE sql STABLE
3278
    AS $_$
3279
SELECT relkind FROM pg_class WHERE oid = $1
3280
$_$;
3281
3282
3283
--
3284 12293 aaronmk
-- Name: remake_diff_table(text, regclass, regclass, text); Type: FUNCTION; Schema: util; Owner: -
3285
--
3286
3287
CREATE FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text) RETURNS void
3288
    LANGUAGE sql
3289
    AS $_$
3290
/* can't have in_table/out_table inherit from *each other*, because inheritance
3291
also causes the rows of the parent table to be included in the child table.
3292
instead, they need to inherit from a common, empty table. */
3293 12382 aaronmk
SELECT util.create_if_not_exists($$SELECT $$||util.quote_func_call(
3294
'util.copy_struct', util.quote_typed($2), util.quote_typed($4)));
3295 13098 aaronmk
SELECT util.rm_freq(ARRAY[$4]); -- left/right_table don't have freq yet
3296 12293 aaronmk
SELECT util.inherit($2, $4);
3297
SELECT util.inherit($3, $4);
3298
3299
SELECT util.rematerialize_query($1, $$
3300
SELECT * FROM util.diff(
3301 12419 aaronmk
  $$||util.quote_typed($2)||$$
3302
, $$||util.quote_typed($3)||$$
3303 12293 aaronmk
, NULL::$$||$4||$$)
3304
$$);
3305 12303 aaronmk
3306
/* the table unfortunately cannot be *materialized* in human-readable form,
3307
because this would create column name collisions between the two sides */
3308 12495 aaronmk
SELECT util.prepend_comment($1, '
3309 12303 aaronmk
to view this table in human-readable form (with each side''s tuple column
3310
expanded to its component fields):
3311 12572 aaronmk
SELECT (left_).*, ('||util.schema($4::regclass)||'.values_(right_)).* FROM '||$1||';
3312 13092 aaronmk
3313
to display NULL values that are extra or missing:
3314
SELECT * FROM '||$1||';
3315 12303 aaronmk
');
3316 12293 aaronmk
$_$;
3317
3318
3319
--
3320
-- Name: FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text); Type: COMMENT; Schema: util; Owner: -
3321
--
3322
3323
COMMENT ON FUNCTION remake_diff_table(diff_table text, left_table regclass, right_table regclass, type_table text) IS '
3324
type_table (*required*): table to create as the shared base type
3325
';
3326
3327
3328
--
3329 12265 aaronmk
-- Name: rematerialize_query(text, text); Type: FUNCTION; Schema: util; Owner: -
3330
--
3331
3332
CREATE FUNCTION rematerialize_query(table_esc text, sql text) RETURNS void
3333
    LANGUAGE sql
3334
    AS $_$
3335
SELECT util.drop_table($1);
3336
SELECT util.materialize_query($1, $2);
3337
$_$;
3338
3339
3340
--
3341
-- Name: FUNCTION rematerialize_query(table_esc text, sql text); Type: COMMENT; Schema: util; Owner: -
3342
--
3343
3344
COMMENT ON FUNCTION rematerialize_query(table_esc text, sql text) IS '
3345
idempotent, but repeats action each time
3346
';
3347
3348
3349
--
3350 12247 aaronmk
-- Name: rematerialize_view(text, regclass); Type: FUNCTION; Schema: util; Owner: -
3351
--
3352
3353 12262 aaronmk
CREATE FUNCTION rematerialize_view(table_esc text, view_ regclass) RETURNS void
3354 12247 aaronmk
    LANGUAGE sql
3355
    AS $_$
3356
SELECT util.drop_table($1);
3357
SELECT util.materialize_view($1, $2);
3358
$_$;
3359
3360
3361
--
3362 12262 aaronmk
-- Name: FUNCTION rematerialize_view(table_esc text, view_ regclass); Type: COMMENT; Schema: util; Owner: -
3363 12247 aaronmk
--
3364
3365 12262 aaronmk
COMMENT ON FUNCTION rematerialize_view(table_esc text, view_ regclass) IS '
3366 12247 aaronmk
idempotent, but repeats action each time
3367
';
3368
3369
3370
--
3371 8183 aaronmk
-- Name: rename_cols(regclass, anyelement); Type: FUNCTION; Schema: util; Owner: -
3372 8137 aaronmk
--
3373
3374 8148 aaronmk
CREATE FUNCTION rename_cols(table_ regclass, renames anyelement) RETURNS void
3375 12446 aaronmk
    LANGUAGE sql
3376 8137 aaronmk
    AS $_$
3377 8212 aaronmk
SELECT util.try_create($$ALTER TABLE $$||$1||$$ RENAME $$
3378 8137 aaronmk
||quote_ident(name)||$$ TO $$||quote_ident($2 -> name))
3379 10309 aaronmk
FROM util.col_names($1::text::regtype) f (name);
3380
SELECT NULL::void; -- don't fold away functions called in previous query
3381 8137 aaronmk
$_$;
3382
3383
3384
--
3385 8183 aaronmk
-- Name: FUNCTION rename_cols(table_ regclass, renames anyelement); Type: COMMENT; Schema: util; Owner: -
3386 8137 aaronmk
--
3387
3388 12235 aaronmk
COMMENT ON FUNCTION rename_cols(table_ regclass, renames anyelement) IS '
3389
idempotent
3390
';
3391 8137 aaronmk
3392
3393
--
3394 12349 aaronmk
-- Name: rename_relation(regclass, text); Type: FUNCTION; Schema: util; Owner: -
3395
--
3396
3397
CREATE FUNCTION rename_relation(from_ regclass, to_ text) RETURNS void
3398
    LANGUAGE sql
3399
    AS $_$
3400 12353 aaronmk
/* use util.qual_name() instead of ::text so that the schema qualifier is always
3401
included in the debug SQL */
3402
SELECT util.rename_relation(util.qual_name($1), $2)
3403 12349 aaronmk
$_$;
3404
3405
3406
--
3407
-- Name: rename_relation(text, text); Type: FUNCTION; Schema: util; Owner: -
3408
--
3409
3410 12364 aaronmk
CREATE FUNCTION rename_relation(from_esc text, to_name text) RETURNS void
3411 12349 aaronmk
    LANGUAGE sql
3412
    AS $_$
3413
/* 'ALTER TABLE can be used with views too'
3414
(http://www.postgresql.org/docs/9.3/static/sql-alterview.html) */
3415 12363 aaronmk
SELECT util.eval($$ALTER TABLE IF EXISTS $$||$1||$$ RENAME TO $$
3416
||quote_ident($2))
3417 12349 aaronmk
$_$;
3418
3419
3420
--
3421 12364 aaronmk
-- Name: FUNCTION rename_relation(from_esc text, to_name text); Type: COMMENT; Schema: util; Owner: -
3422 12349 aaronmk
--
3423
3424 12364 aaronmk
COMMENT ON FUNCTION rename_relation(from_esc text, to_name text) IS '
3425 12349 aaronmk
idempotent
3426
';
3427
3428
3429
--
3430 12358 aaronmk
-- Name: replace_suffix(text, text, text, integer); Type: FUNCTION; Schema: util; Owner: -
3431 12350 aaronmk
--
3432
3433 12358 aaronmk
CREATE FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer DEFAULT 0) RETURNS text
3434 12350 aaronmk
    LANGUAGE sql IMMUTABLE
3435
    AS $_$
3436 12358 aaronmk
SELECT regexp_replace($1, util.truncated_prefixed_name_regexp($2, $4), '\1'||$3)
3437 12350 aaronmk
$_$;
3438
3439
3440
--
3441 12358 aaronmk
-- Name: FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer); Type: COMMENT; Schema: util; Owner: -
3442
--
3443
3444
COMMENT ON FUNCTION replace_suffix(str text, old_suffix text, new_suffix text, max_prefix_len integer) IS '
3445
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
3446
';
3447
3448
3449
--
3450 10297 aaronmk
-- Name: reset_col_names(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
3451
--
3452
3453
CREATE FUNCTION reset_col_names(table_ regclass, names regclass) RETURNS void
3454 12446 aaronmk
    LANGUAGE sql
3455 10297 aaronmk
    AS $_$
3456 10596 aaronmk
SELECT util.eval($$DELETE FROM $$||$2||$$ WHERE "from" LIKE ':%'$$);
3457
SELECT util.mk_derived_col(($2, 'to'), $$"from"$$, overwrite := true);
3458 10297 aaronmk
SELECT util.set_col_names($1, $2);
3459
$_$;
3460
3461
3462
--
3463
-- Name: FUNCTION reset_col_names(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
3464
--
3465
3466 12235 aaronmk
COMMENT ON FUNCTION reset_col_names(table_ regclass, names regclass) IS '
3467
idempotent.
3468
alters the names table, so it will need to be repopulated after running this function.
3469
';
3470 10297 aaronmk
3471
3472
--
3473 8183 aaronmk
-- Name: reset_map_table(text); Type: FUNCTION; Schema: util; Owner: -
3474 8143 aaronmk
--
3475
3476
CREATE FUNCTION reset_map_table(table_ text) RETURNS void
3477 12446 aaronmk
    LANGUAGE sql
3478 8143 aaronmk
    AS $_$
3479 10152 aaronmk
SELECT util.drop_table($1);
3480 8183 aaronmk
SELECT util.mk_map_table($1);
3481 8143 aaronmk
$_$;
3482
3483
3484
--
3485 13488 aaronmk
-- Name: restore_views(restore_views_info); Type: FUNCTION; Schema: util; Owner: -
3486 13486 aaronmk
--
3487
3488 13488 aaronmk
CREATE FUNCTION restore_views(restore_views_info) RETURNS void
3489 13486 aaronmk
    LANGUAGE sql
3490
    AS $_$
3491
SELECT util.debug_print_var('views', $1);
3492 13497 aaronmk
SELECT util.create_if_not_exists((view_).def, (view_).path)
3493
	/* need to specify view name for manual existence check, in case view def
3494
	becomes invalid, which would produce nonstandard (uncatchable) exception */
3495 13491 aaronmk
FROM unnest($1.views) view_; -- in forward dependency order
3496 13486 aaronmk
	/* create_if_not_exists() rather than eval(), because cmd might manually
3497
	re-create a deleted dependent view, causing it to already exist */
3498
SELECT NULL::void; -- don't fold away functions called in previous query
3499
$_$;
3500
3501
3502
--
3503 13096 aaronmk
-- Name: rm_freq(regclass[], text); Type: FUNCTION; Schema: util; Owner: -
3504
--
3505
3506
CREATE FUNCTION rm_freq(tables regclass[], freq_col text DEFAULT 'copies'::text) RETURNS void
3507
    LANGUAGE sql
3508
    AS $_$
3509
SELECT util.drop_column($1, $2, force := true)
3510
$_$;
3511
3512
3513
--
3514 12356 aaronmk
-- Name: rtrim_n(text, integer); Type: FUNCTION; Schema: util; Owner: -
3515
--
3516
3517
CREATE FUNCTION rtrim_n(str text, count integer) RETURNS text
3518
    LANGUAGE sql IMMUTABLE
3519
    AS $_$
3520
SELECT (CASE WHEN $2 <= 0 THEN $1 ELSE left($1, -$2) END)
3521
$_$;
3522
3523
3524
--
3525 12473 aaronmk
-- Name: runnable_sql(text); Type: FUNCTION; Schema: util; Owner: -
3526
--
3527
3528
CREATE FUNCTION runnable_sql(sql text) RETURNS text
3529
    LANGUAGE sql IMMUTABLE
3530
    AS $_$
3531 12481 aaronmk
SELECT (CASE WHEN util.is_set_stmt($1) THEN ''
3532
ELSE util.mk_set_search_path(for_printing := true)||$$;
3533
$$ END)||$1
3534 12473 aaronmk
$_$;
3535
3536
3537
--
3538 11652 aaronmk
-- Name: save_drop_view(text); Type: FUNCTION; Schema: util; Owner: -
3539
--
3540
3541
CREATE FUNCTION save_drop_view(view_ text) RETURNS text
3542
    LANGUAGE plpgsql STRICT
3543 13467 aaronmk
    AS $$
3544 11652 aaronmk
DECLARE
3545
	result text = NULL;
3546
BEGIN
3547
	BEGIN
3548 13470 aaronmk
		result = util.show_create_view(view_, replace := false);
3549
			/* replace: no `OR REPLACE` because that causes nonuniform errors
3550
			(eg. invalid_table_definition), instead of the standard
3551
			duplicate_table exception caught by util.create_if_not_exists() */
3552 13467 aaronmk
		PERFORM util.drop_view(view_);
3553 11652 aaronmk
	EXCEPTION
3554
		WHEN undefined_table THEN NULL;
3555
	END;
3556
	RETURN result;
3557
END;
3558 13467 aaronmk
$$;
3559 11652 aaronmk
3560
3561
--
3562 11660 aaronmk
-- Name: save_drop_views(text[]); Type: FUNCTION; Schema: util; Owner: -
3563
--
3564
3565 13488 aaronmk
CREATE FUNCTION save_drop_views(views text[]) RETURNS restore_views_info
3566 11660 aaronmk
    LANGUAGE sql
3567
    AS $_$
3568 13488 aaronmk
SELECT ROW(/*return in forward dependency order*/util.array_reverse(array(
3569 13491 aaronmk
SELECT (view_, util.save_drop_view(view_))::util.db_item
3570 13485 aaronmk
FROM unnest(/*drop in reverse dependency order*/util.array_reverse($1)) view_
3571 13488 aaronmk
)))::util.restore_views_info
3572 11660 aaronmk
$_$;
3573
3574
3575
--
3576 12244 aaronmk
-- Name: schema(oid); Type: FUNCTION; Schema: util; Owner: -
3577
--
3578
3579
CREATE FUNCTION schema(pg_namespace_oid oid) RETURNS text
3580
    LANGUAGE sql STABLE
3581
    AS $_$
3582
SELECT nspname::text FROM pg_namespace WHERE pg_namespace.oid = $1
3583
$_$;
3584
3585
3586
--
3587 12242 aaronmk
-- Name: schema(regclass); Type: FUNCTION; Schema: util; Owner: -
3588
--
3589
3590
CREATE FUNCTION schema(table_ regclass) RETURNS text
3591
    LANGUAGE sql STABLE
3592
    AS $_$
3593 12245 aaronmk
SELECT util.schema(relnamespace) FROM pg_class WHERE oid = $1
3594 12242 aaronmk
$_$;
3595
3596
3597
--
3598 10794 aaronmk
-- Name: schema(regtype); Type: FUNCTION; Schema: util; Owner: -
3599
--
3600
3601
CREATE FUNCTION schema(type regtype) RETURNS text
3602
    LANGUAGE sql STABLE
3603
    AS $_$
3604 12245 aaronmk
SELECT util.schema(typnamespace) FROM pg_type WHERE oid = $1
3605 10794 aaronmk
$_$;
3606
3607
3608
--
3609
-- Name: schema(anyelement); Type: FUNCTION; Schema: util; Owner: -
3610
--
3611
3612
CREATE FUNCTION schema(type_null anyelement) RETURNS text
3613
    LANGUAGE sql STABLE
3614
    AS $_$
3615
SELECT util.schema(pg_typeof($1))
3616
$_$;
3617
3618
3619
--
3620 12134 aaronmk
-- Name: schema_bundle_get_schemas(text); Type: FUNCTION; Schema: util; Owner: -
3621
--
3622
3623
CREATE FUNCTION schema_bundle_get_schemas(schema_bundle text) RETURNS SETOF text
3624
    LANGUAGE sql STABLE
3625
    AS $_$
3626
SELECT nspname::text FROM pg_namespace WHERE nspname ~ ('^'||$1||'(?=\y|_)')
3627
$_$;
3628
3629
3630
--
3631 12135 aaronmk
-- Name: FUNCTION schema_bundle_get_schemas(schema_bundle text); Type: COMMENT; Schema: util; Owner: -
3632
--
3633
3634 12235 aaronmk
COMMENT ON FUNCTION schema_bundle_get_schemas(schema_bundle text) IS '
3635
a schema bundle is a group of schemas with a common prefix
3636
';
3637 12135 aaronmk
3638
3639
--
3640
-- Name: schema_bundle_rename(text, text); Type: FUNCTION; Schema: util; Owner: -
3641
--
3642
3643
CREATE FUNCTION schema_bundle_rename(old text, new text) RETURNS void
3644
    LANGUAGE sql
3645
    AS $_$
3646
SELECT util.schema_rename(old_schema,
3647
	overlay(old_schema placing new from 1 for length(old))) -- replace prefix
3648
FROM util.schema_bundle_get_schemas($1) f (old_schema);
3649
SELECT NULL::void; -- don't fold away functions called in previous query
3650
$_$;
3651
3652
3653
--
3654
-- Name: schema_bundle_replace(text, text); Type: FUNCTION; Schema: util; Owner: -
3655
--
3656
3657
CREATE FUNCTION schema_bundle_replace(replace text, with_ text) RETURNS void
3658
    LANGUAGE plpgsql
3659
    AS $$
3660
BEGIN
3661
	-- don't schema_bundle_rm() the schema_bundle to keep!
3662
	IF replace = with_ THEN RETURN; END IF;
3663
3664
	PERFORM util.schema_bundle_rm(replace);
3665
	PERFORM util.schema_bundle_rename(with_, replace);
3666
END;
3667
$$;
3668
3669
3670
--
3671
-- Name: schema_bundle_rm(text); Type: FUNCTION; Schema: util; Owner: -
3672
--
3673
3674
CREATE FUNCTION schema_bundle_rm(schema_bundle text) RETURNS void
3675
    LANGUAGE sql
3676
    AS $_$
3677
SELECT util.schema_rm(schema)
3678
FROM util.schema_bundle_get_schemas($1) f (schema);
3679
SELECT NULL::void; -- don't fold away functions called in previous query
3680
$_$;
3681
3682
3683
--
3684 12238 aaronmk
-- Name: schema_esc(anyelement); Type: FUNCTION; Schema: util; Owner: -
3685 10795 aaronmk
--
3686
3687 12238 aaronmk
CREATE FUNCTION schema_esc(type_null anyelement) RETURNS text
3688 10795 aaronmk
    LANGUAGE sql STABLE
3689
    AS $_$
3690
SELECT quote_ident(util.schema($1))
3691
$_$;
3692
3693
3694
--
3695 12324 aaronmk
-- Name: schema_matches(text, text); Type: FUNCTION; Schema: util; Owner: -
3696
--
3697
3698
CREATE FUNCTION schema_matches(schema text, schema_regexp text) RETURNS boolean
3699 12334 aaronmk
    LANGUAGE sql IMMUTABLE
3700 12324 aaronmk
    AS $_$
3701
SELECT $1 ~ $2 AND /*in userspace*/$1 !~ '^(?:information_schema|pg_.*)$'
3702
$_$;
3703
3704
3705
--
3706 12304 aaronmk
-- Name: schema_oid(text); Type: FUNCTION; Schema: util; Owner: -
3707
--
3708
3709
CREATE FUNCTION schema_oid(schema text) RETURNS oid
3710
    LANGUAGE sql STABLE
3711
    AS $_$
3712
SELECT oid FROM pg_namespace WHERE nspname = $1
3713
$_$;
3714
3715
3716
--
3717 12504 aaronmk
-- Name: schema_regexp(regclass); Type: FUNCTION; Schema: util; Owner: -
3718
--
3719
3720
CREATE FUNCTION schema_regexp(relation regclass) RETURNS text
3721
    LANGUAGE sql IMMUTABLE
3722
    AS $_$
3723
SELECT util.schema_regexp(schema_anchor := $1)
3724
$_$;
3725
3726
3727
--
3728 12501 aaronmk
-- Name: schema_regexp(anyelement); Type: FUNCTION; Schema: util; Owner: -
3729
--
3730
3731
CREATE FUNCTION schema_regexp(schema_anchor anyelement) RETURNS text
3732
    LANGUAGE sql IMMUTABLE
3733
    AS $_$
3734
SELECT util.str_equality_regexp(util.schema($1))
3735
$_$;
3736
3737
3738
--
3739 12132 aaronmk
-- Name: schema_rename(text, text); Type: FUNCTION; Schema: util; Owner: -
3740
--
3741
3742
CREATE FUNCTION schema_rename(old text, new text) RETURNS void
3743
    LANGUAGE sql
3744
    AS $_$
3745
SELECT util.eval($$ALTER SCHEMA $$||quote_ident($1)||$$ RENAME TO $$||quote_ident($2));
3746
$_$;
3747
3748
3749
--
3750 12133 aaronmk
-- Name: schema_replace(text, text); Type: FUNCTION; Schema: util; Owner: -
3751
--
3752
3753
CREATE FUNCTION schema_replace(replace text, with_ text) RETURNS void
3754
    LANGUAGE plpgsql
3755
    AS $$
3756
BEGIN
3757
	-- don't schema_rm() the schema to keep!
3758
	IF replace = with_ THEN RETURN; END IF;
3759
3760
	PERFORM util.schema_rm(replace);
3761
	PERFORM util.schema_rename(with_, replace);
3762
END;
3763
$$;
3764
3765
3766
--
3767 12132 aaronmk
-- Name: schema_rm(text); Type: FUNCTION; Schema: util; Owner: -
3768
--
3769
3770
CREATE FUNCTION schema_rm(schema text) RETURNS void
3771
    LANGUAGE sql
3772
    AS $_$
3773
SELECT util.eval($$DROP SCHEMA IF EXISTS $$||quote_ident($1)||$$ CASCADE$$);
3774
$_$;
3775
3776
3777
--
3778 9825 aaronmk
-- Name: search_path_append(text); Type: FUNCTION; Schema: util; Owner: -
3779
--
3780
3781
CREATE FUNCTION search_path_append(schemas text) RETURNS void
3782 12446 aaronmk
    LANGUAGE sql
3783 9825 aaronmk
    AS $_$
3784
SELECT util.eval(
3785
$$SET search_path TO $$||current_setting('search_path')||$$, $$||$1);
3786
$_$;
3787
3788
3789
--
3790 13565 aaronmk
-- Name: seq__create(text, integer); Type: FUNCTION; Schema: util; Owner: -
3791
--
3792
3793
CREATE FUNCTION seq__create(seq text, start integer DEFAULT 0) RETURNS void
3794
    LANGUAGE sql
3795
    AS $_$
3796
SELECT util.create_if_not_exists($$CREATE SEQUENCE $$||$1||$$ MINVALUE $$||$2,
3797
$1)
3798
$_$;
3799
3800
3801
--
3802
-- Name: FUNCTION seq__create(seq text, start integer); Type: COMMENT; Schema: util; Owner: -
3803
--
3804
3805
COMMENT ON FUNCTION seq__create(seq text, start integer) IS '
3806
idempotent
3807
';
3808
3809
3810
--
3811 13566 aaronmk
-- Name: seq__reset(text, integer); Type: FUNCTION; Schema: util; Owner: -
3812
--
3813
3814
CREATE FUNCTION seq__reset(seq text, start integer DEFAULT 0) RETURNS void
3815
    LANGUAGE sql
3816
    AS $_$
3817
SELECT util.seq__create($1, $2);
3818
SELECT util.eval($$ALTER SEQUENCE $$||$1||$$ RESTART$$);
3819
$_$;
3820
3821
3822
--
3823
-- Name: FUNCTION seq__reset(seq text, start integer); Type: COMMENT; Schema: util; Owner: -
3824
--
3825
3826
COMMENT ON FUNCTION seq__reset(seq text, start integer) IS '
3827
creates sequence if doesn''t exist
3828
3829
idempotent
3830
3831
start: *note*: only used if sequence doesn''t exist
3832
';
3833
3834
3835
--
3836 8183 aaronmk
-- Name: set_col_names(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
3837 8153 aaronmk
--
3838
3839
CREATE FUNCTION set_col_names(table_ regclass, names regclass) RETURNS void
3840
    LANGUAGE plpgsql STRICT
3841
    AS $_$
3842
DECLARE
3843 8183 aaronmk
    old text[] = ARRAY(SELECT util.col_names(table_));
3844
    new text[] = ARRAY(SELECT util.map_values(names));
3845 8153 aaronmk
BEGIN
3846
    old = old[1:array_length(new, 1)]; -- truncate to same length
3847 10345 aaronmk
    PERFORM util.eval($$ALTER TABLE $$||$1||$$ RENAME $$||quote_ident(key)
3848
||$$ TO $$||quote_ident(value))
3849 10149 aaronmk
    FROM each(hstore(old, new))
3850
    WHERE value != key -- not same name
3851
    ;
3852 8153 aaronmk
END;
3853
$_$;
3854
3855
3856
--
3857 8183 aaronmk
-- Name: FUNCTION set_col_names(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
3858 8153 aaronmk
--
3859
3860 12235 aaronmk
COMMENT ON FUNCTION set_col_names(table_ regclass, names regclass) IS '
3861
idempotent
3862
';
3863 8153 aaronmk
3864
3865
--
3866 10145 aaronmk
-- Name: set_col_names_with_metadata(regclass, regclass); Type: FUNCTION; Schema: util; Owner: -
3867
--
3868
3869
CREATE FUNCTION set_col_names_with_metadata(table_ regclass, names regclass) RETURNS void
3870
    LANGUAGE plpgsql STRICT
3871
    AS $_$
3872
DECLARE
3873
	row_ util.map;
3874
BEGIN
3875 10715 aaronmk
	-- rename any metadata cols rather than re-adding them with new names
3876
	BEGIN
3877
		PERFORM util.set_col_names(table_, names);
3878
	EXCEPTION
3879
		WHEN array_subscript_error THEN -- selective suppress
3880
			IF SQLERRM LIKE 'arrays must have same bounds' THEN NULL;
3881
				-- metadata cols not yet added
3882 12568 aaronmk
			ELSE RAISE;
3883 10715 aaronmk
			END IF;
3884
	END;
3885
3886 10157 aaronmk
	FOR row_ IN EXECUTE $$SELECT * FROM $$||names||$$ WHERE "from" LIKE ':%'$$
3887 10145 aaronmk
	LOOP
3888 10147 aaronmk
		PERFORM util.mk_const_col((table_, row_."to"),
3889
			substring(row_."from" from 2));
3890 10145 aaronmk
	END LOOP;
3891
3892
	PERFORM util.set_col_names(table_, names);
3893
END;
3894
$_$;
3895
3896
3897
--
3898
-- Name: FUNCTION set_col_names_with_metadata(table_ regclass, names regclass); Type: COMMENT; Schema: util; Owner: -
3899
--
3900
3901 12235 aaronmk
COMMENT ON FUNCTION set_col_names_with_metadata(table_ regclass, names regclass) IS '
3902
idempotent.
3903
the metadata mappings must be *last* in the names table.
3904
';
3905 10145 aaronmk
3906
3907
--
3908 8183 aaronmk
-- Name: set_col_types(regclass, col_cast[]); Type: FUNCTION; Schema: util; Owner: -
3909 8107 aaronmk
--
3910
3911
CREATE FUNCTION set_col_types(table_ regclass, col_casts col_cast[]) RETURNS void
3912 12733 aaronmk
    LANGUAGE sql
3913 8107 aaronmk
    AS $_$
3914 12734 aaronmk
SELECT util.eval(COALESCE(
3915
$$ALTER TABLE $$||$1||$$
3916 12732 aaronmk
$$||(
3917
	SELECT
3918
	string_agg($$ALTER COLUMN $$||col_name_sql||$$ TYPE $$||target_type
3919
	||$$ USING $$||col_name_sql||$$::$$||target_type, $$
3920
, $$)
3921
	FROM
3922
	(
3923
		SELECT
3924
		  quote_ident(col_name) AS col_name_sql
3925 12733 aaronmk
		, util.col_type(($1, col_name)) AS curr_type
3926 12732 aaronmk
		, type AS target_type
3927 12733 aaronmk
		FROM unnest($2)
3928 12732 aaronmk
	) s
3929
	WHERE curr_type != target_type
3930 12734 aaronmk
), ''))
3931 8107 aaronmk
$_$;
3932
3933
3934
--
3935 8183 aaronmk
-- Name: FUNCTION set_col_types(table_ regclass, col_casts col_cast[]); Type: COMMENT; Schema: util; Owner: -
3936 8107 aaronmk
--
3937
3938 12235 aaronmk
COMMENT ON FUNCTION set_col_types(table_ regclass, col_casts col_cast[]) IS '
3939
idempotent
3940
';
3941 8107 aaronmk
3942
3943
--
3944 12302 aaronmk
-- Name: set_comment(regclass, text); Type: FUNCTION; Schema: util; Owner: -
3945
--
3946
3947
CREATE FUNCTION set_comment(table_ regclass, comment text) RETURNS void
3948 12446 aaronmk
    LANGUAGE sql
3949 12302 aaronmk
    AS $_$
3950 13477 aaronmk
SELECT util.eval(util.mk_set_comment($1, $2))
3951 12302 aaronmk
$_$;
3952
3953
3954
--
3955 12482 aaronmk
-- Name: set_search_path(text, boolean); Type: FUNCTION; Schema: util; Owner: -
3956
--
3957
3958
CREATE FUNCTION set_search_path(search_path text, for_session boolean DEFAULT false) RETURNS void
3959
    LANGUAGE sql
3960
    AS $_$
3961
SELECT util.eval(util.mk_set_search_path($1, $2))
3962
$_$;
3963
3964
3965
--
3966 13468 aaronmk
-- Name: show_create_view(regclass, boolean); Type: FUNCTION; Schema: util; Owner: -
3967 11651 aaronmk
--
3968
3969 13468 aaronmk
CREATE FUNCTION show_create_view(view_ regclass, replace boolean DEFAULT true) RETURNS text
3970 11651 aaronmk
    LANGUAGE sql STABLE
3971
    AS $_$
3972 13468 aaronmk
SELECT $$CREATE$$||(CASE WHEN $2 THEN $$ OR REPLACE$$ ELSE '' END)||$$ VIEW $$
3973
||$1||$$ AS
3974 13482 aaronmk
$$||pg_get_viewdef($1)/*no ; because pg_get_viewdef() includes one*/||$$
3975 13505 aaronmk
$$||util.mk_set_relation_metadata($1)
3976 11651 aaronmk
$_$;
3977
3978
3979
--
3980 11655 aaronmk
-- Name: show_grants_for(regclass); Type: FUNCTION; Schema: util; Owner: -
3981
--
3982
3983
CREATE FUNCTION show_grants_for(table_ regclass) RETURNS text
3984
    LANGUAGE sql STABLE
3985
    AS $_$
3986 12269 aaronmk
SELECT string_agg(cmd, '')
3987 11655 aaronmk
FROM
3988
(
3989
	SELECT (CASE WHEN has_table_privilege(user_, $1, 'SELECT') THEN
3990
$$GRANT SELECT ON TABLE $$||$1||$$ TO $$||quote_ident(user_)||$$;
3991
$$ ELSE '' END) AS cmd
3992
	FROM util.grants_users() f (user_)
3993
) s
3994
$_$;
3995
3996
3997
--
3998 12325 aaronmk
-- Name: show_relations_like(text, text, character[]); Type: FUNCTION; Schema: util; Owner: -
3999
--
4000
4001 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
4002 12325 aaronmk
    LANGUAGE sql STABLE
4003
    AS $_$
4004
SELECT oid FROM pg_class
4005
WHERE relkind = ANY($3) AND relname ~ $1
4006
AND util.schema_matches(util.schema(relnamespace), $2)
4007
ORDER BY relname
4008
$_$;
4009
4010
4011
--
4012 13478 aaronmk
-- Name: show_set_comment(regclass); Type: FUNCTION; Schema: util; Owner: -
4013
--
4014
4015
CREATE FUNCTION show_set_comment(table_ regclass) RETURNS text
4016
    LANGUAGE sql STABLE
4017
    AS $_$
4018 13480 aaronmk
SELECT util.mk_set_comment($1, util.comment($1))
4019 13478 aaronmk
$_$;
4020
4021
4022
--
4023 12592 aaronmk
-- Name: show_types_like(text, text); Type: FUNCTION; Schema: util; Owner: -
4024
--
4025
4026
CREATE FUNCTION show_types_like(name_regexp text DEFAULT ''::text, schema_regexp text DEFAULT ''::text) RETURNS SETOF regtype
4027
    LANGUAGE sql STABLE
4028
    AS $_$
4029
SELECT oid
4030
FROM pg_type
4031
WHERE typname ~ $1 AND util.schema_matches(util.schema(typnamespace), $2)
4032
ORDER BY typname
4033
$_$;
4034
4035
4036
--
4037 12305 aaronmk
-- Name: show_views_like(text, text); Type: FUNCTION; Schema: util; Owner: -
4038
--
4039
4040 12385 aaronmk
CREATE FUNCTION show_views_like(name_regexp text, schema_regexp text DEFAULT ''::text) RETURNS SETOF regclass
4041 12305 aaronmk
    LANGUAGE sql STABLE
4042
    AS $_$
4043 12385 aaronmk
SELECT * FROM util.show_relations_like($1, $2, ARRAY['v'])
4044 12305 aaronmk
$_$;
4045
4046
4047
--
4048 12384 aaronmk
-- Name: str_equality_regexp(text); Type: FUNCTION; Schema: util; Owner: -
4049
--
4050
4051
CREATE FUNCTION str_equality_regexp(literal text) RETURNS text
4052
    LANGUAGE sql IMMUTABLE
4053
    AS $_$
4054
SELECT '^'||util.regexp_quote($1)||'$'
4055
$_$;
4056
4057
4058
--
4059 8183 aaronmk
-- Name: table2hstore(regclass); Type: FUNCTION; Schema: util; Owner: -
4060 8144 aaronmk
--
4061
4062
CREATE FUNCTION table2hstore(table_ regclass) RETURNS hstore
4063 8145 aaronmk
    LANGUAGE plpgsql STABLE STRICT
4064 8144 aaronmk
    AS $_$
4065
DECLARE
4066
    hstore hstore;
4067
BEGIN
4068
    EXECUTE $$SELECT hstore(ARRAY(SELECT unnest(ARRAY["from", "to"]) FROM $$||
4069
        table_||$$))$$ INTO STRICT hstore;
4070
    RETURN hstore;
4071
END;
4072
$_$;
4073
4074
4075
--
4076 10184 aaronmk
-- Name: table_flag__get(regclass, text); Type: FUNCTION; Schema: util; Owner: -
4077
--
4078
4079
CREATE FUNCTION table_flag__get(table_ regclass, flag text) RETURNS boolean
4080 12446 aaronmk
    LANGUAGE sql STABLE
4081 10184 aaronmk
    AS $_$
4082
SELECT COUNT(*) > 0 FROM pg_constraint
4083
WHERE conrelid = $1 AND contype = 'c' AND conname = $2
4084
$_$;
4085
4086
4087
--
4088
-- Name: FUNCTION table_flag__get(table_ regclass, flag text); Type: COMMENT; Schema: util; Owner: -
4089
--
4090
4091 12235 aaronmk
COMMENT ON FUNCTION table_flag__get(table_ regclass, flag text) IS '
4092
gets whether a status flag is set by the presence of a table constraint
4093
';
4094 10184 aaronmk
4095
4096
--
4097 10182 aaronmk
-- Name: table_flag__set(regclass, text); Type: FUNCTION; Schema: util; Owner: -
4098
--
4099
4100
CREATE FUNCTION table_flag__set(table_ regclass, flag text) RETURNS void
4101 12446 aaronmk
    LANGUAGE sql
4102 10182 aaronmk
    AS $_$
4103
SELECT util.create_if_not_exists($$ALTER TABLE $$||$1||$$ ADD CONSTRAINT $$
4104
||quote_ident($2)||$$ CHECK (true)$$)
4105
$_$;
4106
4107
4108
--
4109
-- Name: FUNCTION table_flag__set(table_ regclass, flag text); Type: COMMENT; Schema: util; Owner: -
4110
--
4111
4112 12235 aaronmk
COMMENT ON FUNCTION table_flag__set(table_ regclass, flag text) IS '
4113
stores a status flag by the presence of a table constraint.
4114
idempotent.
4115
';
4116 10182 aaronmk
4117
4118
--
4119 10185 aaronmk
-- Name: table_nulls_mapped__get(regclass); Type: FUNCTION; Schema: util; Owner: -
4120
--
4121
4122
CREATE FUNCTION table_nulls_mapped__get(table_ regclass) RETURNS boolean
4123 12446 aaronmk
    LANGUAGE sql STABLE
4124 10185 aaronmk
    AS $_$
4125
SELECT util.table_flag__get($1, 'nulls_mapped')
4126
$_$;
4127
4128
4129
--
4130
-- Name: FUNCTION table_nulls_mapped__get(table_ regclass); Type: COMMENT; Schema: util; Owner: -
4131
--
4132
4133 12235 aaronmk
COMMENT ON FUNCTION table_nulls_mapped__get(table_ regclass) IS '
4134
gets whether a table''s NULL-equivalent strings have been replaced with NULL
4135
';
4136 10185 aaronmk
4137
4138
--
4139 10183 aaronmk
-- Name: table_nulls_mapped__set(regclass); Type: FUNCTION; Schema: util; Owner: -
4140
--
4141
4142
CREATE FUNCTION table_nulls_mapped__set(table_ regclass) RETURNS void
4143 12446 aaronmk
    LANGUAGE sql
4144 10183 aaronmk
    AS $_$
4145
SELECT util.table_flag__set($1, 'nulls_mapped')
4146
$_$;
4147
4148
4149
--
4150
-- Name: FUNCTION table_nulls_mapped__set(table_ regclass); Type: COMMENT; Schema: util; Owner: -
4151
--
4152
4153 12235 aaronmk
COMMENT ON FUNCTION table_nulls_mapped__set(table_ regclass) IS '
4154
sets that a table''s NULL-equivalent strings have been replaced with NULL.
4155
idempotent.
4156
';
4157 10183 aaronmk
4158
4159
--
4160 12652 aaronmk
-- Name: to_freq(regclass); Type: FUNCTION; Schema: util; Owner: -
4161
--
4162
4163
CREATE FUNCTION to_freq(table_ regclass) RETURNS void
4164
    LANGUAGE sql
4165
    AS $_$
4166
-- save data before truncating main table
4167
SELECT util.copy_types_and_data($1, 'pg_temp.__copy');
4168
4169
-- repopulate main table w/ copies column
4170
SELECT util.truncate($1);
4171
SELECT util.eval($$ALTER TABLE $$||$1||$$ ADD COLUMN copies bigint NOT NULL$$);
4172
SELECT util.populate_table($1, $$
4173
SELECT (table_).*, copies
4174
FROM (
4175
	SELECT table_, COUNT(*) AS copies
4176
	FROM pg_temp.__copy table_
4177
	GROUP BY table_
4178
) s
4179
$$);
4180
4181
-- delete temp table so it doesn't stay around until end of connection
4182
SELECT util.drop_table('pg_temp.__copy');
4183
$_$;
4184
4185
4186
--
4187 8183 aaronmk
-- Name: to_global_col_names(regclass); Type: FUNCTION; Schema: util; Owner: -
4188 8088 aaronmk
--
4189
4190
CREATE FUNCTION to_global_col_names(table_ regclass) RETURNS void
4191
    LANGUAGE plpgsql STRICT
4192
    AS $_$
4193
DECLARE
4194
    row record;
4195
BEGIN
4196 8183 aaronmk
    FOR row IN SELECT * FROM util.col_global_names(table_::text::regtype)
4197 8088 aaronmk
    LOOP
4198
        IF row.global_name != row.name THEN
4199
            EXECUTE $$ALTER TABLE $$||table_||$$ RENAME $$
4200
                ||quote_ident(row.name)||$$ TO $$||quote_ident(row.global_name);
4201
        END IF;
4202
    END LOOP;
4203
END;
4204
$_$;
4205
4206
4207
--
4208 8183 aaronmk
-- Name: FUNCTION to_global_col_names(table_ regclass); Type: COMMENT; Schema: util; Owner: -
4209 8088 aaronmk
--
4210
4211 12235 aaronmk
COMMENT ON FUNCTION to_global_col_names(table_ regclass) IS '
4212
idempotent
4213
';
4214 8088 aaronmk
4215
4216
--
4217 12874 aaronmk
-- Name: trim(regclass, regclass, boolean); Type: FUNCTION; Schema: util; Owner: -
4218 10365 aaronmk
--
4219
4220 12874 aaronmk
CREATE FUNCTION "trim"(table_ regclass, names regclass, force boolean DEFAULT true) RETURNS void
4221 12446 aaronmk
    LANGUAGE sql
4222 10365 aaronmk
    AS $_$
4223 12874 aaronmk
SELECT util.drop_column(($1, col), $3) FROM util.added_cols($1, $2) f (col);
4224 10365 aaronmk
SELECT NULL::void; -- don't fold away functions called in previous query
4225
$_$;
4226
4227
4228
--
4229 12874 aaronmk
-- Name: FUNCTION "trim"(table_ regclass, names regclass, force boolean); Type: COMMENT; Schema: util; Owner: -
4230 10365 aaronmk
--
4231
4232 12874 aaronmk
COMMENT ON FUNCTION "trim"(table_ regclass, names regclass, force boolean) IS '
4233
trims table_ to include only columns in the original data
4234
4235
by default, cascadingly drops dependent columns so that they don''t prevent
4236
trim() from succeeding. note that this requires the dependent columns to then be
4237
manually re-created.
4238
4239
idempotent
4240 12235 aaronmk
';
4241 10365 aaronmk
4242
4243
--
4244 8183 aaronmk
-- Name: truncate(regclass); Type: FUNCTION; Schema: util; Owner: -
4245 8142 aaronmk
--
4246
4247
CREATE FUNCTION truncate(table_ regclass) RETURNS void
4248
    LANGUAGE plpgsql STRICT
4249
    AS $_$
4250
BEGIN
4251
    EXECUTE $$TRUNCATE $$||table_||$$ CASCADE$$;
4252
END;
4253
$_$;
4254
4255
4256
--
4257 8183 aaronmk
-- Name: FUNCTION truncate(table_ regclass); Type: COMMENT; Schema: util; Owner: -
4258 8142 aaronmk
--
4259
4260 12235 aaronmk
COMMENT ON FUNCTION truncate(table_ regclass) IS '
4261
idempotent
4262
';
4263 8142 aaronmk
4264
4265
--
4266 12357 aaronmk
-- Name: truncated_prefixed_name_regexp(text, integer); Type: FUNCTION; Schema: util; Owner: -
4267
--
4268
4269
CREATE FUNCTION truncated_prefixed_name_regexp(name text, max_prefix_len integer) RETURNS text
4270
    LANGUAGE sql IMMUTABLE
4271
    AS $_$
4272 12361 aaronmk
SELECT '^(.*)'||util._if(util.name_was_truncated($1, $2),
4273 12362 aaronmk
util.regexp_quote(util.rtrim_n($1, $2))||'.*', util.regexp_quote($1)) ||'$'
4274 12357 aaronmk
$_$;
4275
4276
4277
--
4278 13135 aaronmk
-- Name: try_cast(text, anyelement); Type: FUNCTION; Schema: util; Owner: -
4279
--
4280
4281
CREATE FUNCTION try_cast(value text, ret_type_null anyelement) RETURNS anyelement
4282
    LANGUAGE plpgsql IMMUTABLE
4283
    AS $$
4284
BEGIN
4285
	/* need explicit cast because some types not implicitly-castable, and also
4286
	to make the cast happen inside the try block. (*implicit* casts to the
4287
	return type happen at the end of the function, outside any block.) */
4288
	RETURN util.cast(value, ret_type_null);
4289
EXCEPTION
4290 13493 aaronmk
WHEN   data_exception
4291 13564 aaronmk
	OR invalid_schema_name -- eg. 'pg_temp.__'::regclass
4292 13493 aaronmk
	OR syntax_error_or_access_rule_violation -- eg. ::regclass
4293
	THEN
4294 13135 aaronmk
	PERFORM util.raise('WARNING', SQLERRM);
4295
	RETURN NULL;
4296
END;
4297
$$;
4298
4299
4300
--
4301
-- Name: FUNCTION try_cast(value text, ret_type_null anyelement); Type: COMMENT; Schema: util; Owner: -
4302
--
4303
4304
COMMENT ON FUNCTION try_cast(value text, ret_type_null anyelement) IS '
4305
ret_type_null: NULL::ret_type
4306
';
4307
4308
4309
--
4310 8199 aaronmk
-- Name: try_create(text); Type: FUNCTION; Schema: util; Owner: -
4311
--
4312
4313
CREATE FUNCTION try_create(sql text) RETURNS void
4314
    LANGUAGE plpgsql STRICT
4315
    AS $$
4316
BEGIN
4317 12658 aaronmk
	PERFORM util.eval(sql);
4318 8199 aaronmk
EXCEPTION
4319 12676 aaronmk
WHEN   not_null_violation
4320
		/* trying to add NOT NULL column to parent table, which cascades to
4321
		child table whose values for the new column will be NULL */
4322
	OR wrong_object_type -- trying to alter a view's columns
4323
	OR undefined_column
4324
	OR duplicate_column
4325
THEN NULL;
4326 12684 aaronmk
WHEN datatype_mismatch THEN
4327
	IF SQLERRM LIKE 'child table is missing column %' THEN NULL;
4328
	ELSE RAISE; -- rethrow
4329
	END IF;
4330 8199 aaronmk
END;
4331
$$;
4332
4333
4334
--
4335
-- Name: FUNCTION try_create(sql text); Type: COMMENT; Schema: util; Owner: -
4336
--
4337
4338 12235 aaronmk
COMMENT ON FUNCTION try_create(sql text) IS '
4339
idempotent
4340
';
4341 8199 aaronmk
4342
4343
--
4344 8209 aaronmk
-- Name: try_mk_derived_col(col_ref, text); Type: FUNCTION; Schema: util; Owner: -
4345
--
4346
4347
CREATE FUNCTION try_mk_derived_col(col col_ref, expr text) RETURNS void
4348 12446 aaronmk
    LANGUAGE sql
4349 8209 aaronmk
    AS $_$
4350
SELECT util.try_create($$SELECT util.mk_derived_col($$||quote_literal($1)||$$, $$||quote_literal($2)||$$)$$)
4351
$_$;
4352
4353
4354
--
4355
-- Name: FUNCTION try_mk_derived_col(col col_ref, expr text); Type: COMMENT; Schema: util; Owner: -
4356
--
4357
4358 12235 aaronmk
COMMENT ON FUNCTION try_mk_derived_col(col col_ref, expr text) IS '
4359
idempotent
4360
';
4361 8209 aaronmk
4362
4363
--
4364 8189 aaronmk
-- Name: type_qual(anyelement); Type: FUNCTION; Schema: util; Owner: -
4365
--
4366
4367
CREATE FUNCTION type_qual(value anyelement) RETURNS text
4368
    LANGUAGE sql IMMUTABLE
4369
    AS $_$
4370
SELECT CASE WHEN $1 IS NULL THEN '' ELSE $$ NOT NULL$$ END
4371
$_$;
4372
4373
4374
--
4375 10161 aaronmk
-- Name: FUNCTION type_qual(value anyelement); Type: COMMENT; Schema: util; Owner: -
4376
--
4377
4378 12235 aaronmk
COMMENT ON FUNCTION type_qual(value anyelement) IS '
4379
a type''s NOT NULL qualifier
4380
';
4381 10161 aaronmk
4382
4383
--
4384 12562 aaronmk
-- Name: typed_cols(regtype); Type: FUNCTION; Schema: util; Owner: -
4385
--
4386
4387
CREATE FUNCTION typed_cols(type regtype) RETURNS SETOF col_cast
4388 12590 aaronmk
    LANGUAGE sql STABLE
4389 12562 aaronmk
    AS $_$
4390
SELECT (attname::text, atttypid)::util.col_cast
4391
FROM pg_attribute
4392
WHERE attrelid = $1::text::regclass AND attnum >= 1 AND NOT attisdropped
4393
ORDER BY attnum
4394
$_$;
4395
4396
4397
--
4398 12438 aaronmk
-- Name: typeof(anyelement); Type: FUNCTION; Schema: util; Owner: -
4399
--
4400
4401
CREATE FUNCTION typeof(value anyelement) RETURNS text
4402
    LANGUAGE sql IMMUTABLE
4403
    AS $_$
4404
SELECT util.qual_name(pg_typeof($1))
4405
$_$;
4406
4407
4408
--
4409 8185 aaronmk
-- Name: typeof(text, regtype); Type: FUNCTION; Schema: util; Owner: -
4410
--
4411
4412 10160 aaronmk
CREATE FUNCTION typeof(expr text, table_ regtype DEFAULT NULL::regtype) RETURNS regtype
4413
    LANGUAGE plpgsql STABLE
4414 8185 aaronmk
    AS $_$
4415
DECLARE
4416
    type regtype;
4417
BEGIN
4418 10160 aaronmk
    EXECUTE $$SELECT pg_typeof($$||expr||$$)$$||
4419
COALESCE($$ FROM (SELECT (NULL::$$||table_||$$).*) _s$$, '') INTO STRICT type;
4420 8185 aaronmk
    RETURN type;
4421
END;
4422
$_$;
4423
4424
4425
--
4426 12490 aaronmk
-- Name: use_schema(anyelement); Type: FUNCTION; Schema: util; Owner: -
4427 12483 aaronmk
--
4428
4429 12490 aaronmk
CREATE FUNCTION use_schema(schema_anchor anyelement) RETURNS void
4430 12483 aaronmk
    LANGUAGE sql
4431
    AS $_$
4432 12488 aaronmk
SELECT util.set_search_path(util.mk_search_path(util.schema($1)))
4433 12483 aaronmk
$_$;
4434
4435
4436
--
4437 12490 aaronmk
-- Name: FUNCTION use_schema(schema_anchor anyelement); Type: COMMENT; Schema: util; Owner: -
4438 12488 aaronmk
--
4439
4440 12490 aaronmk
COMMENT ON FUNCTION use_schema(schema_anchor anyelement) IS '
4441 12488 aaronmk
auto-appends util to the search_path to enable use of util operators
4442
';
4443
4444
4445
--
4446 13639 aaronmk
-- Name: view_def_to_orig(text); Type: FUNCTION; Schema: util; Owner: -
4447
--
4448
4449
CREATE FUNCTION view_def_to_orig(view_def text) RETURNS text
4450
    LANGUAGE sql IMMUTABLE
4451
    AS $_$
4452 13645 aaronmk
SELECT
4453
regexp_replace(
4454
regexp_replace(
4455
$1
4456
,
4457 13644 aaronmk
/* unexpand expanded * expressions. any list of 6+ cols from the same table is
4458 13639 aaronmk
treated as a * expression. */
4459 13643 aaronmk
'((?:"[^"\s]+"|\w+)\.)(?:"[^"\s]+"|\w+)'||
4460
	/* 1st col, which lacks separator before.
4461
	*note*: can't prepend \y because it considers only \w chars, not " */
4462 13639 aaronmk
'(,[[:blank:]]*
4463 13642 aaronmk
[[:blank:]]*)\1(?:"[^"\s]+"|\w+)'|| -- 2nd col, which has separator before
4464
'(?:\2\1(?:"[^"\s]+"|\w+)){4,}', -- later cols, w/ same table name and separator
4465 13645 aaronmk
'\1*'/*prefix w/ table*/,
4466
'g')
4467
,
4468
/* merge .* expressions resulting from a SELECT * of a join. any list of
4469
multiple .* expressions is treated as a SELECT * . */
4470
'(?:"[^"\s]+"|\w+)\.\*'||
4471
	/* 1st table, which lacks separator before.
4472
	*note*: can't prepend \y because it considers only \w chars, not " */
4473
'(,[[:blank:]]*
4474
[[:blank:]]*)(?:"[^"\s]+"|\w+)\.\*'|| -- 2nd table, which has separator before
4475
'(?:\1(?:"[^"\s]+"|\w+)\.\*)*', -- later tables, w/ same separator
4476
'*',
4477
'g')
4478 13639 aaronmk
$_$;
4479
4480
4481
--
4482 9959 aaronmk
-- Name: all_same(anyelement); Type: AGGREGATE; Schema: util; Owner: -
4483
--
4484
4485
CREATE AGGREGATE all_same(anyelement) (
4486
    SFUNC = all_same_transform,
4487
    STYPE = anyarray,
4488
    FINALFUNC = all_same_final
4489
);
4490
4491
4492
--
4493
-- Name: AGGREGATE all_same(anyelement); Type: COMMENT; Schema: util; Owner: -
4494
--
4495
4496 12235 aaronmk
COMMENT ON AGGREGATE all_same(anyelement) IS '
4497
includes NULLs in comparison
4498
';
4499 9959 aaronmk
4500
4501
--
4502 8183 aaronmk
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: util; Owner: -
4503 2595 aaronmk
--
4504
4505
CREATE AGGREGATE join_strs(text, text) (
4506 4052 aaronmk
    SFUNC = join_strs_transform,
4507 4010 aaronmk
    STYPE = text
4508 2595 aaronmk
);
4509
4510
4511 8147 aaronmk
--
4512 12423 aaronmk
-- Name: %==; Type: OPERATOR; Schema: util; Owner: -
4513
--
4514
4515
CREATE OPERATOR %== (
4516
    PROCEDURE = "%==",
4517
    LEFTARG = anyelement,
4518
    RIGHTARG = anyelement
4519
);
4520
4521
4522
--
4523
-- Name: OPERATOR %== (anyelement, anyelement); Type: COMMENT; Schema: util; Owner: -
4524
--
4525
4526
COMMENT ON OPERATOR %== (anyelement, anyelement) IS '
4527
returns whether the map-keys of the compared values are the same
4528
(mnemonic: % is the Perl symbol for a hash map)
4529
4530
should be overridden for types that store both keys and values
4531
4532
used in a FULL JOIN to select which columns to join on
4533
';
4534
4535
4536
--
4537 8183 aaronmk
-- Name: ->; Type: OPERATOR; Schema: util; Owner: -
4538 8147 aaronmk
--
4539
4540
CREATE OPERATOR -> (
4541
    PROCEDURE = map_get,
4542
    LEFTARG = regclass,
4543
    RIGHTARG = text
4544
);
4545
4546
4547 10308 aaronmk
--
4548
-- Name: =>; Type: OPERATOR; Schema: util; Owner: -
4549
--
4550
4551
CREATE OPERATOR => (
4552
    PROCEDURE = hstore,
4553 10357 aaronmk
    LEFTARG = text[],
4554 10608 aaronmk
    RIGHTARG = text
4555 10308 aaronmk
);
4556
4557
4558
--
4559 10608 aaronmk
-- Name: OPERATOR => (text[], text); Type: COMMENT; Schema: util; Owner: -
4560 10308 aaronmk
--
4561
4562 12235 aaronmk
COMMENT ON OPERATOR => (text[], text) IS '
4563
usage: array[''key1'', ...]::text[] => ''value''
4564
';
4565 10308 aaronmk
4566
4567 10391 aaronmk
--
4568 10613 aaronmk
-- Name: ?*>=; Type: OPERATOR; Schema: util; Owner: -
4569
--
4570
4571
CREATE OPERATOR ?*>= (
4572
    PROCEDURE = is_populated_more_often_than,
4573
    LEFTARG = anyelement,
4574
    RIGHTARG = anyelement
4575
);
4576
4577
4578
--
4579 10391 aaronmk
-- Name: ?>=; Type: OPERATOR; Schema: util; Owner: -
4580
--
4581
4582
CREATE OPERATOR ?>= (
4583
    PROCEDURE = is_more_complete_than,
4584
    LEFTARG = anyelement,
4585
    RIGHTARG = anyelement
4586
);
4587
4588
4589 11005 aaronmk
--
4590
-- Name: ||%; Type: OPERATOR; Schema: util; Owner: -
4591
--
4592
4593
CREATE OPERATOR ||% (
4594
    PROCEDURE = concat_esc,
4595
    LEFTARG = text,
4596
    RIGHTARG = text
4597
);
4598
4599
4600
--
4601
-- Name: OPERATOR ||% (text, text); Type: COMMENT; Schema: util; Owner: -
4602
--
4603
4604 12235 aaronmk
COMMENT ON OPERATOR ||% (text, text) IS '
4605
% indicates an identifier, as in Perl hashes and one of the x86 assembler syntaxes for registers
4606
';
4607 11005 aaronmk
4608
4609 2107 aaronmk
--
4610 8183 aaronmk
-- Name: map; Type: TABLE; Schema: util; Owner: -; Tablespace:
4611 8140 aaronmk
--
4612
4613
CREATE TABLE map (
4614
    "from" text NOT NULL,
4615 8158 aaronmk
    "to" text,
4616
    filter text,
4617
    notes text
4618 8140 aaronmk
);
4619
4620
4621
--
4622 11834 aaronmk
-- Data for Name: explain; Type: TABLE DATA; Schema: util; Owner: -
4623
--
4624
4625
4626
4627
--
4628 8183 aaronmk
-- Data for Name: map; Type: TABLE DATA; Schema: util; Owner: -
4629 8140 aaronmk
--
4630
4631
4632
4633
--
4634 10342 aaronmk
-- Name: map__unique__from; Type: CONSTRAINT; Schema: util; Owner: -; Tablespace:
4635 8140 aaronmk
--
4636
4637
ALTER TABLE ONLY map
4638 10342 aaronmk
    ADD CONSTRAINT map__unique__from UNIQUE ("from");
4639 8140 aaronmk
4640
4641
--
4642 10343 aaronmk
-- Name: map__unique__to; Type: CONSTRAINT; Schema: util; Owner: -; Tablespace:
4643
--
4644
4645
ALTER TABLE ONLY map
4646
    ADD CONSTRAINT map__unique__to UNIQUE ("to");
4647
4648
4649
--
4650 10110 aaronmk
-- Name: map_filter_insert; Type: TRIGGER; Schema: util; Owner: -
4651
--
4652
4653
CREATE TRIGGER map_filter_insert BEFORE INSERT ON map FOR EACH ROW EXECUTE PROCEDURE map_filter_insert();
4654
4655
4656
--
4657 2136 aaronmk
-- PostgreSQL database dump complete
4658
--