Home Explore Blog CI



postgresql

8th chunk of `doc/src/sgml/hstore.sgml`
7c13c2f1c655daf45b23bc9ab84536cc498efb2c648e55810000000100000fa5
 contain key?
       </para>
       <para>
        <literal>exist('a=&gt;1', 'a')</literal>
        <returnvalue>t</returnvalue>
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm><primary>defined</primary></indexterm>
        <function>defined</function> ( <type>hstore</type>, <type>text</type> )
        <returnvalue>boolean</returnvalue>
       </para>
       <para>
        Does <type>hstore</type> contain a non-<literal>NULL</literal> value
        for key?
       </para>
       <para>
        <literal>defined('a=&gt;NULL', 'a')</literal>
        <returnvalue>f</returnvalue>
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm><primary>delete</primary></indexterm>
        <function>delete</function> ( <type>hstore</type>, <type>text</type> )
        <returnvalue>hstore</returnvalue>
       </para>
       <para>
        Deletes pair with matching key.
       </para>
       <para>
        <literal>delete('a=&gt;1,b=&gt;2', 'b')</literal>
        <returnvalue>"a"=&gt;"1"</returnvalue>
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <function>delete</function> ( <type>hstore</type>, <type>text[]</type> )
        <returnvalue>hstore</returnvalue>
       </para>
       <para>
        Deletes pairs with matching keys.
       </para>
       <para>
        <literal>delete('a=&gt;1,b=&gt;2,c=&gt;3', ARRAY['a','b'])</literal>
        <returnvalue>"c"=&gt;"3"</returnvalue>
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <function>delete</function> ( <type>hstore</type>, <type>hstore</type> )
        <returnvalue>hstore</returnvalue>
       </para>
       <para>
        Deletes pairs matching those in the second argument.
       </para>
       <para>
        <literal>delete('a=&gt;1,b=&gt;2', 'a=&gt;4,b=&gt;2'::hstore)</literal>
        <returnvalue>"a"=&gt;"1"</returnvalue>
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm><primary>populate_record</primary></indexterm>
        <function>populate_record</function> ( <type>anyelement</type>, <type>hstore</type> )
        <returnvalue>anyelement</returnvalue>
       </para>
       <para>
        Replaces fields in the left operand (which must be a composite type)
        with matching values from <type>hstore</type>.
       </para>
       <para>
        <literal>populate_record(ROW(1,2), 'f1=>42'::hstore)</literal>
        <returnvalue>(42,2)</returnvalue>
       </para></entry>
      </row>
    </tbody>
   </tgroup>
  </table>

  <para>
   In addition to these operators and functions, values of
   the <type>hstore</type> type can be subscripted, allowing them to act
   like associative arrays.  Only a single subscript of type <type>text</type>
   can be specified; it is interpreted as a key and the corresponding
   value is fetched or stored.  For example,

<programlisting>
CREATE TABLE mytable (h hstore);
INSERT INTO mytable VALUES ('a=>b, c=>d');
SELECT h['a'] FROM mytable;
 h
---
 b
(1 row)

UPDATE mytable SET h['c'] = 'new';
SELECT h FROM mytable;
          h
----------------------
 "a"=>"b", "c"=>"new"
(1 row)
</programlisting>

   A subscripted fetch returns <literal>NULL</literal> if the subscript
   is <literal>NULL</literal> or that key does not exist in
   the <type>hstore</type>.  (Thus, a subscripted fetch is not greatly
   different from the <literal>-&gt;</literal> operator.)
   A subscripted update fails if the subscript is <literal>NULL</literal>;
   otherwise, it replaces the value for that key, adding an entry to
   the <type>hstore</type> if the key does not already exist.
  </para>
 </sect2>

 <sect2 id="hstore-indexes">
  <title>Indexes</title>

  <para>
   <type>hstore</type>

Title: hstore Functions and Subscripting in PostgreSQL
Summary
This section details various functions and operations for the hstore data type in PostgreSQL. It covers functions like exist() for checking key existence, defined() for verifying non-NULL values, and delete() for removing key-value pairs. The text also explains how hstore values can be subscripted, allowing them to behave like associative arrays. Users can fetch or store values using a single text subscript as a key. Examples demonstrate how to use these functions and subscripting in SQL queries, including SELECT, INSERT, and UPDATE operations. The subscripting behavior is compared to the -> operator, noting that a subscripted fetch returns NULL for non-existent keys or NULL subscripts, while a subscripted update fails for NULL subscripts but adds new entries for non-existent keys.