hstore 技巧

针对 hstore 数据类型的技巧。

确定 hstore 是否为空

问题:您需要确定 hstore 是否为空(即不包含任何键/值,但不是NULL).

解决方案:与空 hstore 进行比较

testdb=# SELECT ''::HSTORE = ''::HSTORE, ''::HSTORE = '{foo=>bar}'::HSTORE;
 ?column? | ?column? 
----------+----------
 t        | f
(1 row)

表数据示例

testdb=*# INSERT INTO hstore_test VALUES(DEFAULT, 'foo=>bar');
INSERT 0 1
testdb=*# INSERT INTO hstore_test VALUES(DEFAULT, '');
INSERT 0 1
testdb=*# INSERT INTO hstore_test VALUES(DEFAULT, NULL);
INSERT 0 1
testdb=*# SELECT * FROM hstore_test;
 id |     val      
----+--------------
  9 | "foo"=>"bar"
 10 | 
 11 | 
(3 rows)

testdb=*# SELECT * FROM hstore_test WHERE val = ''::HSTORE;
 id | val 
----+-----
 10 | 
(1 row)