F.7. citext

Note: The following description applies both to Postgres-XC and PostgreSQL if not described explicitly.

The citext module provides a case-insensitive character string type, citext. Essentially, it internally calls lower when comparing values. Otherwise, it behaves almost exactly like text.

F.7.1. Rationale

Note: The following description applies both to Postgres-XC and PostgreSQL if not described explicitly.

The standard approach to doing case-insensitive matches in PostgreSQL has been to use the lower function when comparing values, for example

SELECT * FROM tab WHERE lower(col) = LOWER(?);

This works reasonably well, but has a number of drawbacks:

The citext data type allows you to eliminate calls to lower in SQL queries, and allows a primary key to be case-insensitive. citext is locale-aware, just like text, which means that the matching of upper case and lower case characters is dependent on the rules of the database's LC_CTYPE setting. Again, this behavior is identical to the use of lower in queries. But because it's done transparently by the data type, you don't have to remember to do anything special in your queries.

F.7.2. How to Use It

Note: The following description applies only to Postgres-XC

Here's a simple example of usage:

CREATE TABLE users (
    id   int    PRIMARY KEY,
    nick CITEXT,
    pass TEXT   NOT NULL
);

INSERT INTO users VALUES (1,  'larry',  md5(random()::text) );
INSERT INTO users VALUES (2,  'Tom',    md5(random()::text) );
INSERT INTO users VALUES (3,  'Damian', md5(random()::text) );
INSERT INTO users VALUES (4,  'NEAL',   md5(random()::text) );
INSERT INTO users VALUES (5,  'Bjørn',  md5(random()::text) );

SELECT * FROM users WHERE nick = 'Larry';

The SELECT statement will return one tuple, even though the nick column was set to larry and the query was for Larry.

Note: The following description applies only to Postgres-XC

Please note that the column of the type CITEXT cannot be the distribution column;

F.7.3. String Comparison Behavior

Note: The following description applies both to Postgres-XC and PostgreSQL if not described explicitly.

citext performs comparisons by converting each string to lower case (as though lower were called) and then comparing the results normally. Thus, for example, two strings are considered equal if lower would produce identical results for them.

In order to emulate a case-insensitive collation as closely as possible, there are citext-specific versions of a number of string-processing operators and functions. So, for example, the regular expression operators ~ and ~* exhibit the same behavior when applied to citext: they both match case-insensitively. The same is true for !~ and !~*, as well as for the LIKE operators ~~ and ~~*, and !~~ and !~~*. If you'd like to match case-sensitively, you can cast the operator's arguments to text.

Similarly, all of the following functions perform matching case-insensitively if their arguments are citext:

For the regexp functions, if you want to match case-sensitively, you can specify the "c" flag to force a case-sensitive match. Otherwise, you must cast to text before using one of these functions if you want case-sensitive behavior.

F.7.4. Limitations

F.7.5. Author

David E. Wheeler

Inspired by the original citext module by Donald Fraser.