I have some columns in PostgreSQL database that are array. I want to add a new value (in UPDATE) in it if the value don’t exists, otherwise, don’t add anytihing. I don’t want to overwrite the current value of the array, but only add the element to it.
Is possible do this in a query or I need to do this inside a function? I’m using PostgreSQL.
This should be as simple as this example for an integer array (
integer[]):A
WHEREclause like:would also catch the case of an empty array
'{}'::int[], but fail if aNULLvalue appears as element of the array.If your arrays never contain NULL as element, consider actual array operators, possibly supported by a GIN index.
See: