postgresql_16:9.6.18.2 hstore Operators and Functions
PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > hstore — hstore key/value datatype > hstore Operators and Functions
The operators provided by the hstore module are shown in (see hstore-op-table), the functions in (see hstore-func-table).
| Operator Description Example(s) | | --- | | hstore -> text text Returns value associated with given key, or NULL if not present. 'a=>x, b=>y'::hstore -> 'a' x | | hstore -> text[] text[] Returns values associated with given keys, or NULL if not present. 'a=>x, b=>y, c=>z'::hstore -> ARRAY['c','a'] {"z","x"} | | hstore \|\| hstore hstore Concatenates two hstores. 'a=>b, c=>d'::hstore \|\| 'c=>x, d=>q'::hstore "a"=>"b", "c"=>"x", "d"=>"q" | | hstore ? text boolean Does hstore contain key? 'a=>1'::hstore ? 'a' t | | hstore ?& text[] boolean Does hstore contain all the specified keys? 'a=>1,b=>2'::hstore ?& ARRAY['a','b'] t | | hstore ?\| text[] boolean Does hstore contain any of the specified keys? 'a=>1,b=>2'::hstore ?\| ARRAY['b','c'] t | | hstore @> hstore boolean Does left operand contain right? 'a=>b, b=>1, c=>NULL'::hstore @> 'b=>1' t | | hstore <@ hstore boolean Is left operand contained in right? 'a=>c'::hstore <@ 'a=>b, b=>1, c=>NULL' f | | hstore - text hstore Deletes key from left operand. 'a=>1, b=>2, c=>3'::hstore - 'b'::text "a"=>"1", "c"=>"3" | | hstore - text[] hstore Deletes keys from left operand. 'a=>1, b=>2, c=>3'::hstore - ARRAY['a','b'] "c"=>"3" | | hstore - hstore hstore Deletes pairs from left operand that match pairs in the right operand. 'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore "a"=>"1", "c"=>"3" | | anyelement #= hstore anyelement Replaces fields in the left operand (which must be a composite type) with matching values from hstore. ROW(1,3) #= 'f1=>11'::hstore (11,3) | | %% hstore text[] Converts hstore to an array of alternating keys and values. %% 'a=>foo, b=>bar'::hstore {a,foo,b,bar} | | %# hstore text[] Converts hstore to a two-dimensional key/value array. %# 'a=>foo, b=>bar'::hstore {{a,foo},{b,bar}} |
| Function Description Example(s) | | --- | | hstore ( record ) hstore Constructs an hstore from a record or row. hstore(ROW(1,2)) "f1"=>"1", "f2"=>"2" | | hstore ( text[] ) hstore Constructs an hstore from an array, which may be either a key/value array, or a two-dimensional array. hstore(ARRAY['a','1','b','2']) "a"=>"1", "b"=>"2" hstore(ARRAY[['c','3'],['d','4']]) "c"=>"3", "d"=>"4" | | hstore ( text[], text[] ) hstore Constructs an hstore from separate key and value arrays. hstore(ARRAY['a','b'], ARRAY['1','2']) "a"=>"1", "b"=>"2" | | hstore ( text, text ) hstore Makes a single-item hstore. hstore('a', 'b') "a"=>"b" | | akeys ( hstore ) text[] Extracts an hstore's keys as an array. akeys('a=>1,b=>2') {a,b} | | skeys ( hstore ) setof text Extracts an hstore's keys as a set. skeys('a=>1,b=>2') a b | | avals ( hstore ) text[] Extracts an hstore's values as an array. avals('a=>1,b=>2') {1,2} | | svals ( hstore ) setof text Extracts an hstore's values as a set. svals('a=>1,b=>2') 1 2 | | hstore_to_array ( hstore ) text[] Extracts an hstore's keys and values as an array of alternating keys and values. hstore_to_array('a=>1,b=>2') {a,1,b,2} | | hstore_to_matrix ( hstore ) text[] Extracts an hstore's keys and values as a two-dimensional array. hstore_to_matrix('a=>1,b=>2') {{a,1},{b,2}} | | hstore_to_json ( hstore ) json Converts an hstore to a json value, converting all non-null values to JSON strings. This function is used implicitly when an hstore value is cast to json. hstore_to_json('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4') {"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"} | | hstore_to_jsonb ( hstore ) jsonb Converts an hstore to a jsonb value, converting all non-null values to JSON strings. This function is used implicitly when an hstore value is cast to jsonb. hstore_to_jsonb('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4') {"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"} | | hstore_to_json_loose ( hstore ) json Converts an hstore to a json value, but attempts to distinguish numerical and Boolean values so they are unquoted in the JSON. hstore_to_json_loose('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4') {"a key": 1, "b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4} | | hstore_to_jsonb_loose ( hstore ) jsonb Converts an hstore to a jsonb value, but attempts to distinguish numerical and Boolean values so they are unquoted in the JSON. hstore_to_jsonb_loose('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4') {"a key": 1, "b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4} | | slice ( hstore, text[] ) hstore Extracts a subset of an hstore containing only the specified keys. slice('a=>1,b=>2,c=>3'::hstore, ARRAY['b','c','x']) "b"=>"2", "c"=>"3" | | each ( hstore ) setof record ( key text, value text ) Extracts an hstore's keys and values as a set of records. select * from each('a=>1,b=>2') key \| value -----+------- a \| 1 b \| 2 | | exist ( hstore, text ) boolean Does hstore contain key? exist('a=>1', 'a') t | | defined ( hstore, text ) boolean Does hstore contain a non-NULL value for key? defined('a=>NULL', 'a') f | | delete ( hstore, text ) hstore Deletes pair with matching key. delete('a=>1,b=>2', 'b') "a"=>"1" | | delete ( hstore, text[] ) hstore Deletes pairs with matching keys. delete('a=>1,b=>2,c=>3', ARRAY['a','b']) "c"=>"3" | | delete ( hstore, hstore ) hstore Deletes pairs matching those in the second argument. delete('a=>1,b=>2', 'a=>4,b=>2'::hstore) "a"=>"1" | | populate_record ( anyelement, hstore ) anyelement Replaces fields in the left operand (which must be a composite type) with matching values from hstore. populate_record(ROW(1,2), 'f1=>42'::hstore) (42,2) |
In addition to these operators and functions, values of the hstore type can be subscripted, allowing them to act like associative arrays. Only a single subscript of type text can be specified; it is interpreted as a key and the corresponding value is fetched or stored. For example, 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) A subscripted fetch returns NULL if the subscript is NULL or that key does not exist in the hstore. (Thus, a subscripted fetch is not greatly different from the -> operator.) A subscripted update fails if the subscript is NULL; otherwise, it replaces the value for that key, adding an entry to the hstore if the key does not already exist.