Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Possible bug > Does AR JDBC Adapter handle Postgres arrays? #510

saurabhnanda opened this Issue · 7 comments

2 participants


I'm migrating a fairly complex Rails 3.1.x project from MRI-Ruby to JRuby. Database is PostgreSQL and we're using Postgres arrays in our schema. With MRI-Ruby we needed the AR-Postgres-Arrays gem to deal with Postgres arrays.

However, that gem seems to be barfing with JRuby, with a weird issues: a 1-d boolean array in Postgres is being converted to a single boolean value in Ruby, whereas a 1-d string array is being converted to a Ruby string array just fine.

I dug deeper into the gem's code and narrowed the issue down to Basically type is boolean / string / integer instead of the expected boolean_array / string_array / integer_array

Does the AR JDBC Adapter have some sort of support for Postgres arrays which is conflicting with the AR-Postgres-Arrays gem? I tried without the AR-Postgres-Array gem as well, but got the same result.

PS: I've tried this against the master branch of AR JDBC Adapter.


this is likely a bug on our side not behaving exactly as MRI+Rails 3.1 esp. since we support 3.x/4.x from the same code and for 4.0 there's array support added (which gets turned off but still might have issues) ... 3 options :

  • fix it on our side ( please contribute at least some code in the comments to reproduce )
  • update to Rails 4.0
  • avoid using posgres-array gem ... and try to figure if you can handle the cases

@kares -- thank you for the quick response.

For someone switching from MRI+Rails 3.x what's the recommendation? Remove activerecord-postgres-array & activerecord-postgres-hstore from the Gemfile because AR JDBC Adapter supports these postgres data-types by default?

Btw, I played around with activerecord-postgres-array and figured out that by changing the array-detection condition on to:

if array?

It gets to sorta work, but is still buggy (the string returned by type_cast_code_with_array is never executed/evaled by anything)

I tested postgres array support with bare AR JDBC Adapater, and it doesn't seem to support anything out of the box. Test code available at


@kares -- Is the structure of ActiveRecord::ConnectionAdapters::JdbcColumn & Arjdbc::PostgreSQL::Column exactly the same as ?

I've spend the last 3 hours trying various things to track down the bug, and I'm at my wit's end as to what's happening here. Some observations:

  • In MRI, type_cast_code_with_array is called for all columns in a table, at least once. The code (string) returned by this function is then executed for every array column/cell that is read from the DB.
  • However, in JRUBY, type_cast_code_with_array is NOT called for all columns in a table. It is not getting called for character varying[] or timestamp without time zone[] while it is called for boolean[]. Moreover, the code (string) returned by this function, is never executed!
  • MRI does not have the ActiveRecord::ConnectionAdapters::PostgreSQLColumn.array? method, while jRuby does. On the other hand, the value of ActiveRecord::ConnectionAdapters::PostgreSQLColumn.sql_type in MRI is boolean[], decimal[], etc. whereas in jRuby it lacks the trailing square brackets.

If you give me some hint about what the bug is, I think I can fix the bug and submit a PR. But, I'll be unable to follow the code-flow in the jRuby version without your help.


I'm sorry I would need to look deep as well to have useful hints/help. in JDBC there's type information available thus we might no go through all the string/type_cast methods (but than it might be a bug as well). array? is there due AR 4.0 and also sql_type should have been correct but than that might be due AR 4.0 and/or an incompatibility ...

I was new to this code as well thus you definitely can figure things out :) ... on the other hand if it's not fun anymore just come up with (more) test code - that will help a lot - e.g. for the expected sql_type for the columns on 3.1/3.2 ...


@kares --

  1. which version of AR JDBC Adapter did not have AR4.0 compatibility?
  2. At what point is ActiveRecord::ConnectionAdapters::JdbcTypeConverter used? I tried putting a puts in initialize, choose_best_types, & choose_type and _none_of them seem to be getting called.

@kares, tried with 1.2.9 and everything seems to be working fine. So, it definitely is a regression, and I have a hunch it may be due to which is non-existent in pre-AR4.0 code


OK, thanks just add some tests than (if you have more than in the gists) - hopefully I'll look at this next week or so ...

@kares kares referenced this issue from a commit in kares/activerecord-jdbc-adapter
@kares kares [postgres] allow returning string values for (JDBC) arrays with (fixes

... due compatibility with AR < 4.0 (and gems that depend on previous func)
@kares kares closed this issue from a commit
@kares kares [postgres] allow returning string values for (JDBC) arrays with (fixes

... due compatibility with AR < 4.0 (and gems that depend on previous func)
@kares kares closed this in fa546ee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.