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.

241 lines
28KB

  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.16. hstore</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="fuzzystrmatch.html" title="F.15. fuzzystrmatch" /><link rel="next" href="intagg.html" title="F.17. intagg" /></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.16. hstore</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="fuzzystrmatch.html" title="F.15. fuzzystrmatch">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="intagg.html" title="F.17. intagg">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="HSTORE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">F.16. hstore</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="hstore.html#id-1.11.7.25.4">F.16.1. <code class="type">hstore</code> External Representation</a></span></dt><dt><span class="sect2"><a href="hstore.html#id-1.11.7.25.5">F.16.2. <code class="type">hstore</code> Operators and Functions</a></span></dt><dt><span class="sect2"><a href="hstore.html#id-1.11.7.25.6">F.16.3. Indexes</a></span></dt><dt><span class="sect2"><a href="hstore.html#id-1.11.7.25.7">F.16.4. Examples</a></span></dt><dt><span class="sect2"><a href="hstore.html#id-1.11.7.25.8">F.16.5. Statistics</a></span></dt><dt><span class="sect2"><a href="hstore.html#id-1.11.7.25.9">F.16.6. Compatibility</a></span></dt><dt><span class="sect2"><a href="hstore.html#id-1.11.7.25.10">F.16.7. Transforms</a></span></dt><dt><span class="sect2"><a href="hstore.html#id-1.11.7.25.11">F.16.8. Authors</a></span></dt></dl></div><a id="id-1.11.7.25.2" class="indexterm"></a><p>
  3. This module implements the <code class="type">hstore</code> data type for storing sets of
  4. key/value pairs within a single <span class="productname">PostgreSQL</span> value.
  5. This can be useful in various scenarios, such as rows with many attributes
  6. that are rarely examined, or semi-structured data. Keys and values are
  7. simply text strings.
  8. </p><div class="sect2" id="id-1.11.7.25.4"><div class="titlepage"><div><div><h3 class="title">F.16.1. <code class="type">hstore</code> External Representation</h3></div></div></div><p>
  9. The text representation of an <code class="type">hstore</code>, used for input and output,
  10. includes zero or more <em class="replaceable"><code>key</code></em> <code class="literal">=&gt;</code>
  11. <em class="replaceable"><code>value</code></em> pairs separated by commas. Some examples:
  12. </p><pre class="synopsis">
  13. k =&gt; v
  14. foo =&gt; bar, baz =&gt; whatever
  15. "1-a" =&gt; "anything at all"
  16. </pre><p>
  17. The order of the pairs is not significant (and may not be reproduced on
  18. output). Whitespace between pairs or around the <code class="literal">=&gt;</code> sign is
  19. ignored. Double-quote keys and values that include whitespace, commas,
  20. <code class="literal">=</code>s or <code class="literal">&gt;</code>s. To include a double quote or a
  21. backslash in a key or value, escape it with a backslash.
  22. </p><p>
  23. Each key in an <code class="type">hstore</code> is unique. If you declare an <code class="type">hstore</code>
  24. with duplicate keys, only one will be stored in the <code class="type">hstore</code> and
  25. there is no guarantee as to which will be kept:
  26. </p><pre class="programlisting">
  27. SELECT 'a=&gt;1,a=&gt;2'::hstore;
  28. hstore
  29. ----------
  30. "a"=&gt;"1"
  31. </pre><p>
  32. </p><p>
  33. A value (but not a key) can be an SQL <code class="literal">NULL</code>. For example:
  34. </p><pre class="programlisting">
  35. key =&gt; NULL
  36. </pre><p>
  37. The <code class="literal">NULL</code> keyword is case-insensitive. Double-quote the
  38. <code class="literal">NULL</code> to treat it as the ordinary string <span class="quote">“<span class="quote">NULL</span>”</span>.
  39. </p><div class="note"><h3 class="title">Note</h3><p>
  40. Keep in mind that the <code class="type">hstore</code> text format, when used for input,
  41. applies <span class="emphasis"><em>before</em></span> any required quoting or escaping. If you are
  42. passing an <code class="type">hstore</code> literal via a parameter, then no additional
  43. processing is needed. But if you're passing it as a quoted literal
  44. constant, then any single-quote characters and (depending on the setting of
  45. the <code class="varname">standard_conforming_strings</code> configuration parameter)
  46. backslash characters need to be escaped correctly. See
  47. <a class="xref" href="sql-syntax-lexical.html#SQL-SYNTAX-STRINGS" title="4.1.2.1. String Constants">Section 4.1.2.1</a> for more on the handling of string
  48. constants.
  49. </p></div><p>
  50. On output, double quotes always surround keys and values, even when it's
  51. not strictly necessary.
  52. </p></div><div class="sect2" id="id-1.11.7.25.5"><div class="titlepage"><div><div><h3 class="title">F.16.2. <code class="type">hstore</code> Operators and Functions</h3></div></div></div><p>
  53. The operators provided by the <code class="literal">hstore</code> module are
  54. shown in <a class="xref" href="hstore.html#HSTORE-OP-TABLE" title="Table F.7. hstore Operators">Table F.7</a>, the functions
  55. in <a class="xref" href="hstore.html#HSTORE-FUNC-TABLE" title="Table F.8. hstore Functions">Table F.8</a>.
  56. </p><div class="table" id="HSTORE-OP-TABLE"><p class="title"><strong>Table F.7. <code class="type">hstore</code> Operators</strong></p><div class="table-contents"><table class="table" summary="hstore Operators" border="1"><colgroup><col /><col /><col /><col /></colgroup><thead><tr><th>Operator</th><th>Description</th><th>Example</th><th>Result</th></tr></thead><tbody><tr><td><code class="type">hstore</code> <code class="literal">-&gt;</code> <code class="type">text</code></td><td>get value for key (<code class="literal">NULL</code> if not present)</td><td><code class="literal">'a=&gt;x, b=&gt;y'::hstore -&gt; 'a'</code></td><td><code class="literal">x</code></td></tr><tr><td><code class="type">hstore</code> <code class="literal">-&gt;</code> <code class="type">text[]</code></td><td>get values for keys (<code class="literal">NULL</code> if not present)</td><td><code class="literal">'a=&gt;x, b=&gt;y, c=&gt;z'::hstore -&gt; ARRAY['c','a']</code></td><td><code class="literal">{"z","x"}</code></td></tr><tr><td><code class="type">hstore</code> <code class="literal">||</code> <code class="type">hstore</code></td><td>concatenate <code class="type">hstore</code>s</td><td><code class="literal">'a=&gt;b, c=&gt;d'::hstore || 'c=&gt;x, d=&gt;q'::hstore</code></td><td><code class="literal">"a"=&gt;"b", "c"=&gt;"x", "d"=&gt;"q"</code></td></tr><tr><td><code class="type">hstore</code> <code class="literal">?</code> <code class="type">text</code></td><td>does <code class="type">hstore</code> contain key?</td><td><code class="literal">'a=&gt;1'::hstore ? 'a'</code></td><td><code class="literal">t</code></td></tr><tr><td><code class="type">hstore</code> <code class="literal">?&amp;</code> <code class="type">text[]</code></td><td>does <code class="type">hstore</code> contain all specified keys?</td><td><code class="literal">'a=&gt;1,b=&gt;2'::hstore ?&amp; ARRAY['a','b']</code></td><td><code class="literal">t</code></td></tr><tr><td><code class="type">hstore</code> <code class="literal">?|</code> <code class="type">text[]</code></td><td>does <code class="type">hstore</code> contain any of the specified keys?</td><td><code class="literal">'a=&gt;1,b=&gt;2'::hstore ?| ARRAY['b','c']</code></td><td><code class="literal">t</code></td></tr><tr><td><code class="type">hstore</code> <code class="literal">@&gt;</code> <code class="type">hstore</code></td><td>does left operand contain right?</td><td><code class="literal">'a=&gt;b, b=&gt;1, c=&gt;NULL'::hstore @&gt; 'b=&gt;1'</code></td><td><code class="literal">t</code></td></tr><tr><td><code class="type">hstore</code> <code class="literal">&lt;@</code> <code class="type">hstore</code></td><td>is left operand contained in right?</td><td><code class="literal">'a=&gt;c'::hstore &lt;@ 'a=&gt;b, b=&gt;1, c=&gt;NULL'</code></td><td><code class="literal">f</code></td></tr><tr><td><code class="type">hstore</code> <code class="literal">-</code> <code class="type">text</code></td><td>delete key from left operand</td><td><code class="literal">'a=&gt;1, b=&gt;2, c=&gt;3'::hstore - 'b'::text</code></td><td><code class="literal">"a"=&gt;"1", "c"=&gt;"3"</code></td></tr><tr><td><code class="type">hstore</code> <code class="literal">-</code> <code class="type">text[]</code></td><td>delete keys from left operand</td><td><code class="literal">'a=&gt;1, b=&gt;2, c=&gt;3'::hstore - ARRAY['a','b']</code></td><td><code class="literal">"c"=&gt;"3"</code></td></tr><tr><td><code class="type">hstore</code> <code class="literal">-</code> <code class="type">hstore</code></td><td>delete matching pairs from left operand</td><td><code class="literal">'a=&gt;1, b=&gt;2, c=&gt;3'::hstore - 'a=&gt;4, b=&gt;2'::hstore</code></td><td><code class="literal">"a"=&gt;"1", "c"=&gt;"3"</code></td></tr><tr><td><code class="type">record</code> <code class="literal">#=</code> <code class="type">hstore</code></td><td>replace fields in <code class="type">record</code> with matching values from <code class="type">hstore</code></td><td>see Examples section</td><td> </td></tr><tr><td><code class="literal">%%</code> <code class="type">hstore</code></td><td>convert <code class="type">hstore</code> to array of alternating keys and values</td><td><code class="literal">%% 'a=&gt;foo, b=&gt;bar'::hstore</code></td><td><code class="literal">{a,foo,b,bar}</code></td></tr><tr><td><code class="literal">%#</code> <code class="type">hstore</code></td><td>convert <code class="type">hstore</code> to two-dimensional key/value array</td><td><code class="literal">%# 'a=&gt;foo, b=&gt;bar'::hstore</code></td><td><code class="literal">{{a,foo},{b,bar}}</code></td></tr></tbody></table></div></div><br class="table-break" /><div class="note"><h3 class="title">Note</h3><p>
  57. Prior to PostgreSQL 8.2, the containment operators <code class="literal">@&gt;</code>
  58. and <code class="literal">&lt;@</code> were called <code class="literal">@</code> and <code class="literal">~</code>,
  59. respectively. These names are still available, but are deprecated and will
  60. eventually be removed. Notice that the old names are reversed from the
  61. convention formerly followed by the core geometric data types!
  62. </p></div><div class="table" id="HSTORE-FUNC-TABLE"><p class="title"><strong>Table F.8. <code class="type">hstore</code> Functions</strong></p><div class="table-contents"><table class="table" summary="hstore Functions" border="1"><colgroup><col /><col /><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Return Type</th><th>Description</th><th>Example</th><th>Result</th></tr></thead><tbody><tr><td><code class="function">hstore(record)</code><a id="id-1.11.7.25.5.5.2.2.1.1.2" class="indexterm"></a></td><td><code class="type">hstore</code></td><td>construct an <code class="type">hstore</code> from a record or row</td><td><code class="literal">hstore(ROW(1,2))</code></td><td><code class="literal">f1=&gt;1,f2=&gt;2</code></td></tr><tr><td><code class="function">hstore(text[])</code></td><td><code class="type">hstore</code></td><td>construct an <code class="type">hstore</code> from an array, which may be either
  63. a key/value array, or a two-dimensional array</td><td><code class="literal">hstore(ARRAY['a','1','b','2']) || hstore(ARRAY[['c','3'],['d','4']])</code></td><td><code class="literal">a=&gt;1, b=&gt;2, c=&gt;3, d=&gt;4</code></td></tr><tr><td><code class="function">hstore(text[], text[])</code></td><td><code class="type">hstore</code></td><td>construct an <code class="type">hstore</code> from separate key and value arrays</td><td><code class="literal">hstore(ARRAY['a','b'], ARRAY['1','2'])</code></td><td><code class="literal">"a"=&gt;"1","b"=&gt;"2"</code></td></tr><tr><td><code class="function">hstore(text, text)</code></td><td><code class="type">hstore</code></td><td>make single-item <code class="type">hstore</code></td><td><code class="literal">hstore('a', 'b')</code></td><td><code class="literal">"a"=&gt;"b"</code></td></tr><tr><td><code class="function">akeys(hstore)</code><a id="id-1.11.7.25.5.5.2.2.5.1.2" class="indexterm"></a></td><td><code class="type">text[]</code></td><td>get <code class="type">hstore</code>'s keys as an array</td><td><code class="literal">akeys('a=&gt;1,b=&gt;2')</code></td><td><code class="literal">{a,b}</code></td></tr><tr><td><code class="function">skeys(hstore)</code><a id="id-1.11.7.25.5.5.2.2.6.1.2" class="indexterm"></a></td><td><code class="type">setof text</code></td><td>get <code class="type">hstore</code>'s keys as a set</td><td><code class="literal">skeys('a=&gt;1,b=&gt;2')</code></td><td>
  64. <pre class="programlisting">
  65. a
  66. b
  67. </pre></td></tr><tr><td><code class="function">avals(hstore)</code><a id="id-1.11.7.25.5.5.2.2.7.1.2" class="indexterm"></a></td><td><code class="type">text[]</code></td><td>get <code class="type">hstore</code>'s values as an array</td><td><code class="literal">avals('a=&gt;1,b=&gt;2')</code></td><td><code class="literal">{1,2}</code></td></tr><tr><td><code class="function">svals(hstore)</code><a id="id-1.11.7.25.5.5.2.2.8.1.2" class="indexterm"></a></td><td><code class="type">setof text</code></td><td>get <code class="type">hstore</code>'s values as a set</td><td><code class="literal">svals('a=&gt;1,b=&gt;2')</code></td><td>
  68. <pre class="programlisting">
  69. 1
  70. 2
  71. </pre></td></tr><tr><td><code class="function">hstore_to_array(hstore)</code><a id="id-1.11.7.25.5.5.2.2.9.1.2" class="indexterm"></a></td><td><code class="type">text[]</code></td><td>get <code class="type">hstore</code>'s keys and values as an array of alternating
  72. keys and values</td><td><code class="literal">hstore_to_array('a=&gt;1,b=&gt;2')</code></td><td><code class="literal">{a,1,b,2}</code></td></tr><tr><td><code class="function">hstore_to_matrix(hstore)</code><a id="id-1.11.7.25.5.5.2.2.10.1.2" class="indexterm"></a></td><td><code class="type">text[]</code></td><td>get <code class="type">hstore</code>'s keys and values as a two-dimensional array</td><td><code class="literal">hstore_to_matrix('a=&gt;1,b=&gt;2')</code></td><td><code class="literal">{{a,1},{b,2}}</code></td></tr><tr><td><code class="function">hstore_to_json(hstore)</code><a id="id-1.11.7.25.5.5.2.2.11.1.2" class="indexterm"></a></td><td><code class="type">json</code></td><td>get <code class="type">hstore</code> as a <code class="type">json</code> value, converting
  73. all non-null values to JSON strings</td><td><code class="literal">hstore_to_json('"a key"=&gt;1, b=&gt;t, c=&gt;null, d=&gt;12345, e=&gt;012345, f=&gt;1.234, g=&gt;2.345e+4')</code></td><td><code class="literal">{"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"}</code></td></tr><tr><td><code class="function">hstore_to_jsonb(hstore)</code><a id="id-1.11.7.25.5.5.2.2.12.1.2" class="indexterm"></a></td><td><code class="type">jsonb</code></td><td>get <code class="type">hstore</code> as a <code class="type">jsonb</code> value, converting
  74. all non-null values to JSON strings</td><td><code class="literal">hstore_to_jsonb('"a key"=&gt;1, b=&gt;t, c=&gt;null, d=&gt;12345, e=&gt;012345, f=&gt;1.234, g=&gt;2.345e+4')</code></td><td><code class="literal">{"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"}</code></td></tr><tr><td><code class="function">hstore_to_json_loose(hstore)</code><a id="id-1.11.7.25.5.5.2.2.13.1.2" class="indexterm"></a></td><td><code class="type">json</code></td><td>get <code class="type">hstore</code> as a <code class="type">json</code> value, but attempt to distinguish numerical and Boolean values so they are unquoted in the JSON</td><td><code class="literal">hstore_to_json_loose('"a key"=&gt;1, b=&gt;t, c=&gt;null, d=&gt;12345, e=&gt;012345, f=&gt;1.234, g=&gt;2.345e+4')</code></td><td><code class="literal">{"a key": 1, "b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4}</code></td></tr><tr><td><code class="function">hstore_to_jsonb_loose(hstore)</code><a id="id-1.11.7.25.5.5.2.2.14.1.2" class="indexterm"></a></td><td><code class="type">jsonb</code></td><td>get <code class="type">hstore</code> as a <code class="type">jsonb</code> value, but attempt to distinguish numerical and Boolean values so they are unquoted in the JSON</td><td><code class="literal">hstore_to_jsonb_loose('"a key"=&gt;1, b=&gt;t, c=&gt;null, d=&gt;12345, e=&gt;012345, f=&gt;1.234, g=&gt;2.345e+4')</code></td><td><code class="literal">{"a key": 1, "b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4}</code></td></tr><tr><td><code class="function">slice(hstore, text[])</code><a id="id-1.11.7.25.5.5.2.2.15.1.2" class="indexterm"></a></td><td><code class="type">hstore</code></td><td>extract a subset of an <code class="type">hstore</code></td><td><code class="literal">slice('a=&gt;1,b=&gt;2,c=&gt;3'::hstore, ARRAY['b','c','x'])</code></td><td><code class="literal">"b"=&gt;"2", "c"=&gt;"3"</code></td></tr><tr><td><code class="function">each(hstore)</code><a id="id-1.11.7.25.5.5.2.2.16.1.2" class="indexterm"></a></td><td><code class="type">setof(key text, value text)</code></td><td>get <code class="type">hstore</code>'s keys and values as a set</td><td><code class="literal">select * from each('a=&gt;1,b=&gt;2')</code></td><td>
  75. <pre class="programlisting">
  76. key | value
  77. -----+-------
  78. a | 1
  79. b | 2
  80. </pre></td></tr><tr><td><code class="function">exist(hstore,text)</code><a id="id-1.11.7.25.5.5.2.2.17.1.2" class="indexterm"></a></td><td><code class="type">boolean</code></td><td>does <code class="type">hstore</code> contain key?</td><td><code class="literal">exist('a=&gt;1','a')</code></td><td><code class="literal">t</code></td></tr><tr><td><code class="function">defined(hstore,text)</code><a id="id-1.11.7.25.5.5.2.2.18.1.2" class="indexterm"></a></td><td><code class="type">boolean</code></td><td>does <code class="type">hstore</code> contain non-<code class="literal">NULL</code> value for key?</td><td><code class="literal">defined('a=&gt;NULL','a')</code></td><td><code class="literal">f</code></td></tr><tr><td><code class="function">delete(hstore,text)</code><a id="id-1.11.7.25.5.5.2.2.19.1.2" class="indexterm"></a></td><td><code class="type">hstore</code></td><td>delete pair with matching key</td><td><code class="literal">delete('a=&gt;1,b=&gt;2','b')</code></td><td><code class="literal">"a"=&gt;"1"</code></td></tr><tr><td><code class="function">delete(hstore,text[])</code></td><td><code class="type">hstore</code></td><td>delete pairs with matching keys</td><td><code class="literal">delete('a=&gt;1,b=&gt;2,c=&gt;3',ARRAY['a','b'])</code></td><td><code class="literal">"c"=&gt;"3"</code></td></tr><tr><td><code class="function">delete(hstore,hstore)</code></td><td><code class="type">hstore</code></td><td>delete pairs matching those in the second argument</td><td><code class="literal">delete('a=&gt;1,b=&gt;2','a=&gt;4,b=&gt;2'::hstore)</code></td><td><code class="literal">"a"=&gt;"1"</code></td></tr><tr><td><code class="function">populate_record(record,hstore)</code><a id="id-1.11.7.25.5.5.2.2.22.1.2" class="indexterm"></a></td><td><code class="type">record</code></td><td>replace fields in <code class="type">record</code> with matching values from <code class="type">hstore</code></td><td>see Examples section</td><td> </td></tr></tbody></table></div></div><br class="table-break" /><div class="note"><h3 class="title">Note</h3><p>
  81. The function <code class="function">hstore_to_json</code> is used when
  82. an <code class="type">hstore</code> value is cast to <code class="type">json</code>.
  83. Likewise, <code class="function">hstore_to_jsonb</code> is used when
  84. an <code class="type">hstore</code> value is cast to <code class="type">jsonb</code>.
  85. </p></div><div class="note"><h3 class="title">Note</h3><p>
  86. The function <code class="function">populate_record</code> is actually declared
  87. with <code class="type">anyelement</code>, not <code class="type">record</code>, as its first argument,
  88. but it will reject non-record types with a run-time error.
  89. </p></div></div><div class="sect2" id="id-1.11.7.25.6"><div class="titlepage"><div><div><h3 class="title">F.16.3. Indexes</h3></div></div></div><p>
  90. <code class="type">hstore</code> has GiST and GIN index support for the <code class="literal">@&gt;</code>,
  91. <code class="literal">?</code>, <code class="literal">?&amp;</code> and <code class="literal">?|</code> operators. For example:
  92. </p><pre class="programlisting">
  93. CREATE INDEX hidx ON testhstore USING GIST (h);
  94. CREATE INDEX hidx ON testhstore USING GIN (h);
  95. </pre><p>
  96. <code class="type">hstore</code> also supports <code class="type">btree</code> or <code class="type">hash</code> indexes for
  97. the <code class="literal">=</code> operator. This allows <code class="type">hstore</code> columns to be
  98. declared <code class="literal">UNIQUE</code>, or to be used in <code class="literal">GROUP BY</code>,
  99. <code class="literal">ORDER BY</code> or <code class="literal">DISTINCT</code> expressions. The sort ordering
  100. for <code class="type">hstore</code> values is not particularly useful, but these indexes
  101. may be useful for equivalence lookups. Create indexes for <code class="literal">=</code>
  102. comparisons as follows:
  103. </p><pre class="programlisting">
  104. CREATE INDEX hidx ON testhstore USING BTREE (h);
  105. CREATE INDEX hidx ON testhstore USING HASH (h);
  106. </pre></div><div class="sect2" id="id-1.11.7.25.7"><div class="titlepage"><div><div><h3 class="title">F.16.4. Examples</h3></div></div></div><p>
  107. Add a key, or update an existing key with a new value:
  108. </p><pre class="programlisting">
  109. UPDATE tab SET h = h || hstore('c', '3');
  110. </pre><p>
  111. </p><p>
  112. Delete a key:
  113. </p><pre class="programlisting">
  114. UPDATE tab SET h = delete(h, 'k1');
  115. </pre><p>
  116. </p><p>
  117. Convert a <code class="type">record</code> to an <code class="type">hstore</code>:
  118. </p><pre class="programlisting">
  119. CREATE TABLE test (col1 integer, col2 text, col3 text);
  120. INSERT INTO test VALUES (123, 'foo', 'bar');
  121. SELECT hstore(t) FROM test AS t;
  122. hstore
  123. ---------------------------------------------
  124. "col1"=&gt;"123", "col2"=&gt;"foo", "col3"=&gt;"bar"
  125. (1 row)
  126. </pre><p>
  127. </p><p>
  128. Convert an <code class="type">hstore</code> to a predefined <code class="type">record</code> type:
  129. </p><pre class="programlisting">
  130. CREATE TABLE test (col1 integer, col2 text, col3 text);
  131. SELECT * FROM populate_record(null::test,
  132. '"col1"=&gt;"456", "col2"=&gt;"zzz"');
  133. col1 | col2 | col3
  134. ------+------+------
  135. 456 | zzz |
  136. (1 row)
  137. </pre><p>
  138. </p><p>
  139. Modify an existing record using the values from an <code class="type">hstore</code>:
  140. </p><pre class="programlisting">
  141. CREATE TABLE test (col1 integer, col2 text, col3 text);
  142. INSERT INTO test VALUES (123, 'foo', 'bar');
  143. SELECT (r).* FROM (SELECT t #= '"col3"=&gt;"baz"' AS r FROM test t) s;
  144. col1 | col2 | col3
  145. ------+------+------
  146. 123 | foo | baz
  147. (1 row)
  148. </pre><p>
  149. </p></div><div class="sect2" id="id-1.11.7.25.8"><div class="titlepage"><div><div><h3 class="title">F.16.5. Statistics</h3></div></div></div><p>
  150. The <code class="type">hstore</code> type, because of its intrinsic liberality, could
  151. contain a lot of different keys. Checking for valid keys is the task of the
  152. application. The following examples demonstrate several techniques for
  153. checking keys and obtaining statistics.
  154. </p><p>
  155. Simple example:
  156. </p><pre class="programlisting">
  157. SELECT * FROM each('aaa=&gt;bq, b=&gt;NULL, ""=&gt;1');
  158. </pre><p>
  159. </p><p>
  160. Using a table:
  161. </p><pre class="programlisting">
  162. SELECT (each(h)).key, (each(h)).value INTO stat FROM testhstore;
  163. </pre><p>
  164. </p><p>
  165. Online statistics:
  166. </p><pre class="programlisting">
  167. SELECT key, count(*) FROM
  168. (SELECT (each(h)).key FROM testhstore) AS stat
  169. GROUP BY key
  170. ORDER BY count DESC, key;
  171. key | count
  172. -----------+-------
  173. line | 883
  174. query | 207
  175. pos | 203
  176. node | 202
  177. space | 197
  178. status | 195
  179. public | 194
  180. title | 190
  181. org | 189
  182. ...................
  183. </pre><p>
  184. </p></div><div class="sect2" id="id-1.11.7.25.9"><div class="titlepage"><div><div><h3 class="title">F.16.6. Compatibility</h3></div></div></div><p>
  185. As of PostgreSQL 9.0, <code class="type">hstore</code> uses a different internal
  186. representation than previous versions. This presents no obstacle for
  187. dump/restore upgrades since the text representation (used in the dump) is
  188. unchanged.
  189. </p><p>
  190. In the event of a binary upgrade, upward compatibility is maintained by
  191. having the new code recognize old-format data. This will entail a slight
  192. performance penalty when processing data that has not yet been modified by
  193. the new code. It is possible to force an upgrade of all values in a table
  194. column by doing an <code class="literal">UPDATE</code> statement as follows:
  195. </p><pre class="programlisting">
  196. UPDATE tablename SET hstorecol = hstorecol || '';
  197. </pre><p>
  198. </p><p>
  199. Another way to do it is:
  200. </p><pre class="programlisting">
  201. ALTER TABLE tablename ALTER hstorecol TYPE hstore USING hstorecol || '';
  202. </pre><p>
  203. The <code class="command">ALTER TABLE</code> method requires an exclusive lock on the table,
  204. but does not result in bloating the table with old row versions.
  205. </p></div><div class="sect2" id="id-1.11.7.25.10"><div class="titlepage"><div><div><h3 class="title">F.16.7. Transforms</h3></div></div></div><p>
  206. Additional extensions are available that implement transforms for
  207. the <code class="type">hstore</code> type for the languages PL/Perl and PL/Python. The
  208. extensions for PL/Perl are called <code class="literal">hstore_plperl</code>
  209. and <code class="literal">hstore_plperlu</code>, for trusted and untrusted PL/Perl.
  210. If you install these transforms and specify them when creating a
  211. function, <code class="type">hstore</code> values are mapped to Perl hashes. The
  212. extensions for PL/Python are
  213. called <code class="literal">hstore_plpythonu</code>, <code class="literal">hstore_plpython2u</code>,
  214. and <code class="literal">hstore_plpython3u</code>
  215. (see <a class="xref" href="plpython-python23.html" title="45.1. Python 2 vs. Python 3">Section 45.1</a> for the PL/Python naming
  216. convention). If you use them, <code class="type">hstore</code> values are mapped to
  217. Python dictionaries.
  218. </p><div class="caution"><h3 class="title">Caution</h3><p>
  219. It is strongly recommended that the transform extensions be installed in
  220. the same schema as <code class="filename">hstore</code>. Otherwise there are
  221. installation-time security hazards if a transform extension's schema
  222. contains objects defined by a hostile user.
  223. </p></div></div><div class="sect2" id="id-1.11.7.25.11"><div class="titlepage"><div><div><h3 class="title">F.16.8. Authors</h3></div></div></div><p>
  224. Oleg Bartunov <code class="email">&lt;<a class="email" href="mailto:oleg@sai.msu.su">oleg@sai.msu.su</a>&gt;</code>, Moscow, Moscow University, Russia
  225. </p><p>
  226. Teodor Sigaev <code class="email">&lt;<a class="email" href="mailto:teodor@sigaev.ru">teodor@sigaev.ru</a>&gt;</code>, Moscow, Delta-Soft Ltd., Russia
  227. </p><p>
  228. Additional enhancements by Andrew Gierth <code class="email">&lt;<a class="email" href="mailto:andrew@tao11.riddles.org.uk">andrew@tao11.riddles.org.uk</a>&gt;</code>,
  229. United Kingdom
  230. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="fuzzystrmatch.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="intagg.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F.15. fuzzystrmatch </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> F.17. intagg</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1