Skip to content

Commit 1885cd1

Browse files
committed
CITEXT data type
1 parent fe49ce6 commit 1885cd1

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

src/current/v25.3/citext.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
title: CITEXT
3+
summary: The CITEXT data type stores case-insensitive text values.
4+
toc: true
5+
docs_area: reference.sql
6+
---
7+
8+
The `CITEXT` [data type]({% link {{ page.version.version }}/data-types.md %}) stores case-insensitive strings.
9+
10+
All `CITEXT` values are folded to lowercase before comparison. This is handled internally with the [`lower()`]({% link {{ page.version.version }}/functions-and-operators.md %}#string-and-byte-functions) function.
11+
12+
For example, the following comparison evaluates to `true`:
13+
14+
{% include_cached copy-clipboard.html %}
15+
~~~ sql
16+
SELECT 'Roach'::CITEXT = 'roach'::CITEXT;
17+
~~~
18+
19+
~~~
20+
?column?
21+
------------
22+
t
23+
~~~
24+
25+
With `CITEXT`, equality operators (`=`, `!=`, `<>`), ordering operators (`<`, `>`, etc.), and [`STRING` functions]({% link {{ page.version.version }}/functions-and-operators.md %}#string-and-byte-functions), treat values as case-insensitive by default. Refer to the [example](#example).
26+
27+
Aside from comparisons, `CITEXT` behaves like [`STRING`]({% link {{ page.version.version }}/string.md %}).
28+
29+
## Syntax
30+
31+
To declare a `CITEXT` column, use the type name directly in your `CREATE TABLE` statement:
32+
33+
{% include_cached copy-clipboard.html %}
34+
~~~ sql
35+
CREATE TABLE logins (
36+
name CITEXT PRIMARY KEY,
37+
email TEXT NOT NULL
38+
);
39+
~~~
40+
41+
## Size
42+
43+
As with `STRING`, `CITEXT` values should be kept below 64 KB for best performance. Because `CITEXT` values are folded to lowercase on every comparison, `CITEXT` columns and indexes consume marginally more CPU and memory than their `STRING` equivalents, especially on write-heavy workloads.
44+
45+
## Collations
46+
47+
`CITEXT` compares values as a `STRING` column with the `und-u-ks-level2` [collation]({% link {{ page.version.version }}/collate.md %}), meaning it is case-insensitive but accent-sensitive. If you need accent-insensitive behavior, consider using `STRING` with a nondeterministic collation instead.
48+
49+
## Example
50+
51+
Create and populate a table:
52+
53+
{% include_cached copy-clipboard.html %}
54+
~~~ sql
55+
CREATE TABLE logins (
56+
username CITEXT,
57+
email STRING
58+
);
59+
~~~
60+
61+
{% include_cached copy-clipboard.html %}
62+
~~~ sql
63+
INSERT INTO logins VALUES
64+
('roach', 'roach@example.com'),
65+
('juno', 'juno@example.com');
66+
~~~
67+
68+
Because `CITEXT` comparisons are case-insensitive, an equality predicate matches regardless of letter case:
69+
70+
{% include_cached copy-clipboard.html %}
71+
~~~ sql
72+
SELECT * FROM logins WHERE username = 'Roach';
73+
~~~
74+
75+
~~~
76+
name | email
77+
--------+--------------------
78+
roach | roach@example.com
79+
(1 row)
80+
~~~
81+
82+
An ordering comparison is also case-insensitive with `CITEXT`. In the following eaxmple, `'Xavi'` is folded to lowercase before the comparison:
83+
84+
{% include_cached copy-clipboard.html %}
85+
~~~ sql
86+
SELECT username FROM logins WHERE username < 'Xavi';
87+
~~~
88+
89+
~~~
90+
username
91+
------------
92+
roach
93+
juno
94+
(2 rows)
95+
~~~
96+
97+
For case-sensitive comparisons on `CITEXT` values, cast to `STRING` explicitly. In the default Unicode ordering, the uppercase value is considered less than the lowercase values in the table:
98+
99+
{% include_cached copy-clipboard.html %}
100+
~~~ sql
101+
SELECT username FROM logins WHERE username::STRING < 'Xavi';
102+
~~~
103+
104+
~~~
105+
username | email
106+
-----------+--------
107+
(0 rows)
108+
~~~
109+
110+
## Supported casting and conversion
111+
112+
`CITEXT` values can be [cast]({% link {{ page.version.version }}/data-types.md %}#data-type-conversions-and-casts) to the following data types:
113+
114+
Type | Details
115+
-----|--------
116+
`STRING` | Preserves case information when casting to `STRING`.
117+
118+
## See also
119+
120+
- [`STRING`]({% link {{ page.version.version }}/string.md %})
121+
- [Data Types]({% link {{ page.version.version }}/data-types.md %})
122+
- [`COLLATE`]({% link {{ page.version.version }}/collate.md %})

src/current/v25.3/data-types.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Type | Description | Example
1616
[`BOOL`]({% link {{ page.version.version }}/bool.md %}) | A Boolean value. | `true`
1717
[`BYTES`]({% link {{ page.version.version }}/bytes.md %}) | A string of binary characters. | `b'\141\061\142\062\143\063'`
1818
[`COLLATE`]({% link {{ page.version.version }}/collate.md %}) | The `COLLATE` feature lets you sort [`STRING`]({% link {{ page.version.version }}/string.md %}) values according to language- and country-specific rules, known as collations. | `'a1b2c3' COLLATE en`
19+
[`CITEXT`]({% link {{ page.version.version }}/citext.md %}) | Case-insensitive text. | `'Roach'`
1920
[`DATE`]({% link {{ page.version.version }}/date.md %}) | A date. | `DATE '2016-01-25'`
2021
[`ENUM`]({% link {{ page.version.version }}/enum.md %}) | A user-defined data type comprised of a set of static values. | `ENUM ('club, 'diamond', 'heart', 'spade')`
2122
[`DECIMAL`]({% link {{ page.version.version }}/decimal.md %}) | An exact, fixed-point number. | `1.2345`

src/current/v25.3/string.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ Type | Details
135135
`BIT` | Requires supported [`BIT`]({% link {{ page.version.version }}/bit.md %}) string format, e.g., `'101001'` or `'xAB'`.
136136
`BOOL` | Requires supported [`BOOL`]({% link {{ page.version.version }}/bool.md %}) string format, e.g., `'true'`.
137137
`BYTES` | For more details, [see here]({% link {{ page.version.version }}/bytes.md %}#supported-conversions).
138+
`CITEXT` | Preserves the original letter case, but value comparisons are treated case-insensitively. Refer to [`CITEXT`]({% link {{ page.version.version }}/citext.md %}).
138139
`DATE` | Requires supported [`DATE`]({% link {{ page.version.version }}/date.md %}) string format, e.g., `'2016-01-25'`.
139140
`DECIMAL` | Requires supported [`DECIMAL`]({% link {{ page.version.version }}/decimal.md %}) string format, e.g., `'1.1'`.
140141
`FLOAT` | Requires supported [`FLOAT`]({% link {{ page.version.version }}/float.md %}) string format, e.g., `'1.1'`.

0 commit comments

Comments
 (0)