Adding hstore entries to an uninitialized (NULL) column

In SQL, NULL (operator) (value) is generally NULL.

This is not unique to hstore, and is the norm for everything.

The empty string '' is different to NULL. '' || 'somestring' is 'somestring', wheras NULL || 'somestring' is NULL.

The same is true for hstore. Just like NULL + 1 is NULL.

If this is a problem for you, you should probably store empty hstore values instead of NULL and assign a NOT NULL constraint on the column.


@Craig provides a detailed explanation and the best advice to avoid the problem: define the column NOT NULL DEFAULT '' - which adds 1 byte per row to storage (typically) where the columns could be NULL instead.

The simple, standard solution for the problem at hand is COALESCE() - like with any other data type that can be NULL. It's a completely reasonable design to allow NULL values in the column, you just have to handle it properly.

Your idea with IF was close but that's not part of the SQL language (neither in standard SQL nor in Postgres). Some other RDBMS like MySQL introduce IF and IFNULL to SQL, but those add nothing over the standard features CASE and COALESCE.

CREATE TEMP TABLE test_hstore AS
SELECT '1'::int AS id, NULL::hstore AS map;  -- test table with 1 row

UPDATE test_hstore SET map = map || hstore('key1', 'value1')
RETURNING *;

id  |  map   
----+--------
1   | (null)

UPDATE test_hstore SET map = COALESCE(map, '') || hstore('key1', 'value1')
RETURNING *;

id  |   map        
----+------------------
1   | "key1"=>"value1"

db<>fiddle here
Old sqlfiddle