1 /* contrib/citext/citext--1.4.sql */
3 -- complain if script is sourced in psql, rather than via CREATE EXTENSION
4 \echo Use "CREATE EXTENSION citext" to load this file. \quit
7 -- PostgreSQL code for CITEXT.
9 -- Most I/O functions, and a few others, piggyback on the "text" type
10 -- functions via the implicit cast to text.
14 -- Shell type to keep things a bit quieter.
20 -- Input and output functions.
22 CREATE FUNCTION citextin(cstring)
25 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
27 CREATE FUNCTION citextout(citext)
30 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
32 CREATE FUNCTION citextrecv(internal)
35 LANGUAGE internal STABLE STRICT PARALLEL SAFE;
37 CREATE FUNCTION citextsend(citext)
40 LANGUAGE internal STABLE STRICT PARALLEL SAFE;
51 INTERNALLENGTH = VARIABLE,
53 -- make it a non-preferred member of string type category
60 -- Type casting functions for those situations where the I/O casts don't
61 -- automatically kick in.
64 CREATE FUNCTION citext(bpchar)
67 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
69 CREATE FUNCTION citext(boolean)
72 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
74 CREATE FUNCTION citext(inet)
77 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
80 -- Implicit and assignment type casts.
83 CREATE CAST (citext AS text) WITHOUT FUNCTION AS IMPLICIT;
84 CREATE CAST (citext AS varchar) WITHOUT FUNCTION AS IMPLICIT;
85 CREATE CAST (citext AS bpchar) WITHOUT FUNCTION AS ASSIGNMENT;
86 CREATE CAST (text AS citext) WITHOUT FUNCTION AS ASSIGNMENT;
87 CREATE CAST (varchar AS citext) WITHOUT FUNCTION AS ASSIGNMENT;
88 CREATE CAST (bpchar AS citext) WITH FUNCTION citext(bpchar) AS ASSIGNMENT;
89 CREATE CAST (boolean AS citext) WITH FUNCTION citext(boolean) AS ASSIGNMENT;
90 CREATE CAST (inet AS citext) WITH FUNCTION citext(inet) AS ASSIGNMENT;
93 -- Operator Functions.
96 CREATE FUNCTION citext_eq( citext, citext )
99 LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
101 CREATE FUNCTION citext_ne( citext, citext )
104 LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
106 CREATE FUNCTION citext_lt( citext, citext )
109 LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
111 CREATE FUNCTION citext_le( citext, citext )
114 LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
116 CREATE FUNCTION citext_gt( citext, citext )
119 LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
121 CREATE FUNCTION citext_ge( citext, citext )
124 LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
135 PROCEDURE = citext_eq,
147 PROCEDURE = citext_ne,
157 PROCEDURE = citext_lt,
158 RESTRICT = scalarltsel,
159 JOIN = scalarltjoinsel
167 PROCEDURE = citext_le,
168 RESTRICT = scalarltsel,
169 JOIN = scalarltjoinsel
177 PROCEDURE = citext_ge,
178 RESTRICT = scalargtsel,
179 JOIN = scalargtjoinsel
187 PROCEDURE = citext_gt,
188 RESTRICT = scalargtsel,
189 JOIN = scalargtjoinsel
193 -- Support functions for indexing.
196 CREATE FUNCTION citext_cmp(citext, citext)
199 LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
201 CREATE FUNCTION citext_hash(citext)
204 LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
207 -- The btree indexing operator class.
210 CREATE OPERATOR CLASS citext_ops
211 DEFAULT FOR TYPE CITEXT USING btree AS
212 OPERATOR 1 < (citext, citext),
213 OPERATOR 2 <= (citext, citext),
214 OPERATOR 3 = (citext, citext),
215 OPERATOR 4 >= (citext, citext),
216 OPERATOR 5 > (citext, citext),
217 FUNCTION 1 citext_cmp(citext, citext);
220 -- The hash indexing operator class.
223 CREATE OPERATOR CLASS citext_ops
224 DEFAULT FOR TYPE citext USING hash AS
225 OPERATOR 1 = (citext, citext),
226 FUNCTION 1 citext_hash(citext);
232 CREATE FUNCTION citext_smaller(citext, citext)
235 LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
237 CREATE FUNCTION citext_larger(citext, citext)
240 LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
242 CREATE AGGREGATE min(citext) (
243 SFUNC = citext_smaller,
247 COMBINEFUNC = citext_smaller
250 CREATE AGGREGATE max(citext) (
251 SFUNC = citext_larger,
255 COMBINEFUNC = citext_larger
259 -- CITEXT pattern matching.
262 CREATE FUNCTION texticlike(citext, citext)
263 RETURNS bool AS 'texticlike'
264 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
266 CREATE FUNCTION texticnlike(citext, citext)
267 RETURNS bool AS 'texticnlike'
268 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
270 CREATE FUNCTION texticregexeq(citext, citext)
271 RETURNS bool AS 'texticregexeq'
272 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
274 CREATE FUNCTION texticregexne(citext, citext)
275 RETURNS bool AS 'texticregexne'
276 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
279 PROCEDURE = texticregexeq,
283 RESTRICT = icregexeqsel,
284 JOIN = icregexeqjoinsel
288 PROCEDURE = texticregexeq,
292 RESTRICT = icregexeqsel,
293 JOIN = icregexeqjoinsel
297 PROCEDURE = texticregexne,
301 RESTRICT = icregexnesel,
302 JOIN = icregexnejoinsel
305 CREATE OPERATOR !~* (
306 PROCEDURE = texticregexne,
310 RESTRICT = icregexnesel,
311 JOIN = icregexnejoinsel
315 PROCEDURE = texticlike,
319 RESTRICT = iclikesel,
323 CREATE OPERATOR ~~* (
324 PROCEDURE = texticlike,
328 RESTRICT = iclikesel,
332 CREATE OPERATOR !~~ (
333 PROCEDURE = texticnlike,
337 RESTRICT = icnlikesel,
338 JOIN = icnlikejoinsel
341 CREATE OPERATOR !~~* (
342 PROCEDURE = texticnlike,
346 RESTRICT = icnlikesel,
347 JOIN = icnlikejoinsel
351 -- Matching citext to text.
354 CREATE FUNCTION texticlike(citext, text)
355 RETURNS bool AS 'texticlike'
356 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
358 CREATE FUNCTION texticnlike(citext, text)
359 RETURNS bool AS 'texticnlike'
360 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
362 CREATE FUNCTION texticregexeq(citext, text)
363 RETURNS bool AS 'texticregexeq'
364 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
366 CREATE FUNCTION texticregexne(citext, text)
367 RETURNS bool AS 'texticregexne'
368 LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE;
371 PROCEDURE = texticregexeq,
375 RESTRICT = icregexeqsel,
376 JOIN = icregexeqjoinsel
380 PROCEDURE = texticregexeq,
384 RESTRICT = icregexeqsel,
385 JOIN = icregexeqjoinsel
389 PROCEDURE = texticregexne,
393 RESTRICT = icregexnesel,
394 JOIN = icregexnejoinsel
397 CREATE OPERATOR !~* (
398 PROCEDURE = texticregexne,
402 RESTRICT = icregexnesel,
403 JOIN = icregexnejoinsel
407 PROCEDURE = texticlike,
411 RESTRICT = iclikesel,
415 CREATE OPERATOR ~~* (
416 PROCEDURE = texticlike,
420 RESTRICT = iclikesel,
424 CREATE OPERATOR !~~ (
425 PROCEDURE = texticnlike,
429 RESTRICT = icnlikesel,
430 JOIN = icnlikejoinsel
433 CREATE OPERATOR !~~* (
434 PROCEDURE = texticnlike,
438 RESTRICT = icnlikesel,
439 JOIN = icnlikejoinsel
443 -- Matching citext in string comparison functions.
444 -- XXX TODO Ideally these would be implemented in C.
447 CREATE FUNCTION regexp_match( citext, citext ) RETURNS TEXT[] AS $$
448 SELECT pg_catalog.regexp_match( $1::pg_catalog.text, $2::pg_catalog.text, 'i' );
449 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
451 CREATE FUNCTION regexp_match( citext, citext, text ) RETURNS TEXT[] AS $$
452 SELECT pg_catalog.regexp_match( $1::pg_catalog.text, $2::pg_catalog.text, CASE WHEN pg_catalog.strpos($3, 'c') = 0 THEN $3 || 'i' ELSE $3 END );
453 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
455 CREATE FUNCTION regexp_matches( citext, citext ) RETURNS SETOF TEXT[] AS $$
456 SELECT pg_catalog.regexp_matches( $1::pg_catalog.text, $2::pg_catalog.text, 'i' );
457 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE ROWS 1;
459 CREATE FUNCTION regexp_matches( citext, citext, text ) RETURNS SETOF TEXT[] AS $$
460 SELECT pg_catalog.regexp_matches( $1::pg_catalog.text, $2::pg_catalog.text, CASE WHEN pg_catalog.strpos($3, 'c') = 0 THEN $3 || 'i' ELSE $3 END );
461 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE ROWS 10;
463 CREATE FUNCTION regexp_replace( citext, citext, text ) returns TEXT AS $$
464 SELECT pg_catalog.regexp_replace( $1::pg_catalog.text, $2::pg_catalog.text, $3, 'i');
465 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
467 CREATE FUNCTION regexp_replace( citext, citext, text, text ) returns TEXT AS $$
468 SELECT pg_catalog.regexp_replace( $1::pg_catalog.text, $2::pg_catalog.text, $3, CASE WHEN pg_catalog.strpos($4, 'c') = 0 THEN $4 || 'i' ELSE $4 END);
469 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
471 CREATE FUNCTION regexp_split_to_array( citext, citext ) RETURNS TEXT[] AS $$
472 SELECT pg_catalog.regexp_split_to_array( $1::pg_catalog.text, $2::pg_catalog.text, 'i' );
473 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
475 CREATE FUNCTION regexp_split_to_array( citext, citext, text ) RETURNS TEXT[] AS $$
476 SELECT pg_catalog.regexp_split_to_array( $1::pg_catalog.text, $2::pg_catalog.text, CASE WHEN pg_catalog.strpos($3, 'c') = 0 THEN $3 || 'i' ELSE $3 END );
477 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
479 CREATE FUNCTION regexp_split_to_table( citext, citext ) RETURNS SETOF TEXT AS $$
480 SELECT pg_catalog.regexp_split_to_table( $1::pg_catalog.text, $2::pg_catalog.text, 'i' );
481 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
483 CREATE FUNCTION regexp_split_to_table( citext, citext, text ) RETURNS SETOF TEXT AS $$
484 SELECT pg_catalog.regexp_split_to_table( $1::pg_catalog.text, $2::pg_catalog.text, CASE WHEN pg_catalog.strpos($3, 'c') = 0 THEN $3 || 'i' ELSE $3 END );
485 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
487 CREATE FUNCTION strpos( citext, citext ) RETURNS INT AS $$
488 SELECT pg_catalog.strpos( pg_catalog.lower( $1::pg_catalog.text ), pg_catalog.lower( $2::pg_catalog.text ) );
489 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
491 CREATE FUNCTION replace( citext, citext, citext ) RETURNS TEXT AS $$
492 SELECT pg_catalog.regexp_replace( $1::pg_catalog.text, pg_catalog.regexp_replace($2::pg_catalog.text, '([^a-zA-Z_0-9])', E'\\\\\\1', 'g'), $3::pg_catalog.text, 'gi' );
493 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
495 CREATE FUNCTION split_part( citext, citext, int ) RETURNS TEXT AS $$
496 SELECT (pg_catalog.regexp_split_to_array( $1::pg_catalog.text, pg_catalog.regexp_replace($2::pg_catalog.text, '([^a-zA-Z_0-9])', E'\\\\\\1', 'g'), 'i'))[$3];
497 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
499 CREATE FUNCTION translate( citext, citext, text ) RETURNS TEXT AS $$
500 SELECT pg_catalog.translate( pg_catalog.translate( $1::pg_catalog.text, pg_catalog.lower($2::pg_catalog.text), $3), pg_catalog.upper($2::pg_catalog.text), $3);
501 $$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;