Is it possible to parse nested rows / arrays of nested rows using postgresql-simple?

I would like to be able to parse results returned by the following query using postgresql-simple.

psql> select 'string' as string, 1 as number, row (1,'hello') as "row with number and string", ARRAY[row(1,'hello'), row(2,'bye')] as "array of rows";
 string | number | row with number and string |      array of rows      
--------+--------+----------------------------+-------------------------
 string |      1 | (1,hello)                  | {"(1,hello)","(2,bye)"}
(1 row)

It doesn’t seem to be possible using the provided FromField / FromRow instances.

On the other hand hasql seems to have support for this kind of thing (viz Composite in Hasql.Decoders)

Is this scenario supported by postgresql-simple? If so could you please sketch how to implement it?

It should be possible with postgresql-simple so long as a custom FromField instance is provided. This blog post walks through an example: joncfoo | Composite Types with postgresql-simple

For the ARRAY[row(1,'hello') value, I’d expect this to work fine so long as that custom FromField instance is provided and PGArray is used on the Haskell side, as PGArray has a (FromField a, Typeable a) => FromField (PGArray a) instance.

2 Likes

Thanks for the tip!
I was able to make it work using the provided suggestions.

1 Like