postgresqlhaskellpostgresql-simple

count(*) type compatibility error with Database.PostgreSQL.Simple?


The error is

*** Exception: Incompatible {errSQLType = "int8", errHaskellType = "Int", errMessage = "types incompatible"}

It looks like any value returned by count(*) in the query must be converted into Integer rather than Int. If I change those specific variables to type Integer, the queries work.

But this error wasn't being raised on another machine with the same exact code. The first machine was 32 bit and this other one 64-bit. That's the only difference I could discern.

Does anyone have any insight into what is going on?


Solution

  • The PostgreSQL count() functions returns a Bigint type, see

    http://www.postgresql.org/docs/9.2/static/functions-aggregate.html

    Bigint are 8 bytes see http://www.postgresql.org/docs/9.2/static/datatype-numeric.html

    Haskell int is ~ 2**29 which implies it a 4 byte integer.

    http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Int.html

    Then its normal that PostgreSQL or its API will not do an implicit downwards conversion in precision.

    So use a Haskell int64 type or cast count(*) to integer.