gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 line
13KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>F.8. citext</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="btree-gist.html" title="F.7. btree_gist" /><link rel="next" href="cube.html" title="F.9. cube" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">F.8. citext</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="btree-gist.html" title="F.7. btree_gist">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="contrib.html" title="Appendix F. Additional Supplied Modules">Up</a></td><th width="60%" align="center">Appendix F. Additional Supplied Modules</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="cube.html" title="F.9. cube">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="CITEXT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">F.8. citext</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="citext.html#id-1.11.7.17.5">F.8.1. Rationale</a></span></dt><dt><span class="sect2"><a href="citext.html#id-1.11.7.17.6">F.8.2. How to Use It</a></span></dt><dt><span class="sect2"><a href="citext.html#id-1.11.7.17.7">F.8.3. String Comparison Behavior</a></span></dt><dt><span class="sect2"><a href="citext.html#id-1.11.7.17.8">F.8.4. Limitations</a></span></dt><dt><span class="sect2"><a href="citext.html#id-1.11.7.17.9">F.8.5. Author</a></span></dt></dl></div><a id="id-1.11.7.17.2" class="indexterm"></a><p>
  3. The <code class="filename">citext</code> module provides a case-insensitive
  4. character string type, <code class="type">citext</code>. Essentially, it internally calls
  5. <code class="function">lower</code> when comparing values. Otherwise, it behaves almost
  6. exactly like <code class="type">text</code>.
  7. </p><div class="tip"><h3 class="title">Tip</h3><p>
  8. Consider using <em class="firstterm">nondeterministic collations</em> (see
  9. <a class="xref" href="collation.html#COLLATION-NONDETERMINISTIC" title="23.2.2.4. Nondeterministic Collations">Section 23.2.2.4</a>) instead of this module. They
  10. can be used for case-insensitive comparisons, accent-insensitive
  11. comparisons, and other combinations, and they handle more Unicode special
  12. cases correctly.
  13. </p></div><div class="sect2" id="id-1.11.7.17.5"><div class="titlepage"><div><div><h3 class="title">F.8.1. Rationale</h3></div></div></div><p>
  14. The standard approach to doing case-insensitive matches
  15. in <span class="productname">PostgreSQL</span> has been to use the <code class="function">lower</code>
  16. function when comparing values, for example
  17. </p><pre class="programlisting">
  18. SELECT * FROM tab WHERE lower(col) = LOWER(?);
  19. </pre><p>
  20. </p><p>
  21. This works reasonably well, but has a number of drawbacks:
  22. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  23. It makes your SQL statements verbose, and you always have to remember to
  24. use <code class="function">lower</code> on both the column and the query value.
  25. </p></li><li class="listitem"><p>
  26. It won't use an index, unless you create a functional index using
  27. <code class="function">lower</code>.
  28. </p></li><li class="listitem"><p>
  29. If you declare a column as <code class="literal">UNIQUE</code> or <code class="literal">PRIMARY
  30. KEY</code>, the implicitly generated index is case-sensitive. So it's
  31. useless for case-insensitive searches, and it won't enforce
  32. uniqueness case-insensitively.
  33. </p></li></ul></div><p>
  34. The <code class="type">citext</code> data type allows you to eliminate calls
  35. to <code class="function">lower</code> in SQL queries, and allows a primary key to
  36. be case-insensitive. <code class="type">citext</code> is locale-aware, just
  37. like <code class="type">text</code>, which means that the matching of upper case and
  38. lower case characters is dependent on the rules of
  39. the database's <code class="literal">LC_CTYPE</code> setting. Again, this behavior is
  40. identical to the use of <code class="function">lower</code> in queries. But because it's
  41. done transparently by the data type, you don't have to remember to do
  42. anything special in your queries.
  43. </p></div><div class="sect2" id="id-1.11.7.17.6"><div class="titlepage"><div><div><h3 class="title">F.8.2. How to Use It</h3></div></div></div><p>
  44. Here's a simple example of usage:
  45. </p><pre class="programlisting">
  46. CREATE TABLE users (
  47. nick CITEXT PRIMARY KEY,
  48. pass TEXT NOT NULL
  49. );
  50. INSERT INTO users VALUES ( 'larry', sha256(random()::text::bytea) );
  51. INSERT INTO users VALUES ( 'Tom', sha256(random()::text::bytea) );
  52. INSERT INTO users VALUES ( 'Damian', sha256(random()::text::bytea) );
  53. INSERT INTO users VALUES ( 'NEAL', sha256(random()::text::bytea) );
  54. INSERT INTO users VALUES ( 'Bjørn', sha256(random()::text::bytea) );
  55. SELECT * FROM users WHERE nick = 'Larry';
  56. </pre><p>
  57. The <code class="command">SELECT</code> statement will return one tuple, even though
  58. the <code class="structfield">nick</code> column was set to <code class="literal">larry</code> and the query
  59. was for <code class="literal">Larry</code>.
  60. </p></div><div class="sect2" id="id-1.11.7.17.7"><div class="titlepage"><div><div><h3 class="title">F.8.3. String Comparison Behavior</h3></div></div></div><p>
  61. <code class="type">citext</code> performs comparisons by converting each string to lower
  62. case (as though <code class="function">lower</code> were called) and then comparing the
  63. results normally. Thus, for example, two strings are considered equal
  64. if <code class="function">lower</code> would produce identical results for them.
  65. </p><p>
  66. In order to emulate a case-insensitive collation as closely as possible,
  67. there are <code class="type">citext</code>-specific versions of a number of string-processing
  68. operators and functions. So, for example, the regular expression
  69. operators <code class="literal">~</code> and <code class="literal">~*</code> exhibit the same behavior when
  70. applied to <code class="type">citext</code>: they both match case-insensitively.
  71. The same is true
  72. for <code class="literal">!~</code> and <code class="literal">!~*</code>, as well as for the
  73. <code class="literal">LIKE</code> operators <code class="literal">~~</code> and <code class="literal">~~*</code>, and
  74. <code class="literal">!~~</code> and <code class="literal">!~~*</code>. If you'd like to match
  75. case-sensitively, you can cast the operator's arguments to <code class="type">text</code>.
  76. </p><p>
  77. Similarly, all of the following functions perform matching
  78. case-insensitively if their arguments are <code class="type">citext</code>:
  79. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  80. <code class="function">regexp_match()</code>
  81. </p></li><li class="listitem"><p>
  82. <code class="function">regexp_matches()</code>
  83. </p></li><li class="listitem"><p>
  84. <code class="function">regexp_replace()</code>
  85. </p></li><li class="listitem"><p>
  86. <code class="function">regexp_split_to_array()</code>
  87. </p></li><li class="listitem"><p>
  88. <code class="function">regexp_split_to_table()</code>
  89. </p></li><li class="listitem"><p>
  90. <code class="function">replace()</code>
  91. </p></li><li class="listitem"><p>
  92. <code class="function">split_part()</code>
  93. </p></li><li class="listitem"><p>
  94. <code class="function">strpos()</code>
  95. </p></li><li class="listitem"><p>
  96. <code class="function">translate()</code>
  97. </p></li></ul></div><p>
  98. For the regexp functions, if you want to match case-sensitively, you can
  99. specify the <span class="quote">“<span class="quote">c</span>”</span> flag to force a case-sensitive match. Otherwise,
  100. you must cast to <code class="type">text</code> before using one of these functions if
  101. you want case-sensitive behavior.
  102. </p></div><div class="sect2" id="id-1.11.7.17.8"><div class="titlepage"><div><div><h3 class="title">F.8.4. Limitations</h3></div></div></div><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  103. <code class="type">citext</code>'s case-folding behavior depends on
  104. the <code class="literal">LC_CTYPE</code> setting of your database. How it compares
  105. values is therefore determined when the database is created.
  106. It is not truly
  107. case-insensitive in the terms defined by the Unicode standard.
  108. Effectively, what this means is that, as long as you're happy with your
  109. collation, you should be happy with <code class="type">citext</code>'s comparisons. But
  110. if you have data in different languages stored in your database, users
  111. of one language may find their query results are not as expected if the
  112. collation is for another language.
  113. </p></li><li class="listitem"><p>
  114. As of <span class="productname">PostgreSQL</span> 9.1, you can attach a
  115. <code class="literal">COLLATE</code> specification to <code class="type">citext</code> columns or data
  116. values. Currently, <code class="type">citext</code> operators will honor a non-default
  117. <code class="literal">COLLATE</code> specification while comparing case-folded strings,
  118. but the initial folding to lower case is always done according to the
  119. database's <code class="literal">LC_CTYPE</code> setting (that is, as though
  120. <code class="literal">COLLATE "default"</code> were given). This may be changed in a
  121. future release so that both steps follow the input <code class="literal">COLLATE</code>
  122. specification.
  123. </p></li><li class="listitem"><p>
  124. <code class="type">citext</code> is not as efficient as <code class="type">text</code> because the
  125. operator functions and the B-tree comparison functions must make copies
  126. of the data and convert it to lower case for comparisons. It is,
  127. however, slightly more efficient than using <code class="function">lower</code> to get
  128. case-insensitive matching.
  129. </p></li><li class="listitem"><p>
  130. <code class="type">citext</code> doesn't help much if you need data to compare
  131. case-sensitively in some contexts and case-insensitively in other
  132. contexts. The standard answer is to use the <code class="type">text</code> type and
  133. manually use the <code class="function">lower</code> function when you need to compare
  134. case-insensitively; this works all right if case-insensitive comparison
  135. is needed only infrequently. If you need case-insensitive behavior most
  136. of the time and case-sensitive infrequently, consider storing the data
  137. as <code class="type">citext</code> and explicitly casting the column to <code class="type">text</code>
  138. when you want case-sensitive comparison. In either situation, you will
  139. need two indexes if you want both types of searches to be fast.
  140. </p></li><li class="listitem"><p>
  141. The schema containing the <code class="type">citext</code> operators must be
  142. in the current <code class="varname">search_path</code> (typically <code class="literal">public</code>);
  143. if it is not, the normal case-sensitive <code class="type">text</code> operators
  144. will be invoked instead.
  145. </p></li><li class="listitem"><p>
  146. The approach of lower-casing strings for comparison does not handle some
  147. Unicode special cases correctly, for example when one upper-case letter
  148. has two lower-case letter equivalents. Unicode distinguishes between
  149. <em class="firstterm">case mapping</em> and <em class="firstterm">case
  150. folding</em> for this reason. Use nondeterministic collations
  151. instead of <code class="type">citext</code> to handle that correctly.
  152. </p></li></ul></div></div><div class="sect2" id="id-1.11.7.17.9"><div class="titlepage"><div><div><h3 class="title">F.8.5. Author</h3></div></div></div><p>
  153. David E. Wheeler <code class="email">&lt;<a class="email" href="mailto:david@kineticode.com">david@kineticode.com</a>&gt;</code>
  154. </p><p>
  155. Inspired by the original <code class="type">citext</code> module by Donald Fraser.
  156. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="btree-gist.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="contrib.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="cube.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F.7. btree_gist </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> F.9. cube</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1