I’m using the %% operator on PostgreSQL’s hstore type which converts a hstore (key-value type effectively) into an array whose elements alternate {{key, value}, {key value}}.
When I want to return array of these flattened hstores I get this error: could not find array type for data type text[]due to PostgreSQL lack of support for an array of arrays.
From a curiosity standpoint, does anyone know why these are not supported? And more importantly, is there a work around for this type of scenario?
At the moment I’m concatenating the results into a string (comma separated) and parsing them on the application (C# and NPGSQL) side. However, this approach doesn’t feel quite right, I’d like to be able to read the row back as a .NET array of arrays or array of key-values etc.
Many thanks.
One generic answer is because arrays are intrinsically anti-relational. Removing repeating values is how you achieve 1st normal form. To have repeating groups of repeating groups seems quite insane from a relational theoretical standpoint.
In general, the relationally-correct thing to do is to extract a table for your repeating values. So if you modeled something like this:
it would behoove you to redefine this relationally like so:
Or even:
Hstore supports a lot of functions, many of which would make it easy to integrate it into a relational worldview. I think the simplest way to solve your problem would be to use the
eachfunction to convert your hstore values into relations you can then use like a normal set of values. This is how you address having multiple values in other databases anyway: querying, and working with result sets.