[database-devel] Arrays and postgresql-simple

Jason Dusek jason.dusek at gmail.com
Thu Aug 16 15:22:54 CEST 2012

I have integrated Bas's type cache modifications with a more
robust parser and pretty printer, which handle quoted strings
and nested arrays.


In an earlier email, Leon P. Smith raised some concerns about
the types of arrays of arrays.

2012-07-30 21:00:34 UTC, leon.p.smith at gmail.com:
> Now, presumably, postgresql does actually support arrays of
> arrays, but I'm guessing that you need to create the array of
> array of string type before you can actually run this query...
> The reason I ask is that Bas van Dijk has done some work on
> adding support for arrays to postgresql-simple. And he's
> modified the type cache from TypeOID -> IO TypeName to become
> a TypeOID -> IO TypeInfo, where the TypeInfo type is defined
> as follows:
> data NamedOid = NamedOid { typoid  :: !PQ.Oid
>                          , typname :: !ByteString
>                          } deriving Show
> data TypeInfo = TypeInfo { typ     :: !NamedOid
>                          , typelem :: !(Maybe NamedOid)
>                          } deriving Show
> I think this is a perfectly reasonable first attempt, but I
> don't think it's correct, because I'm pretty sure that
> PostgreSQL does actually support arrays of arrays if you know
> what you are doing. So I think that TypeInfo needs to look
> something more like
> data TypeInfo = Plain { typ     :: !NamedOid }
>               | Array { typ     :: !NamedOid
>                       , typelem :: !TypeInfo }
>                 deriving Show
> The real issue here is one dealing with purity and effects: In
> some sense, Bas's first attempt carries the same information,
> but you might need to do some IO in order to retrieve it.
> (Though this assumes that one can query the type cache
> directly, which is something I should probably add anyway...)
> But the fromField method, which is is the most likely consumer
> of this information, isn't allowed to do IO. So I think we
> really want to change this type.

Because a Postgres array is multi-dimensional, it would stand to
reason that the Haskell types [Int4], [[Int4]] and [[[Int4]]]
would all map to _int4 in Postgres. The array parsing code
linked to above relies on that assumption.

Jason Dusek
pgp // solidsnack // C1EBC57DC55144F35460C8DF1FD4C6C1FED18A2B

More information about the database-devel mailing list